% Created on 9/07/2018; Version 1.0
% Version 2.0; 12/07/2018
%
% Data used from load_E1_buoy_data.m function, datetime.m script and
%   flag_E1.m function
%
% Output is saved into E1_output.csv file
%
%%
% Open a file for writing 
fid = fopen('E1_ouput.csv', 'w');

t = datetime('now')

% print general information 
fprintf(fid, 'Contact Name: Eleanor Webber (Plymouth University)\n'); 
fprintf(fid, 'File produced: %s\n',t);
fprintf(fid, 'Station Description: Western Channel Observatory (WCO) - E1\n');
fprintf(fid, 'Data Source: Plymouth Marine Laboratory\n');
fprintf(fid, 'Processing: The data was processed using the flag_E1.m script created\n\r');

% print flag information 
fprintf(fid, 'NOTE: For the flag columns the numbering is based on the ODV data flagging system: 0 = good quality data using mean data (within desired range); 1 = poor quality data using mean data; 4 = questionable quality data due to high standard deviation; 8 = bad quality data using mean data (outside of desired range)\n');
fprintf(fid, '......For SST the desired range of values is 5-25(degC); the maximum difference between the current and previous value is 0.5; the desired standard deviation is 0.5(degC)\n');
fprintf(fid, '......For Salinity the desired range of values is 30-40(psu); the maximum difference between the current and previous value is 0.1; the desired standard deviation is 0.1(psu)\n');
fprintf(fid, '......For Dissolved Oxygen the desired range of values is 150-400(uM); the maximum difference between the current and previous value is 5; the desired standard deviation is 5(uM)\n');
fprintf(fid, '......For Turbidity the desired range of values is 0.1-5(NTU); the maximum difference between the current and previous value is 0.1 ; the desired standard deviation is 0.1(NTU)\n');
fprintf(fid, '......For CDOM the desired range of values is 0-10(QDE ppb); the maximum difference between the current and previous value is 1; the desired standard deviation is 1(QDE ppb)\n');
fprintf(fid, '......For Chlorophyll the desired range of values is 0.05-20(mg/m3); the maximum difference between the current and previous value is 0.5; the desired standard deviation is 0.5(mg/m3)\n');
fprintf(fid, '......For PAR the desired range of values is 0-2000(umol/photo0s/m2/s); the maximum difference between the current and previous value is 100; the desired standard deviation is 100(umol/photo0s/m2/s)\n\r');

% print header line
fprintf(fid, 'Date(YYMMDD), Time(HHMMSS), Latitude (DDMM.MMMM), Longitude (DDDMM.MMMM), Average Wind speed (KN), Average Wind direction (deg), Max gust (K0), Sea Temperature (degC), Air Temperature (degC), Humidity (percentage), Dew point (degC), Pressure (hPa), Latitude (DDMM.MMMM), Longitude (DDDMM.MMMM), Number of zero crossings, Havg (m), Tz (mean spectral period - s), Hmax(m), Hsig(m), Tsig(s), H10(m), T10(m), Tavg(s), TP(s), TP5, HMO, Mean direction (deg), Mean spread (deg), Date (YYMMDD), Time (HHMMSS), N PAR, Mean PAR (umol/photo0s/m2/s), SD PAR (umol/photo0s/m2/s), PAR_FLAG, N CDOM, MeaN CDOM (QDE ppb), SD CDOM (QDE ppb), CDOM_FLAG, N Dissolved Oxygen, Mean Dissolved Oxygen (uM), SD Dissolved Oxygen (uM), OXYGEN_FLAG, N SST, Mean SST (degC), SD SST (degC), SST_FLAG, N Salinity, Mean Salinity (PSU), SD Salimity (PSU), SAL_FLAG, N Turbidity, Mean Turbidity (NTU), SD Turbidity (NTU), TURB_FLAG, N Chlorophyll fluorescence, Mean Chlorophyll (mg/m3), SD Chlorophyll (mg/m3) \n');
 
% print values in column order from data structure 
for p = 1:length(data.lat)
    %disp(data.date(p))
    datestr = string(data.date(p)); % convert date from cell array to string 
    %disp(datestr)
    timestr = string(data.time(p)); % convert time from cell array to string
    
    fprintf(fid,'%s, %s, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i, %9.3f, %9.3f, %9.3f, %3i \r', datestr, timestr, data.lat(p), data.long(p), data.avws(p), data.avwd(p), data.maxg(p), data.seatmp(p), data.airtmp(p), data.humid(p), data.dewp(p), data.pres(p), data.lat2(p), data.long2(p), data.noZ(p), data.havg(p), data.T2(p), data.hmax(p), data.hsig(p), data.Tsig(p), data.h10(p), data.T10(p), data.Tavg(p), data.TP(p), data.TP5(p), data.hmo(p), data.Mndir(p), data.Mnspr(p), data.date2(p), data.time2(p), data.Npar(p), data.Mnpar(p), data.SDpar(p), flag.par(p), data.Ncdom(p), data.Mncdom(p), data.SDcdom(p), flag.cdom(p), data.NDissO(p), data.MnDissO(p), data.SDDissO(p), flag.dissO(p), data.Nsst(p), data.Mnsst(p), data.SDsst(p), flag.sst(p), data.Nsal(p), data.Mnsal(p), data.SDsal(p), flag.sal(p), data.Nturb(p), data.Mnturb(p), data.SDturb(p), flag.turb(p), data.Nchl(p), data.Mnchl(p), data.SDchl(p), flag.chl(p));
end

% close data file
fclose(fid)