%% changed from loc_RBSP_CCHF_plt_HOPE_Ne_HF2.m
% upload with publication.
    close all; clear;
    dash_dir = './';
    ncfn = 'CIMI_HF_fig1.nc';
    ncname = [dash_dir,ncfn];
    disp(['Loading data from ',ncname]);
    
    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
    
    var_plt2s = {FPDO2d_plt, FODO2d_plt}; % FHEDO2d_plt,

    %%
% nelim = log10([1,3600]);
fpdlim = [3,6];
fodlim = [3,6];
fhedlim= [3,6];
% englim = log10([30,50000]);
clims = {fpdlim,fodlim}; % ,fhedlim
cmaps = {'*Spectral','*Spectral'}; % ,'*Spectral'
cmap_hfe = '*YlGnBu';
ylabs = {{'HOPE/H+','log_{10}[eV]'}, {'HOPE/O+','log_{10}[eV]'}}; 

% tlim = [smin,emin]/1440;
% ttick = (smin:120:emin)/1440;
tlab1 = {};
tlab2 = {};
tlab3 = {};
iticks = [];
for ii=1:length(ttick)
    [~,itick] = min(abs(numdate0-ttick(ii)));
    iticks(ii) = itick;
    tlab1{ii} = datestr(ttick(ii),'HH:MM');
    tlab2{ii} = num2str(L1(itick),'%.1f');
    tlab3{ii} = num2str(MLT1(itick),'%.1f');
end
labelArray = [tlab1; tlab2; tlab3]; 
tlabs = strtrim(sprintf('%s\\newline%s\\newline%s\n', labelArray{:}));
tlab0 = {'UT','L','MLT'};

cl_H = [0.6350    0.0780    0.1840];%cl_red;
cl_O = [0.4660    0.6740    0.1880];%cl_gre;
cl_tot = 'k';
cl_HE = 'b';
cl_mod = 'm';
cl_grd = 'k';
cl_llb = [0.4660    0.6740    0.1880];
dfemap = '*YlGnBu';
hps = {}; cls = {};
lwid = 2; font = 16;
panlabs = {'(a)','(b)','(c)','(d)','(e)','(f)','(g)','(h)'};
xpan = tlim(1)+0.5/24;
xpan1 = -175; ypan1 = 80;

%%
pngname = [dash_dir,'CIMI_HF_fig1'];
disp(['Plotting ',pngname]);
nrow = 6; ncol = 1; fpos = [10,10,800,900];
fig = figure('position',fpos);

for isub = 1:2
    ispc = isub;
    hps{isub} = subplot(nrow,ncol,isub);
    hold on; box on; grid on; axis tight;
    set(gca,'LineWidth',lwid,'Fontsize',font);
    colormap(hps{isub},brewermap([],cmaps{ispc}));
    % var_plt2s = {FPDO2d_plt, FODO2d_plt}; % FHEDO2d_plt,
    pcolor(numtime,Engy1d,var_plt2s{ispc}); 
    shading interp;
    cls{ispc} = colorbar;
    caxis(clims{ispc});
    xlim(tlim);
    ylim(englim);
    set(gca,'xtick',ttick,'xticklabel','');
%     if isub==1
        set(cls{ispc},'ytick',3:6);
        ylabel(cls{ispc},{'Omni-directional differential flux','log_{10}[#/s-cm^2-sr-keV/q]'});
%     end
    ylabel(ylabs{isub});
    ylimp = get(gca,'ylim');
    ypan = ylimp(1)+0.9*(ylimp(2)-ylimp(1));
    text(xpan,ypan,panlabs{isub},'Fontsize',font,'FontWeight','Bold');
    if isub==1
        title('2013-06-01');
    end
end

for isub = 3
    hps{isub} = subplot(nrow,ncol,isub);
    hold on; box on; grid on; axis tight;
    set(gca,'LineWidth',lwid,'Fontsize',font);
    plot(numdate0,ne_plt,'LineWidth',lwid);
    xlim(tlim);
    ylim(nelim);
    set(gca,'xtick',ttick,'xticklabel','','ytick',0:3);
    ylabel({'N_e', 'log_{10}[/cc]'});
    ylimp = get(gca,'ylim');
    ypan = ylimp(1)+0.9*(ylimp(2)-ylimp(1));
    text(xpan,ypan,panlabs{isub},'Fontsize',font,'FontWeight','Bold');
end

for isub = 4
    hps{isub} = subplot(nrow,ncol,isub);
    hold on; box on; grid on; axis tight;
    set(gca,'LineWidth',lwid,'Fontsize',font);    
    plot(numdate0,log10(HF_H),'LineWidth',lwid,'color',cl_H);
    plot(numdate0,log10(HF_O),'LineWidth',lwid,'color',cl_O);
    plot(numdate0,log10(HF_O+HF_H),'LineWidth',lwid,'color',cl_tot);
    plot(numtime,log10(ehfx),'LineWidth',lwid,'color',cl_mod);
    hg = legend({'RBSP-B H+','RBSP-B O+','RBSP-B total','CIMI'});
    xlim(tlim);
    ylim([6,10.5]);
    ylabel({'Heat Flux','log_{10}[eV/cm^2/s]'});
    % datetick('x','HH:MM','keepticks');
    if isub==4
%         xlabel(strdate0(1:10));
        set(gca,'xtick',ttick,'xticklabel',tlabs);
        ylimp = get(gca,'ylim');
        htt4a = text(tlim(1)-120/1440,ylimp(1)-0.28*(ylimp(2)-ylimp(1)),tlab0,'Fontsize',font);
    else
        set(gca,'xtick',ttick,'xticklabel','');
    end
    ylimp = get(gca,'ylim');
    ypan = ylimp(1)+0.9*(ylimp(2)-ylimp(1));
    text(xpan,ypan,panlabs{isub},'Fontsize',font,'FontWeight','Bold');
end

for isub=5
    hps{isub} = subplot(nrow,ncol,isub+[0,1]);
    hold on; box on; grid on; axis equal tight;
    set(gca,'Fontsize',font,'Linewidth',lwid);
    colormap(hps{isub},brewermap([],dfemap));
    dfe_plt = log10(dfeall(isort,:)); 
    dfe_plt(dfe_plt<8) = NaN;
    pcolor(LON,LAT,dfe_plt);
    shading flat; 
    cls{isub} = colorbar; 
    caxis(dfelim);
    plot(aurllbN(:,1),aurllbN(:,2),'Linewidth',lwid,'color',cl_llb);
    plot(aurllbS(:,1),aurllbS(:,2),'Linewidth',lwid,'color',cl_llb);
    plot(LON(ilon,ilat),LAT(ilon,ilat),'m*','Linewidth',lwid*1.5,'Markersize',10)
    xlim(lonlim);
    ylim(latlim);
    xlabel('SLT [Hour]');
    ylabel('Geo Lat [deg]');
    slt2lon = 15;
    lontick = slttick*slt2lon-180;
    sltticklab={};
    for ii=1:length(slttick)
        sltticklab{ii} = num2str(slttick(ii),'%02d');
    end
    strdate = '2013-06-01T01-00-00';
    set(gca,'xtick',lontick,'ytick',lattick,'xticklabel',sltticklab);
    plot_grid(lontick,lattick,cl_grd);
%     title(strdate);
    ylabel(cls{isub},{'CIMI Electron Heat Flux','log_{10} [eV/cm^2/s]'},'Fontsize',font);
    xpan5 = -175;
    ypan5 = 80;
    text(xpan5,ypan5,panlabs{isub},'Fontsize',font,'FontWeight','Bold','color','k');
    xpan6 = -175;
    ypan6 = -80;
    text(xpan6,ypan6,strdate,'Fontsize',font,'FontWeight','Bold','color','k');
end
%
for isub=1:4
    hpos = get(hps{isub},'position');
    set(hps{isub},'position',hpos+[0.0, 0.0, -0.01, 0.03]);
end
%
for isub=1:2
    cpos = get(cls{isub},'position');
%     set(cls{isub},'position',cpos+[0.0525,0,0,0]);
    set(cls{isub},'position',[0.9    0.6811    0.0200    0.2739]);
end
%
for isub=1:2
    hpos = get(hps{isub},'position');
    set(hps{isub},'position',hpos+[0, 0, 0.055, 0]);
end
%
set(hg,'position',[0.8116    0.2012    0.1594    0.1079]);
%
for isub=5
    hpos = get(hps{isub},'position');
    set(hps{isub},'position',hpos+[-0.08,-0.05,0,0.0]);
end
%
print(fig,pngname,'-dpng');
% print(fig,pngname,'-depsc');

%%
save_nc = 0;
if save_nc
        %% DMSP_tplt0,log10(DMSP_chen0),log10(DMSP_eden0)
        disp('Do you really want to overwrite the nc files?');
        pause
        dash_dir = dat_dir;
        disp(dash_dir);
        if(~exist(dash_dir,'dir')); mkdir(dash_dir); end

        ncfn = 'CIMI_HF_fig1.nc';
        ncname = [dash_dir,ncfn];
        disp(['Writing data to ',ncname]);

        % Open a netcdf file:
        ncid = netcdf.create(ncname,'NC_WRITE');
        varnames = {'tlabs','tlab0','font','lwid','cl_H','cl_O','cl_tot','cl_mod','cl_grd','xpan','xpan5','ypan5','xpan6','ypan6', ...
            'strdate0','strdate','ylabs','panlabs','clims','cls'}; 
        % 'tie_dat',
        varnames = {'FPDO2d_plt', 'FODO2d_plt','slt2lon','HF_H','HF_O','ehfx','ne_plt','numdate0','nelim','ttick','tlim','englim','numtime','Engy1d',...
            'dfeall','isort','LON','LAT','dfelim','aurllbN','aurllbS','ilon','ilat','lonlim','latlim','lontick'};
        run("save_nc_file_cell.m");
end