(* CHECKLIST:
 1. File Load name
2. plottitle
3. searchstationnum
4. adjnum
5. save plot name
*)

AKgage=OpenRead["Z:\\CHL-Archive-Projects\\Texas-Studies\\Hydro-observations\\StormSurge-data\\2008-Ike\\Observations-sourcedata\\ND-AndrewKennedy-Deployable-Hydrographs+HighResWaves\\out_wave_Y.txt"];
AKper=OpenRead["Z:\\CHL-Archive-Projects\\Texas-Studies\\Hydro-observations\\StormSurge-data\\2008-Ike\\Observations-sourcedata\\ND-AndrewKennedy-Deployable-Hydrographs+HighResWaves\\out_per_Y.txt"];
finaltitle="OWI_short_notides_AK_wave+per_Collins+Madsen_Y.png";
gridtitle="Dr. Kennedy Station Y";
searchstationnum=16;
filew611=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_HS_wave.61"
];
filew612=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_HS_Collins.61"
];
filew613=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_HS_Madsen.61"
];
filep611=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_TPS.61"
];
filep612=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_TPS_Collins.61"
];
filep613=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\NDBC_AK_TPS_Madsen.61"
];





(* Wave HS Plot - Data *)
plotstart=AbsoluteTime[{2008,9,9,0}];
plotend=AbsoluteTime[{2008,9,16,12}];

raw=ReadList[AKgage, String];
raw=StringSplit[raw];
raw=Drop[raw,-1];
LengthData=Length[raw];
PlotArray={};

For[i=1, i≤LengthData,i++,
If[Length[raw[[i]]]==2,
raw[[i]]=Append[raw[[i]], ToExpression[M]]
];

yearday=ToExpression[raw[[i,1]]];
hour=ToExpression[raw[[i,2]]];

PlotArray=Append[PlotArray,{AbsoluteTime[{2008,1,yearday,hour,0,0}],ToExpression[raw[[i,3]]]}];
];

PlotLength=Length[PlotArray];

plot=ListPlot[PlotArray,
Joined->True,
Frame->True,
PlotRange->{{plotstart,plotend},{0.0,7}},
ImageSize->500,
Axes->False,
FrameTicks->  { { {AbsoluteTime[{2008,9,7,0,0,0}],"9/7"},{AbsoluteTime[{2008,9,8,0,0,0}],"9/8"},{AbsoluteTime[{2008,9,9,0,0,0}],"9/9"},{AbsoluteTime[{2008,9,10,0,0,0}],"9/10"},{AbsoluteTime[{2008,9,11,0,0,0}],"9/11"},{AbsoluteTime[{2008,9,12,0,0,0}],"9/12"},{AbsoluteTime[{2008,9,13,0,0,0}],"9/13"},{AbsoluteTime[{2008,9,14,0,0,0}],"9/14"},{AbsoluteTime[{2008,9,15,0,0,0}],"9/15"},{AbsoluteTime[{2008,9,16,0,0,0}],"9/16"}}, {-.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7}, None, None} ,
ImageMargins->{{5,20},{10,10}},
PlotStyle->{Black,Dashed},
FrameLabel->{Style[""],Style["Wave Height (m)",12]},
BaseStyle->{FontFamily->"Times",FontSize->10},
AspectRatio->0.75,
GridLines->{{AbsoluteTime[{2008,9,9,0}],{AbsoluteTime[{2008,9,9,12}],Dashed},AbsoluteTime[{2008,9,10,0}],{AbsoluteTime[{2008,9,10,12}],Dashed},
AbsoluteTime[{2008,9,11,0}],{AbsoluteTime[{2008,9,11,12}],Dashed},AbsoluteTime[{2008,9,12,0}],{AbsoluteTime[{2008,9,12,12}],Dashed},AbsoluteTime[{2008,9,13,0}],
{AbsoluteTime[{2008,9,13,8,10}],{Dashed,Green,Thick}},
{AbsoluteTime[{2008,9,13,12}],Dashed},
AbsoluteTime[{2008,9,14,0}],{AbsoluteTime[{2008,9,14,12}],Dashed},
AbsoluteTime[{2008,9,15,0}],{AbsoluteTime[{2008,9,15,12}],Dashed},AbsoluteTime[{2008,9,16,0}]},
{-.5,0,{0.5,Dashed},1,{1.5,Dashed},2,{2.5,Dashed},3,{3.5,Dashed},4,{4.5,Dashed},5,{5.5,Dashed},6,{6.5,Dashed}}}
];



(* Wave Period Plot - Data *)
plotstart=AbsoluteTime[{2008,9,9,0}];
plotend=AbsoluteTime[{2008,9,16,12}];

NAVD88adj=0;

raw=ReadList[AKper, String];
raw=StringSplit[raw];
raw=Drop[raw,-1];
LengthData=Length[raw];
perArray={};

For[i=1, i≤LengthData,i++,
If[Length[raw[[i]]]==2,
raw[[i]]=Append[raw[[i]], ToExpression[M]]
];

yearday=ToExpression[raw[[i,1]]];
hour=ToExpression[raw[[i,2]]];

perArray=Append[perArray,{AbsoluteTime[{2008,1,yearday,hour,0,0}],ToExpression[raw[[i,3]]]}];
];

PlotLength=Length[perArray];

perplot=ListPlot[perArray,
Joined->True,
Frame->True,
PlotRange->{{plotstart,plotend},{0.0,20}},
ImageSize->500,
Axes->False,
FrameTicks->  { { {AbsoluteTime[{2008,9,7,0,0,0}],"9/7"},{AbsoluteTime[{2008,9,8,0,0,0}],"9/8"},{AbsoluteTime[{2008,9,9,0,0,0}],"9/9"},{AbsoluteTime[{2008,9,10,0,0,0}],"9/10"},{AbsoluteTime[{2008,9,11,0,0,0}],"9/11"},{AbsoluteTime[{2008,9,12,0,0,0}],"9/12"},{AbsoluteTime[{2008,9,13,0,0,0}],"9/13"},{AbsoluteTime[{2008,9,14,0,0,0}],"9/14"},{AbsoluteTime[{2008,9,15,0,0,0}],"9/15"},{AbsoluteTime[{2008,9,16,0,0,0}],"9/16"}}, {0,2,4,6,8,10,12,14,16,18,20}, None, None} ,
ImageMargins->{{5,20},{10,10}},
PlotStyle->{Black,Dashed},
FrameLabel->{Style[""],Style["Peak Period (s)",12]},
BaseStyle->{FontFamily->"Times",FontSize->10},
AspectRatio->0.75,
GridLines->{{AbsoluteTime[{2008,9,9,0}],{AbsoluteTime[{2008,9,9,12}],Dashed},AbsoluteTime[{2008,9,10,0}],{AbsoluteTime[{2008,9,10,12}],Dashed},
AbsoluteTime[{2008,9,11,0}],{AbsoluteTime[{2008,9,11,12}],Dashed},AbsoluteTime[{2008,9,12,0}],{AbsoluteTime[{2008,9,12,12}],Dashed},AbsoluteTime[{2008,9,13,0}],
{AbsoluteTime[{2008,9,13,8,10}],{Dashed,Green,Thick}},
{AbsoluteTime[{2008,9,13,12}],Dashed},
AbsoluteTime[{2008,9,14,0}],{AbsoluteTime[{2008,9,14,12}],Dashed},
AbsoluteTime[{2008,9,15,0}],{AbsoluteTime[{2008,9,15,12}],Dashed},AbsoluteTime[{2008,9,16,0}]},
{0,2,4,6,8,10,12,14,16,18,20}}
];



(* 61 wave plotter 1*)
starttime61=AbsoluteTime[{2008,9,5,12}];

Skip[filew611,String,1];
line1=Read[filew611,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
wavearray1={};

For[i=1,i≤numsteps-1,i++,
Skip[filew611,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filew611,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
wavearray1=Append[wavearray1,{starttime61+dt*(i-1),line1[[2]]}];
];
];
];

waveplot1=ListPlot[wavearray1,PlotStyle->{Thick,Red},Joined->True];

(* 61 wave plotter 2*)
starttime61=AbsoluteTime[{2008,9,9,12}];

Skip[filew612,String,1];
line1=Read[filew612,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
wavearray2={};

For[i=1,i≤numsteps-1,i++,
Skip[filew612,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filew612,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
wavearray2=Append[wavearray2,{starttime61+dt*(i-1),line1[[2]]}];
];
];
];

waveplot2=ListPlot[wavearray2,PlotStyle->{Thick,Gray},Joined->True];

(* 61 wave plotter 3*)
starttime61=AbsoluteTime[{2008,9,9,12}];

Skip[filew613,String,1];
line1=Read[filew613,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
wavearray3={};

For[i=1,i≤numsteps-1,i++,
Skip[filew613,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filew613,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
wavearray3=Append[wavearray3,{starttime61+dt*(i-1),line1[[2]]}];
];
];
];

waveplot3=ListPlot[wavearray3,PlotStyle->{Thick,Brown},Joined->True];



(* 61 period plotter 1 *)
starttime61=AbsoluteTime[{2008,9,5,12}];

Skip[filep611,String,1];
line1=Read[filep611,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
perarray1={};

For[i=1,i≤numsteps-1,i++,
Skip[filep611,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filep611,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
perarray1=Append[perarray1,{starttime61+dt*(i-1),line1[[2]]}];
];
];
If[Abs[perarray1[[i,2]]]>60,
perarray1[[i,2]]="E"
];
];

perplot1=ListPlot[perarray1,PlotStyle->{Thick,Red},Joined->True];


(* 61 period plotter 2*)
starttime61=AbsoluteTime[{2008,9,9,12}];

Skip[filep612,String,1];
line1=Read[filep612,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
perarray2={};

For[i=1,i≤numsteps-1,i++,
Skip[filep612,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filep612,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
perarray2=Append[perarray2,{starttime61+dt*(i-1),line1[[2]]}];
];
];
If[Abs[perarray2[[i,2]]]>60,
perarray2[[i,2]]="E"
];
];
perplot2=ListPlot[perarray2,PlotStyle->{Thick,Gray},Joined->True];


(* 61 period plotter 3*)
starttime61=AbsoluteTime[{2008,9,9,12}];

Skip[filep613,String,1];
line1=Read[filep613,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
perarray3={};

For[i=1,i≤numsteps-1,i++,
Skip[filep613,String,1];
For[j=1,j≤numstations,j++,
line1=Read[filep613,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
perarray3=Append[perarray3,{starttime61+dt*(i-1),line1[[2]]}];
];
];
If[Abs[perarray3[[i,2]]]>60,
perarray3[[i,2]]="E"
]
];
perplot3=ListPlot[perarray3,PlotStyle->{Thick,Brown},Joined->True];



box=Graphics[{EdgeForm[Thick],White,Rectangle[{AbsoluteTime[{2008,9,9,4}],6.05}, {AbsoluteTime[{2008,9,11.1}],6.95}],
Text[Style["Data",Darker[Blue],Bold,12],{AbsoluteTime[{2008,9,10.15}],6.75}],
Text[Style["ADCIRC+SWAN",Red,Bold,12],{AbsoluteTime[{2008,9,10.15}],6.5}],
Text[Style["Landfall",Green,Bold,12],{AbsoluteTime[{2008,9,10.15}],6.25}]
}];


(*
(* Difference Plot *)
diffarray={};
interp=Interpolation[hydroarray,InterpolationOrder->1];

For[i=1,i≤LengthData,i++,
If[PlotArray[[i,1]]>AbsoluteTime[{2008,9,15,12}],
Break[]
];
diffarray=Append[diffarray,{PlotArray[[i,1]],PlotArray[[i,2]]-interp[PlotArray[[i,1]]]}]
];

diffplot=ListPlot[diffarray,PlotStyle->{Thick,Black, Dashed},Joined->True];


startsearch=AbsoluteTime[{2008,9,14}];
endsearch=AbsoluteTime[{2008,9,15}];
*)

(*
(* Find peak difference *)
max=-9999;

For[i=1,i≤Length[diffarray],i++,
If[diffarray[[i,1]]>startsearch && diffarray[[i,1]]<endsearch,
If[diffarray[[i,2]]>max,
max=diffarray[[i,2]];
peaktime=diffarray[[i,1]]
];
];
];

max
DateList[peaktime]
*)

(*
(* Plot second data set *)
file61=OpenRead[
"C:\\Documents and Settings\\Mark\\Desktop\\qc\\Ike\\hydrographs\\ike_extended_storm+tide_fort.61"
];

starttime61=AbsoluteTime[{2008,9,5,12}];

Skip[file61,String,1];
line1=Read[file61,{Number,Number,Number,String}];
numsteps=line1[[1]];
numstations=line1[[2]];
dt=line1[[3]];
totallines=numsteps*(numstations+1);
hydroarray2={};

For[i=1,i≤numsteps,i++,
Skip[file61,String,1];
For[j=1,j≤numstations,j++,
line1=Read[file61,{Number,Number}];
node=line1[[1]];
If[node==searchstationnum,
hydroarray2=Append[hydroarray2,{starttime61+dt*(i-1),line1[[2]]}];
];
];
];

hydroplot2=ListPlot[hydroarray2,PlotStyle->{Thick,Gray},Joined->True];
*)


comp3box=Graphics[{EdgeForm[Thick],White,Rectangle[{AbsoluteTime[{2008,9,9,4}],5.55}, {AbsoluteTime[{2008,9,12.1}],6.9}],
Text[Style["JONSWAP- OWI Long",Red,Bold,14],{AbsoluteTime[{2008,9,10.65}],6.75}],
Text[Style["Collins - OWI Short",Gray,Bold,14],{AbsoluteTime[{2008,9,10.65}],6.5}],
Text[Style["Madsen - OWI Short",Brown,Bold,14],{AbsoluteTime[{2008,9,10.65}],6.25}],
Text[Style["Data",Black,14],{AbsoluteTime[{2008,9,10.65}],6.00}],
Text[Style["Landfall",Green,Bold,14],{AbsoluteTime[{2008,9,10.65}],5.75}]
}];


finalwaveplot=Show[plot,waveplot1,waveplot2,waveplot3, comp3box];

finalperplot=Show[perplot,perplot1,perplot2,perplot3];
(* difffullplotZ=Show[plot,hydroplot,diffplot, box] *)
(* compplotY=Show[plot,hydroplot,hydroplot2, compbox] *)

grid=GraphicsGrid[{{finalwaveplot},{finalperplot}}];


finalgridY=Show[Graphics[{EdgeForm[Black],White,Rectangle[{0,25},{540,-820}]}], 
grid,
Graphics[Text[Style["Date 2008",Black,18],{265,-800}]],
Graphics[Text[Style[gridtitle,Black,18],{270,5}]]
,ImageSize->600
]


 Export[finaltitle,finalgridY]

box1=Graphics[{EdgeForm[Thick],White,Rectangle[{AbsoluteTime[{2008,9,9,4}],2.55}, {AbsoluteTime[{2008,9,10,20}],2.95}],Text[Style["Difference",Black,Bold,14],{AbsoluteTime[{2008,9,10}],2.75}]
}];


diffplotonly=ListPlot[diffarray,
Joined->True,
Frame->True,
PlotRange->{{plotstart,plotend},{-1.0,3.0}},
ImageSize->500,
Axes->False,
FrameTicks->  { { {AbsoluteTime[{2008,9,7,0,0,0}],"9/7"},{AbsoluteTime[{2008,9,8,0,0,0}],"9/8"},{AbsoluteTime[{2008,9,9,0,0,0}],"9/9"},{AbsoluteTime[{2008,9,10,0,0,0}],"9/10"},{AbsoluteTime[{2008,9,11,0,0,0}],"9/11"},{AbsoluteTime[{2008,9,12,0,0,0}],"9/12"},{AbsoluteTime[{2008,9,13,0,0,0}],"9/13"},{AbsoluteTime[{2008,9,14,0,0,0}],"9/14"},{AbsoluteTime[{2008,9,15,0,0,0}],"9/15"},{AbsoluteTime[{2008,9,16,0,0,0}],"9/16"}}, {-.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5}, None, None} ,
ImageMargins->{{5,20},{10,10}},
PlotStyle->{Thick,Black,Dashed},
FrameLabel->{Style["Date - 2008",12],Style["m above NAVD88",12]},
PlotLabel->Style[plottitle,16],
BaseStyle->{FontFamily->"Times",FontSize->10},
AspectRatio->0.75,
GridLines->{{AbsoluteTime[{2008,9,9,0}],{AbsoluteTime[{2008,9,9,12}],Dashed},AbsoluteTime[{2008,9,10,0}],{AbsoluteTime[{2008,9,10,12}],Dashed},
AbsoluteTime[{2008,9,11,0}],{AbsoluteTime[{2008,9,11,12}],Dashed},AbsoluteTime[{2008,9,12,0}],{AbsoluteTime[{2008,9,12,12}],Dashed},AbsoluteTime[{2008,9,13,0}],
{AbsoluteTime[{2008,9,13,8,10}],{Dashed,Green,Thick}},
{AbsoluteTime[{2008,9,13,12}],Dashed},
AbsoluteTime[{2008,9,14,0}],{AbsoluteTime[{2008,9,14,12}],Dashed},
AbsoluteTime[{2008,9,15,0}],{AbsoluteTime[{2008,9,15,12}],Dashed},AbsoluteTime[{2008,9,16,0}]},
{-.5,{0,Thick},{0.5,Dashed},1,{1.5,Dashed},2,{2.5,Dashed},3,{3.5,Dashed},4,{4.5,Dashed},5,{5.5,Dashed}}}
];

diffplotX=Show[diffplotonly,box1]

(*
GridPlot=GraphicsGrid[{{finalplotR,finalplotS,
finalplotU,finalplotV},
{finalplotW,finalplotX,
finalplotY,finalplotZ}},ImageSize->2200
]

GriddifffullPlot=GraphicsGrid[{{difffullplotR,difffullplotS,
difffullplotU,difffullplotV},
{difffullplotW,difffullplotX,
difffullplotY,difffullplotZ}},ImageSize->1800
]

GriddiffPlot=GraphicsGrid[{{diffplotR,diffplotS,
diffplotU,diffplotV},
{diffplotW,diffplotX,
diffplotY,diffplotZ}},ImageSize->1800
]

GridcompPlot=GraphicsGrid[{{compplotR,compplotS,
compplotU,compplotV},
{compplotW,compplotX,
compplotY,compplotZ}},ImageSize->2200
];
*)

fullfinalgrid=GraphicsGrid[{{finalgridR,finalgridS,finalgridU,finalgridV},
{finalgridW,finalgridX,finalgridY,finalgridZ}}];
box1=Graphics[Text[Style["Comparison of ADCIRC+UnSWAN Sig. Wave Height & Peak Period Results at Dr. A Kennedy's Stations for Various Bottom Friction Parameters",Black,Bold,28,FontFamily->"Helvetica"],{1250,10}]
];
FINAL=Show[fullfinalgrid,box1]
Export["OWI_short_notides_AK_wave+per_Collins+Madsen.png",FINAL]