%%% program to add or change variables to .mat files of 1-min met_sea files
%%% or 10-min flux_met_sea files. 
%%% Elizabeth Thompson. Jan 2020. elizabeth.thompson@noaa.gov
%%% NOAA ESRL PSD

clear all;
close all;
warning ('off','MATLAB:MKDIR:DirectoryExists');

% cruise ID and paths
cruise = 'ATOMIC_2020';  % cruise name
cruise_str = 'ATOMIC';  % cruise name without _
ship = 'Brown';             % research vessel
plotit = true;                   % create plots and save to .png format
yyyy = '2020';


% system specific path defs
sysType = computer;
username=char(java.lang.System.getProperty('user.name'));
if strncmp(sysType,'MACI64',7)     % set Mac paths
    data_drive = '/Users/eliz/DATA/';
    path_prog = fullfile(data_drive,cruise,ship,'Scientific_analysis','programs');
elseif strncmp(sysType,'PCWIN64',7)  % set PSD DAC paths
    data_drive = 'D:\DATA\';
    path_prog = fullfile(data_drive,cruise,ship,'Scientific_analysis','programs');
end

% matlab script path
restoredefaultpath
cd(fullfile(path_prog,'flux'));
addpath(genpath(fullfile(path_prog,'flux')));
rehash toolboxcache;

%%% choose program type: 1: MANUALflux_eval or 2: run_motcorr
for program_type = [2]
    
    %------------------------------------------------------------------
        %% do something a little different for 1 or 10 min files
        for file_type = [10]

        %%% load both 1 min and 10 min files for MANUALflux_eval. 
        %%% nfiles should be same for both types of files.

        %%% choose file_type 1 or 10 (minute update interval)
        if file_type == 1 && program_type == 1
            dir_str = 'met_sea';
        elseif file_type == 10 && program_type == 1
            dir_str = 'met_sea';
        elseif file_type == 10 && program_type == 2
            dir_str = 'flux_met_sea';        
        end
        
        path = ['/Users/eliz/DATA/ATOMIC_2020/Brown/flux/Field_Processed/' dir_str '_v0/'];
        path_v2 = ['/Users/eliz/DATA/ATOMIC_2020/Brown/flux/Field_Processed/' dir_str '/'];
        mkdir(path_v2);

        mat_files = dir([path 'ATOMIC_2020_' sprintf('%i',file_type) 'min*_jd*.mat']);
        nfiles = length(mat_files);
        
        %% for all the files of this type and program
        for i = 1:nfiles
            
            %% load each 1-min or 10-min file
            file = [char(mat_files(i).folder) '/' char(mat_files(i).name)];
            file_save = [path_v2 char(mat_files(i).name)];
            disp(['Loading file # ' sprintf('%i',i) '/' sprintf('%i',nfiles)  ': ' file]);
            load(file);
            
            %%% choose file_type 1 or 10 (minute update interval)
            if file_type == 1 && program_type == 1
                s = f;
            elseif file_type == 10 && program_type == 1
                s = g;
            elseif file_type == 10 && program_type == 2
                s = ff;
            end
   
            the_jd = s.jd(1);

            fmet = fields(s);
            field1 = zeros(1,length(fmet));

            %% nan out data from port stops
            port_time_a = find(s.t >= datenum(2020,1,26,10,0,0) & s.t <= datenum(2020,1,29,0,0,0));
            port_time_b = find(s.t >= datenum(2020,2,4,20,0,0) & s.t <= datenum(2020,2,6,17,0,0));
            port_times = [port_time_a port_time_b];

            for j = 1:length(fmet)
                x1 = s.(fmet{j});
                if length(x1) > 100 
                    if strcmp(fmet(j),'t') ~= 1 && strcmp(fmet(j),'jd') ~= 1
                        x1(port_times) = nan;
                        s.(fmet{j}) = x1;
                     end
                else
                    % track this as a 1-member array
                    field1(j) = 1;
                end
            end   
            
            %% other potential file actions
%             %% nan out bad snake data
%             if the_jd == 16
%                 out_for_mooring_1 = find(s.t > datenum(2020,1,16,14,0,0) & s.t < datenum(2020,1,16,20,0,0));
%                 s.Tsnk(out_for_mooring_1) = NaN;
%             end
%             if the_jd == 39
%                 pull_for_recovery = find(s.t >= datenum(2020,2,8,11,45,0) & s.t <= datenum(2020,2,8,12,30,0));
%                 s.Tsnk(pull_for_recovery) = NaN;
%             end
            
%             %% If adding a new field... remove readme so it gets added at end, reorder, and then add readme back in
%             x = s.readme;
%             s = rmfield(s,{'readme'});
%             s = orderfields(ff);
%             s.readme = x;  

            %% rename s to f or g depending on file type, save as same .mat file
            if file_type == 1 && program_type == 1
                f = s;
                save(file_save,'f'); 
            elseif file_type == 10 && program_type == 1
                g = s;
                save(file_save,'g');  
            elseif file_type == 10 && program_type == 2
                ff = s;
                save(file_save, 'ff');
            end

            %% save as text file without the 1-D fields
            allfields = fields(s);
            ns = length(allfields);
            %%% remove the 1D fields
            fields_1D = allfields(field1 == 1);
            s2 = rmfield(s,fields_1D);
            %%% add in a human readable time array
            [s2.year, s2.month, s2.day, s2.hour, s2.minute] = datevec(s.t);
            s2 = orderfields(s2);
            %%% save text file
            fname = char(mat_files(i).name);
            mat2txt(fname(1:end-4), 'n', path_v2 , s2);
            %%% put entire structure into same ordered array with columns and rows
            fields_s2 = fields(s2);
            ns2 = length(fields_s2);


        end  %%% for all files

        %%% num vars in each file
        disp(['num vars in txt file: ' sprintf('%i',ns2)]);
        disp(['num vars in mat file: ' sprintf('%i',ns)]);
     
        %% print header at end, just once per file type / program type
        disp('this is the header for each file:');
        nchars = length(s.t);
        k = repmat('                    ',nchars,1);
        for j = 1:ns2
            k(j) = fprintf('%20s',char(fields_s2(j)));
        end

        
        end % for both file types of 1 and 10 minutes
end % for both program types 1 (manual flux) and 2 (mot corr)