Newer
Older
function [error] = readTsgDataNetCDF( hMainFig, filename)
% Function to read the TSG data. Should be a NetCDF file
% GOSUD data format TSG V1.6
%
% Input
% -----
% hMainFig ............ Handle to the main user interface
% filename ............ Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%
% The data are store using setappdata - Variable name : 'tsg_data'
%
% DAYD, DAYD_WS and DAYD_EXT in tsg structure use Matlab datenum, they
% should be converted from julian days using 1950 reference in NetCDF file
%
%% Check Netcdf library version
% -----------------------------
switch ( version('-release') )
case { '11', '12', '13', '14', '2006a', '2006b', '2007a', '2007b', '2008a' }
read_netcdf_toolbox;
otherwise
read_netcdf_native;

jacques.grelet_ird.fr
committed
end
% Perform somme automatic tests
% -----------------------------
automaticQC( hMainFig )
%% Nested functions
% -----------------
% use Charles Dunham toolbox
% --------------------------
function read_netcdf_toolbox
% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
% Display read file info on console
% ---------------------------------
fprintf('\nREAD_NETCDF_FILE\n'); tic;
% Open netCDF file
% ----------------
nc = netcdf(filename,'read');
if isempty(nc)
msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
warndlg( msg_error, 'NetCDF error dialog');
sprintf('...cannot locate %s\n', filename);
error = -1;
return;
end
% Display more info about read file on console
% --------------------------------------------
fprintf('...reading %s : ', filename);
% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
fileparts(filename);
tsg.file.type = 'NETCDF';
% get global attributes: meta data
% --------------------------------
global_att = att(nc);
for i=1:length(global_att)
% extract name and value from netcdf globals attributes
% -----------------------------------------------------
attribute = name(global_att{i});
value = global_att{i}(:);
% assign globals attributes in base workspace
% -------------------------------------------
assignin('base', attribute, value);
% populate tsg structure with globals attributes
% ----------------------------------------------
tsg.(attribute) = value;
end
% get variables describing TSG installation
% -----------------------------------------
variables = var(nc);
for i=1:length(variables)
% extract name and value from netcdf variables
% --------------------------------------------
variable = name(variables{i});
%value = variables{i}(:);
nv = nc{i};
% use autonan mode, remplace fillValue with NaN
% ---------------------------------------------
nv = autonan(nv, true);
% finally replace fillvalue with NaN when only fillvalue exist
% ------------------------------------------------------------
% if ~isempty(fillval(nc{i}))
% value(value == fillval(nc{i})) = NaN;
% end
% populate tsg structure with netcdf variables
% --------------------------------------------
tsg.(variable) = nv(:);
% transforme julian days variables to Matlab datenum
% --------------------------------------------------
if strmatch('DAYD', variable)
tsg.(variable) = julianToDatenum(tsg.(variable));
end

jacques.grelet_ird.fr
committed
% assign netcdf variables in base workspace
% -----------------------------------------
assignin('base', variable, nv(:));
end % end of variables loop
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
% Keep somme information for the log file
% ---------------------------------------
tsg.report.tsgfile = filename;
% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );
% Close the file
% --------------
close(nc);
% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%5.2f sec).\n\n',t);
% Everything OK
% -------------
error = 1;
end % end of read_netcdf_toolbox
% use native toolbox +netcdf since R2008b
% ---------------------------------------
function read_netcdf_native
% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
% Display read file info on console
% ---------------------------------
fprintf('\nREAD_NETCDF_FILE\n'); tic;
% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
fileparts(filename);
tsg.file.type = 'NETCDF';
% Open netCDF file
% ----------------
nc = netcdf_native(filename);
% loop over all variables and get associated value in tsg structure
% -----------------------------------------------------------------
for key = keys(nc.VARIABLES)
var = char(key);
tsg.(var) = nc.VARIABLES.(var).data__;
% when variable is julian days from 1950 reference, transform to
% Matlab datenum
% --------------------------------------------------------------

jacques.grelet_ird.fr
committed
if strmatch('DAYD', var)
tsg.(var) = julianToDatenum(tsg.(var));
end
% assign variable in base workspace
% -------------------------------------------
assignin('base', var , tsg.(var));
% loop over all global attributes and get associated value in
% tsg structure
% -----------------------------------------------------------------
for key = keys(nc.ATTRIBUTES)
att = char(key);
tsg.(att) = nc.ATTRIBUTES.(att).data__;
% assign globals attributes in base workspace
% -------------------------------------------
assignin('base', att , tsg.(att));
end
% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );
% Close the file
% --------------
%close(nc);
% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%5.2f sec).\n\n',t);
% Everything OK
% -------------
error = 1;
end % end of read_netcdf_native
end % end of readTsgDataNetCDF