%
% This Matlab script is designed to update the FC Section Transport Table
% that is posted in the Florida Current Project's Web pages.
%
% Steps in this process: 
%
% 1) Read the file/table FC_section_transport.asc
% 2) Erase the cruisers with bad data (Transport=NaN)
% 3) Reorder cruisers by dates.
% 4) Erase the repited cruises. The last value is taked.
% 5) Create the updated section transport table to be send to the web:
%    FC_section_transport.dat
%
% New version: v2024.08.30 (sent by Ryan Smith on 8/30/2024) to change the
% header of the FC_section_transport.dat file

clear all

%%%%
%%%% Download the Florida Straits Section Transport Table (the last version)
%%%%

load  ('/home/cable/Florida_Current_Transport/Section_Process/FC_section_transport.asc')
temp = FC_section_transport ;
clear FC_section_transport

%%%%
%%%% Looking to those cruisers that have 'bad' Transport data...
%%%%
table_transport = temp(:,5);
I_NaN = find(isnan(table_transport)==1) ;
temp(I_NaN,:) = [];
clear table_transport I_NaN

%%%%
%%%% Reorder all cruisers by date
%%%%
[n_cruisers m] = size(temp);

tyear = temp(:,1);
tmonth = temp(:,2);
tday = temp(:,3);
thour = temp(:,4);
tminute(n_cruisers,:)=0; % Minute and Second are not relevant data in this case
tsecond(n_cruisers,:)=0;
flag=temp(:,6);

date_cruisers = datenum (tyear,tmonth,tday,thour,tminute,tsecond);
[date_sort, I_sort] = sort(date_cruisers) ;
newtemp= temp(I_sort,:) ;

%%%%
%%%% We eliminate the cruises repited
%%%%
ind=find(diff(date_sort)==0);
newtemp(ind,:)=[];
[n_cruisers m] = size(newtemp);   % Number of cruises after remove the bad ones.

%%%%
%%%% When the Table has been completed? Use matlab variable 'date'
%%%%
today=date;
date_completed=['Table updated: ' today] ;
%%%%

 fid=fopen('/home/cable/Florida_Current_Transport/Section_Process/FC_section_transport.dat','w');
 fprintf(fid,'%%  FLORIDA CURRENT SECTION TRANSPORTS FROM SHIP MEASUREMENTS  \n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  This files contains in situ Florida Current transport measurements from  \n');
 fprintf(fid,'%%  cable calibration cruises and hydrographic surveys conducted across the \n');
 fprintf(fid,'%%  Florida Straits at 27N dating back to 1982. These section transports are \n');
 fprintf(fid,'%%  integrated from ocean velocity data measured at nine sites spanning the \n');
 fprintf(fid,'%%  Straits at 27N (note: on some cruises in the 1990s, only eight sites were \n');
 fprintf(fid,'%%  used). Data are collected via Pegasus profiler, GPS dropsonde, or lowered \n');
 fprintf(fid,'%%  acoustic Doppler current profiler (LADCP), measuring the full water \n');
 fprintf(fid,'%%  column. Data have been corrected for the barotropic tide using the tidal \n');
 fprintf(fid,'%%  constituents derived by Mayer et al. (1984): \n');
 fprintf(fid,'%%  https://doi.org/10.1175/1520-0485(1984)014<1551:TMITFC>2.0.CO;2\n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  This data product is produced and publicly distributed by NOAA/AOML''s \n');
 fprintf(fid,'%%  Western Boundary Time Series (WBTS) project.  Funding is provided by \n');
 fprintf(fid,'%%  NOAA/AOML and NOAA''s Global Ocean Monitoring and Observing (GOMO) Program. \n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  FILENAME: FC_section_transport.dat \n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  For further information, please visit the project web site at: \n');
 fprintf(fid,'%%  https://www.aoml.noaa.gov/western-boundary-time-series/ \n');
 fprintf(fid,'%%  or contact: \n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  Dr. Denis Volkov\n');
 fprintf(fid,'%%  Denis.Volkov@noaa.gov\n');
 fprintf(fid,'%%  (305) 361-4344 \n');
 fprintf(fid,'%% \n%% \n');
 fprintf(fid,'%%  COLUMN 1 - Year \n');
 fprintf(fid,'%%  COLUMN 2 - Month \n');
 fprintf(fid,'%%  COLUMN 3 - Day \n');
 fprintf(fid,'%%  COLUMN 4 - Hour (GMT) \n');
 fprintf(fid,'%%  COLUMN 5 - Transport in Sverdrups (Sv), 1 Sv = 10^6 m^3 s^(-1) \n');
 fprintf(fid,'%%  COLUMN 6 - Instrument Type \n');
 fprintf(fid,'%% \n');
 fprintf(fid,'%%  Instrument Type:  1  =  Dropsonde measurements \n');
 fprintf(fid,'%%                    2  =  LADCP measurements  \n');
 fprintf(fid,'%%                    3  =  Pegasus measurements \n');
 fprintf(fid,'%%                    4  =  Pegasus in dropsonde mode measurements \n');
 fprintf(fid,'%% \n%% \n');
 fprintf(fid,['%%  ' date_completed '\n%% \n%%\n']);
 
 fprintf(fid,'%%Year  Month  Day  Hour  Transport  Type\n');
 fprintf(fid,'%%                          (Sv)          \n');
 for ii=1:n_cruisers
     for jj=1:m
         if jj==1
           fprintf(fid,'%4.0f ',newtemp(ii,jj));
           
           elseif jj==2 | jj==3 | jj==4
           fprintf(fid,'%5.0f ',newtemp(ii,jj));

           elseif jj==5
           fprintf(fid,'%8.1f ',newtemp(ii,jj));
           
           elseif jj==6
           fprintf(fid,'%6.f ',newtemp(ii,jj));
         end
     end
     fprintf(fid,'\n');
 end 
fclose(fid);