% define_specs_and_process_scs_files.m
%
%
% Define all the specs for scs data in this code for a given ship/cruise.
% Then call read_scs_file_v3 which will actually parse the scs files and
% save .mat files
%
% First define directories:
% datadirup = directory where ship saves scs data files
% localdirup = local directory where you want to save processed .mat files
%   
% Then define all the necessary parameters for each scs feel that you would
% like to parse.
%   foldername = name of folder for that data-type in datadirup (note, on
%       some ships, this may be left blank by just using filesep
%   filename   = name of file (not including date)  
%   strformat  = format of each line in the text file with raw data
%       (%s is for string, %f is for number; values must be separated by
%       commas)
%   strformaterror = used if there are errors in the raw data. Same number
%       of fields as strformat, but all strings (%s) no numbers (%f)
%   vars = names of the variables (do not include time)
%   vars_position = column number for each variable named in 'vars' that
%       corresponds to strformat. (MUST have same length as vars!)
%   readmetext = text that you would like to include in the readme 
%
% Written by Sally Warner over the course of many cruises on many ships
% refined most recently for PISTON, August 2018

clear

% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define directories
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
% directory where ship saves raw scs data as text files
datadirup = '~/tgt-data/scs/';

% local directory where you want to save processed .mat files
localdirup = '~/Dropbox/data/piston18/scs/processed_testv3/';

% figure directory
figdir = '~/Dropbox/data/piston18/scs/figures/';

% remote directory where you would like to backup the processed data
% (note: you may define multiple locations here... just add more rsync
% lines at the end)
backupdirproc = '~/wdmycloud/piston18/scs/processed/';
backupdirproc_cruiseshare = '~/cruiseshare/scs/processed/';

% remote directory where you would like to backup the raw data from the
% ship's server
backupdirraw  = '~/wdmycloud/piston18/scs/raw/';

% remote directory for figures
backupdirfigs_cruiseshare = '~/cruiseshare/scs/figures/';

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% define important parameters and process data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

yn = 0;

if yn
    
end
    
%%%% MET/PAR-RAW %%%%
foldername      = 'MET';
filename        = 'PAR-RAW';
strformat       = '%s %s %s %f %f %f';
strformaterror  = '%s %s %s %s %s %s';
vars            = {'par';'air_temp';'voltage'};
vars_position   = 4:6;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'PAR measurements',...
                    'par = PAR [uE/m^2]',...
                    'air_temp = [°C]',...
                    'voltage = [V]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[par] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


%%%% MET/RAD-RAW %%%%
foldername      = 'MET';
filename        = 'RAD-RAW';
strformat       = '%s %s %s %s %s %f %f %f %f %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'pir';'lw';'pir_case_temp';'pir_dome_temp';'sw'};
vars_position   = 7:11;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Radiation measurements',...
                    'pir = PIR [mV]',...
                    'lw = longwave radiation [W/m^2]',...
                    'pir_case_temp = PIR case temperature [°C]',...
                    'pir_dome_temp = PIR dome temperature [°C]',...
                    'sw = longwave radiation [W/m^2]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[rad] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


%%%% MET/SONIC-TW-SPD10-AVE %%%%
foldername      = 'MET';
filename        = 'SONIC-TW-SPD10-AVE';
strformat       = '%s %s %s %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s';
vars            = {'true_wind_spd'};
vars_position   = 4;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Sonic wind sensor 10 minute average',...
                    'True wind is measured wind speed minus ship velocity and motion',...
                    'true_wind_spd = true wind speed [kts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[wind_sonic_spd_10min] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% MET/SONIC-TW-DIR10-AVE %%%%
foldername      = 'MET';
filename        = 'SONIC-TW-DIR10-AVE';
strformat       = '%s %s %s %f %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s %s';
vars            = {'true_wind_dir'};
vars_position   = 4;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Sonic wind sensor 10 minute average',...
                    'True wind is measured wind speed minus ship velocity and motion',...
                    'true_wind_dir = true wind direction [deg CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[wind_sonic_dir_10min] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


%%%% MET/SONIC-TWIND-RAW %%%%
foldername      = 'MET';
filename        = 'SONIC-TWIND-RAW';
strformat       = '%s %s %s %f %f %f %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s';
vars            = {'true_wind_spd';'true_wind_dir'};
vars_position   = 4:5;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Sonic wind sensor',...
                    'True wind is measured wind speed minus ship velocity and motion',...
                    'true_wind_spd = true wind speed [kts]',...
                    'true_wind_dir = true wind direction [deg CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[wind_sonic] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


%%%% MET/BRIDGE-WIND-STBD-DRV-Data %%%%
foldername      = 'MET';
filename        = 'BRIDGE-WIND-STBD-DRV-Data';
strformat       = '%s %s %s %f %f %f %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s';
vars            = {'true_wind_spd';'true_wind_dir'};
vars_position   = 4:5;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Bridge wind sensor on starboard side',...
                    'True wind is measured wind speed minus ship velocity and motion',...
                    'true_wind_spd = true wind speed [kts]',...
                    'true_wind_dir = true wind direction [deg CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[wind_stbd] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


%%%% MET/BRIDGE-WIND-PORT-DRV-Data %%%%
foldername      = 'MET';
filename        = 'BRIDGE-WIND-PORT-DRV-Data';
strformat       = '%s %s %s %f %f %f %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s';
vars            = {'true_wind_spd';'true_wind_dir'};
vars_position   = 4:5;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Bridge wind sensor on port side',...
                    'True wind is measured wind speed minus ship velocity and motion',...
                    'true_wind_spd = true wind speed [kts]',...
                    'true_wind_dir = true wind direction [deg CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[wind_port] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% MET/BOW-MET-RAW %%%%
foldername      = 'MET';
filename        = 'BOW-MET-RAW';
strformat       = '%s %s %s %f %f %f %f %f %*[^\n]';
strformaterror  = '%s %s %s %s %s %s %s %s %*[^\n]';
vars            = {'rel_wind_spd';'rel_wind_dir';'air_temp';'rh';'pressure'};
vars_position   = 4:8;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Bow met data',...
                    'rel_wind_spd = relative wind speed (Ultrasonic) [kts]',...
                    'rel_wind_dir = relative wind direction (Ultrasonic) [deg CW from N]',...
                    'air_temp = air temperature [°C]',...
                    'rh = relative humidity [%]',...
                    'pressure = barometric pressure [mbar]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[met] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);


% %%%% ANCILLARY/WAMOS-RAW %%%%
foldername      = 'ANCILLARY';
filename        = 'WAMOS-RAW';
strformat       = '%s %s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %s';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'sig_wave_height';'mean_period';'peak_wave_dir';...
    'peak_wave_period';'peak_wave_length';'swell_wave_dir';...
    'swell_wave_period';'swell_wave_length';'wind_wave_dir';...
    'wind_wave_period';'wind_wave_current_dir';'current_dir';'current_speed'};
vars_position   = 4:16;
startfilenum = 2;
startlinenum = 2;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'Wave information',...
                    'sig_wave_height = significant wave height [m]',...
                    'mean_period = mean wave period [s]',...
                    'peak_wave_dir = peak wave direction (coming from) [° CW from N]',...
                    'peak_wave_period = peak wave period [s]',...
                    'peak_wave_length = peak wave length [m]',...
                    'swell_wave_dir = swell wave direction (coming from) [° CW from N]',...
                    'swell_wave_period = swell wave period [s]',...
                    'swell_wave_length = swell wave length [m]',...
                    'wind_wave_dir = wind sea wave direction (coming from) [° CW from N]',...
                    'wind_wave_period = wind sea wave period [s]',...
                    'wind_wave_current_dir = wind sea wave current direction (coming from) [° CW from N]',...
                    'current_dir = current direction (coming from) [° CW from N]',...
                    'current_speed = current speed [m/s]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[waves] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext,...
    'startfilenum',startfilenum,'startlinenum',startlinenum);




%%%% NAV/POSMV-V5-INVTG-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V5-INVTG-RAW';
strformat       = '%s %s %s %f %s %s %s %f %s %f %s %s';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'COG';'SOG'};
vars_position   = [4 8];
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'CNAV course over ground and speed over ground from POSMV5',...
                    'COG = course over ground [° CW from N]',...
                    'SOG = speed over ground [kts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[cog_sog_posmv5] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/POSMV-V4-INVTG-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V4-INVTG-RAW';
strformat       = '%s %s %s %f %s %s %s %f %s %f %s %s';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'COG';'SOG'};
vars_position   = [4 8];
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'CNAV course over ground and speed over ground from POSMV4',...
                    'COG = course over ground [° CW from N]',...
                    'SOG = speed over ground [kts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[cog_sog_posmv4] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/CNAV3050-VTG-RAW %%%%
foldername      = 'NAV';
filename        = 'CNAV3050-VTG-RAW';
strformat       = '%s %s %s %f %s %s %s %f %s %f %s %s';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'COG';'SOG'};
vars_position   = [4 8];
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'CNAV course over ground and speed over ground',...
                    'COG = course over ground [° CW from N]',...
                    'SOG = speed over ground [kts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[cog_sog_cnav] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/CNAV3050-SOG-10min-AVE-DRV %%%%
foldername      = 'NAV';
filename        = 'CNAV3050-SOG-10min-AVE-DRV';
strformat       = '%s %s %s %f %f %f %f';
strformaterror  = '%s %s %s %s %s %s %s';
vars            = {'SOG'};
vars_position   = 4;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'CNAV 10 min averaged speed over ground',...
                    'SOG = speed over ground [kts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[sog_10min] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/CNAV3050-GGA-RAW %%%%
foldername      = 'NAV';
filename        = 'CNAV3050-GGA-RAW';
strformat       = '%s %s %s %s %f %s %f %s %*[^\n]';
strformaterror  = '%s %s %s %s %s %s %s %s %*[^\n]';
vars            = {'lat_degmin';'latNS';'lon_degmin';'lonEW'};
vars_position   = 5:8;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'CNAV3050 GGA lat/lon',...
                    'lat_degmin = ship''s latitude as DDMM.FFF',...
                    '    where DD = degree, MM = minutes, FFF = minute decimal',...
                    'lat = ship''s latitude in degree fraction [°]',...
                    'latNS = latitude North or South ''N'' or ''S''',...
                    'lon_degmin = ship''s longitude as DDDMM.FFF',...
                    '    where DDD = degree, MM = minutes, FFF = minute decimal',...
                    'lon = ship''s longitude in degree fraction [°]',...
                    'lonEW = longitude East or West ''E'' or ''W''',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[gps_cnav] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/POSMV-V4-INGGA-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V4-INGGA-RAW';
strformat       = '%s %s %s %s %f %s %f %s %*[^\n]';
strformaterror  = '%s %s %s %s %s %s %s %s %*[^\n]';
vars            = {'lat_degmin';'latNS';'lon_degmin';'lonEW'};
vars_position   = 5:8;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'POSMV V4 lat/lon',...
                    'lat_degmin = ship''s latitude as DDMM.FFF',...
                    '    where DD = degree, MM = minutes, FFF = minute decimal',...
                    'lat = ship''s latitude in degree fraction [°]',...
                    'latNS = latitude North or South ''N'' or ''S''',...
                    'lon_degmin = ship''s longitude as DDDMM.FFF',...
                    '    where DDD = degree, MM = minutes, FFF = minute decimal',...
                    'lon = ship''s longitude in degree fraction [°]',...
                    'lonEW = longitude East or West ''E'' or ''W''',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[gps_posmv4] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/POSMV-V5-INGGA-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V5-INGGA-RAW';
strformat       = '%s %s %s %s %f %s %f %s %*[^\n]';
strformaterror  = '%s %s %s %s %s %s %s %s %*[^\n]';
vars            = {'lat_degmin';'latNS';'lon_degmin';'lonEW'};
vars_position   = 5:8;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'POSMV V5 lat/lon',...
                    'lat_degmin = ship''s latitude as DDMM.FFF',...
                    '    where DD = degree, MM = minutes, FFF = minute decimal',...
                    'lat = ship''s latitude in degree fraction [°]',...
                    'latNS = latitude North or South ''N'' or ''S''',...
                    'lon_degmin = ship''s longitude as DDDMM.FFF',...
                    '    where DDD = degree, MM = minutes, FFF = minute decimal',...
                    'lon = ship''s longitude in degree fraction [°]',...
                    'lonEW = longitude East or West ''E'' or ''W''',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[gps_posmv5] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);




% %%%% NAV/DECIMAL-DEGREES-LOOP-BOW %%%%
foldername      = 'NAV';
filename        = 'DECIMAL-DEGREES-LOOP-BOW';
strformat       = '%s %s %s %f %f';
strformaterror  = '%s %s %s %s %s';
vars            = {'lat';'lon'};
vars_position   = [4 5];
startfilenum    = 2;
startlinenum    = 16;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'DECIMAL-DEGREES-LOOP-BOW lat/lon',...
                    'lat = ship''s latitude [°]',...
                    'lon = ship''s longitude [°]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[gps_decdeg] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext,...
    'startfilenum',startfilenum,'startlinenum',startlinenum);



%%%% NAV/POSMV-V4-PASHR-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V4-PASHR-RAW';
strformat       = '%s %s %s %s %f %*[^\n]';
strformaterror  = '%s %s %s %s %s %*[^\n]';
vars            = {'heading'};
vars_position   = 5;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'POSMV 04 PASHR',...
                    'heading = ship''s heading [deg. CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[heading_posmv4] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/POSMV-V5-PASHR-RAW %%%%
foldername      = 'NAV';
filename        = 'POSMV-V5-PASHR-RAW';
strformat       = '%s %s %s %s %f %*[^\n]';
strformaterror  = '%s %s %s %s %s %*[^\n]';
vars            = {'heading'};
vars_position   = 5;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'POSMV 05 PASHR',...
                    'heading = ship''s heading [deg. CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[heading_posmv5] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/GYRO-01-HDT-RAW %%%%
foldername      = 'NAV';
filename        = 'GYRO-01-HDT-RAW';
strformat       = '%s %s %s %f %*[^\n]';
strformaterror  = '%s %s %s %s %*[^\n]';
vars            = {'heading'};
vars_position   = 4;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'GYRO 01',...
                    'heading = ship''s heading [deg. CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[heading_gyro1] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/GYRO-02-HDT-RAW %%%%
foldername      = 'NAV';
filename        = 'GYRO-02-HDT-RAW';
strformat       = '%s %s %s %f %*[^\n]';
strformaterror  = '%s %s %s %s %*[^\n]';
vars            = {'heading'};
vars_position   = 4;
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'GYRO 02',...
                    'heading = ship''s heading [deg. CW from N]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[heading_gyro2] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



%%%% NAV/BOW-SCS-ACQ-SENSORS %%%%
foldername      = 'SEAWATER';
filename        = 'BOW-SCS-ACQ-SENSORS';
strformat       = '%s %s %s %f %f %f %f %f %f %f %s %f';
strformaterror  = '%s %s %s %s %s %s %s %s %s %s %s %s';
vars            = {'T_intake';'T_TSG';'COND';'S';'flow';'trans';'fluor';'rain'};
vars_position   = [4:10 12];
readmetext      = strvcat('PISTON LEG 2 (2018) on R/V THOMPSON',...
                    'Data collected in August/September 2018',...
                    ' ',...
                    [foldername '/' filename],...
                    ' ',...
                    'thermosal from the Thermosalinograph (TSG) flowthrough at ~5m',...
                    'T_intake = intake temperature [°C]',...
                    'T_TSG = TSG temperature [°C]',...
                    'COND = TSG conductivity [siemens per m]',...
                    'S = TSG salinity [ ]',...
                    'flow = flow rate [l/min]',...
                    'trans = transmissometer [volts]',...
                    'fluor = fluorometer [counts]',...
                    'rain = rain fall [volts]',...
                    'time = matlab datenum',...
                    ' ',...
                    'processed by read_define_specs_and_process_scs_files.m',...
                    'See Sally Warner (swarner@coas.oregonstate.edu)',...
                    'if you have questions/concerns');
                    
[thermosal] = read_scs_file_v3(datadirup,localdirup,foldername,...
    filename,strformat,strformaterror,vars,vars_position,readmetext);



 



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vars = {'par';'rad';'wind_sonic_spd_10min';'wind_sonic_dir_10min';'wind_sonic';...
'wind_stbd';'wind_port';'met';'waves';'cog_sog_posmv5';'cog_sog_posmv4';...
'cog_sog_cnav';'sog_10min';'gps_cnav';'gps_posmv4';'gps_posmv5';'gps_decdeg';...
'heading_posmv4';'heading_posmv5';'heading_gyro1';'heading_gyro2';'thermosal'};

for ii = 1:length(vars)
    
    varname = vars{ii};
    var = eval(varname);
    varlabel = varname;
    for kk = 1:length(varlabel)
        if strcmp(varlabel(kk),'_')
            varlabel(kk) = ' ';
        end
    end
    
    fields = fieldnames(var);
    
    for jj = 1:length(fields)
        
        field = fields{jj};
        
        if ~strcmp(field,'time') & ~strcmp(field,'readme') & ...
                ~strcmp(field,'foldername') & ~strcmp(field,'filename') ...
                & ~iscell(var.(field))
        
            fieldlabel = field;
            for kk = 1:length(fieldlabel)
                if strcmp(fieldlabel(kk),'_')
                    fieldlabel(kk) = ' ';
                end
            end

            figure(32)
            clf
            set(gcf,'position',[64         595        1480         360])

            plot(var.time,var.(field),'.')
            datetick
            grid on
            set(gca,'tickdir','out')

            title({['piston18   |   ' varlabel '.' fieldlabel '   |   figure ' ...
                'made at ' datestr(now-9/24) ' GMT'];' '})


            export_fig([figdir 'plot_' var.foldername '_' var.filename ...
                '_' field '.png'],'-r200')
        end
    end
    
end





%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% backup processed (and raw) data using rsync
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% raw data directory on ship's server to wdmycloud
% disp('backing up raw scs data from //tgt-data/scs/ to wdmycloud')
% tic
% unix(['rsync -rvh --size-only ' datadir ' ' backupdirraw]);
% toc
% disp(' ')

%  local processed directory to wdmycloud
% disp('backing up processed scs data to wdmycloud')
% tic
% unix(['rsync -rvh --size-only ' localdirup ' ' backupdirproc]);
% toc
% disp(' ')

%  local processed directory to cruiseshare
disp('backing up processed scs data to cruiseshare')
tic
unix(['rsync -rvh --size-only ' localdirup ' ' backupdirproc_cruiseshare]);
toc
disp(' ')


%  figures to cruiseshare
disp('backing up figures to cruiseshare')
tic
unix(['rsync -rvh --size-only ' figdir ' ' backupdirfigs_cruiseshare]);
toc
disp(' ')












