%% SAPS paper figure: solar wind and CPCP
% Actual Figure 2 in the paper.
close all; clear;
% addpath(genpath('~binzheng/matlab_source'));
% addpath('./subaxis/');

rootdir = [pwd,'/'];%'/glade/scratch/ldong/'; 
subdir = '/';%'LTR/';
srun = 'LTR_qq_W17'; srttl = 'LTR-Baseline';
hdf_fldr = [pwd,'/'];%[rootdir,subdir,srun,'/'];
plt_dir0 = [pwd,'/'];%['/glade/work/ldong/Plots/',subdir,srun,'/'];
plt_dir = pwd;%[plt_dir0,'/SAPS_Paper1/'];
if(~exist(plt_dir,'dir')); mkdir(plt_dir); end

dmin=1; nskip=1;
smin0 = datenum('2013-03-16T18-00-00','yyyy-mm-ddTHH-MM-ss')*1440;
t_start= '2013-03-17T00-00-00'; t_stop = '2013-03-18T00-00-00'; 
smin = datenum(t_start,'yyyy-mm-ddTHH-MM-ss')*1440;
emin = datenum(t_stop ,'yyyy-mm-ddTHH-MM-ss')*1440;
%     save([plt_dir,srun,'_series_NS_',datestr(smin/1440,'HHMM'),'_',...
%         datestr(emin/1440,'HHMM'),'.mat'],...
%         'UT','CPCPn','TFACn','TNUMn','TENGn','TJOUn','CPCPs','TFACs','TNUMs','TENGs','TJOUs');
load([plt_dir0,srun,'_series_NS_',...
    datestr(smin/1440,'HHMM'),'_',datestr(emin/1440,'HHMM'),'.mat']);
UTday = UT/24;
SW_SM_DAT = load_SW_SM_DAT([hdf_fldr,'SW-SM-DAT']); % UT, Nsw, Vx, Vy, Vz, Cs, Bx, By, Bz, Bt, Zangle
indIMF = smin-smin0:dmin*nskip:emin-smin0;
Nsw = SW_SM_DAT(indIMF,2);
Vsw = SW_SM_DAT(indIMF,3);
IMFBy = SW_SM_DAT(indIMF,8); 
IMFBz = SW_SM_DAT(indIMF,9);
% [strdate, strhms, data] = read_OMIN('OMNI_HRO_1MIN_20130317_GSM.txt',6);

% DstFname = [hdf_fldr,'dst.pkl'];
% [a] = loadpickle(DstFname);
dstmat = load([hdf_fldr,'dst.mat']);
Dst = dstmat.dst; Dst(end+1)=Dst(end);
DstWDC = [ -9 ...
           -11  -13  -11   -8   -8   -7 ...
            15   -5  -23  -66  -89  -82 ...
           -86  -85  -89  -89  -84  -99 ...
           -98 -115 -132 -131 -116 -105];
load([plt_dir0,'20130317_symH.mat']); % symH
load([plt_dir0,'20130317_TFAC_North.mat']); % TFAC_UT, TFAC_N

%% Plot
spc = 0.0; pdn = 0.0; mgn = 0.1; lwid=2; font=16;
ttick = UTday(1)+(0:3:24)/24;
xtext = UTday(1)+0.5/24;
fig = figure('position',[601    35   962   818]); 
subaxis(7,1,1,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
% subplot(7,1,1); hold on; box on;
plot(UTday,Nsw,'k','LineWidth',lwid);
ylim([0,25]); set(gca,'ytick',0:5:25)
ylabel('\rho [cm^{-3}]');
title('Solar Wind Conditions and Ionospheric Parameters');
text(xtext,20,'(a)','FontSize',font,'color','k');
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel',''); 

subaxis(7,1,2,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,Vsw,'k','LineWidth',lwid);
ylim([-750,-400]); set(gca,'ytick',-700:100:-400)
ylabel('V_X [km/s]');
text(xtext,-470,'(b)','FontSize',font,'color','k');
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel','','YAxisLocation','right'); 

subaxis(7,1,3,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,IMFBy,'k','LineWidth',lwid);
% plot(UTday,data(:,2),'g-','LineWidth',lwid);
plot(xlim,[0,0],'k--','LineWidth',lwid*0.8);
ylim([-25,20]); set(gca,'ytick',-20:10:20)
ylabel('B_Y [nT]');
text(xtext,11,'(c)','FontSize',font,'color','k');
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel',''); 

subaxis(7,1,4,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,IMFBz,'k','LineWidth',lwid);
% plot(UTday,data(:,3),'g-','LineWidth',lwid);
plot(xlim,[0,0],'k--','LineWidth',lwid*0.8);
ylim([-25,20]); set(gca,'ytick',-20:10:20)
ylabel('B_Z [nT]');
text(xtext,11,'(d)','FontSize',font,'color','k');
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel','','YAxisLocation','right'); 

subaxis(7,1,5,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,CPCPn,'LineWidth',lwid);
ylim([0,600]); set(gca,'ytick',0:100:600,'yticklabel',{'0','','200','','400','','600'})
ylabel('\Phi [kV]');
text(xtext,480,'(e)','FontSize',font,'color','k');
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel',''); 

subaxis(7,1,6,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,TFACn,'LineWidth',lwid);
plot(TFAC_UT, TFAC_N,'k','LineWidth',lwid);
ylim([0,20]); set(gca,'ytick',0:5:20)
ylabel('J_{||} [MA]');
text(xtext,16,'(f)','FontSize',font,'color','k');
hg=legend({'LTR','Obs'},'location','southwest','position',[0.1108    0.2375    0.0813    0.0499]);
set(gca,'FontSize',font,'xtick',ttick,'XTickLabel','','YAxisLocation','right'); 

subaxis(7,1,7,'Spacing',spc,'Padding',pdn,'Margin',mgn); hold on; box on;
plot(UTday,Dst,'LineWidth',lwid);
% plot(UTday(1:60:end),DstWDC,'k','LineWidth',lwid);
plot(UTday,symH,'k','LineWidth',lwid);
ylim([-140,40]); set(gca,'ytick',-140:40:40)
ylabel('Dst [nT]');
set(gca,'FontSize',font,'xtick',ttick); 
text(xtext,-100,'(g)','FontSize',font,'color','k');
datetick('x','HH:MM','keepticks');
xlabel(datestr(UTday(1),'yyyy-mmm-dd'));

% print(fig,[plt_dir,'fig02_sw'],'-depsc');

%% END of CODE