%% changed from Lin20GRL_SAPS_fig3R.m.
close all; clear;

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

    % Prepare for variables to be written to nc file.
    ncname = [dash_dir,'fig1a_sw.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

    %% Plot
    vars = {{IMFBy, IMFBz}, Vsw, Nsw, {SYMH, Kp}};
use_lims = 0;
% lims = {[-1,1]*10, [-1,1]*10, [-750,-200], [0,20], [-65,20], [-0.5,6]};
lims = {[-1,1]*50, [-750,-250], [0,25], {[-500,100], [-0.5,10]}};
ylabs = {{'B_Y [nT]', 'B_Z [nT]'}, 'V_{X} [km/s]', 'N_{SW} [/cc]', {'SYMH [nT]', 'Kp'}};
yticks = {{-20:10:40, -40:10:20}, -700:100:-100, 0:5:25, {-400:100:0, 0:3:9}};

imflim = [-1,1]*50;
imftick = -40:20:40;
imflab = 'IMF [nT]';
vswlim = [-750,-250];
vswtick = -700:100:-100;
vswlab = 'V_{X} [km/s]';
nswlim = [0,25];
nswtick = 0:5:25;
nswlab = 'N_{SW} [/cc]';
symhlim = [-500,100];
symhtick = -400:100:0;
symhlab = 'SYMH [nT]';
kplim = [-0.5,9.5];
kptick = 0:3:9;
kplab = 'Kp';
% Canvas
pngname = [plt_dir,'fig1a_sw'];
disp(['Plotting ',pngname]);
nrow=4; ncol=1; fpos=[1,39,928,800];
% Parameters
lwid=2; font=14; 
cl = '';
cl0 = 'k';
cl1 = 'g';
cl2 = 'b';
% ttick = UTday(1)+(0:24:24*18)/24;
dtick = 2;
ndays = 1;
ttick = UTday(1)+(0:dtick:24*ndays)/24;
xtext = UTday(1)+0.3/24;
ytextr = 0.88;
tlim=([UTday(1),UTday(end)+1/1440]);
tlim=([UTday(1),UTday(1)+ndays]);
xlgd = tlim(2)+0.02*(tlim(2)-tlim(1));
panlabs = {'(a)','(b)','(c)','(d)'};
if ndays>1
    ttickfmt = 'dd/HH';
else
    ttickfmt = 'HH';
end
% Plot
fig = figure('position',fpos);

    irow = 1;
    hps{irow}=subplot(nrow,ncol,irow); 
    hold on; box on; grid on;
    set(gca,'FontSize',font,'LineWidth',lwid);
    plot(UTday,IMFBy,'color',cl1,'LineWidth',lwid);
    plot(UTday,IMFBz,'color',cl2,'LineWidth',lwid);
    xlim(tlim);
    ylim(imflim);
    set(gca,'ytick',imftick,'xtick',ttick,'XTickLabel','');
    plot(xlim,[0,0],'k--');
    ylabel(imflab);
    title('Solar Wind Conditions and SymH/Kp');
    ylime = get(gca,'ylim');
    yplab = ylime(1)+ytextr*(ylime(2)-ylime(1));
    text(xtext,yplab,panlabs{irow},'FontSize',font,'Fontweight','bold','color','k');
    text(xlgd,yplab,'B_Y','color',cl1,'FontSize',font,'FontWeight','Bold');
    text(xlgd,yplab-0.2*(ylime(2)-ylime(1)),'B_Z','color',cl2,'FontSize',font,'FontWeight','Bold');
    
    irow = 2;
    hps{irow}=subplot(nrow,ncol,irow); 
    hold on; box on; grid on;
    set(gca,'FontSize',font,'LineWidth',lwid);
    plot(UTday,Vsw,'color',cl0,'LineWidth',lwid);
    xlim(tlim);
    ylim(vswlim);
    set(gca,'ytick',vswtick,'xtick',ttick,'XTickLabel','');
    ylabel(vswlab);
    ylime = get(gca,'ylim');
    yplab = ylime(1)+ytextr*(ylime(2)-ylime(1));
    text(xtext,yplab,panlabs{irow},'FontSize',font,'Fontweight','bold','color','k');
    
    irow = 3;
    hps{irow}=subplot(nrow,ncol,irow); 
    hold on; box on; grid on;
    set(gca,'FontSize',font,'LineWidth',lwid);
    plot(UTday,Nsw,'color',cl0,'LineWidth',lwid);
    xlim(tlim);
    ylim(nswlim);
    set(gca,'ytick',nswtick,'xtick',ttick,'XTickLabel','');
    ylabel(nswlab);
    ylime = get(gca,'ylim');
    yplab = ylime(1)+ytextr*(ylime(2)-ylime(1));
    text(xtext,yplab,panlabs{irow},'FontSize',font,'Fontweight','bold','color','k');
    
    irow = 4;
    hps{irow}=subplot(nrow,ncol,irow); 
    hold on; box on; grid on;
    set(gca,'FontSize',font,'LineWidth',lwid);
    yyaxis left;
    plot(UTday,SYMH,'color',cl1,'LineWidth',lwid);
    xlim(tlim);
    ylim(symhlim);
    set(gca,'ytick',symhtick,'xtick',ttick,'XTickLabel','','YColor',cl0);
    ylabel(symhlab,'color',cl1);
    yyaxis right;
    plot(UTday,Kp,'color',cl2,'LineWidth',lwid);
    xlim(tlim);
    ylim(kplim);
    set(gca,'ytick',kptick,'xtick',ttick,'YColor',cl0);
    datetick('x',ttickfmt,'keepticks');
    ylabel(kplab,'color',cl2);
    ylime = get(gca,'ylim');
    yplab = ylime(1)+ytextr*(ylime(2)-ylime(1));
    text(xtext,yplab,panlabs{irow},'FontSize',font,'Fontweight','bold','color','k');
    xlabel([datestr(UTday(1),'yyyy-mmm-dd')]);
    
    dxc = -0.02; dyc = -0.02; ddy = 0.03;
    for ii=1:nrow
        pos=get(hps{ii},'position');
        set(hps{ii},'position',pos+[dxc,dyc,0,ddy]);
    end
    print(fig,pngname,'-dpng');
    close(fig);