function [] = convert_s2gliderviztxt_CUGN_ArgoFlags(s, CUGN_name, dir2save)
%
% adopted from convert_mergedmat2fltviztxt.m
%
% This script converts the merged float data (created by
% merge_ctd_nitrate_matfiles.m) into a text file in a format that can be
% used by SAGE. The text file format follows that from floatviz
% (https://www.mbari.org/science/upper-ocean-systems/chemical-sensor-group/floatviz/).
%
% Options for is_sat are:
% 1 = real-time sat data (saves with RT.txt suffix)
% 2 = full resolution data (saves with .txt suffix)
% 3 = QC'd data (saves with *QC.txt suffix and under \QC\ folder)
% User input is fltname (string).
%
% Currently the text file name is fltnameKEO.txt (i.e. 2903330KEO.txt).
%
%
% Created by Yui Takeshita
% MBARI
% Dec 4, 2018
%
% Modification Notes
%
% December 10, 2018
% - Data file is now written from highest pressure to lowest, to be
% consistent with FltVIZ.
% - Moved the std_str outside of the for loop for rows to prevent errors
% with the profile spacer fprintf
% - Now does not write data from a specific row if all P, T, and S data are
% missing
%
% December 11, 2018
% - Added 'ODV' to the beginning of the written textfile (for SAGE), and
% now is included in the user input
% - Added line in // header for '//WMO ID: fltnum'. SAGE needs this in
% order to write the QC file using the 'Reprocess' button.
% - Added a flag conversion, where if JAMSTEC flag = 9, make it 8 (bad
% data; for oxygen these values were -9999.9999).
% - Added display at end of script to notify what file was written
%
% April 5, 2019
% - changed so it can do spray glider conversion
% - kept 'flt' structure, and added code to transcribe data from 's'
% structure to 'flt' structure
% - loops through and sees if the fieldname exists. if not, creates an NaN
% matrix
% - Commented out flag conversions, because the sat mat file alreayd uses
% argo flags
% - adjusted headers that are written (kept float ID though, cause I think
% it might break something downstream)
% - uncommented line that checks to see if all TSP are missing. If so,
% finishes writing that profile and moves onto the next.
%
% April 14-15, 2019
% - Changed this into a function so that it can be called from
% update_GliderVIZ_sat.m
%
% April 16, 2019
% - Default array for missing variable_QC is now ones instead of NaN.
% Before, the MVI was getting written into the QF column, when it should've
% been 1.
%
% June 23, 2020
% - added input of 'is_sat' boolan to determine whether the text and cfg file names will have the
% 'RT' extension.

% Parts of code taken from 'argo2odv_LIAR.m', author Josh Plant and Tanya
% Maurer at MBARI.

% *************************************************************************
% USER INPUT HERE: CHOOSE FLOAT NAME. WILL NEED A fltname_merged.mat FILE
% IN THE PATH, AND WILL CREATE fltname_fltviz.txt
% *************************************************************************

% path = '\\sirocco\wwwroot\lobo\data\glidervizdata\';
% path = [filesep 'Volumes' filesep '901805_Coastal_Biogeochemical_Sensing' filesep 'Spray_Data' filesep 'QCd_txt_files' filesep];
path = [filesep filesep 'atlas' filesep 'ProjectLibrary' filesep '901805_Coastal_Biogeochemical_Sensing' filesep 'Spray_Data'  filesep 'QCd_txt_files' filesep];


txtfilename = [CUGN_name,'_argoQC.txt'];
% 
% 
% % missionname = '19402901';
% if(is_sat == 1)
%     txtfilename = [missionname,'RT_argo.txt']; % output file name with RT extension (real time)
%     cfgfilename = [missionname,'RT_argo.cfg']; % output file name with RT extension (real time)
% elseif (is_sat == 2)
%     txtfilename = [missionname,'_argo.txt']; % output file name
%     cfgfilename = [missionname,'_argo.cfg']; % config file name
% elseif (is_sat == 3)
%     txtfilename = [missionname,'QC_argo.txt']; % output file name
%     cfgfilename = [missionname,'QC_argo.cfg']; % config file name
% end

% ************************************************************************
% LOAD DATA AND CHANGE QUALITY FLAG TO ARGO FORMAT
% ************************************************************************

% load sat mat file
% load([strippath(cd,1),'\Deployments\',missionname,'\',missionname,'_sat.mat']);

% ================ NEW CODE ==============================
% extract variables and flags from s strcuture (spray) to flt structure (so we can
% recycle code)
flt.SDN = s.sdn;

% //commented 4/12/2023 YT
% if(isfield(s, 'lat_'))
%     flt.LAT = s.lat_(2,:); % end of dive location
%     flt.LONG = s.lon_(2,:); % end of dive location
% else
%     flt.LAT = s.lat2;
%     flt.LONG = s.lon2;
% end

flt.LAT = s.lat;
flt.LONG = s.lon; 

flt.POSITION_FLAG = s.position_QC;
% flt.POSITION_FLAG = ones(size(s.lat));

flt.PRES = s.pres;
    flt.PRES_FLAG = s.pres_QC;
% flt.PRES_FLAG = ones(size(s.tc));
flt.TEMP = s.tc;
    flt.TEMP_FLAG = s.tc_QC;
%     flt.TEMP_FLAG = ones(size(s.tc));
flt.SAL  = s.psal;
    flt.SAL_FLAG = s.psal_QC;
%     flt.SAL_FLAG = ones(size(s.psal));
flt.SIGMA_THETA = s.pdens;
    flt.SIGMA_THETA_FLAG = s.pdens_QC;
% flt.SIGMA_THETA_FLAG = ones(size(s.pdens));
flt.DEPTH = -s.depth;
    flt.DEPTH_FLAG = s.depth_QC;
%     flt.DEPTH_FLAG = ones(size(s.depth));
flt.SBE63DO = s.doxy;
    flt.SBE63DO_FLAG = s.doxy_QC;
%     flt.SBE63DO_FLAG = ones(size(s.doxy));
% flt.O2SATPER = s.o2satper;
% flt.O2SATPER_FLAG = s.o2satper_QC;
% flt.CHL = s.opt;
% flt.CHL_FLAG = s.opt_QC;
flt.PH_IN_SITU_TOTAL_EST = s.pH_avg; % modified 4/12/2023 YT
    flt.PH_IN_SITU_TOTAL_EST_FLAG = s.pH_avg_QC ;
%     flt.PH_IN_SITU_TOTAL_EST_FLAG = ones(size(s.pH_avg));
% flt.PH_25_ATM_TOTAL = s.pH25atm;
% flt.PH_25_ATM_TOTAL_FLAG = s.pH25atm_QC;
flt.TA_EST = s.ta_avg;
    flt.TA_EST_FLAG = s.ta_avg_QC;
%     flt.TA_EST_FLAG = ones(size(s.ta_avg));
flt.DIC_EST = s.dic_avg;
    flt.DIC_EST_FLAG = s.dic_avg_QC;
%     flt.DIC_EST_FLAG = ones(size(s.dic_avg));
flt.NITRATE_EST = s.no3_avg;
    flt.NITRATE_EST_FLAG = s.no3_avg_QC;
%     flt.NITRATE_EST_FLAG = ones(size(s.no3_avg));
% flt.pCO2_CANYONB = s.pco2in;
% flt.pCO2_CANYONB_FLAG = s.pco2in_QC;
% flt.SAT_AR_CANYONB = s.satarin;
% flt.SAT_AR_CANYONB_FLAG = s.satarin_QC;
% if isfield(s,'par')
%     flt.PAR = s.par;
%     flt.PAR_FLAG = s.par_QC;
%     flt.IRRAD_380 = s.rad01;
%     flt.IRRAD_380_FLAG = s.rad01_QC;
%     flt.IRRAD_443 = s.rad02;
%     flt.IRRAD_443_FLAG = s.rad01_QC;
%     flt.IRRAD_490 = s.rad03;
%     flt.IRRAD_490_FLAG = s.rad03_QC;
% else
%     flt.PAR = nan(size(s.pres));
%     flt.PAR_FLAG = 9.*ones(size(s.pres));
%     flt.IRRAD_380 = nan(size(s.pres));
%     flt.IRRAD_380_FLAG = 9.*ones(size(s.pres));
%     flt.IRRAD_443 = nan(size(s.pres));
%     flt.IRRAD_443_FLAG = 9.*ones(size(s.pres));
%     flt.IRRAD_490 = nan(size(s.pres));
%     flt.IRRAD_490_FLAG = 9.*ones(size(s.pres));
% end
% add any additional variables here.

% =========================================================================

% define some useful variables
[flt_c, flt_r] = size(flt.TEMP);
MVI_str = '-1e10'; % missing value indicator for ODV

% ************************************************************************
% BUILD LOOK UP CELL ARRAY to match variables and set format string
% ************************************************************************
%
% this section basically taken from argo2odv_LIAR.m

%RAW ODV FILE
% column 1: header that will be printed to file
% column 2: string format for file
% column 3: variable name in float structure
 
% !! MOdified to match CUGN; original commented below 4/12/2023 YT
flt_vars(1,:)  = {'Pressure[dbar]'        '%0.2f' 'PRES' '' '' ''}; % ?
flt_vars(2,:)  = {'Temperature[°C]'       '%0.4f' 'TEMP' '' '' ''};
flt_vars(3,:)  = {'Salinity[pss]'         '%0.4f' 'SAL' '' '' ''};
flt_vars(4,:)  = {'Sigma_theta[kg/m^3]'   '%0.3f' 'SIGMA_THETA' '' '' ''};
flt_vars(5,:)  = {'Depth[m]'              '%0.3f' 'DEPTH' '' '' ''};
flt_vars(6,:)  = {'Oxygen[µmol/kg]'       '%0.2f' 'SBE63DO' '' '' ''};
flt_vars(7,:)  = {'Nitrate_EST[µmol/kg]'  '%0.1f' 'NITRATE_EST' '' '' ''};
flt_vars(8,:)  = {'pHinsitu_EST[Total]'   '%0.3f' 'PH_IN_SITU_TOTAL_EST' '' '' ''};
flt_vars(9,:)  = {'TALK_EST[µmol/kg]'     '%0.0f' 'TA_EST' '' '' ''};
flt_vars(10,:) = {'DIC_EST[µmol/kg]'      '%0.0f' 'DIC_EST' '' '' ''};

% % !! original !! 
% flt_vars(1,:)  = {'Pressure[dbar]'        '%0.2f' 'PRES' '' '' ''}; % ?
% flt_vars(2,:)  = {'Temperature[°C]'       '%0.4f' 'TEMP' '' '' ''};
% flt_vars(3,:)  = {'Salinity[pss]'         '%0.4f' 'SAL' '' '' ''};
% flt_vars(4,:)  = {'Sigma_theta[kg/m^3]'   '%0.3f' 'SIGMA_THETA' '' '' ''};
% flt_vars(5,:)  = {'Depth[m]'              '%0.3f' 'DEPTH' '' '' ''};
% flt_vars(6,:)  = {'Oxygen[µmol/kg]'       '%0.2f' 'SBE63DO' '' '' ''};
% flt_vars(7,:)  = {'OxygenSat[%]'          '%0.1f' 'O2SATPER' '' '' ''};
% flt_vars(8,:)  = {'Nitrate_EST[µmol/kg]'      '%0.2f' 'NITRATE' '' '' ''};
% flt_vars(9,:)  = {'Chl_a[mg/m^3]'         '%0.4f' 'CHL' '' '' ''};
% flt_vars(10,:) = {'b_bp700[1/m]'          '%0.6f' 'BBP700' '' '' ''};
% 
% % ADD THESE FOR ODV FLAVOR #2
% flt_vars(11,:) = {'pHinsitu_EST[Total]'       '%0.4f' 'PH_IN_SITU_TOTAL' '' '' ''};
% 
% % ADD THESE FOR ODV FLAVOR #3 - NAVIS
% flt_vars(12,:) = {'b_bp532[1/m]'          '%0.6f' 'BBP532' '' '' ''};
% flt_vars(13,:) = {'CDOM[ppb]'             '%0.2f' 'FDOM' '' '' ''};
% 
% % These are for calculate carbonate parameters
% flt_vars(14,:) = {'TALK_EST[µmol/kg]' '%0.0f' 'TA_CANYONB' '' '' ''};
% flt_vars(15,:) = {'DIC_EST[µmol/kg]'  '%0.0f' 'DIC_CANYONB' '' '' ''};
% flt_vars(16,:) = {'pCO2_CANYONB[µatm]'    '%0.1f' 'pCO2_CANYONB' '' '' ''};
% flt_vars(17,:) = {'SAT_AR_CANYONB[]'      '%0.3f' 'SAT_AR_CANYONB' '' '' ''};
% flt_vars(18,:) = {'pH25C_1atm[Total]'     '%0.4f' 'PH_25_ATM_TOTAL' '' '' ''};
% flt_vars(19,:) = {'DOWNWELL_PAR[µmol Quanta/m^2/sec]'     '%0.1f' 'PAR' '' '' ''};
% flt_vars(20,:) = {'DOWN_IRRAD380[W/m^2/nm]'     '%0.1f' 'IRRAD_380' '' '' ''};
% flt_vars(21,:) = {'DOWN_IRRAD443[W/m^2/nm]'     '%0.1f' 'IRRAD_443' '' '' ''};
% flt_vars(22,:) = {'DOWN_IRRAD490[W/m^2/nm]'     '%0.1f' 'IRRAD_490' '' '' ''};


% add additional variables here.

flt_var_ct = size(flt_vars,1); % number of variables to write

% ========================================================================
% Create NaN arrays for variables that don't exist: Sigma_theta, % modified
% for spray, 4/5/2019
% Changed the default array for the QC array to ones. 4/16/2019 YT

% check to see if the variables exist. if not, create it
for i = 1:size(flt_vars,1)
    if(~isfield(flt, flt_vars{i,3}))
        flt.(flt_vars{i,3}) = NaN(size(flt.TEMP));
        flt.([flt_vars{i,3},'_FLAG']) = 9.*ones(size(flt.TEMP));
    end

end
% =======================================================================

% commented out this section because parse_spraysat.m already uses argo QC
% flags
%
% % CHANGE QUALITY FLAGS TO ARGO FORMAT
% for i = 1:size(flt_vars,1)
%     flagvar = [flt_vars{i,3},'_FLAG']; % get name of flag variable
%     tempflag = flt.(flagvar); % temporarly variable to save flags into
%
%     % if NaN -> 1; missing data
%     tempflag(isnan(flt.(flagvar))) = 1;
%     % if 1 -> 0; good data
%     tempflag(flt.(flagvar)==1) = 0;
%     % if 4 or 9 -> 8; bad data. I don't know what flag 9
%     tempflag(flt.(flagvar)==4 | flt.(flagvar)==9) = 8;
%     % if 2 or 3 -> 4; questionable data
%     tempflag(flt.(flagvar)==2 | flt.(flagvar)==3) = 4;
%
%     flt.(flagvar) = tempflag; % save over original flag
% end

% Change quality flag for POSITION_FLAG and juld_QC (They were all good so
% just convert 1 to 0)
% flt.JULD_QC(flt.JULD_QC=='1') = '0';
% flt.POSITION_FLAG(flt.POSITION_FLAG=='1') = '0';

% *************************************************************************
% PRINT COMMENTED HEADERS
% *************************************************************************

% Open file, write access
fid = fopen([dir2save,filesep,txtfilename], 'w');

% if(is_sat)
%     fprintf(fid, '//File Created from Spray glider sat file\r\n');
% else
%     fprintf(fid, '//File Created from full resolution Spray data upon recovery\r\n');
% end

fprintf(fid, '//File Created from CUGN data, downloaded April 2023\r\n'); % modified 4/12/2023 YT
fprintf(fid, ['//File created on',datestr(now,'mm/dd/yyyy HH:MM'),'\r\n']);
fprintf(fid, ['//Transect ID: ',CUGN_name,'\r\n']); % modified 4/12/2023 YT
fprintf(fid, ['//Estimated parameters are average of CANYONB, ESPER-LIR, and ESPER-NN \r\n']); % modified for CUGN; 4/12/2023 YT
fprintf(fid, ['//SigmaTA = 15umol/kg, SigmaDIC = 20umol/kg, sigmapH = 0.05, sigmaNO3 = 3umol/kg\r\n']); % added 4/12/2023 YT 
fprintf(fid, '//Missing data value = -1e10\r\n');
fprintf(fid, ['//Data quality flags: 0 = Uninspected, 1=Good, 2=Probably Good,' ...
    ' 3=Probably Bad, 4=Bad, 9=Missing or not inspected \r\n']);

% *************************************************************************
% PRINT VARIABLE HEADERS
% *************************************************************************
%
% this section basically taken from argo2odv_LIAR.m

% Standard ODV variable headers
std_ODV_vars   = {'Cruise' 'Station' 'Type' 'mon/day/yr' 'hh:mm' ...
    'Lon [°E]' 'Lat [°N]' 'LOCATION_QV:ARGO'}; % SIZE = 8
std_size = size(std_ODV_vars,2);

for i = 1:std_size % PRINT STANDARD HEADER VARS
    fprintf(fid,'%s\t',std_ODV_vars{1,i}); % std vars
end

% Float specific variable headers
for i = 1:flt_var_ct % PRINT FLOAT SPECIFIC HEADER VARS
    if i < flt_var_ct
        fprintf(fid,'%s\t%s\t',flt_vars{i,1},[flt_vars{i,1},'_QV:ARGO']); % std vars
    else
        fprintf(fid,'%s\t%s\r\n',flt_vars{i,1},[flt_vars{i,1},'_QV:ARGO']); % std vars
    end
end

% *************************************************************************
% NOW PRINT DATA LINES TO FILE
% *************************************************************************

ODV_std_f = '%s\t%0.0f\t%s\t%s\t%s\t%0.3f\t%0.3f\t%0.0f\t'; %std_var format

% create string of n tabs, where n = number of variables
profspacertabs = '';
for i = 1:size(flt_vars,1)-1
    profspacertabs = [profspacertabs,'\t'];
end

nlines = 0; % counter for number of lines in the floatviz file

% loop through columns (c) and rows (r)
for c = 1:length(flt.SDN)
    if(~isnan(flt.SDN(c)))
        % get date str and time str
        date_str = datestr(flt.SDN(c), 'mm/dd/yyyy');
        time_str = datestr(flt.SDN(c), 'HH:MM');
        % Build standard variable string and write to file.
        % moved this section outside of for loop for r below. 12/10/2018 YT
        std_str = sprintf(ODV_std_f, CUGN_name, c, 'C', ...
            date_str, time_str, flt.LONG(c), flt.LAT(c), flt.POSITION_FLAG(c));
        regexprep(std_str, 'NaN', MVI_str); % replace Nans with MVI
        % now loop through rows
        for r = size(flt.DEPTH,1):-1:1 % Changed this to decrement 12/10/2018 YT
            % check to see if there's any pressure data. if not, break.
            if(~isnan(flt.DEPTH(r,c)) && ~isnan(flt.TEMP(r,c)) && ~isnan(flt.SAL(r,c))) %Only write data if pressure, temp, or salt data exists % modified from just pressure, Dec-10-2018 YT

                fprintf(fid, std_str); % write std to file

                % Build float variable string and write to file

                var_str = ''; % initialize
                for v = 1:size(flt_vars,1) % loop through each variable
                    if(v < size(flt_vars,1)) % check to see if it's last one
                        % if it's not the last one, add tab
                        var_str = [var_str,sprintf([flt_vars{v,2},'\t%0.0f\t'], flt.(flt_vars{v,3})(r,c), flt.([flt_vars{v,3},'_FLAG'])(r,c))];
                    else
                        % if it's the last one, don't add tab, but \r\n
                        var_str = [var_str,sprintf([flt_vars{v,2},'\t%0.0f\r\n'], flt.(flt_vars{v,3})(r,c), flt.([flt_vars{v,3},'_FLAG'])(r,c))];
                    end
                end

                % replace NaN' w/ missing value indicator
                var_str = regexprep(var_str,'NaN',MVI_str);
                % write variable
                fprintf(fid, var_str);
                nlines = nlines+1;
            end
        end

        % at the end of each profile, add profile spacer line, which
        fprintf(fid, [std_str,profspacertabs,'\r\n']);
        nlines = nlines+1;
    end
end

% close file
fid = fclose(fid);

disp([txtfilename, 'complete!']);

% 
% % make .cfg file
% fcfg = fopen(cfgfilename, 'w');
% fprintf(fcfg, ['//',num2str(nlines,'%0.0f')]);
% fclose(fcfg);
% 
% % copy files to sirocco
% copyfile(txtfilename, path);
% copyfile(cfgfilename, path);


