Calculate the Maidenhead locator for latitude/longitude coordinates

This Scilab program calculates the Maidenhead locator from coordinates given in decimal latitude and longitude.

//////////////////////////////////////////////////////////////////////////////
//
// Maidenhead Locator calculator
//
// Convert a longitude/latitude coordinate to Maidenhead Locator
//
//                  Copyright (C) 2012 Christophe DAVID (ON6ZQ)
//              http://www.on6zq.be/Scilab/LatLong2MaidenheadLocator
//
//  This program is free software; you  can redistribute it and/or modify it
//  under the terms of the GNU General Public License version 2 as published
//  by the Free Software Foundation (http://www.gnu.org/licenses/gpl.html).
//
//////////////////////////////////////////////////////////////////////////////
//
//                This program was last tested with Scilab 5.5
//                            http://www.scilab.org
//
//////////////////////////////////////////////////////////////////////////////

// A semicolon at the end of an instruction causes the result not to be displayed.

errclear;         // clear all errors
clear;            // kill variables
clearglobal       // kill global variables
clc;              // clear command window
tohome;           // move the cursor to the upper left corner of the Command Window

// stacksize('max'); // increase the size of this stack to the maximum.
stacksize(1e8);   //stacksize('max') does not appear to work on my system ;-(

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

function[locator]= MaidenheadLocator(latitude, longitude)
   locator ="";

   if((latitude >-90)&(latitude <90)&(longitude >-180)&(longitude <180))then
      longitude2 = longitude +180;      
      latitude2  = latitude  +  90;

      lon1 =int(longitude2 /20);
      lat1 =int(latitude2  /10);      

      lon2 =int(modulo(longitude2 /2,10));
      lat2 =int(modulo(latitude2     ,10));      

      lon3 =60*(longitude2 -(2*int(longitude2 /2)));
      lat3 =60*(latitude2 -int(latitude2));

      lon4 =int(lon3 /5);
      lat4 =int(lat3 /2.5);

      locator =strcat([ascii(65+ lon1),ascii(65+ lat1),ascii(48+ lon2),ascii(48+ lat2),ascii(97+ lon4),ascii(97+ lat4)]);
      // printf("\nlon1=%f, lon2=%f, lon3=%f, lon4=%f, lat1=%f, lat2=%f, lat3=%f, lat4=%f, locator=%s", lon1, lon2, lon3, lon4, lat1, lat2, lat3, lat4, locator);
   end
endfunction

//////////////////////////////////////////////////////////////////////////////


assert_checkequal(MaidenheadLocator(  48.14666  ,   11.60833   ),"JN58td");
assert_checkequal(MaidenheadLocator(-34.91     ,  -56.21166   ),"GF15vc");
assert_checkequal(MaidenheadLocator(  41.714775,  -72.727260  ),"FN31pr");
assert_checkequal(MaidenheadLocator(  37.413708,-122.1073236),"CM87wj");
assert_checkequal(MaidenheadLocator(  59.19222  ,-115.5925    ),"DO29ee");
assert_checkequal(MaidenheadLocator(-51.6922   ,  -58.8349    ),"GD08nh");
assert_checkequal(MaidenheadLocator(  45.8325   ,    6.8644    ),"JN35kt");
assert_checkequal(MaidenheadLocator(-35.65944  ,  148.79      ),"QF44ji");

//assert_checkequal(MaidenheadLocator() , "");

//////////////////////////////////////////////////////////////////////////////

InputLatitude  =45.8325;
InputLongitude =  6.8644;

printf("\n%f, %f => %s", InputLatitude, InputLongitude, MaidenheadLocator(InputLatitude, InputLongitude));


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////


// Calculate the Maidenhead Locator for all SOTA summits

// summitslist = "/home/cda/Downloads/summitslist.csv";

// [filedescriptor, err] = mopen(summitslist , "rt")
// if (err <> 0) then
//     printf("\nerror reading %s", summitslist);    
// else
//    while 1
//       line = mgetl(filedescriptor, 1)
//          if (meof(filedescriptor))
//             break;
//          else
//             fields = tokens(line,',');
//             if size(fields, "r") > 10 then
//                 //printf("\n%s", line)
//                 loc = MaidenheadLocator(strtod(fields(10)), strtod(fields(9)));
//                 printf("\n%s %s %s %s", fields(1), fields(10), fields(9), loc);                
//              end
//          end
//    end
//    mclose(filedescriptor);
// end

// mclose('all') // MUST be the last line of the script

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////