function coordsys=m_coord(coordsys,igrfdate)
% M_COORD Initializes the coordinate system for varous conversions.
%
%   M_COORD('set') tells you the current coordinate system
%   M_COORD('get') gives you the current possibilities
%   M_COORD('usage') gives you usage information
%   M_COORD(SYSTEM) sets the coordinate system to SYSTEM.
%
%   Currently the available coordinate systems are:
%              'geographic' (usual lat/long)
%              'IGRFXXXX-geomagnetic' (referenced to magnetic poles, for
%                           a few specific years XXXX (for backwards
%                           consistency these are 2000, 2011, 2020)
%              'IGRF-geomagnetic'  Allows you to set the date to
%                           anytime between 1900 and 2025 with a second
%                           argument which is a matlab-format time
%                           as generated by DATENUM(YYYY,MM,DD), using the
%                           13th generation IGRF from 
%                           https://www.ngdc.noaa.gov/IAGA/vmod/igrf.html
% 
%

% Rich Pawlowicz (rich@ocgy.ubc.ca) nOV/2001
%
% This software is provided "as is" without warranty of any kind. But
% it's mine, so you can't sell it.
%
% Changes
%   Jan/2020 - added 'IGRF' option with a 2nd option for year.


global MAP_COORDS

if nargin==0, coordsys='usage'; end
if nargin==1
    if isstruct(coordsys)
        igrfdate=coordsys.mdate;
        coordsys=coordsys.name;
    else
        igrfdate=NaN; 
    end
end
coordsys=lower(coordsys);

coords=mc_coords('name');

switch coordsys

   case 'get'
      disp(' ');
      disp('Available coordinate systems are:'); 
      for k=1:length(coords.name)
        disp(['     ' coords.name{k}]);
      end
   
   case 'set'
      if isempty(MAP_COORDS)
         disp('No coordinate system initialized');
         m_coord('usage');
      else
         if nargout==0
           disp(MAP_COORDS.name);
         else
	       coordsys=MAP_COORDS;
         end   
      end
 
   case 'usage'
      disp(' ');
      disp('Possible calling options are:');
      disp('  ''usage''                    - this list');
      disp('  ''set''                      - list of coordinate systems');
      disp('  ''get''                      - get current coordinate (if defined)');
      disp('  ''[system]''                   - initialize coordinate system\n');
   
   otherwise
     k=strcmp(coordsys,lower(coords.name));
     MAP_COORDS=mc_coords('parameters',coords.name{k},igrfdate);
   
end   
    % Check to see if a non-lat/long coordinate system is being used.
