%% Plot FAC+Pot at three UTs.

close all; clear;
strdatefmt = 'yyyy-mm-ddTHH-MM-ss';
MJD0 = datenum('2000-01-01T00-00-00',strdatefmt)-51544;

addpath('~ldong/mcode/color_brewer/'); %colormap(brewermap([],'YlGnBu'))
dash_dir = '/glade/work/ldong/DASH/Shi21_concussion/';
plt_dir = dash_dir;

    % Prepare for variables to be written to nc file.
    ncname = [dash_dir,'fig4_MI.nc']; % generated with SAPS_paper2_fig5.m and SAPS_paper2_fig5_plt.m
    ncid = netcdf.open(ncname,'NC_NOWRITE'); 
    info = ncinfo(ncname);
    varnames = {}; % store all available variable names.
    for ii=1:size(info.Variables,2)
        varnames{ii} = info.Variables(ii).Name;
        disp([num2str(ii,'%02d'),' ',info.Variables(ii).Name]); 
    end
    
    for ii=1:length(varnames)
        varid = netcdf.inqVarID(ncid,varnames{ii});   
        eval([varnames{ii},' = netcdf.getVar(ncid,varid);']);
    end
    
    %%
    strdates = {'2011-10-24T18-29-30','2011-10-24T18-31-00','2011-10-24T18-33-00'};
    %% Plot
    mix_lat = acos(sqrt(xi.^2+yi.^2))*180/pi;
    mix_lon = atan2(yi,xi)*180/pi;
    rmax = max(sqrt(xi(:).^2+yi(:).^2));
    llb = ceil(min(acos(rmax)*180/pi)/10)*10;
    llb = 40;
    [~,illb] = min(abs(mix_lat(1,:)-llb));
    lats = llb:10:90;

    % lims
    faclim = [-1,1]*0.5;
    dpot = 2.5;
    cl_pot = 'm';
    cl_lat = 'k';
    cmap_fac = '*PuOr';
    panlabs = {'(a)','(b)','(c)'};
    xpan = 0.6; ypan = 0.8;
    
    hps = {}; cls = {};
    az = -90; el = 90;
    lwid = 2; font = 14;
    
    pngname = ncname(1:end-3);
    disp(['Plotting ',pngname]);
    nrow = 1; ncol = 3; fpos = [10,10,1540,400];
    fig = figure('position',fpos);
    for isub=1:ncol
        hps{isub} = subplot(nrow,ncol,isub);
        hold on; axis equal tight off; view(az,el);
        set(gca,'Fontsize',font,'LineWidth',lwid);
        colormap(gca,brewermap([],cmap_fac));
        pcolor(xi(:,1:illb),yi(:,1:illb),facns(:,1:illb,isub));
        shading interp;
        if isub==3
            c1 = colorbar;
            ylabel(c1,'FAC [\mu A/m^2]');
        end
        caxis(faclim);
        plot_pot(xi(:,1:illb),yi(:,1:illb),potns(:,1:illb,isub),dpot,cl_pot,lwid/3);
        plotion(cl_lat,lats);
        title(strdates{isub});
        text(xpan,ypan,panlabs{isub},'Fontweight','bold','Fontsize',font);
        if isub==2
            text(-xpan,-ypan,['Contour step ',num2str(dpot,'%.1f'),' kV'],'color',cl_pot);
        end
    end
    set(c1,'position',get(c1,'position')+[0.05,0,0,0]);
    set(gcf,'renderer','painters');
    print(fig,pngname,'-dpng');
    close(fig);
%% END of CODE