Newer
Older
gael.alory_legos.obs-mip.fr
committed
function [error] = readTsgDataAstrolabe( hMainFig, filename )
% readTsgDataAstrolabe( hMainFig, filename )
% Function to read TSG data from Astrolabe ship.
%
% Input
% -----
% hMainFig ........... Handle to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%
% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
% Get the fieldnames of tsg structure
% -----------------------------------
tsgNames = fieldnames(tsg);
nbFieldNames = length( tsgNames );
gael.alory_legos.obs-mip.fr
committed
% Get NO_CONTROL, GOOD, PROBABLY_BAD codes
% ----------------------------------------

jacques.grelet_ird.fr
committed
%NO_CONTROL = int8(tsg.qc.hash.NO_CONTROL.code);
GOOD = int8(tsg.qc.hash.GOOD.code);
PROBABLY_BAD = int8(tsg.qc.hash.PROBABLY_BAD.code);
BAD = int8(tsg.qc.hash.BAD.code);
% Display read file info on console
% ---------------------------------
fprintf('\nREAD_ASCII_FILE\n'); tic;
% Open the file
% -------------
fid = fopen( filename, 'rt' );
% Check file
% -----------
if fid == -1
gael.alory_legos.obs-mip.fr
committed
msg_error = ['TSG file : Open file error : ' filename];
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
warndlg( msg_error, 'ASCII error dialog');
sprintf('...cannot locate %s\n', filename);
error = -1;
return;
end
% Display more info about read file on console
% --------------------------------------------
fprintf('...reading %s : ', filename);
% Read the header till the header line has been read
% --------------------------------------------------
OK = 0;
while ~OK
% Read every line
% ---------------
line = fgetl( fid );
c = textscan( line, '%s');
switch char( c{1}(1) )
case '%HEADER'
% Read the header then quit the while loop
% ----------------------------------------
header = c{1}(2:end);
nHeader = length( header );
OK = 1;
otherwise
% Get the parameter Name (Delete '%')
% ----------------------------------
Para = char( strtok(c{1}(1), '%') );
% Read the parameter value
% ------------------------
ind = strmatch( Para, tsgNames);
if ~isempty( ind )
% patch added for composed name
% -----------------------------
a = c{1}(2:end)';
str=[];
for i=a
str = sprintf('%s %s', str, char(i));
end
% copy to tsg struct & remove leading and trailing white space
% ------------------------------------------------------------
tsg.(Para) = strtrim(str);
end
end
end
gael.alory_legos.obs-mip.fr
committed
% Build the format depending on the header parameters
% 1 - Decimate the HEADER - The 5th first parameters are always
% %HEADER YEAR MNTH DAYX hh mm ss
% 2 - The 4 Date and time parametes are read in %d
% -------------------------------------------------------------
gael.alory_legos.obs-mip.fr
committed
format = '%d %d %d %d';
for i = 5 : nHeader
format = [format ' %f'];
end
% Read the data in a cell
% -----------------------
cellData = textscan( fid, format );
% Convert cell to a structure
% ---------------------------
s = cell2struct(cellData, header, 2);
clear cellData
% Date (y m d h m s) in the first 4 elements in data
% --------------------------------------------------
gael.alory_legos.obs-mip.fr
committed
% ddmmyy = char(s.(char(header(1)))) ;
% dd = str2num(ddmmyy(:,1:2));
% mm = str2num(ddmmyy(:,3:4));
ddmmyy = double(s.(char(header(1)))) ;
nn = length(ddmmyy);
dd = repmat(NaN, [nn 1]);
mm = repmat(NaN, [nn 1]);
yy = repmat(NaN, [nn 1]);
for it = 1:nn
ddmmyy_str = sprintf('%06d', ddmmyy(it));

jacques.grelet_ird.fr
committed
dd(it) = str2num(ddmmyy_str(:,1:2)); %#ok<ST2NM>
mm(it) = str2num(ddmmyy_str(:,3:4)); %#ok<ST2NM>
yy(it) = 2000 + str2num(ddmmyy_str(:,5:6)); %#ok<ST2NM>
gael.alory_legos.obs-mip.fr
committed
end
gael.alory_legos.obs-mip.fr
committed
% Warning: for post-2000 files only!
% ----------------------------------
gael.alory_legos.obs-mip.fr
committed
%yy = 2000 + str2num(ddmmyy(:,5:6));
gael.alory_legos.obs-mip.fr
committed
hh = double( s.(char(header(2))) );
mi = double( s.(char(header(3))) );
ss = double( s.(char(header(4))) );
tsg.report.tsgfile = filename;
tsg.DAYD = datenum(yy, mm, dd, hh, mi, ss);
tsg.DATE = datestr( tsg.DAYD, 'yyyymmddHHMMSS' );
nHeader = length( header );
for i = 5 : nHeader
% QC should be converted in int8
% ----------------------------------
if ~isempty( strfind(char(header(i)), '_QC'))
tsg.(char(header(i))) = int8(s.(char(header(i))));
else
tsg.(char(header(i))) = s.(char(header(i)));
end
end
gael.alory_legos.obs-mip.fr
committed
% Fill TSG attributes
% -------------------

jacques.grelet_ird.fr
committed
tsg.PLATFORM_NAME = 'astrolabe';
tsg.SHIP_CALL_SIGN = 'FHZI';
tsg.PROJECT_NAME = 'SURVOSTRAL';
tsg.PI_NAME = 'Rosemary Morrow';
tsg.DATA_ACQUISITION = 'IPEV';
tsg.PROCESSING_CENTRE = 'SURVOSTRAL';
tsg.PROCESSING_STATES = '2C+';
tsg.SAMPLING_PERIOD = '60';
gael.alory_legos.obs-mip.fr
committed
% Missing value to be replaced by NaNs
% ------------------------------------

jacques.grelet_ird.fr
committed
miss = -90;
gael.alory_legos.obs-mip.fr
committed
% Fill TSG variables
gael.alory_legos.obs-mip.fr
committed
% by default, SSPS before correction flagged PROBABLY_BAD
% SSPS_ADJUSTED and SSTP flagged GOOD
% -------------------------------------------------------
gael.alory_legos.obs-mip.fr
committed
if ~isempty(tsg.SSTP)

jacques.grelet_ird.fr
committed
tsg.SSTP(tsg.SSTP < miss) = NaN;
tsg.SSTP_QC = castByteQC( GOOD, tsg.DAYD );
tsg.SSTP_QC(isnan(tsg.SSTP)) = BAD;
gael.alory_legos.obs-mip.fr
committed
end

jacques.grelet_ird.fr
committed
tsg.SSPS_QC = castByteQC(PROBABLY_BAD, tsg.DAYD);
tsg.SSPS_ADJUSTED_QC = castByteQC(GOOD, tsg.DAYD);
tsg.SSPS(tsg.SSPS < miss) = NaN;
tsg.SSJT(tsg.SSJT < miss) = NaN;
tsg.SSPS_ADJUSTED(tsg.SSPS_ADJUSTED < miss) = NaN;
tsg.SSPS_QC(isnan(tsg.SSPS)) = BAD;
tsg.SSPS_ADJUSTED_QC(isnan(tsg.SSPS)) = BAD;
% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
fileparts(filename);
% Perform somme automatic tests
% -----------------------------
automaticQC( hMainFig );
tsg.file.type = 'ASCII';
% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );
% Close the file
% --------------
fclose( fid );
% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%6.2f sec).\n\n',t);
% Everything OK
% -------------
error = 1;
end