% A script designed to be run in batch mode to process Pacific Gyre's
% .csv drifter data files into .mat files
% Modified from original script to work with data downloaded via the
% new API (http://api.pacificgyre.com/pgapi), that includes salinity.
%
% Run like:
%  > matlab -nojvm -nosplash -r read_SFOScsv_2012 > matlab.out 2>&1
%
% ELD  12/12/11
% 8/10/12 - Updated to process 2012 dataquit
%

clear

ids = { ...
        'SFOS-3S-0001'; ...
        'SFOS-3S-0002'; ...
        'SFOS-MS-0001'; ...
        'SFOS-MS-0004'; ...
        'SFOS-MS-0005'; ...
        'SFOS-MS-0006'
      }

for n = 1:6
    id = char(ids(n))
    fid=fopen( [ id '.csv' ] );
    % read 1 line of header info first
    header=textscan(fid,'%s',1,'delimiter','\n');
    %char(header{1})
    
    % read the data
    %c = textscan( fid, '%f %s %s %*[^\n]', 'delimiter', ',');  % testing ... ignores rest of line
    c = textscan( fid, '%f %s %s %f %f %s %d %f %f %f %f %d %s %f %f %f %f %f %f %f %f %f', 'delimiter', ',');
    fclose(fid);
    
    % Pick out the interesting cell elements, and flip them because original
    % file had most recent first.
    timestr = flipud( c{3} );
    time = datenum(timestr);
    
    battery = flipud( c{8} );

    latitude = flipud( c{4} );
    latitude(find(latitude==0)) = NaN;
    longitude = flipud( c{5} );
    longitude(find(longitude==0)) = NaN;
    % now clean up quality
    submerged = flipud( c{6} );
    GPSquality = flipud( c{7} );
    latitude(find(GPSquality~=3)) = NaN;
    longitude(find(GPSquality~=3)) = NaN;
   

    temperature0m = flipud( c{9} );
    salinity0m = flipud( c{18} );
    temperature7m = flipud( c{19} );
    salinity7m = flipud( c{20} );
    temperature15m = flipud( c{21} );
    salinity15m = flipud( c{22} );
   
    if 0   %plots for testing
        plot (temperature0m)
        hold on 
        plot (temperature7m,'r')
        plot (temperature15m,'y')
        title('Temperature')
        hold off
        pause
        
        plot (salinity0m)
        hold on 
        plot (salinity7m,'r')
        plot (salinity15m,'y')
        title('Salinity')
        hold off
        pause
    end
    
    clear fid ans;
    save(id)
    clearvars -except ids
end

quit
