Newer
Older
function [error] = readTsgDataNetCDF( hMainFig, filename)
% Function to read the TSG data. Should be a NetCDF file
% GOSUD data format TSG V1.4
%
% Input
% -----
% hMainFig ............ Handel 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'
%
% $Id$
%% 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 )
32
33
34
35
36
37
38
39
40
41
42
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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
165
166
167
168
169
170
171
172
173
%% 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, tsg.file.versn] = ...
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
% assign netcdf variables in base workspace
% -----------------------------------------
assignin('base', variable, nv(:));
% 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
end
% 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, tsg.file.versn] = ...
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__;
% 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