%% SABOR
load('/Users/alisonchase/Documents/TARA Oceans/Radiometry/TSRB_processing/HyperPro_Rrs_SABOR_16Aug2017_norm.mat')

textfile='sabor_HyperPro_2014.txt';

wl = wlEd_all(:,1);
roundwl=roundn(wl,-1);
for iwl=1:length(wl)
    wlarray{iwl}=['Ed',num2str(roundwl(iwl)),','];
    wlarray2{iwl}=['Ed',num2str(roundwl(iwl)),'_sd,'];
    wlarray3{iwl}=['Lu',num2str(roundwl(iwl)),','];
    wlarray4{iwl}=['Lu',num2str(roundwl(iwl)),'_sd,'];
    wlarray5{iwl}=['Rrs',num2str(roundwl(iwl)),','];
end
wlarray5{iwl}=['Rrs',num2str(roundwl(iwl))]; %last entry will not be followed by a comma

startdt=datestr(startdate_all,'yyyymmdd HH:MM:SS');
starttime=startdt(:,10:end);

enddt=datestr(enddate_all,'yyyymmdd HH:MM:SS');
endtime=enddt(:,10:end);

%data to be printed:
date=datestr(startdate_all,'yyyymmdd');
time=[starttime,repmat(' ',length(startdt(:,1)),1)];%,endtime];

elapsed = datestr((enddate_all)-(startdate_all),'MM:ss');
sec_elap = str2num(elapsed(:,1:2))*60 + str2num(elapsed(:,4:5));

% create arrays of units for the file header
EDunits=repmat('uW/cm^2/nm,',1,length(wlem));
%ESunits=[ESunits,'uW/cm^2/nm'];

LUunits=repmat('uW/cm^2/nm/sr,',1,length(wlem));
%LUunits=[LUunits,'uW/cm^2/nm/sr'];

RRSunits=repmat('1/sr,',1,length(wlem)-1);
RRSunits=[RRSunits,'1/sr'];

%wlprint = strrep(num2str(wlem'),'       ',' ')

fileID=fopen(textfile,'w');

%header:
fprintf(fileID,'%-30s\n','/begin_header');
fprintf(fileID,'%-30s\n','/investigators=Ivona_Cetinic,Wayne_Slade,Alison_Chase');
fprintf(fileID,'%-30s\n','/affiliations=NASA_GSFC,Sequoia_Scientific,UMaine');
fprintf(fileID,'%-30s\n','/contact=ivona.cetinic@nasa.gov,wslade@sequoiasci.com,alison.p.chase@maine.edu');
fprintf(fileID,'%-30s\n','/experiment=SABOR');
fprintf(fileID,'%-30s\n','/cruise=SABOR');
fprintf(fileID,'%-30s\n','/station=NA');
fprintf(fileID,'%-30s\n',['/data_file_name=',textfile]);
fprintf(fileID,'%-30s\n','/documents=');
fprintf(fileID,'%-30s\n','/calibration_files=');
fprintf(fileID,'%-30s\n','/data_type=mooring');
fprintf(fileID,'%-30s\n','/data_status=final');
fprintf(fileID,'%-30s\n','/start_date=20140722');
fprintf(fileID,'%-30s\n','/end_date=20140805');
fprintf(fileID,'%-30s\n','/start_time=19:19:26[GMT]');
fprintf(fileID,'%-30s\n','/end_time=18:03:10[GMT]');
fprintf(fileID,'%-30s\n','/north_latitude=41.8488[DEG]');
fprintf(fileID,'%-30s\n','/south_latitude=36.6452[DEG]');
fprintf(fileID,'%-30s\n','/east_longitude=-66.7986[DEG]');
fprintf(fileID,'%-30s\n','/west_longitude=-75.8084[DEG]');
fprintf(fileID,'%-30s\n','/water_depth=NA');
fprintf(fileID,'%-30s\n','/measurement_depth=0.2');
fprintf(fileID,'%-30s\n','/delimiter=space');
fprintf(fileID,'%-30s\n','/missing=-999');
fprintf(fileID,'%-30s\n','!COMMENTS');
fprintf(fileID,'%-30s\n','!Es and Lu values reported here are mean values of approx. minute deployments; Rrs values are determined using Ed and Lw (not Lu); calculations explained in associated documents');
fprintf(fileID,'%-300s\n',['/fields=date,time,elapsed_time,lat,lon,',wlarray{:},wlarray2{:},wlarray3{:},wlarray4{:},wlarray5{:}]);
fprintf(fileID,'%-300s\n',['/units=yyyymmdd,hh:mm:ss,seconds,degrees,degrees,',EDunits,EDunits,LUunits,LUunits,RRSunits]);
fprintf(fileID,'%-30s\n','/end_header');

%define the format for the data part formdate=repmat('%9s ',1,3);
formrad=repmat('%8.4f ',1,(length(wlem)*5)-1);
format=['%9s ','%9s ','%4d ','%6.4f ','%6.4f ',formrad,'%8.4f\n'];

%change NaN values to 0
%Ed_avg(isnan(Ed_avg))=0;Ed_std(isnan(Ed_std))=0;Lu_avg(isnan(Lu_avg))=0;Lu_std(isnan(Lu_std))=0;Rrs_avg(isnan(Rrs_avg))=0;Rrs_std(isnan(Rrs_std))=0;

[B,I] = sort(startdate_all);

data=[Ed_avg_all(:,I)',Ed_std_all(:,I)',Lu_avg_all(:,I)',Lu_std_all(:,I)',Rrs_all(:,I)'];

for ii = 1:length(startdt(:,1))
    fprintf(fileID,format,date(I(ii),:),time(I(ii),:),sec_elap(ii),lat_all(I(ii)),lon_all(I(ii)),data(I(ii),:));
end

%% AE1319
load('/Users/alisonchase/Documents/TARA Oceans/Radiometry/TSRB_processing/HyperPro_Rrs_AE1319_16Aug2017_norm.mat')

textfile='AE1319_HyperPro_2013.txt';

wl = wlEd_all(:,1);
roundwl=roundn(wl,-1);
for iwl=1:length(wl)
    wlarray{iwl}=['Ed',num2str(roundwl(iwl)),','];
    wlarray2{iwl}=['Ed',num2str(roundwl(iwl)),'_sd,'];
    wlarray3{iwl}=['Lu',num2str(roundwl(iwl)),','];
    wlarray4{iwl}=['Lu',num2str(roundwl(iwl)),'_sd,'];
    wlarray5{iwl}=['Rrs',num2str(roundwl(iwl)),','];
end
wlarray5{iwl}=['Rrs',num2str(roundwl(iwl))]; %last entry will not be followed by a comma

%fix an error with one of the enddates
enddate_all(11) = enddate_all(11)-1;

startdt=datestr(startdate_all,'yyyymmdd HH:MM:SS');
starttime=startdt(:,10:end);

enddt=datestr(enddate_all,'yyyymmdd HH:MM:SS');
endtime=enddt(:,10:end);

%data to be printed:
date=datestr(startdate_all,'yyyymmdd');
time=[starttime,repmat(' ',length(startdt(:,1)),1)];%,endtime];

elapsed = datestr((enddate_all)-(startdate_all),'MM:ss');
sec_elap = str2num(elapsed(:,1:2))*60 + str2num(elapsed(:,4:5));

% create arrays of units for the file header
EDunits=repmat('uW/cm^2/nm,',1,length(wlem));
%ESunits=[ESunits,'uW/cm^2/nm'];

LUunits=repmat('uW/cm^2/nm/sr,',1,length(wlem));
%LUunits=[LUunits,'uW/cm^2/nm/sr'];

RRSunits=repmat('1/sr,',1,length(wlem)-1);
RRSunits=[RRSunits,'1/sr'];

%wlprint = strrep(num2str(wlem'),'       ',' ')

fileID=fopen(textfile,'w');

%header:
fprintf(fileID,'%-30s\n','/begin_header');
fprintf(fileID,'%-30s\n','/investigators=Ivona_Cetinic,Wayne_Slade,Alison_Chase');
fprintf(fileID,'%-30s\n','/affiliations=NASA_GSFC,Sequoia_Scientific,UMaine');
fprintf(fileID,'%-30s\n','/contact=ivona.cetinic@nasa.gov,wslade@sequoiasci.com,alison.p.chase@maine.edu');
fprintf(fileID,'%-30s\n','/experiment=AE1319');
fprintf(fileID,'%-30s\n','/cruise=AE1319');
fprintf(fileID,'%-30s\n','/station=NA');
fprintf(fileID,'%-30s\n',['/data_file_name=',textfile]);
fprintf(fileID,'%-30s\n','/documents=');
fprintf(fileID,'%-30s\n','/calibration_files=');
fprintf(fileID,'%-30s\n','/data_type=mooring');
fprintf(fileID,'%-30s\n','/data_status=final');
fprintf(fileID,'%-30s\n','/start_date=20130827');
fprintf(fileID,'%-30s\n','/end_date=20130908');
fprintf(fileID,'%-30s\n','/start_time=14:53:11[GMT]');
fprintf(fileID,'%-30s\n','/end_time=15:46:54[GMT]');
fprintf(fileID,'%-30s\n','/north_latitude=54.9987[DEG]');
fprintf(fileID,'%-30s\n','/south_latitude=31.6671[DEG]');
fprintf(fileID,'%-30s\n','/east_longitude=-40.0302[DEG]');
fprintf(fileID,'%-30s\n','/west_longitude=-64.1708[DEG]');
fprintf(fileID,'%-30s\n','/water_depth=NA');
fprintf(fileID,'%-30s\n','/measurement_depth=0.2');
fprintf(fileID,'%-30s\n','/delimiter=space');
fprintf(fileID,'%-30s\n','/missing=-999');
fprintf(fileID,'%-30s\n','!COMMENTS');
fprintf(fileID,'%-30s\n','!Es and Lu values reported here are mean values of approx. minute deployments; Rrs values are determined using Ed and Lw (not Lu); calculations explained in associated documents');
fprintf(fileID,'%-300s\n',['/fields=date,time,elapsed_time,lat,lon,',wlarray{:},wlarray2{:},wlarray3{:},wlarray4{:},wlarray5{:}]);
fprintf(fileID,'%-300s\n',['/units=yyyymmdd,hh:mm:ss,seconds,degrees,degrees,',EDunits,EDunits,LUunits,LUunits,RRSunits]);
fprintf(fileID,'%-30s\n','/end_header');

%define the format for the data part formdate=repmat('%9s ',1,3);
formrad=repmat('%8.4f ',1,(length(wlem)*5)-1);
format=['%9s ','%9s ','%4d ','%6.4f ','%6.4f ',formrad,'%8.4f\n'];

%change NaN values to 0
%Ed_avg(isnan(Ed_avg))=0;Ed_std(isnan(Ed_std))=0;Lu_avg(isnan(Lu_avg))=0;Lu_std(isnan(Lu_std))=0;Rrs_avg(isnan(Rrs_avg))=0;Rrs_std(isnan(Rrs_std))=0;

[B,I] = sort(startdate_all);

data=[Ed_avg_all(:,I)',Ed_std_all(:,I)',Lu_avg_all(:,I)',Lu_std_all(:,I)',Rrs_all(:,I)'];

for ii = 1:length(startdt(:,1))
    fprintf(fileID,format,date(I(ii),:),time(I(ii),:),sec_elap(ii),lat_all(I(ii)),lon_all(I(ii)),data(I(ii),:));
end




%% NH1418
load('/Users/alisonchase/Documents/TARA Oceans/Radiometry/TSRB_processing/HyperPro_Rrs_NH1418_16Aug2017_norm.mat')

textfile='NH1418_HyperPro_2014.txt';

wl = wlEd_all(:,1);
roundwl=roundn(wl,-1);
for iwl=1:length(wl)
    wlarray{iwl}=['Ed',num2str(roundwl(iwl)),','];
    wlarray2{iwl}=['Ed',num2str(roundwl(iwl)),'_sd,'];
    wlarray3{iwl}=['Lu',num2str(roundwl(iwl)),','];
    wlarray4{iwl}=['Lu',num2str(roundwl(iwl)),'_sd,'];
    wlarray5{iwl}=['Rrs',num2str(roundwl(iwl)),','];
end
wlarray5{iwl}=['Rrs',num2str(roundwl(iwl))]; %last entry will not be followed by a comma

startdt=datestr(startdate_all,'yyyymmdd HH:MM:SS');
starttime=startdt(:,10:end);

enddt=datestr(enddate_all,'yyyymmdd HH:MM:SS');
endtime=enddt(:,10:end);

%data to be printed:
date=datestr(startdate_all,'yyyymmdd');
time=[starttime,repmat(' ',length(startdt(:,1)),1)];%,endtime];

elapsed = datestr((enddate_all)-(startdate_all),'MM:ss');
sec_elap = str2num(elapsed(:,1:2))*60 + str2num(elapsed(:,4:5));

% create arrays of units for the file header
EDunits=repmat('uW/cm^2/nm,',1,length(wlem));
%ESunits=[ESunits,'uW/cm^2/nm'];

LUunits=repmat('uW/cm^2/nm/sr,',1,length(wlem));
%LUunits=[LUunits,'uW/cm^2/nm/sr'];

RRSunits=repmat('1/sr,',1,length(wlem)-1);
RRSunits=[RRSunits,'1/sr'];

%wlprint = strrep(num2str(wlem'),'       ',' ')

fileID=fopen(textfile,'w');

%header:
fprintf(fileID,'%-30s\n','/begin_header');
fprintf(fileID,'%-30s\n','/investigators=Ivona_Cetinic,Wayne_Slade,Alison_Chase');
fprintf(fileID,'%-30s\n','/affiliations=NASA_GSFC,Sequoia_Scientific,UMaine');
fprintf(fileID,'%-30s\n','/contact=ivona.cetinic@nasa.gov,wslade@sequoiasci.com,alison.p.chase@maine.edu');
fprintf(fileID,'%-30s\n','/experiment=NH1418');
fprintf(fileID,'%-30s\n','/cruise=NH1418');
fprintf(fileID,'%-30s\n','/station=NA');
fprintf(fileID,'%-30s\n',['/data_file_name=',textfile]);
fprintf(fileID,'%-30s\n','/documents=');
fprintf(fileID,'%-30s\n','/calibration_files=');
fprintf(fileID,'%-30s\n','/data_type=mooring');
fprintf(fileID,'%-30s\n','/data_status=final');
fprintf(fileID,'%-30s\n','/start_date=20140920');
fprintf(fileID,'%-30s\n','/end_date=20141004');
fprintf(fileID,'%-30s\n','/start_time=23:05:09[GMT]');
fprintf(fileID,'%-30s\n','/end_time=21:51:01[GMT]');
fprintf(fileID,'%-30s\n','/north_latitude=18.9992[DEG]');
fprintf(fileID,'%-30s\n','/south_latitude=-2.6783[DEG]');
fprintf(fileID,'%-30s\n','/east_longitude=-149.7790[DEG]');
fprintf(fileID,'%-30s\n','/west_longitude=-157.9993[DEG]');
fprintf(fileID,'%-30s\n','/water_depth=NA');
fprintf(fileID,'%-30s\n','/measurement_depth=0.2');
fprintf(fileID,'%-30s\n','/delimiter=space');
fprintf(fileID,'%-30s\n','/missing=-999');
fprintf(fileID,'%-30s\n','!COMMENTS');
fprintf(fileID,'%-30s\n','!Es and Lu values reported here are mean values of approx. minute deployments; Rrs values are determined using Ed and Lw (not Lu); calculations explained in associated documents');
fprintf(fileID,'%-300s\n',['/fields=date,time,elapsed_time,lat,lon,',wlarray{:},wlarray2{:},wlarray3{:},wlarray4{:},wlarray5{:}]);
fprintf(fileID,'%-300s\n',['/units=yyyymmdd,hh:mm:ss,seconds,degrees,degrees,',EDunits,EDunits,LUunits,LUunits,RRSunits]);
fprintf(fileID,'%-30s\n','/end_header');

%define the format for the data part formdate=repmat('%9s ',1,3);
formrad=repmat('%8.4f ',1,(length(wlem)*5)-1);
format=['%9s ','%9s ','%4d ','%6.4f ','%6.4f ',formrad,'%8.4f\n'];

%change NaN values to 0
%Ed_avg(isnan(Ed_avg))=0;Ed_std(isnan(Ed_std))=0;Lu_avg(isnan(Lu_avg))=0;Lu_std(isnan(Lu_std))=0;Rrs_avg(isnan(Rrs_avg))=0;Rrs_std(isnan(Rrs_std))=0;

[B,I] = sort(startdate_all);

data=[Ed_avg_all(:,I)',Ed_std_all(:,I)',Lu_avg_all(:,I)',Lu_std_all(:,I)',Rrs_all(:,I)'];

for ii = 1:length(startdt(:,1))
    fprintf(fileID,format,date(I(ii),:),time(I(ii),:),sec_elap(ii),lat_all(I(ii)),lon_all(I(ii)),data(I(ii),:));
end



