function scsm = read_samos_nc_WHOTS_2014(ddd,path_working_ddd,fnames)
%{
Reads daily SAMOS ship data for Hi'ialakai - 1-min avg values
There may be multiple files for one day if a gap occurs.

Inputs are: ddd: day-of-year variable
            path: string path variable to raw data folder containing
                  SAMOS nc files.
            fnames: Nx1 struct of file names, e.g.'WTEY_20140721v10001.nc',
                    'WTEY_20140721v10002.nc', etc.

Returns 1440x15 array scs, with NaN for data gaps.

As of May 30, 2015, Hi'ialakai SAMOS nc variable names are:
    time: minutes since 1-1-1980 00:00 UTC, time at end of 60 min period
    lat: degrees (+N), Applanix POSMV4
    lon: degrees (-W/+E), Applanix POSMV4
    PL_HD: platform heading, degrees, Applanix POSMV4
    PL_HD2: platform heading, degrees, SG Brown Gyrocompass (starboard)
    PL_HD3: platform heading, degrees, SG Brown Gyrocompass (port)
    PL_CRS: platform course, degrees (clockwise towards true north)
    DIR: earth relative wind direction, RM Young 05103
    PL_WDIR: platform relative wind direction, degrees (clockwise from bow)
    PL_SPD: platform speed over ground, m/s
    SPD: earth relative wind speed, m/s, RM Young 05103
    PL_WSPD: platform relative wind speed, m/s, RM Young 05103
    P: atmospheric pressure, mb
    T: air temperature, RM Young 41372 VC
    RH: relative humidity, RM Young 41372 VC
    TS: sea temperature, C, SBE38
    TS2: sea temperature, C, Seabird SBE21 thermosalinograph
	SSPS: salinity, ppth, Seabird SBE21 thermosalinograph
    CNDC: conductivity, Si/m, Seabird SBE21 thermosalinograph
    date: YYYYMMDD UTC
    time_of_day: hhmmss UTC, start of averaging interval
    flag: 19xN array, QC flags for all above, except date and
         time_fo_day, 'Z' flag means good data.
    
%}

scsm = zeros(1440,15)*NaN;
delta = double(1.0/1440);
last = ddd + 1439*delta;
jd_ref = [ddd:delta:last]; % ref 1-min timestamp
scsm(:,1) = jd_ref';

for ii = 1:length(fnames) % read each file if more than one...
    fin = fullfile(path_working_ddd,fnames(ii).name);
    % use date & time_of_day for timestamp
    % 'time' variable does not appear accurate in the test file...
    dateN = double(ncread(fin,'date'));         % composite date number
    timeN = double(ncread(fin,'time_of_day'));  % composite time number
    YY = fix(dateN/10000);
    MM = fix(mod(dateN,10000)/100);
    DD = mod(dateN,100);
    hh = fix(timeN/10000);
    mm = fix(mod(timeN,10000)/100);
    ss = mod(timeN,100);
    
    % timestamp should be on exact 1-min interval
    jdscs = md2yd_vn(YY,MM,DD);
    jdscs = jdscs + hh/24 + mm/1440 + ss/86400;

    % all speeds are m/s
    latm = double(ncread(fin,'lat'));
    lonm = double(ncread(fin,'lon'));
    lrgm = double(ncread(fin,'PL_HD'));     % using POSmv heading
    cogm = double(ncread(fin,'PL_CRS'));
    sogm = double(ncread(fin,'PL_SPD'));
    imrh = double(ncread(fin,'RH'));
    imta = double(ncread(fin,'T'));
    imum = double(ncread(fin,'SPD'));
    imdm = double(ncread(fin,'DIR'));
    impress = double(ncread(fin,'P'));
    imrspd = double(ncread(fin,'PL_WSPD'));
    imrdir = double(ncread(fin,'PL_WDIR'));
    tsgm = double(ncread(fin,'TS'));       % using thermosalinograph temp
    tssm = NaN(size(tsgm));                % no sal in 2014 nc files
    imrdir(imrdir>180) = imrdir(imrdir>180)-360;
    
    % copy data into scs array for this file
    for jj = 1:length(jdscs)
        diff = jd_ref - jdscs(jj);
        [val,idx] = min(abs(diff));
        if val<double(2.0/86400) && length(idx)==1 % 2 sec tolerance
            scsm(idx,2) = latm(jj);
            scsm(idx,3) = lonm(jj);
            scsm(idx,4) = lrgm(jj);
            scsm(idx,5) = cogm(jj);
            scsm(idx,6) = sogm(jj);
            scsm(idx,7) = imrh(jj);
            scsm(idx,8) = imta(jj);
            scsm(idx,9) = imum(jj);
            scsm(idx,10) = imdm(jj);
            scsm(idx,11) = impress(jj);
            scsm(idx,12) = imrspd(jj);
            scsm(idx,13) = imrdir(jj);
            scsm(idx,14) = tsgm(jj);
            scsm(idx,15) = tssm(jj);
        end;
    end;
end;


