%{
    process_raw_psd_wcf.m

    Reads raw esrl long/short radiation and soil moisture data.
    Computes avg and std dev for 10 min intervals, and saves to daily files.

    column ID for 1 min data is as follows:
    
    Field: 1 Year
    Field: 2 Month (beginning time of average)
    Field: 3 Day (beginning time of average)
    Field: 4 Hour (beginning time of average)
    Field: 5 Minute (beginning time of average)
    Field: 6 Decimal Day-of-Year (beginning time of average)
    Field: 7 Battery Voltage (volts)
    Field: 8 Downward PIR Resistance (ohms)
    Field: 9 Downward PIR Temperature (C)
    Field: 10 Downward PIR Dome Resistance (ohms)
    Field: 11 Downward PIR Dome Temperature (C)
    Field: 12 Upward PIR Resistance (ohms)
    Field: 13 Upward PIR Temperature (C)
    Field: 14 Upward PIR Dome Resistance (ohms)
    Field: 15 Upward PIR Dome Temperature (C)
    Field: 16 Downward Longwave (mV)
    Field: 17 Downward Longwave Radiation (W/m^2)
    Field: 18 Downward Short Wave (mV)
    Field: 19 Downward Short Wave Radiation (W/m^2)
    Field: 20 Upward Longwave (mV)
    Field: 21 Upward Longwave Radiation (W/m^2)
    Field: 22 Upward Short Wave (mV)
    Field: 23 Upward Short Wave Radiation (W/m^2)
    Field: 24 Soil Temperature (C) 5cm
    Field: 25 Soil Temperature (C) 10cm
    Field: 26 Soil Temperature (C) 20cm 
    Field: 27 Soil Temperature (C) 50cm
    Field: 28 Soil Temperature (C) 100cm
    Field: 29 Soil Reflectometer Output Period (usec) 5cm
    Field: 30 Soil Moisture (% vol) 5cm
    Field: 31 Soil Reflectometer Output Period (usec) 10cm
    Field: 32 Soil Moisture (% vol) 10cm
    Field: 33 Soil Reflectometer Output Period (usec) 20cm
    Field: 34 Soil Moisture (% vol) 20cm
    Field: 35 Soil Reflectometer Output Period (usec) 50cm
    Field: 36 Soil Moisture (% vol) 50cm
    Field: 37 Soil Reflectometer Output Period (usec) 100cm
    Field: 38 Soil Moisture (% vol) 100cm
    Field: 39 Net Radiation (W/m^2)

    Output column ID for 10 min stats as follows:
        columns 2:16 are averages
        columns 17:31 are std deviations
    
    Field: 1 Decimal Day-of-Year (beginning time of average)
    Field: 2 Downward Longwave Radiation (W/m^2)
    Field: 3 Downward Short Wave Radiation (W/m^2)
    Field: 4 Upward Longwave Radiation (W/m^2)
    Field: 5 Upward Short Wave Radiation (W/m^2)
    Field: 6 Net Radiation (W/m^2)
    Field: 7 Soil Temperature (C) 5cm
    Field: 8 Soil Temperature (C) 10cm
    Field: 9 Soil Temperature (C) 20cm 
    Field: 10 Soil Temperature (C) 50cm
    Field: 11 Soil Temperature (C) 100cm
    Field: 12 Soil Moisture (% vol) 5cm
    Field: 13 Soil Moisture (% vol) 10cm
    Field: 14 Soil Moisture (% vol) 20cm
    Field: 15 Soil Moisture (% vol) 50cm
    Field: 16 Soil Moisture (% vol) 100cm
    Fields 17:31 same ID, but std deviations...
%}

%% initialize run
clear
warning('off','MATLAB:MKDIR:DirectoryExists');

% set start/end julian days for raw 15 min data files
% assume data will be processed in whole 24 hour increments
% start/stop dates should be confined to a single year
yyyy = 2017; yrstr = sprintf('%4i',yyyy); yrstr = yrstr(3:4);
jd_start = md2yd(yyyy,04,24);
jd_end = md2yd(yyyy,05,01);

% matlab script path
path_prog = fullfile('/Users/byronb/Documents/MATLAB/2016_WFIP2');
cd(path_prog);                  % cd to matlab directory
restoredefaultpath              % reset matlab path
addpath(genpath(path_prog));	% add scripts to path
savepath; rehash;

% data paths
raw_path = '/Users/byronb/Raw_Data_Files.noindex/WFIP2_2016/PS01/raw_psd_wcf';
out_path = '/Users/byronb/Raw_Data_Files.noindex/WFIP2_2016/PS01/raw_psd_wcf_10min';
mkdir(out_path);

%% read raw files and save daily data in .txt format
% missing data represented as NaN

for ddd = jd_start:jd_end    % loop through days
    [month,day] = yd2md(yyyy,ddd);
    jdstr = sprintf('%03i',ddd);
    jd_bins = ddd + 0:(10/1440):ddd+1; jd_bins = jd_bins'; % for 10min bin averaging
    
    srcfldr = fullfile(raw_path,[yrstr,jdstr]);
    if ~exist(srcfldr,'dir')
        continue
    end
    
    init = 1;
    for hr = 0:23   % loop through hours, read and concatenate files
        hrstr = sprintf('%02i',hr);
        dfl = fullfile(srcfldr,['wcf',yrstr,jdstr,'.',hrstr,'m']);
        temp  = read_wcf_raw(dfl,yyyy,month,day,hr,ddd);
        if init
            data = temp;
            init = 0;
        else
            data = [data;temp];
        end
    end
    
    % select columns for averaging:
    % jd, rl_dn, rs_dn, rl_up, rs_up, rnet, Tsoil(5,10,20,50,100), Smoist%(5,10,20,50,100)
    data2 = [data(:,6),data(:,17),data(:,19),data(:,21),data(:,23),...
             data(:,39),data(:,24:28),data(:,30),data(:,32),...
             data(:,34),data(:,36),data(:,38),]; % 1440 x 16 array
    
    % 10 min stats for this day
    avg10 = interval_avg(data2(:,1),data2(:,2:end),jd_bins);
    std10 = interval_std(data2(:,1),data2(:,2:end),jd_bins);
    out10 = [avg10,std10(:,2:end)]; % 144 x 31 array
    
    % save data to txt files
    dfl = fullfile(srcfldr,['wcf',yrstr,jdstr,'.all']);
    save(dfl,'data','-ascii','-double');
    
    dfl = fullfile(out_path,['RadSoil_10min_',yrstr,jdstr,'.txt']);
    save(dfl,'out10','-ascii','-double');
end
    
    
    
    