%%% program to concatenate .mat files 
%%% Elizabeth Thompson. Jan 2020. elizabeth.thompson@noaa.gov
%%% NOAA PSL
%%% 
%%% search for lines with **** - alerts where user needs to set the path 
%%% where files live and decide whether to concatenate 1-min or 10-min 
%%% files, or both. Then program returns c1 or c10, which are structures 
%%% of all days for entire cruise data with the same fields as daily files,
%%% plus the readme and meta data stored in each daily file.

clear all;
close all;

%%% **** set your own paths for where you saved the daily files
thepath = '/Users/eliz/DATA/PISTON_2019/Sally_Ride/Ceilometer/Processed/all/';

cc_name     = [thepath 'PISTON_2019_ceilometer_15sec.mat'];
c10_name    = [thepath 'PISTON_2019_ceilometer_10min.mat'];
chr_name    = [thepath 'PISTON_2019_ceilometer_1hour.mat'];

%%% look for all daily .mat files in directory, those with DOY in name
mat_files = dir([thepath 'PISTON_2019_*DOY*.mat']);
%%% count the files
nfiles = length(mat_files);
clear c10 chr cc;

for i = 1:nfiles
    
    %%% load each file
    file = [char(mat_files(i).folder),'/',char(mat_files(i).name)];
    disp(['Loading file # ' sprintf('%i',i) '/' sprintf('%i',nfiles)  ': ' file]);
    load(file);
       
    %%% only grab field names once. they will be same for all files. 
    if i ==1
        thefields = fields(c);
        nc = length(thefields);

        vartypes = cell(nc,1);
        dr = nan(nc,1);
        dc = nan(nc,1);
        for j = 1:nc
            eval(['thisvar = c.' thefields{j} ';']);
            varname = char(thefields(j));
            vartype = class(thisvar);
            vartypes(j) = {vartype};
            [dr(j), dc(j)] = size(thisvar);
        %     disp([' saving ' varname ' = ' vartype]);
        end

        dt       = find(dr == length(c.t)       & dc == 1);   % discretized in time by 15 sec
        dt_10    = find(dr == length(c.t_10min) & dc == 1);   % discretized in time by 10 min
        dt_hr    = find(dr == length(c.t_hr)    & dc == 1);   % discretized in time by hour
        dz       = find(dr == length(c.range)   & dc == 1);   % range
        d2       = find(dr == length(c.range)   & dc == length(c.t));  % ceil_b
        
        f_10 = thefields(dt_10);
        f_hr = thefields(dt_hr);
        f = thefields(dt);
        
        % start data arrays... drop the _hr part of the hour file var names
        for j = 1:length(dt)
           cc.(f{j}) = c.(f{j});
        end
        cc.ceil_b = c.ceil_b;
        cc.height = c.range; % note... switching nomenclature here from range to height %%% only have to do this once too. 
        
        for j = 1:length(dt_10)
           c10.(f_10{j}) = c.(f_10{j});
           chr.(f_hr{j}) = c.(f_hr{j});
        end
        
    else %% for all other days

        %%% grab and put rest of variables in big array
        for j = 1:length(dt)
           cc.(f{j}) = [cc.(f{j}); c.(f{j})];
        end
        cc.ceil_b = [cc.ceil_b c.ceil_b];
        %%% ... drop the _hr part of the hour file var names
        for j = 1:length(dt_10)
           c10.(f_10{j}) = [c10.(f_10{j}); c.(f_10{j})];
           chr.(f_hr{j}) = [chr.(f_hr{j}); c.(f_hr{j})];
        end
    end  

end  %% array of all days

save(cc_name,   'cc');
disp(['saved ' cc_name]);
save(c10_name,  'c10');
disp(['saved ' c10_name]);
save(chr_name,  'chr');
disp(['saved ' chr_name]);
