%%
%% This program is written for plotting the calculated convection location from
%%  tiegcm_kameleon NH and SH electric potentials listed in fort.51 and HP from UTh
%% BA Emery, August 2013 /aim3/emery/tiegcm_cmit/plot_cloc_kameleon.m

clear all; close all

% Set year 
year1 = 2006

% ifaa=0 for SWMF, =1 for ASTRA AMIE, =2 for UMI AMIE
ifaa = 0

nohp = 1
% Set daynumbers for study

% Dec 13-16 2006 
dayn1 = 347;
dayn2 = 351;
x1 = dayn1;	
x2 = dayn2;		
cpx = 300;
cpx = 410;
radn = 10;
radn2 = 6;
radx = 22;
radx = 25;
radx2 = 40;
hpx = 210.;

bmag = 20;


% Set if plot S+N (for dynamo, ifboth=0) or separate (ifboth=1)
ifboth=1

of18x = 14;
%of18x = 12;
of24x = 9;
if ifboth==0
 of18x = 3;
 of24x = 4.5;
end

% Read file from tiegcm_kameleon for SWMF 
if2 = 0;
if (if2==1)
 filloc2 = sprintf('old_out51')
 locin2 = fopen(filloc2,'r');
for n=1:7*360+2*720
 linloc1b = fgetl(locin2);
 in1b = sscanf(linloc1b,'%f');
 linloc2b = fgetl(locin2);
 in2b = sscanf(linloc2b,'%f');
 if (n<=7*360)
  dmin2 = 1.;
  fracdaymo2(n) = 347. + (n-1)*dmin2/(24.*60.);
 else 
  dmin2 = 0.5;
  nm = n-7*360;
  fracdaymo2(n) = 348.75 + (nm-1)*dmin2/(24.*60.);
 end
 cps2(n) = in1b(12);
 cenrads2(n) = in1b(4);
 crads2(n) = in1b(6);
 offcs2(n) = in1b(8);
 dskofcs2(n) = in1b(10);
 cpn2(n) = in1b(13);
 cenradn2(n) = in1b(5);
 cradn2(n) = in1b(7);
 offcn2(n) = in1b(9);
 dskofcn2(n) = in1b(11);
% Save values used for theta0 from in2(1-2)
 userads2(n) = in2b(1);
 useradn2(n) = in2b(2);
% if (in2b(1)>20. | in2b(2)>20.)
%  n,fracdaymo2(n),in2b(1),in2b(2),cps2(n),cpn2(n)
% end
end
end	% if (if2==1)

% Read HP from UTh files
if (nohp==1)
 dmin = 1.
 if (ifaa==1)
  dathp = dlmread('../tiegcm_kameleon/UTh_astra_13-16dec06','',1,0);
  for n=1:4*1440
   fracdayhp(n) = 347. + (n-1)*dmin/(24.*60.);
  end
 else
  if (ifaa==0)
   dathp = dlmread('../tiegcm_kameleon/UTh_swmf_13-15dec06','',1,0);
   for n=1:3*1440
    fracdayhp(n) = 347. + (n-1)*dmin/(24.*60.);
   end
  end
  if (ifaa==2)
   dathp = dlmread('umi_amie_347-8_UTh','',1,0);
   for n=1:2880
    fracdayhp(n) = 347. + (n-1)*dmin/(24.*60.);
   end
  end
 end
 medhpn = median(dathp(:,4))
 medhps = median(dathp(:,3))
end

% Get number for 5 min OMNI data to skip over
nd0 = (dayn1-1)*12*24;


% Open 15-min lagged every 5 min (t=0 uses t=-20 to -5min) .nc IMF OMNI file
 Fname = sprintf('/hao/aim3/tgcm/data/imf_OMNI_2006001-2006365.nc')

% open .nc file Fname
    nc=netcdf(Fname,'nowrite');

    date=nc{'date'}(:);        % (year+daynum+fracday)
    bzyr=nc{'bz'}(:);
    byyr=nc{'by'}(:);
    bxyr=nc{'bx'}(:);
    vswyr=nc{'swvel'}(:);
    dswyr=nc{'swden'}(:);
    close(nc) 
    clear nc

n = 0;
nyn = 0;
nyp = 0;
% Get 1 extra day for IMF
%for nd=dayn1:dayn2-1
for nd=dayn1:dayn2
for nh=1:24
% Read every 5 min for OMNI
 for nl=1:12
% Find medians for 0-14 UT 14 Dec 2006:CPn,s=74.1,60.6kV; off24n,s=0.50,0.97deg;
%  off18n,s=+1.96,-1.96deg; cradn,s=10.94,11.96deg; By=-1.45; Bz=-1.02
%  ie, off18s~+By and off18n~-By
% Find medians for 08080-085 Mar 20-25 2008:CPn,s=33.2,32.6kV; off24n,s=0.50,0.97deg;
%  off18n,s=-3.88,+1.95deg; cradn,s=13.87,13.89deg; By=1.08; Bz=0.94
% Find medians for 08080-085 Mar 20-25 2008 #2:CPn,s=33.2,32.6kV; off24n,s=-0.97,0.97deg;
%  off18n,s=-1.94,+0.97deg; cradn,s=13.47,13.03deg (w Nan=0 so low?); By=1.08; Bz=0.94
%  Neg By: -0.88 vs -0.86; off24 -0.97N,+1.91S; off18 +0.97N,-0.97S   default 08080-85
%  Pos By: +1.79 vs +1.80; off24 -0.97N,+0.02S; off18 -3.91N,+2.94S   default
%  ie, off18s~+By and off18n~-By
%  if nh==14 & nl==1
  if nd== 86 & nh==1 & nl==1
   medby = median(by)
   medbz = median(bz)
   medbyneg = median(byneg)
   medbypos = median(bypos)
  end
 n = n + 1;
%  Assign 5-min IMF data
 daynfrac(n) = date(nd0+n)-year1*1000.;
 bx(n) = bxyr(nd0+n);
 by(n) = byyr(nd0+n);
 bz(n) = bzyr(nd0+n);
 vsw(n) = vswyr(nd0+n);
 dsw(n) = dswyr(nd0+n);
% Sort by By sign
  if (by(n)<0) 
    nyn = nyn + 1;
    byneg(nyn) = by(n);
  else
    nyp = nyp + 1;
    bypos(nyp) = by(n);
  end
 end
end
end	% do nd
% Had to add 1 at end for interpolation with 2min data at end until went 1 day more
% Recalc medians for whole period
   medby = median(by)
   medbz = median(bz)
n,nyn,nyp
   medbyneg = median(byneg)
   medbypos = median(bypos)

% Open file of outputs 
nhpd = 24;
dmin = 1;		% Read STEP=60sec or each minute
%dmin = 0.5;		% Read STEP=30sec or each minute
 nmph = 60/dmin;

if (ifaa==1)
 filloc = sprintf('../tiegcm_kameleon/out51_astra_13-16dec06')
else
 if (ifaa==0)
  filloc = sprintf('../tiegcm_kameleon/out51_swmf_13-15dec06')
 end
 if (ifaa==2)
  filloc = sprintf('out51_umi_amie_347-8');
 end
end
locin = fopen(filloc,'r');

nbadab = 0;
nbadas = 0;
nbadan = 0;
nbadcb = 0;
n = 0;
ntru = 0;
nrlo = 0;
nlo = 0;
nyn = 0;
nyp = 0;
ngs = 0;
ngn = 0;
nog = 0;
nogs = 0;
nogn = 0;
nog1 = 0;
nog60 = 0;
nog50 = 0;
nog40 = 0;
nog30 = 0;
for nd=dayn1:dayn2-1
nd,n
%nhpd = 24;
% if (nd==349)
%  nhpd = 6;
% end
ifrd = 1;
if (nd==dayn2-1 & ifaa==0)
 ifrd = 0;
end
if (nd>dayn1+1 & ifaa==2)
 ifrd = 0;
end
if (ifrd==1)
 for nh=1:nhpd
% Read every dmin minutes for Dec 13-16, 2006
% nmph = 60/dmin;
 for nl=1:nmph
% Find medians for 0-14 UT 14 Dec 2006:CPn,s=74.1,60.6kV; off24n,s=0.50,0.97deg;
%  off18n,s=+1.96,-1.96deg; cradn,s=10.94,11.96deg; By=-1.45; Bz=-1.02
%  ie, off18s~+By and off18n~-By
 if nd==348 & nh==14 & nl==1
 medby2 = median(by2)
 medcpn = median(cpn)
 medcps = median(cps)
 medoff24n = median(offcn)
 medoff24s = median(offcs)
 medoff18n = median(dskofcn)
 medoff18s = median(dskofcs)
 fp = isnan(cradn);
 cradn(fp) = 0;
 fp = isnan(crads);
 crads(fp) = 0;
 medcradn = median(cradn)
 medcrads = median(crads)
 meduseradn = median(useradn)
 meduserads = median(userads)
 medcpsnog = median(cpsnog)
 medcpnnog = median(cpnnog)

n,nogs,nogn,nm,np,fracdaymo(n),daynfrac(nm),daynfrac(np),frac1,frac2,by(nm),by(np),by2(n)
ngs,median(cpsg),median(cradsg),median(offcsg),median(dskofcsg)
ngn,median(cpng),median(cradng),median(offcng),median(dskofcng)
 end

% Read file from tiegcm_cmit on ys
 linloc1 = fgetl(locin);
 in1 = sscanf(linloc1,'%f');
 linloc2 = fgetl(locin);
 in2 = sscanf(linloc2,'%f');
 ntru = ntru + 1;

 fracdaynum = nd + (nh-1)/24. + (nl-1)*dmin/(24.*60.);

% Interpolate 5min IMF data to 1min CMIT times 
nm = fix((ntru-1)*dmin/5.+1);
np = nm + 1;
% daynfrac-dayn1+x1 = fracdaymo
 frac1 = (fracdaynum-daynfrac(nm)+dayn1-x1)/(daynfrac(np)-daynfrac(nm));
 frac2 = (daynfrac(np)-dayn1+x1-fracdaynum)/(daynfrac(np)-daynfrac(nm));
 bxint = frac2*bx(nm) + frac1*bx(np);
 byint = frac2*by(nm) + frac1*by(np);
 bzint = frac2*bz(nm) + frac1*bz(np);
 vswint = frac2*vsw(nm) + frac1*vsw(np);
 dswint = frac2*dsw(nm) + frac1*dsw(np);

 nogood = 0;
 nogoods = in1(2);
 nogoodn = in1(3);

% For new files with ifbad flags, in1(2,3)=ifbadS,N
   nogs = nogs + min(1,in1(2)); 
   nogn = nogn + min(1,in1(3));    
  if (nogoods>=1 & nogoodn>=1)
   nogood = 1;
  end

% TEMP See all!
% nogood = 0;


 n = n + 1;
% Assign interpolated IMF data
 bx2(n) = bxint;
 by2(n) = byint;
 bz2(n) = bzint;
 vsw2(n) = vswint;
 dsw2(n) = dswint;
% Assign read in data
 fracdaymo(n) = fracdaynum;
 cps(n) = in1(12);
 cenrads(n) = in1(4);
 crads(n) = in1(6);
 offcs(n) = in1(8);
 dskofcs(n) = in1(10);
 cpn(n) = in1(13);
 cenradn(n) = in1(5);
 cradn(n) = in1(7);
 offcn(n) = in1(9);
 dskofcn(n) = in1(11);
% Save values used for theta0 from in2(1-2)
 userads(n) = in2(1);
 useradn(n) = in2(2);
% if (in2(1)>20. | in2(2)>20.)
%  n,fracdaymo(n),nogoods,nogoodn,in2(1),in2(2),cps(n),cpn(n)
% end

% average CP and crad
 cradb(n) = 0.5*(in2(1)+in2(2));
 cpb(n) = 0.5*(cps(n)+cpn(n));

% save CP, offc,dskofc,crad for good vs nogood cases
  if (in1(2)>0)
   cpsnog(nogs) = cps(n);
  else
   ngs = ngs + 1;
   cpsg(ngs) = cps(n);
   offcsg(ngs) = offcs(n);
   dskofcsg(ngs) = dskofcs(n);
   cradsg(ngs) = crads(n);
  end
  if (in1(3)>0)
   cpnnog(nogn) = cpn(n);
  else
   ngn = ngn + 1;
   cpng(ngn) = cpn(n);
   offcng(ngn) = offcn(n);
   dskofcng(ngn) = dskofcn(n);
   cradng(ngn) = cradn(n);
  end




 end
end 
end	% if (ifrd==1)
end	% do nd
n,nogs,nogn,fracdaymo(n)

% Recalc medians for whole period
 medby2 = median(by2)
 medcpn = median(cpn)
 medcps = median(cps)

 medoff24n = median(offcn)
 medoff24s = median(offcs)
 medoff18n = median(dskofcn)
 medoff18s = median(dskofcs)

 medcradn = median(cradn)
 medcrads = median(crads)
 meduseradn = median(useradn)
 meduserads = median(userads)

 medcpsnog = median(cpsnog)
 medcpnnog = median(cpnnog)
ngs,median(cpsg),median(cradsg),median(offcsg),median(dskofcsg)
ngn,median(cpng),median(cradng),median(offcng),median(dskofcng)

% Find linear relations between 'good' CP and Rc
cpt = [0 cpx];
rscpt=polyfit(cpsg,cradsg,1)
rncpt=polyfit(cpng,cradng,1)
xscpt = polyval(rscpt,cpt);
xncpt = polyval(rncpt,cpt);

figure
zero = [0.,0.];
xtot = [x1,x2];
x14 = [348.+14./24.,348.+14./24.];


fsize = 12;
% position [left bottom width height]
% Set size to be 8.5x11 inches
set(gcf,'position',[142    99   647   822])
H = gcf;
clf

subplot('position',[0.10,0.82,0.87,0.12])	% 1/4 of 1/2
hold on;
%title('Convection Locations Etc for Dec 13-16, 2006 CMIT-TIEGCM','FontSize',15,'FontWeight','bold');
if (ifaa==1)
 title('Convection Locations Etc for Dec 13-16, 2006 ASTRA AMIE TIEGCM','FontSize',15,'FontWeight','bold');
else
 if (ifaa==0)
  title('Convection Locations Etc for Dec 13-15, 2006 SWMF TIEGCM','FontSize',15,'FontWeight','bold');
 end
 if (ifaa==2)
  title('Convection Locations Etc for Dec 13-14, 2006 U MI AMIE TIEGCM','FontSize',15,'FontWeight','bold');
 end
end

if ifboth==1
%plot (fracdaymo2,cps2,'-c');
%plot (fracdaymo2,cpn2,'-g');
plot (fracdaymo,cps,'-b');
plot (fracdaymo,cpn,'-r');
else
plot (fracdaymo,0.5*(cps+cpn),'-k');
text (x1+0.02*(x2-x1),0.90*cpx,'av(N+S)','Color','k','FontSize',fsize,'FontWeight','bold');
end
ytot = [0,cpx];
plot (x14,ytot,'--k');
if (ifaa==1)
 text (x2,0.90*cpx,'NH (57.9% bad CP~80kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (x2,0.75*cpx,'SH (24.6% bad CP~33kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (348.+13.5/24.,0.75*cpx,'NH (48.6% bad CP~49kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (348.+13.5/24.,0.60*cpx,'SH (21.9% bad CP~30kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (348.+13.5/24.,0.90*cpx,'median SH 36kV, NH 46kV','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (348.+14.5/24.,0.90*cpx,'SH 44kV, NH 68kV','Color','k','FontSize',fsize,'FontWeight','bold');
else
 if (ifaa==0)
  text (x2,0.90*cpx,'NH (33.2% bad CP~29kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*cpx,'SH (47.4% bad CP~28kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.75*cpx,'NH (43.3% bad CP~22kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.60*cpx,'SH (70.4% bad CP~25kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.90*cpx,'median SH 27kV, NH 29kV','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+14.5/24.,0.90*cpx,'SH 39kV, NH 46kV','Color','k','FontSize',fsize,'FontWeight','bold');
 end
 if (ifaa==2)
  text (x2,0.90*cpx,'NH (44.2% bad CP~53kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*cpx,'SH (45.0% bad CP~52kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.75*cpx,'NH (41.9% bad CP~40kV)','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.60*cpx,'SH (42.6% bad CP~38kV)','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+13.5/24.,0.90*cpx,'median SH 44kV, NH 43kV','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (348.+14.5/24.,0.90*cpx,'SH 51kV, NH 50kV','Color','k','FontSize',fsize,'FontWeight','bold');
 end
end
ylabel('CP kV','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[0,450],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[0,cpx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');
subplot('position',[0.10,0.70,0.87,0.12])	% 2/4 of 1/2
hold on;
if ifboth==1
%plot (fracdaymo2,offcs2,'-c');
%plot (fracdaymo2,offcn2,'-g');
plot (fracdaymo,offcs,'-b');
plot (fracdaymo,offcn,'-r');

else
plot (fracdaymo,0.5*(offcs+offcn),'-k');
end
text (x1+0.5*(x2-x1),0.75*of24x,'midnight','Color','k','FontSize',fsize,'FontWeight','bold');
text (x1+0.5*(x2-x1),-0.75*of24x,'noon','Color','k','FontSize',fsize,'FontWeight','bold');

if (ifaa==1)
 text (348.+13.5/24.,0.75*of24x,'median SH 0.00, NH -0.73','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (x2,0.75*of24x,'median SH 0.86 NH 1.31','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
else
 if (ifaa==0)
  text (348.+13.5/24.,0.75*of24x,'median SH 0.71, NH 1.52','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*of24x,'median SH 0.99 NH 1.41','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
 if (ifaa==2)
  text (348.+13.5/24.,0.75*of24x,'median SH 0.15, NH 0.26','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*of24x,'median SH 0.57 NH 0.67','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
end

ytot = [-of24x,of24x];
plot (x14,ytot,'--k');
ylabel('off24 deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[0,7],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[-of24x,of24x],'XTickLabel',{' ';},'FontSize',fsize,'box','on');
subplot('position',[0.10,0.58,0.87,0.12])	% 3/4 of 1/2
hold on;
if ifboth==1
%plot (fracdaymo2,dskofcs2,'-c');
%plot (fracdaymo2,dskofcn2,'-g');
plot (fracdaymo,dskofcs,'-b');
plot (fracdaymo,dskofcn,'-r');

else
plot (fracdaymo,0.5*(dskofcs+dskofcn),'-k');
end
plot (xtot,zero,'--k');
text (x1+0.5*(x2-x1),0.75*of18x,'18 MLT','Color','k','FontSize',fsize,'FontWeight','bold');
text (x1+0.5*(x2-x1),-0.75*of18x,'06 MLT','Color','k','FontSize',fsize,'FontWeight','bold');
ytot = [-of18x,of18x];
plot (x14,ytot,'--k');

if (ifaa==1)
 text (348.+13.5/24.,0.75*of18x,'median SH -1.07 NH 1.07','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (x2,0.75*of18x,'SH 1.07, NH 2.11','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
else
 if (ifaa==0)
  text (348.+13.5/24.,0.75*of18x,'median SH -1.07 NH 1.01','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*of18x,'SH 1.11, NH 0.00','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
 if (ifaa==2)
  text (348.+13.5/24.,0.75*of18x,'median SH -1.04 NH 1.04','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*of18x,'SH -1.07, NH 1.07','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
end
ylabel('off18 deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[-14,14],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[-of18x,of18x],'XTickLabel',{' ';},'FontSize',fsize,'box','on');
subplot('position',[0.10,0.46,0.87,0.12])	% 4/4 of 1/2
hold on;
if ifboth==1
%plot (fracdaymo2,crads2,'-c');
%plot (fracdaymo2,cradn2,'-g');
plot (fracdaymo,crads,'-b');
plot (fracdaymo,cradn,'-r');

else
 plot (fracdaymo,0.5*(crads+cradn),'-k');
 end
ytot = [radn,radx];
plot (x14,ytot,'--k');

if (ifaa==1)
 text (348.+13.5/24.,0.85*(radx-radn)+radn,'median SH 15.81 NH 15.85','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (x2,0.85*(radx-radn)+radn,'SH 15.96, NH 16.53','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
else
 if (ifaa==0)
  text (348.+13.5/24.,0.85*(radx-radn)+radn,'median SH 12.70 NH 13.67','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.85*(radx-radn)+radn,'SH 16.92, NH 15.85','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
 if (ifaa==2)
  text (348.+13.5/24.,0.85*(radx-radn)+radn,'median SH 13.70 NH 13.70','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.85*(radx-radn)+radn,'SH 13.70, NH 13.70','Color','k','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
end
ylabel('radius deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[5,30],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[radn,radx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');

subplot('position',[0.10,0.34,0.87,0.12])	% 5
hold on;
if (nohp==0) 
if ifboth==1
%plot (fracdaymo2,cenrads2,'-c');
%plot (fracdaymo2,cenradn2,'-g');
plot (fracdaymo,cenrads,'-b');
plot (fracdaymo,cenradn,'-r');

else
 plot (fracdaymo,0.5*(cenrads+cenradn),'-k');
 end
ytot = [radn,radx];
plot (x14,ytot,'--k');
ylabel('radcen deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[5,30],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[radn,radx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');

subplot('position',[0.10,0.22,0.87,0.12])	% 6
hold on;
if ifboth==1
%plot (fracdaymo2,userads2,'-c');
%plot (fracdaymo2,useradn2,'-g');
plot (fracdaymo,userads,'-b');
plot (fracdaymo,useradn,'-r');

else
 plot (fracdaymo,0.5*(userads+useradn),'-k');
 end
ytot = [radn,radx];
plot (x14,ytot,'--k');
ylabel('userad deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[5,30],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{' ';'3';'6';'9';'12';'15';'18';'21';'0';'3';' ';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[radn,radx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');

else		% if (nohp==0)
if ifboth==1
%plot (fracdaymo2,userads2,'-c');
%plot (fracdaymo2,useradn2,'-g');
plot (fracdaymo,userads,'-b');
plot (fracdaymo,useradn,'-r');
else
 plot (fracdaymo,0.5*(userads+useradn),'-k');
 end
ytot = [radn,radx];
plot (x14,ytot,'--k');
ylabel('userad deg','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
set (gca,'xlim',[x1,x2],'ylim',[radn,radx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');

subplot('position',[0.10,0.22,0.87,0.12])	% 6
hold on;
if (ifaa==1)
 text (x2,0.90*hpx,'NH  HP~22.0GW','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (x2,0.75*hpx,'SH HP~19.4GW','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
else
 if (ifaa==0)
  text (x2,0.90*hpx,'NH  HP~36.9GW','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*hpx,'SH HP~33.2GW','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
 if (ifaa==2)
  text (x2,0.90*hpx,'NH  HP~36.9GW','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (x2,0.75*hpx,'SH HP~33.2GW','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
end
if ifboth==1
plot (fracdayhp,dathp(:,3),'-b');
plot (fracdayhp,dathp(:,4),'-r');
else
 plot (fracdayhp,0.5*(dathp(:,3)+dathp(:,4)),'-k');
 end
ytot = [0,hpx];
plot (x14,ytot,'--k');
ylabel('HP (GW)','FontSize',fsize,'FontWeight','bold');
xlabel(' ','FontSize',fsize,'FontWeight','bold');
grid;
set (gca,'xlim',[x1,x2],'ylim',[0,hpx],'XTickLabel',{' ';},'FontSize',fsize,'box','on');

end		% if (nohp==0), else
subplot('position',[0.10,0.10,0.87,0.12])	% 7
hold on;
% fracdaymo = dayfracn - 348 + 14
plot (daynfrac-dayn1+x1,by,'-b');
plot (daynfrac-dayn1+x1,bz,'-r');
plot (xtot,zero,'--k');
ytot = [-bmag,bmag];
plot (x14,ytot,'--k');
ylabel('15m IMF nT','FontSize',fsize,'FontWeight','bold');
xlabel('Daynumber','FontSize',fsize,'FontWeight','bold');
text (x2-0.05*(x2-x1),0.80*bmag,'Bz','Color','r','FontSize',fsize,'FontWeight','bold');
text (x2-0.05*(x2-x1),0.55*bmag,'By','Color','b','FontSize',fsize,'FontWeight','bold');
grid;
%set (gca,'xlim',[x1,x2],'ylim',[-19,19],'XTick',[14 14.125 14.25 14.375 14.5 14.625 14.75 14.875 15. 15.125 15.25],'XTickLabel',{'0';'3';'6';'9';'12';'15';'18';'21';'0';'3';'6';},'FontSize',fsize,'box','on');
set (gca,'xlim',[x1,x2],'ylim',[-bmag,bmag],'FontSize',fsize,'box','on');
hold off;




%tit_daynum = sprintf('Dec 13-16, 2006 CMIT-TIEGCM');
if (ifaa==1)
 tit_daynum = sprintf('Dec 13-16, 2006 ASTRA AMIE TIEGCM');
else
 if (ifaa==0)
  tit_daynum = sprintf('Dec 13-15, 2006 SWMF TIEGCM');
 end
 if (ifaa==2)
  tit_daynum = sprintf('Dec 13-14, 2006 U MI AMIE TIEGCM');
 end
end

figure
y18=11;
y24=6;


% position [left bottom width height]
% Set size to be 8.5x11 inches
set(gcf,'position',[142    99   647   822])
H = gcf;

 subplot('Position',[0.07,0.54,0.35,0.42])     % upper left in 4
 hold on;
title(tit_daynum,'FontSize',15,'FontWeight','bold');
 plot(by2,dskofcs,'kx');
 med = medoff18s;
 medt = [med,med];

 ylabel('SH Off18MLT','FontSize',fsize,'Color','k');
 xlabel('By (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y18 y18],'FontSize',fsize,'box','on');

 subplot('Position',[0.555,0.54,0.35,0.42])     % upper right in 4
 hold on;
 plot(by2,offcs,'kx');
 med = medoff24s;
 medt = [med,med];

 ylabel('SH Off24MLT','FontSize',fsize,'Color','k');
 xlabel('By (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y24 y24],'FontSize',fsize,'box','on');

 subplot('Position',[0.07,0.04,0.35,0.42])     % bottom left in 4
 hold on;
 plot(by2,dskofcn,'kx');
 med = medoff18n;
 medt = [med,med];

 ylabel('NH Off18MLT','FontSize',fsize,'Color','k');
 xlabel('By (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y18 y18],'FontSize',fsize,'box','on');

 subplot('Position',[0.555,0.04,0.35,0.42])     % bottom right in 4
 hold on;
 plot(by2,offcn,'kx');
 med = medoff24n;
 medt = [med,med];
 
 ylabel('NH Off24MLT','FontSize',fsize,'Color','k');
 xlabel('By (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y24 y24],'FontSize',fsize,'box','on');
 
hold off;

figure
y18=11;
y24=6;

% position [left bottom width height]
% Set size to be 8.5x11 inches
set(gcf,'position',[142    99   647   822])
H = gcf;

 subplot('Position',[0.07,0.54,0.35,0.42])     % upper left in 4
 hold on;
title(tit_daynum,'FontSize',15,'FontWeight','bold');
 plot(bz2,dskofcs,'kx');

 ylabel('SH Off18MLT','FontSize',fsize,'Color','k');
 xlabel('Bz (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y18 y18],'FontSize',fsize,'box','on');

 subplot('Position',[0.555,0.54,0.35,0.42])     % upper right in 4
 hold on;
 plot(bz2,offcs,'kx');

 ylabel('SH Off24MLT','FontSize',fsize,'Color','k');
 xlabel('Bz (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y24 y24],'FontSize',fsize,'box','on');

 subplot('Position',[0.07,0.04,0.35,0.42])     % bottom left in 4
 hold on;
 plot(bz2,dskofcn,'kx');

 ylabel('NH Off18MLT','FontSize',fsize,'Color','k');
 xlabel('Bz (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y18 y18],'FontSize',fsize,'box','on');

 subplot('Position',[0.555,0.04,0.35,0.42])     % bottom right in 4
 hold on;
 plot(bz2,offcn,'kx');

 ylabel('NH Off24MLT','FontSize',fsize,'Color','k');
 xlabel('Bz (nT)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[-10 10],'ylim',[-y24 y24],'FontSize',fsize,'box','on');
 
hold off;


figure

% position [left bottom width height]
% Set size to be 8.5x11 inches
%set(gcf,'position',[142    99   647   822])
%H = gcf;
% subplot('Position',[0.07,0.06,0.85,0.40]) 
 hold on;
title(tit_daynum,'FontSize',15,'FontWeight','bold');
 plot (cps,userads,'xb');
 plot (cpn,useradn,'+r');
 plot (cpt,xscpt,'-b');
 plot (cpt,xncpt,'-r');
if (ifaa==1)
 text (0.99*cpx,0.95*radx2,'NH Rc=15.3+0.0195*CP','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 text (0.99*cpx,0.90*radx2,'SH Rc=14.8+0.0257*CP','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
else
 if (ifaa==0)
  text (0.99*cpx,0.95*radx2,'NH Rc=12.0+0.0586*CP','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (0.99*cpx,0.90*radx2,'SH Rc=12.4+0.0640*CP','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
 if (ifaa==2)
  text (0.99*cpx,0.95*radx2,'NH Rc=13.8-0.0066*CP','Color','r','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
  text (0.99*cpx,0.90*radx2,'SH Rc=13.8-0.0063*CP','Color','b','FontSize',fsize,'FontWeight','bold','HorizontalAlignment','right');
 end
end
 ylabel ('Convection Radius (deg)','FontSize',fsize,'Color','k');
 xlabel ('CP (kV)','FontSize',fsize,'Color','k');
grid;
set (gca,'xlim',[0 cpx],'ylim',[radn2 radx2],'FontSize',fsize,'box','on');

hold off;





