 function [hh1 cbh1 cc1] = plot_map_projection(data,varargin)
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % plot map out of 2D or 3D field inside a structure
 % Structure must contain "lon" and "lat" fields
 % Needs M_MAP tooldbox
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 A.lon 		= [20 380];	% Longitude range
 A.lat 		= [-90 90];	% Latitude range
 A.var 		= 'temp';	% Variable name in inout structure
 A.depth 	= 0;		% In case depth is a coordinate - select depth level
 A.depthvar 	= 'depth';	% Name of depth coordinate
 A.fact 	= 1;		% Multiplicative factor for variable
 A.fig 		= 1;		% Creates new figure 
 A.visible      = 1;		% Makes figure visible/invisible
 A.mode         = 'pcolor';	% 'pcolor','contourf','contour'
 A.clevels      = 20;		% levels for contour
 A.ccolor       = [0 0 0 ];	% color for contour
 A.proj 	= 'mollweide';	% Projection type - from M_MAP toolbox
 A.cbarpos 	= 'east';	% Position of colorbar
 A.xtick 	= 5;		% Number of xticks 
 A.ytick 	= 5;		% Number of yticks
 A.yaxislocation = 'left';	% Location of y axis
 A.caxis 	= nan;		% Color axis limits (nan=auto) 
 A.palette 	= 'jet';	% Palette
 A.cbnum 	= 20;		% Number of colors in colorbar
 A.cpup 	= 0;		% Adds triangle up in colorbar
 A.fix 		= 0;		% For fixing number of colors
 A.barlabel 	= 'none';	% Label for colorbar
 A.barfont 	= 18;		% Fontsize for colorbar
 A.barlabelfont	= 18;		% Fontsize for bar label
 A.ytickbar	= nan;		% Ticks for colorbar (nan=auto)
 A.fontsizegrid = 13;		% Fontsize of the map grid
 A.landcolor 	= [.2 .2 .2];	% Color of land
 A.varname 	= nan;		% Name of variable - must be in input data structure
 A.title 	= nan;		% Plot title
 A.titlefont 	= 18;		% Title fontsize
 A.interpreter 	= 'none';	% 'latex', 'none' etc.
 A.barinterpreter = 'tex';	% 'latex', 'none' etc.
 A.box  	= 'on';		% Add box
 A.for 		= 'epsc';	% Printing format
 A.figname 	= nan;		% Name for printed figure
 A.printfig 	= 0;		% Prints the figure
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 A = parse_pv_pairs(A, varargin);
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 % Example plots
 % (1)
 % plot_map_projection(structure,'var','variablename')
 % (2)
 %[hh1 cbh1] = plot_map_projection(tgout,'var','sunrisepass','fact',60,'caxis',[-60 90],...
 %                   'title','(a) SSL Departure time at 150 m', ...
 %                   'barlabel','minutes before sunrise','barlabelfont',18, ...
 %                   'palette',cmap3, ...
 %                   'cbnum',15,'fix',1,'ytickbar',[-60:30:90], ...
 %                   'printfig',1,'figname',['Fig4a_sunrisepass_',suffix]);
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(isnan(A.varname)) & ischar(A.var);
    A.varname = A.var;
 elseif any(isnan(A.varname)) & ~ischar(A.var);
    A.varname = 'userdefined';
 end

 if isnumeric(A.var)
    % uses A.var as actual variable input
    data.userdefined = A.var;
    A.var = 'userdefined';
 end

 if isfield(data,A.depthvar) & length(size(data.(A.var)))==3
    depth = abs(data.(A.depthvar));
    indz = findin(abs(A.depth),depth);
    disp(['Sampling 3D field: ' A.varname ' at level: ' num2str(indz)]);  
    thisvar = A.fact * squeeze(data.(A.var)(indz,:,:));
 else
    thisvar = A.fact * data.(A.var);
 end

 % This part was added to mantain compatibility between data with [lon,lat] and [lat,lon]
 svar = size(thisvar);
 nlon = length(data.lon);
 nlat = length(data.lat);
 if svar(2) == nlon & svar(1) == nlat
   %disp(['Flipping lon-lat dimensions']);
    thisvar = thisvar';
 elseif svar(1) == nlon & svar(2) == nlat
   %disp(['Mantaining lon-lat dimensions']);
    thisvar = thisvar;	% redundant - just for completeness
 else
    error(['Mismatch between data lon, lat and dimensions']);
 end

 if isfield(data,'lon')
    data.longitude = data.lon;
 end

 if isfield(data,'lat')
    data.latitude = data.lat;
 end


 % ------------------------------------------------------------
 % Adds a chunck to 390 longitude
 nlat0 = length(data.latitude);
 nlon0 = length(data.longitude);
 ind0 = 1;
 ind1 = findin(30,data.longitude);
 addlon = 360 + data.longitude(ind0:ind1);
 naddlon = length(addlon);
 catdim = find(size(data.longitude)>1);
 data.longitude = cat(catdim,data.longitude,addlon);
 tempvar = nan(length(data.longitude),length(data.latitude));
 tempvar(1:nlon0,:) 			= thisvar(1:nlon0,:);
 tempvar(nlon0+1:nlon0+naddlon,:) 	= thisvar(1:naddlon,:);
 thisvar = tempvar;
 % ------------------------------------------------------------

 if A.fig==1
    fig = figure;
    if A.visible==0
       set(fig,'visible','off');
    end
 end
 
 m_proj(A.proj,'long',A.lon,'lat',A.lat);

 switch A.mode
 case 'pcolor'
    [hh1] = m_pcolor(data.longitude,data.latitude,thisvar');
 case 'contour'
    [cc1 hh1] = m_contour(data.longitude,data.latitude,thisvar',A.clevels,'color',A.ccolor);
 case 'contourf'
    [cc1 hh1] = m_contourf(data.longitude,data.latitude,thisvar',A.clevels);
 end

 hold on

 m_grid('xtick',A.xtick,'ytick',A.ytick,'tickdir','out', ...
        'box',A.box,'yaxislocation',A.yaxislocation,'fontsize',A.fontsizegrid);

%m_grid('xaxis','bottom','linest',':');

 m_coast('patch',[.2 .2 .2],'edgecolor',[0 0 0]);

 grid off
 shading flat

 if ~strcmp(A.mode,'contour')

    [cbh1 cbn cbl] = colorbarn('palette','jet','num',A.cbnum,'fix',A.fix);
    if ischar(A.palette)
       colormap(mycolormaps('num',cbn,'scheme',A.palette))
    else
       colormap(A.palette);
    end
    pos = get(gca,'position');
    switch A.cbarpos
    case 'east'
       set(cbh1,'location','eastoutside');
       if ~strcmp(A.barlabel,'none')
          ylabel(cbh1,A.barlabel,'fontsize',A.barlabelfont,'interpreter',A.barinterpreter);
       end
    case 'south'
       set(cbh1,'location','southoutside');
       if ~strcmp(A.barlabel,'none')
          xlabel(cbh1,A.barlabel,'fontsize',A.barlabelfont,'interpreter',A.barinterpreter);
       end
    end
   
    if any(isnan(A.caxis))
       A.caxis = [min(thisvar(:)) max(thisvar(:))];
    end
   
    caxis(A.caxis);
    
    if all(~isnan(A.ytickbar))
       set(cbh1,'ytick',A.ytickbar);
    end

    set(cbh1,'fontsize',A.barfont);

 else
    cbh1 = nan;
 end

 set(gca,'fontsize',18)

 m_coast('patch',A.landcolor,'edgecolor',[0 0 0]);
 set(gcf,'renderer','zbuffer','InvertHardCopy', 'off','color',[1 1 1])

 if any(isnan(A.title))
    A.title = A.varname;
 end

 title(A.title,'fontsize',A.titlefont,'interpreter',A.interpreter);

 if (A.printfig)
    if any(isnan(A.figname))
       A.figname = ['fig_' A.varname];
    end
    mprint_fig('name',A.figname,'for',A.for,'sty','nor7')
 end

 end

 function iivect = findin(val,vect)

    iivect = nan(size(val));
    for indi=1:length(val)
       dd = abs(vect-val(indi));
       [mm ii] = min(dd);
       iivect(indi) = ii(1);
    end
 end


