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-ddThh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Time[hh:mm:ss.ss]\tGPS Long.[degrees E/W]\tGPS Lat.[degrees N/S]\tGPS Source');
fprintf(ODVfidESP,'Cruise\tStation\tType\tyyyy-mm-ddThh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Time[hh:mm:ss.ss]\tGPS Long.[degrees E/W]\tGPS Lat.[degrees N/S]\tGPS Source');
fprintf(ODVfidPAR,'Cruise\tStation\tType\tyyyy-mm-ddThh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Time[hh:mm:ss.ss]\tGPS Long.[degrees E/W]\tGPS Lat.[degrees N/S]\tGPS Source');
fprintf(ODVfidTension,'Cruise\tStation\tType\tyyyy-mm-ddThh:mm:ss.sss\tLongitude[degrees_east]\tLatitude[degrees_north]\tBot. Depth[m]\tGPS Time[hh:mm:ss.ss]\tGPS Long.[degrees E/W]\tGPS Lat.[degrees N/S]\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,'\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,'\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,'\r\n');

fprintf(ODVfidTension,'\tCable Tension[lbf]\tBus Voltage[VDC]');
fprintf(ODVfidTension,'\r\n');

cruise = 'WF-CANON-Sept2010';
BuoyStation = 1;
ESPStation = 1;
PARStation = 1;
tensionStation = 1;
depth = 'na';

lastDatenum = 0;

lat = '';
long = '';
GPSSource = '';

delimiters = [',' char(9)];

listing = dir('*.csv');

%for i=3:length(listing)
for i=3:8
    LVfid = fopen(listing(i).name, 'r');
    fprintf(1,'Processing file:  %d %s\r\n', i, listing(i).name);
    date = 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'
                [timeStamp, 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,'')
                    fprintf(ODVfidtension,'%s\t%d\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, station, type, strcat(date,'T',timeStamp), long, lat, depth, GPSSource);
                    fprintf(ODVfid,'%5.0f\t%5.1f\r\n', tension, busVoltage);
                    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'
                [timeStamp, 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,'')
                    fprintf(ODVfidBuoy,'%s\t%d\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, station, type, strcat(date,'T',timeStamp), 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\r\n', ...
                        roll, pitch, yaw, xRate,yRate, zRate, xAcc, yAcc, zAcc, xVeloc, yVeloc, zVeloc, long, lat, alt);
                    BuoyStation = BuoyStation + 1;
                end
            case 'iLink'
                [timeStamp, 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,'')
                    fprintf(ODVfidESP,'%s\t%d\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, station, type, strcat(date,'T',timeStamp), long, lat, depth, GPSSource);
                    fprintf(ODVfidESP,'%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\t%9.5f\r\n', ...
                        xAcc, yAcc, zAcc, xRate,yRate, zRate);
                    ESPstation = ESPstation + 1;
                end
            case 'PRR'
                [timeStamp, 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,'')
                    fprintf(ODVfidPAR,'%s\t%d\t%s\t%s\t%13.9f\t%13.9f\t%s\t%s\t',cruise, station, type, strcat(date,'T',timeStamp), 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\r\n', ...
                        ch1, ch2, ch3, ch4, ch5, ch6, ch7, ch8, PARGainErr, PARStale);
                    PARstation = PARstation + 1;
                end
        end
    end
    fclose(LVfid);
end
fclose(ODVfidBuoy);
fclose(ODVfidESP);
fclose(ODVfidPAR);
fclose(ODVfidTension);
toc



        
    
   