clear all;
close all;

% Parse the high resolution met data and concatenate for entire cruise.

%%% do this manually:
disp('-----------------------------------------------------');
disp('do this manually first:');
disp('cp *.MET ~/DATA/PISTON_2019/Sally_Ride/ship/data/');
disp('-----------------------------------------------------');

bigdata = [];

savedir = '/Users/eliz/DATA/PISTON_2019/Sally_Ride/ship/';
datadir = '/Users/eliz/DATA/PISTON_2019/Sally_Ride/ship/MET_data/';
files = dir([datadir '*.MET']);
nfiles = length(files);

halfway = round(length(files)/2);
lastone = length(files)-1;

% for i = 1:8           %%% 9/2 through 9/9    ... already done on 9/15
% for i = 9:15            %%% 9/10 (pt 1 and pt 2) through 9/15  ...
for i = nfiles     %%% 9/16 through 9/25  ... numbered sequentially conveninently
    
    fname = [datadir files(i).name];
    disp(['** reading in file: ' fname]);
    try
    eval(['!grep -v "#" ' fname ' > myfile'])
    a = load('myfile');
    
    %%% because file headers changed mid cruise, don't save any data past
    %%% column 76. Otherwise program will crash because column numbers will
    %%% be different. 
    bigdata = [bigdata; a(:,1:76)];
    catch
        disp('cant do it')
    end
end
delete myfile
clear a datadir files fname i

%%% comment out stuff you don't want

data.air_temperature            = bigdata(:,2); % C
data.barometric_pressure        = bigdata(:,3); % mb 
data.barometric_pressure_cor    = bigdata(:,4); % mb corrected for sea level
data.relative_humidity          = bigdata(:,5); % percent
data.air_temp_rh                = bigdata(:,6); % C
data.dew_point                  = bigdata(:,7); % C
data.precipitation              = bigdata(:,8); % mm
data.rel_wind_speed             = bigdata(:,9); % m/s
data.rel_wind_direction         = bigdata(:,10); % deg (where wind is from)
data.true_wind_speed            = bigdata(:,11); % m/s
data.true_wind_direction        = bigdata(:,12); % deg (where wind is from)
data.LWR_dome_temperature       = bigdata(:,13); % K
data.LWR_body_temperature       = bigdata(:,14); % K
data.LWR_theropile              = bigdata(:,15); % V
data.long_wave_radiation        = bigdata(:,16); % W/m^2
data.short_wave_radiation       = bigdata(:,17); % W/m^2
data.surface_PAR                = bigdata(:,18); % uE/s/m^2
data.ship_speed_longitudinal    = bigdata(:,19); % longitudninal E/W, knots
data.ship_speed_transverse      = bigdata(:,20); % latitudinal N/S, knots
data.latitude                   = bigdata(:,21); % decimal deg
data.longitude                  = bigdata(:,22); % decimal deg
data.gps_time_of_day            = bigdata(:,23); % GMT secs
data.gps_cog                    = bigdata(:,24); % deg
data.gps_sog                    = bigdata(:,25); % knots
data.gps_datetime               = bigdata(:,26); % GMT secs since 01/01/1970
data.ship_heading               = bigdata(:,76); % deg
data.TSG_temperature_1 = bigdata(:,53);      % C (bow SBE45, smoothed)
data.TSG_temperature_2 = bigdata(:,61);      % C (hydro lab SBE45, smoothed)
data.TSG_conductivity_1 = bigdata(:,54);     % mS/cm (bow SBE45, smoothed)
data.TSG_conductivity_2 = bigdata(:,62);     % mS/cm (hydro lab SBE45, smoothed)
data.salinity_1 = bigdata(:,55);             % psu (bow SBE45, smoothed)
data.salinity_2 = bigdata(:,63);             % psu (hydro lab SBE45, smoothed)
data.sigma_t_1 = bigdata(:,56);              % kg/m^3 (bow)
data.sigma_t_2 = bigdata(:,64);              % kg/m^3 (hydro lab)
data.USW_flow_meter_1 = bigdata(:,59);       % L/min (bow)... FI (for first)
data.USW_flow_meter_2 = bigdata(:,67);       % L/min (hydro lab) ... FI-3 (FI-2 is for dysfunctional PCO2 pump... for some strange reason)

%date in matlab datenum format
data.dnum = data.gps_datetime/86400 + datenum(1970,1,1,0,0,0);

[yy0, mm0, dd0, hh0, min0, ss0] = datevec(data.dnum);
jd0 = md2yd_vn(yy0,mm0,dd0);
dayfraction = days(duration(hh0, mm0, ss0));
data.jd = jd0 + dayfraction;


data.readme = {'---- BEGIN README ----';...
               'from PISTON_ship_met.m';...
               'TSG 1 = bow TSG on bow water (1st draw), then seachest (2nd draw)';...
               'TSG 2 = main lab TSG on bow water (2nd draw), then seachest (1st draw)';...
               'UNITS:::: ';...
               '	pressure = mb';...
               '	salinity = PSU';...
               '	conductivity mS/cm';...
               '	temp = degC';...
               '    flow rate = L/min';...
               '	wind dir = deg wind coming from';...
               '	wind speed = m/s';...
               '	SOG = knots';...
               '	sigmaT density =  kg/m^3';...
               '	dnum = matlab time';...
               '	GPS datetime = GMT secs since 01/01/1970';...
               '    pitch, roll, heave, heading in degress';...
               '---- END README ----'};

clear bigdata

part_st = datestr(max(data.dnum),'mmDD');
save([savedir 'MET_SallyRide_PISTON_2019_thru_' part_st], 'data');

%end