(* 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_Z.txt"
];

finaltitle="ike_extended_storm+tide_AK_hydrographs_Z.jpg"
plottitle="Dr_Kennedy_Station_Z";
searchstationnum=123;
adjnum=8;


adjmatrix={-15.62+0.146 (* R --> 1 *),
-12.3+0.146 (* S --> 2 *),
-13.9+0.152 (* U --> 3 *),
-12.5+0.152 (* V --> 4 *),
-12.8+0.152 (* W --> 5 *),
-9.3+0.125 (* X --> 6 *),
-8.45+0.152 (* Y --> 7 *),
-9.2+0.152 (* Z --> 8 *)};

plotstart=AbsoluteTime[{2008,9,9,0}];
plotend=AbsoluteTime[{2008,9,16,12}];

NAVD88adj=adjmatrix[[adjnum]];

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]]]+NAVD88adj}];
];

PlotLength=Length[PlotArray];

plot=ListPlot[PlotArray,
Joined->True,
Frame->True,
PlotRange->{{plotstart,plotend},{-0.5,6}},
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},
FrameLabel->{Style["Date - 2008",12],Style["m above NAVD88",12]},
PlotLabel->Style[plottitle,16],
BaseStyle->{FontFamily->"Times",FontSize->10},
AspectRatio->1,
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}}}
];

(* 61 plotter *)
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);
hydroarray={};

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,
hydroarray=Append[hydroarray,{starttime61+dt*(i-1),line1[[2]]}];
];
];
];

hydroplot=ListPlot[hydroarray,PlotStyle->{Thick,Red},Joined->True];


(* 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];


box=Graphics[{EdgeForm[Thick],White,Rectangle[{AbsoluteTime[{2008,9,9,4}],5.05}, {AbsoluteTime[{2008,9,10,20}],5.95}],
Text[Style["ADCIRC",Red,Bold,14],{AbsoluteTime[{2008,9,10}],5.75}],
Text[Style["Data",Darker[Blue],Bold,14],{AbsoluteTime[{2008,9,10}],5.5}],
Text[Style["Landfall",Green,Bold,14],{AbsoluteTime[{2008,9,10}],5.25}]
}];

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]]
];
];
];


(* 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];

compbox=Graphics[{EdgeForm[Thick],White,Rectangle[{AbsoluteTime[{2008,9,9,4}],4.80}, {AbsoluteTime[{2008,9,12.1}],5.9}],
Text[Style["Data",Darker[Blue],Bold,14],{AbsoluteTime[{2008,9,10.65}],5.75}],
Text[Style["OWI Short Data w/ waves",Red,Bold,14],{AbsoluteTime[{2008,9,10.65}],5.5}],
Text[Style["OWI Long Data w/o waves",Gray,Bold,14],{AbsoluteTime[{2008,9,10.65}],5.25}],
Text[Style["Landfall",Green,Bold,14],{AbsoluteTime[{2008,9,10.65}],5.00}]
}];




finalplot=Show[plot,hydroplot,box] 
(* difffullplotZ=Show[plot,hydroplot,diffplot, box] *)
(* compplotY=Show[plot,hydroplot,hydroplot2, compbox] *)

Export[finaltitle,finalplot]

diffarray[[Length[diffarray]]]
i

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->1800
]

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
];
box1=Graphics[{White,Rectangle[{80,1.0}, {2110,100}],Text[Style["Comparison of OWI Short Winds with waves and OWI Extended Winds without Waves",Black,Bold,32,FontFamily->"Helvetica"],{1100,20}]
}];


finalcomp=Show[GridcompPlot,box1]

Export["UND-AK_hydrographs_fulldiff_wtides_090917.png",GriddifffullPlot]
Export["UND-AK_hydrographs_diffonly_wtides_090917.png",GriddiffPlot]

Export["UND-AK_hydrographs_OWI_comp_090922.png",finalcomp]