% nbp1201_read_jgofs.m
%
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@   INPUT  @@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% JGOFS format data files
% http://science.whoi.edu/users/mcgillic/nbp1201/process/1201JGOF.tar
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% Read original data files under format control;
% save as mat files.

clear;

t0 = clock;
%operation
%etime(clock,t0)

%******************
% C O N S T A N T S
%******************
% Option: Save data as mat file:
%optionSave='yes';
optionSave='no';

path_part_1='/data2/antarctic_data2/nbp1201/underway/process_ship/';
path_part_2='JGOF/';
%time_tag_chars=1:19;  %12+001:00:00:00.086
day_list=[361:365 1:36];
%
% Path, names.
path_name=[path_part_1 path_part_2];
fname_part=['jg']; % e.g. jg001.dat
path_name_out=[path_name 'process/'];
fname_ext='dat';
fname_out_part=fname_part;
fname_out_ext='mat';
npar=26;
%
% LOOP - for each day.
data=ones(100000,7);
%----------------------
%M_not_found=[];
%bad_lines_skipped='';
%----------------------
for day=1%day_list
    %for day=12%day_list
    if day<10
        day_str=['00' num2str(day)];
    elseif day>=10 &  day<100
        day_str=['0' num2str(day)];
    else
        day_str=[num2str(day)];
    end
    %*******************************************************************
    %******************    Open file for reading   *********************
    %*******************************************************************
    pfid = fopen([path_name fname_part day_str '.' fname_ext]);
    if pfid<0
        disp(['File not found: ' fname '.' fname_ext ]);
        return;
    end
    count_line=0;
    data=[];
    while 1
        count_line=count_line+1;
        line = fgetl(pfid);
        if ~isstr(line), break, end
        % C O R R E C T I O N  - DATE,TIME.
        line_cor1 = strrep(line,':',char(9));
        line_cor2 = strrep(line_cor1,'/',char(9));
        line_cor2 = strrep(line_cor2,['NA' char(9)],['NAN' char(9)]);
        % Read line under format control.
        line_data = (sscanf(line_cor2,'%f'))';
        % CHECK for vectors length. If 'good' - MAKE MATRIX, if 'bad' display a message:
        if length(line_data)==npar
            data = [data;line_data];
        else
            disp(['Character (not number) is found in a line ' num2str(count_line) ':']);
            disp(line);
            disp('This part of line is read sucessfully:');
            disp(line_data);
            bad_line=count_line
            return;
        end
        clear line line_data line_cor1 line_cor2;
    end  % For while 1
    % Close file.
    fclose(pfid);
    %*******************
    % Save data as a matfile.
    %*******************
    if strcmp(optionSave,'yes')
        fname_out=[fname_out_part day_str];
        p=[fname_out '= data;'];
        eval(p);
        p=['save ' fname_out ' ' fname_out ';'];
        eval(p);
        p=['! mv ' fname_out '.' fname_out_ext ' ' path_name_out ';'];
        eval(p);
        disp( strvcat(['File has been SAVED, filename: ' fname_out '.mat, size ' num2str(size(data))],...
            ['Location: ' path_name_out]) );
    else
        %   disp([monName ' ' num2str(dayNum) ', filename: ' fname]);
        disp( ['OUTPUT File NOT SAVED, OUTPUT matrix: data, size ' num2str(size(data))] );
    end
    clear data;
end % for day=day_list
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
%@@@@@@@@@  OUTPUT matrix columns   @@@@@@@@@
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
% (1) PCOD GLL (day #1 size: 86400x9):
colYear_M=1;
colDay_M=2;
colHour_M=3;
colMin_M=4;
colSec_M=5;
%colLat_M=6;  % degrees
%colLon_M=7;  % degrees
%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
estimated_time_minutes=etime(clock,t0)/60
%if ~isempty(M_not_found),...
%    disp('!!! Check the variable: M_not_found - it is not empty'); end

