
% 
% Loads data from .cnv CTD files and writes them into .mat files
% 


clear all;
clc;

pathData = '/media/NeNe/DatiVari/AtalanteDisk/OUTPACE_CTD_Classik_DATA/final_cnv/';


%% Get the file names
tmp = dir([pathData filesep '*.cnv']);
nFile = length(tmp);
for iFile = 1 : nFile
    files{iFile} = tmp(iFile).name;
end
clear tmp;


%% Read each file
h = waitbar(0,'Fetching data from the .cnv files');
for iFile = 1 : nFile
    % Open the file
    fid = fopen([pathData files{iFile}]);
    
    % Run through the header
    % - intializations
    fline = '';
    iCnt = 1;
    % let's go
    while ~strcmp(fline,'*END*')
        
        fline = fgetl(fid);     % read a line
        
        ind1 = strfind(fline,'=');
        
        % - infos
        if strcmp(fline(1),'*')
            switch fline(1:min(10,length(fline)))
                case '* NMEA Lat'
                    lat = str2double([fline(ind1+2:ind1+3) fline(ind1+5:ind1+9)]);
                    lat = lonlat_NMEA_to_dec(lat);  % NMEA to dec
                    if strcmp(fline(ind1+11),'S')
                        lat = -lat;
                    end
                case '* NMEA Lon'
                    lon = str2double([fline(ind1+2:ind1+4) fline(ind1+6:ind1+10)]);
                    lon = lonlat_NMEA_to_dec(lon);  % NMEA to dec
                    if strcmp(fline(ind1+12),'W')
                        lon = 360 - lon;
                    end
                case '* NMEA UTC'
                    date = datenum(fline(ind1+2:end),'mmm dd yyyy HH:MM:SS');
                case '** Station'
                    stName = fline(13:end);
                case '** Depth: '
                    depth = str2double(fline(11:end));
            end
            
        % - variables
        elseif strcmp(fline(1),'#')
            switch fline(1:min(6,length(fline)))
                case '# nqua'
                    nVar = str2double(fline(ind1+2:end));
                case '# nval'
                    nData = str2double(fline(ind1+2:end));
                case '# name'
                    ind2 = strfind(fline(ind1+1:end),':');
                    varName{iCnt} = fline(ind1+ind2+2:end);
                    iCnt = iCnt+1;
            end
        end
    end
    
    % Read the data, now
    data = fscanf(fid,'%f');
    data = reshape(data,[nVar nData])';
    
    % Put all nicely into a structure
    station(iFile).lon  = lon;
    station(iFile).lat  = lat;
    station(iFile).date = date;
    if exist('stName','var')
        station(iFile).stName = stName;
    else
        station(iFile).stName = num2str(files{iFile}(end-6:end-4)); %%%%%% OUTPACE
    end
    if exist('depth','var')
        station(iFile).depth  = depth;
    end
    for iVar = 1 : nVar
        station(iFile).var(iVar).name  = varName{iVar};
        station(iFile).var(iVar).value = data(:,iVar);
        station(iFile).var(iVar).value(station(iFile).var(iVar).value == -9.99e-29) = NaN;
    end
    
    % Close the file
    fclose(fid);
    
    % Update the waitbar
    waitbar(iFile/nFile);
end
clear lon lat date ind1 ind2 vars data;
close(h);
pause(0.5);


%% Sort wrt to date
dates = [];
for iFile = 1 : nFile
    dates = [dates; station(iFile).date];
end
[~, ind] = sort(dates);
station = station(ind);
clear ind dates


%% Save an easy-to-read .mat file for each station
for iFile = 1 : nFile
    
    % Date
    data(1).name  = 'Date (Matlab numeric format)';
    data(1).value = station(iFile).date;
    
    % Lon/lat
    data(2).name  = 'Lon';
    data(2).value = station(iFile).lon;
    data(3).name  = 'Lat';
    data(3).value = station(iFile).lat;
    
    % Bottom depth
    if exist('depth','var')
        data(4).name  = 'Bottom depth';
        data(4).value = station(iFile).depth;
        iVarOff = 4;
    else
        iVarOff = 3;
    end
    
    % All the hydrological variables
    nVar = length(station(iFile).var);
    for iVar = 1 : nVar
        data(iVar+iVarOff).name  = station(iFile).var(iVar).name;
        data(iVar+iVarOff).value = station(iFile).var(iVar).value;
        % - change oxygen concentrations from ml/l to mumol/l
        if ~isempty(strfind(data(iVar+iVarOff).name,'Oxygen')) && ...
           ~isempty(strfind(data(iVar+iVarOff).name,'[ml/l]'))
            data(iVar+iVarOff).value = data(iVar+iVarOff).value*1000/22.3916;
        end
        % - fetch the indices for pres, temp and psal (needed later for
        %   sound velocity computation)
        if ~isempty(strfind(data(iVar+iVarOff).name,'Pressure'))
            indPres = iVar+iVarOff;
        elseif ~isempty(strfind(data(iVar+iVarOff).name,'Temperature '))    % space needed not to fetch Temperature, 2
            indTemp = iVar+iVarOff;
        elseif ~isempty(strfind(data(iVar+iVarOff).name,'Salinity, Practical '))    % space needed as above
            indPsal = iVar+iVarOff;
        end
    end
    
    % Compute and add sound velocity
    lat  = data(3).value;
    pres = data(indPres).value;
    temp = data(indTemp).value;
    psal = data(indPsal).value;
    iSV = length(data)+1; 
    data(iSV).name = 'Sound Velocity [m/s]';
    data(iSV).value = soundspeed_from_temppsal(temp,psal,pres,lat,1);
    
    % Save one .mat file per station
    save([pathData filesep files{iFile}(1:end-4)],'data');
    clear data
end


%% Save an easy-to-read .mat file for track
file_name = [pathData filesep 'track.mat'];
date = zeros(nFile,1);
lon  = zeros(nFile,1);
lat  = zeros(nFile,1);
for iFile = 1 : nFile
    date(iFile) = station(iFile).date;
    lon(iFile)  = station(iFile).lon;
    lat(iFile)  = station(iFile).lat;
end
save(file_name,'date','lon','lat');

