SetDirectory["C:\\Users\\jasmi\Desktop\\LIGO\\Scatter_data_anomag" ];

files=FileNames["*txt"];(* Imports txt files from directory*)
LIGOwatermark=Blend[{Import["ligologo.png"],White},0.9]; (* Imports LIGO logo *)

(*Generates tick marks*)
ticksizeS=0.006;
ticksizeL=0.012;
xstepS=5;
xstepL=20;
xmin=-200;
xmax=100;
LstepS=1/1000000000;
LstepL=5/1000000000;
Lmin=1 1/100000000;
Lmax=50 1/100000000;
ystepS=5/100;
ystepL=1/10;
ymin=0;
ymax=10^0;
hh=4.135667516 10^-15;
cc=299792458;

ticksX=Partition[Riffle[Riffle[Table[i,{i,xmin,xmax,xstepS}],Table[If[IntegerQ[i/xstepL],Round[N[i]],""],{i,xmin,xmax,xstepS}]],Table[If[IntegerQ[i/xstepL],{ticksizeL,0},{ticksizeS,0}],{i,xmin,xmax,xstepS}],3],3];

ticksY=Partition[Riffle[Riffle[Table[N[i ],{i,ymin,ymax,ystepS}],Table[If[IntegerQ[i/ystepL],N[i],""],{i,ymin,ymax,ystepS}]],Table[If[IntegerQ[i/ystepL],{ticksizeL,0},{ticksizeS,0}],{i,ymin,ymax,ystepS}],3],3];
ystepS=1;
ystepL=10;
ymin=10^-6;
ymax=10^6;



ticksYLog=Partition[Riffle[Riffle[

Table[N[Log[i]],{i,Join[Table[j,{j,10^-8,10^-7,0.1 10^-7}], Table[j,{j,10^-7,10^-6,0.1 10^-6}],Table[j,{j,10^-6,10^-5,0.1 10^-5}],Table[j,{j,10^-5,10^-4,0.1 10^-4}],Table[j,{j,10^-4,10^-3,0.1 10^-3}],Table[j,{j,10^-3,10^-2,0.1 10^-2}],Table[j,{j,10^-2,10^-1,0.1 10^-1}],Table[j,{j,10^-1,10^0,0.1 10^0}],Table[j,{j,10^0,10^1,0.1 10^1}],Table[j,{j,10^1,10^2,0.1 10^2}],Table[j,{j,10^2,10^3,0.1 10^3}],Table[j,{j,10^3,10^4,0.1 10^4}],Table[j,{j,10^4,10^5,0.1 10^5}],Table[j,{j,10^5,10^6,0.1 10^6}],Table[j,{j,10^6,10^7,0.1 10^7}]]}],

Join[{"(10^(-8))"},Table["",{i,1,9}],{"(10^(-7))"},Table["",{i,1,9}],{"(10^(-6))"},Table["",{i,1,9}],{"(10^(-5))"},Table["",{i,1,9}],{"(10^(-4))"},Table["",{i,1,9}],{"(10^(-3))"},Table["",{i,1,9}],{"(10^(-2))"},Table["",{i,1,9}],{"(10^(-1))"},Table["",{i,1,9}],{"(10^0)  "},Table["",{i,1,9}],{"\!\(\*SuperscriptBox[\(10\), \(\(1\)\(\\ \)\)]\) "},Table["",{i,1,9}],{"\!\(\*SuperscriptBox[\(10\), \(\(2\)\(\\ \)\)]\) "},Table["",{i,1,9}],{"\!\(\*SuperscriptBox[\(10\), \(\(3\)\(\\ \)\)]\) "},Table["",{i,1,9}],{"\!\(\*SuperscriptBox[\(10\), \(\(4\)\(\\ \)\)]\) "},Table["",{i,1,9}],{"\!\(\*SuperscriptBox[\(10\), \(\(5\)\(\\ \)\)]\) "},Table["",{i,1,9}],{"(10^6) "},Table["",{i,1,9}]]],

Join[
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}],
{ticksizeL,0},Table[{ticksizeS,0},{i,1,8}]],

3],3];

Do[
data=Import[files[[n]],"Table","HeaderLines"->22];(* Imports data as a table, ignores heading *)
dataDrop = Drop[data,None,{2}];(* Removes middle column of all data -> polar angle *)

fitdataY=Select[dataDrop,#[[2]]<10^-1&];(* Limits y values for the equation of best fit *)
fitdataXY=Select[fitdataY,#[[1]]>0.4&&#[[1]]<60&];(* Limits x values for the equation of best fit *)

fitdata=ListLogPlot[fitdataXY,PlotStyle->Red,PlotRange->All];(* Plots fitdata in red *)
NLM=NonlinearModelFit[fitdataXY,d+a Exp[-k x],{d,a,k},x];(* Fits an exponentian equation for the selected data *)
fitgraph= LogPlot[Normal[NLM],{x,0,60}];(* Graphs equation of fit *)

AOI=Flatten[Import[files[[n]],"Table", "HeaderLines"->6 ]][[4]];(* Pulls angle of incedence from file *)
reflectance=Flatten[Import[files[[n]],"Table", "HeaderLines"->9 ]][[5]];(* Pulls reflectance from file *)
R2= reflectance^2;
backscatter = Select[data, #[[1]]==-6&];(* Pulls BRDF backscatter from file (@ -6°) *)

G1=Text[Style["(R^2) = "  <> ToString[R2, TraditionalForm], Black,14,FontFamily->"Helvetica"]];
G2 = Text[Style["Backscatter  = " <> ToString[backscatter[[1]][[3]], TraditionalForm], Black,14,FontFamily->"Helvetica"]];
G3=Text[Style[ "Incidence Angle = "  <>ToString[AOI] <>"°",Black,14,FontFamily->"Helvetica"]];
G4=Grid[{{G1},{G2},{G3}},Frame->True];(* Creates info box *)
authorNames =Text[Style["Alena Ananyeva, Jasmine Terrones",FontFamily->"Helvetica",Opacity[.3],12]]; (* Format author names *)

graph=ListLogPlot[{dataDrop},AspectRatio->0.75,Axes->None,Frame->{{True,True},{True,True}},FrameStyle->{{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}},{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}}},FrameLabel->{{Style[ "BRDF [(sr^(-1))]",15],None},{Style["Observation Angle [deg]",15],None}},
ImageSize->500,PlotRange->All,Prolog->Inset[LIGOwatermark,ImageScaled[{0.25,0.8}]],Epilog->{Inset[G4,{55,0.5},{0, 0}],Inset[authorNames,{57,Log[1.5*10^-6]},{0,0}]}]; (* Plots data, LIGO logo, info box, and author names *)

formattedGraph = Show[graph,fitdata, fitgraph, PlotRange->{{-90,90},{Log[10^-6],Log[10]}},AspectRatio -> 0.8, Axes -> None, Frame->{{True,True},{True,True}},
Frame->{{True,True},{True,True}},FrameLabel->{{Style[ "BRDF [(sr^(-1))]",16],None},{Style["Observation Angle [deg]",15],Style[ "",22]}},
FrameStyle->{{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}},{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}}},LabelStyle->Directive[Black,12,FontFamily->"Helvetica"],FrameTicks->{{ticksYLog,None},{ticksX,None}}, FrameTicksStyle->Directive[Black,12,FontFamily->"Helvetica"],
ImageSize->600]; (* Adds in fitdata and line of best fit, formats plot *)
Print[formattedGraph];

title = StringReplace[files[[n]],".txt"-> ".jpg"];
(* Export[title, formattedGraph]; *) (* Saves plot as a jpg*)
, {n, 1, Length[files]} (* Repeats for all txt files *)]

(* plots multiple data sets *)

mdata=FileNames["*clean"];(*Isolates files based on title*)
(* mdata = files[[{1,3,6}]]; *)(* Manually select which files to plot *)
data = List[]; 

title = "After Cleaning";

Do[
tempdata=Import[mdata[[m]],"Table","HeaderLines"->22];(* Imports file as a table, ignores heading *)
AppendTo[data, Drop[tempdata,None,{2}]]; (* Drops middle column -> polar angle, appends to data list *)
,{m, 1, Length[mdata]}
]

authorNames =Text[Style["Alena Ananyeva, Jasmine Terrones",FontFamily->"Helvetica",Opacity[.3],12]]; (* Format author names *)
legend=SwatchLegend[{"Trial 1","Trial 2", "Trial 3"},LegendMarkers->Graphics[{EdgeForm[Black],Opacity[0.5],Rectangle[]}],LegendLabel->title,LegendFunction->(Framed[#,RoundingRadius->5, FrameStyle-> LightGray]&),LegendMargins->5, LabelStyle->Directive[Black,14,FontFamily->"Helvetica"]]; (* Format legend *)

graph=ListLogPlot[data,AspectRatio->0.75,Axes->None, 
PlotLegends -> Placed[legend ,{Right,Top}],
Frame->{{True,True},{True,True}},FrameStyle->{{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}},{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}}},FrameLabel->{{Style[ "BRDF [(sr^(-1))]",15],None},{Style["Observation Angle [deg]",15],None}},
ImageSize->500,PlotRange->All,Prolog->Inset[LIGOwatermark,ImageScaled[{0.25,0.8}]],Epilog->Inset[authorNames,{57,Log[1.5*10^-6]},{0,0}]]; (* Plots data, LIGO logo, info box, and author names *)

formattedGraph = Show[graph, PlotRange->{{-90,90},{Log[10^-6],Log[10]}}, AspectRatio -> 0.8, Axes -> None,
Frame->{{True,True},{True,True}},FrameLabel->{{Style[ "BRDF [(sr^(-1))]",16],None},{Style["Observation Angle [deg]",15],Style[ "",22]}},
FrameStyle->{{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}},{{Black,Directive[Thickness[0.002]]},{Black,Directive[Thickness[0.002]]}}},LabelStyle->Directive[Black,12,FontFamily->"Helvetica"],FrameTicks->{{ticksYLog,None},{ticksX,None}}, FrameTicksStyle->Directive[Black,12,FontFamily->"Helvetica"],
ImageSize->600]; (* Formats plot *)
Print[formattedGraph];

title2 = StringInsert[title,".jpg",StringLength[title]+1];
(* Export[title2, formattedGraph]; *) (* Saves plot as a jpg*)