clear

tic

ODVfidBuoy = fopen('WF-CANONCruiseSept2010-Buoy.txt', 'w');
ODVfidESP = fopen('WF-CANONCruiseSept2010-ESP.txt', 'w');
ODVfidPAR = fopen('WF-CANONCruiseSept2010_PAR.txt', 'w');
ODVfidTension = fopen('WF-CANONCruiseSept2010_Tension.txt', 'w');

fprintf(ODVfidBuoy,'//This data has been parsed from the original ESP drifter files generated by Labview .VI\r\n');
fprintf(ODVfidESP,'//This data has been parsed from the original ESP drifter files generated by Labview .VI\r\n');
fprintf(ODVfidPAR,'//This data has been parsed from the original ESP drifter files generated by Labview .VI\r\n');
fprintf(ODVfidTension,'//This data has been parsed from the original ESP drifter files generated by Labview .VI\r\n');

fprintf(ODVfidBuoy,'Cruise\tStation\tType\tyyyy-mm-dd\thh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Source');
fprintf(ODVfidESP,'Cruise\tStation\tType\tyyyy-mm-dd\thh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Source');
fprintf(ODVfidPAR,'Cruise\tStation\tType\tyyyy-mm-dd\thh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Source');
fprintf(ODVfidTension,'Cruise\tStation\tType\tyyyy-mm-dd\thh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Source');

fprintf(ODVfidBuoy,'\tBuoy Roll Angle[deg]\tBuoy Pitch Angle[deg]\tBuoy Yaw Angle[deg True N]\tBuoy X Rate[deg/sec]\tBuoy Y Rate[deg/sec]\tBuoy Z Rate[deg/sec]');
fprintf(ODVfidBuoy,'\tBuoy X Accel.[g]\tBuoy Y Accel.[g]\tBuoy Z Accel.[g]\tBuoy North Vel.[m/s]\tBuoy East Vel.[m/s]\tBuoy West Vel.[m/s]\tBuoy xBow Long.[deg]\tBuoy xBow Lat.[deg]\tBuoy xBow Alt.[m]');
fprintf(ODVfidBuoy,'\tFile Name');
fprintf(ODVfidBuoy,'\r\n');

fprintf(ODVfidESP,'\tESP X Accel.[g]\tESP Y Accel.[t]\tESP Z Accel.[g]\tESP X Rate[rad/sec]\tESP Y Rate[rad/sec]\tESP Z Rate[rad/sec]');
fprintf(ODVfidESP,'\tFile Name');
fprintf(ODVfidESP,'\r\n');

fprintf(ODVfidPAR,'\tPAR CH1-412nm\tPAR CH1-443nm\tPAR CH1-490nm\tPAR CH1-510nm\tPAR CH1-555nm\tPAR CH1-665nm\tPAR CH1-PAR\tPAR CH1-Gnd.\tPAR Gain Err.\tPAR Stale');
fprintf(ODVfidPAR,'\tFile Name');
fprintf(ODVfidPAR,'\r\n');

fprintf(ODVfidTension,'\tCable Tension[lbf]\tBus Voltage[VDC]');
fprintf(ODVfidTension,'\tFile Name');
fprintf(ODVfidTension,'\r\n');

cruise = 'WF-CANON-Sept2010';
BuoyStation = 1;
ESPStation = 1;
PARStation = 1;
tensionStation = 1;
depth = 'na';

lat = '';
long = '';
GPSSource = '';

delimiters = [',' char(9)];

listing = dir('*.csv');

for i=3:length(listing)
    LVfid = fopen(listing(i).name, 'r');
    fprintf(1,'Processing file:  %d %s\r\n', i, listing(i).name);
    lastTimeStampTension = 0; 
    lastTimeStampBuoy = 0; 
    lastTimeStampESP = 0; 
    lastTimeStampPAR = 0; 
    dateString = datestr(listing(i).datenum, 29);
    
    lineNum = 0;
    while ~feof(LVfid)
        line = fgetl(LVfid);
        lineNum = lineNum + 1;
        [token, r] = strtok(line, delimiters);
        
        switch token
            case 'Tens/Bus'
                [timeStampTension, r] = strtok(r, delimiters);    %Labview Time stamp
                [t, r] = strtok(r, delimiters);
                tension = str2num(t);
                [t, r] = strtok(r, delimiters);
                busVoltage = str2num(t);
                
                type = 'Load Cell';
                if ~strcmp(lat,'')
%                     if datenum(timeStampTension) < lastTimeStampTension
%                         dateString = datestr(listing(i).datenum + 1, 29);
%                         fprintf(1,'Rollover\r\n');
%                     end
%                     lastTimeStampTension = datenum(timeStampTension);
                    fprintf(ODVfidTension,'%s\t%d\t%s\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, tensionStation, type, dateString, timeStampTension, long, lat, depth, GPSSource);
                    fprintf(ODVfidTension,'%5.0f\t%5.1f%s\r\n', tension, busVoltage, listing(i).name);
                    tensionStation = tensionStation + 1;
                end
            case 'xBow GPS'
                if length(line) > 82
                    [timeStamp, r] = strtok(r, delimiters); % Labview time stamp
                    [GGAHeader, r] = strtok(r, delimiters);
                    [temp, r] = strtok(r, delimiters);
                    GGATime = strcat(temp(1:2),':',temp(3:4),':',temp(5:length(temp)));
                    [latTemp, r] = strtok(r, delimiters);
                    if length(latTemp) > 5
                        lat = str2num(latTemp(1:2)) + str2num(latTemp(3:length(latTemp)))/60.0;
                        [NS, r] = strtok(r, delimiters);
                        [longTemp, r] = strtok(r, delimiters);
                        if length(longTemp) > 5
                            long = -str2num(longTemp(1:3)) - str2num(longTemp(4:length(longTemp)))/60.0;
                            [EW, r] = strtok(r, delimiters);
                            GPSSource = 'xBow GPS';
                        else
                            fprintf(1,'Skipping line %d in file %s: %s\r\n', lineNum, listing(i).name, line);
                        end
                    else
                        fprintf(1,'Skipping line %d in file %s: %s\r\n', lineNum, listing(i).name, line);
                    end
                else
                    fprintf(1,'Skipping line %d in file %s: %s\r\n', lineNum, listing(i).name, line);
                end
            case 'xBow'
                [timeStampBuoy, r] = strtok(r, delimiters); % Labview time stamp
                [temp, r] = strtok(r, delimiters);
                roll = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                pitch = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yaw = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                xRate = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yRate = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                zRate = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                xAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                zAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                xVeloc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yVeloc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                zVeloc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                long = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                lat = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                alt = str2num(temp);
                
                GPSSource = 'xBow';
                type = 'Buoy xBow';
                if ~strcmp(lat,'')
%                     if datenum(timeStampBuoy) < lastTimeStampBuoy
%                         dateString = datestr(listing(i).datenum + 1, 29);
%                         fprintf(1,'Rollover\r\n');
%                     end
%                     lastTimeStampBuoy = datenum(timeStampBuoy);
                    fprintf(ODVfidBuoy,'%s\t%d\t%s\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, BuoyStation, type, dateString ,timeStampBuoy, long, lat, depth, GPSSource);
                    fprintf(ODVfidBuoy,'%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%13.9f\t%13.9f\t%9.5f\t%s\r\n', ...
                        roll, pitch, yaw, xRate,yRate, zRate, xAcc, yAcc, zAcc, xVeloc, yVeloc, zVeloc, long, lat, alt, listing(i).name);
                    BuoyStation = BuoyStation + 1;
                end
            case 'iLink'
                [timeStampESP, r] = strtok(r, delimiters); % Labview time stamp
                [temp, r] = strtok(r, delimiters);
                xAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                zAcc = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                xRate = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                yRate = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                zRate = str2num(temp);
                
                type = 'ESP iLink';
                if ~strcmp(lat,'')
%                     if datenum(timeStampESP) < lastTimeStampESP
%                         dateString = datestr(listing(i).datenum + 1, 29);
%                         fprintf(1,'Rollover\r\n');
%                     end
%                     lastTimeStampESP = datenum(timeStampESP);
                    fprintf(ODVfidESP,'%s\t%d\t%s\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, ESPStation, type, dateString, timeStampESP, long, lat, depth, GPSSource);
                    fprintf(ODVfidESP,'%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%s\r\n', ...
                        xAcc, yAcc, zAcc, xRate,yRate, zRate, listing(i).name);
                    ESPStation = ESPStation + 1;
                end
            case 'PRR'
                [timeStampPAR, r] = strtok(r, delimiters); % Labview time stamp
                [temp, r] = strtok(r, delimiters);
                ch1 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch2 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch3 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch4 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch5 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch6 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch7 = str2num(temp);
                [temp, r] = strtok(r, delimiters);
                ch8 = str2num(temp);
                [PARGainErr, r] = strtok(r, delimiters);
                [PARStale, r] = strtok(r, delimiters);
                
                type = 'Biosph. PRR610';
                if ~strcmp(lat,'')
%                     if datenum(timeStampPAR) < lastTimeStampPAR
%                         dateString = datestr(listing(i).datenum + 1, 29);
%                         fprintf(1,'Rollover\r\n');
%                     end
%                     lastTimeStampPAR = datenum(timeStampPAR);
                    fprintf(ODVfidPAR,'%s\t%d\t%s\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, PARStation, type, dateString ,timeStampPAR, long, lat, depth, GPSSource);
                    fprintf(ODVfidPAR,'%8.5f\t%8.5f\t%8.5f\t%8.5f\t%8.5f\t%8.5f\t%8.5f\t%8.5f\t%s\t%s\t%s\r\n', ...
                        ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, PARGainErr, PARStale, listing(i).name);
                    PARStation = PARStation + 1;
                end
        end
    end
    fclose(LVfid);
end
fclose(ODVfidBuoy);
fclose(ODVfidESP);
fclose(ODVfidPAR);
fclose(ODVfidTension);
toc



        
    
   