Newer
Older
% levitus = read_file_woa(file)
% Read WOA01 Levitus T-S-O2, Stddev & nb_obs Netcdf file in memory

jacques.grelet_ird.fr
committed
%

jacques.grelet_ird.fr
committed
% check for file existence
% ------------------------
if filesfound == 0;
warning(['Couldn''t find ', file]);
levitus = -1;
return;
%% 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

jacques.grelet_ird.fr
committed
% -----------------
29
30
31
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
% use Charles Dunham toolbox
% --------------------------
function read_netcdf_toolbox
% il existe un "bug" dans netcdf ligne 409, ncitem retourne une
% erreur dans la variable result
% open file in read mode
% ----------------------
nc = netcdf(which(file), 'nowrite');
% Initialise la waitbar de chargement du fichier netCDF
% -----------------------------------------------------
%wb = waitbar(0,'');
% We cannot change title property to 'none' (default set as 'tex') inside
% waitbar function (line 81), see
% http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/115725
% -----------------------------------------------------------------------
% hchild = get(wb,'children');
% htitle = get(hchild,'title');
% set(htitle,'Interpreter','None');
% ('none' as opposed to 'latex') to leave underscores as underscores
set(0,'DefaulttextInterpreter','none')
% update waitbar title with Tex mode disable
% ------------------------------------------
wb = waitbar(0, ['Loading file: ' file ' Please wait...']);
% loop for every variable
% -----------------------
for i = 1:length(var(nc))
% first initialize the dimensions of the data array, this is due to a bug
% (?) in netcdf toolbox, with an array [1,1,n,m], netcdf operator (:)
% return only [n,m]. This is true for surface climatology file !!!!!
% -------------------------------------------------------------------
data = zeros(size(nc{i}));
% return current variable (object)
% --------------------------------
nv = nc{i};
% use autonan mode on this variable (object), remplace fillValue with NaN
% -----------------------------------------------------------------------
nv = autonan(nv, true);
% then fill with values, we use a temporary object data, because nv
% is an netcdf object, see line 75
% -----------------------------------------------------------------
data(:) = nv(:);
% other Matlab way to replace fillValue by NaN
% --------------------------------------------
% if ~isempty(fillval(nc{i}))
% data(data == fillval(nc{i})) = NaN;
% end
% assign dynamically variable in structure levitus
% ------------------------------------------------
% be carreful !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
% don't use data(:) ...
% whem data dimensions are [1,1,m,n]
% [m,n] = data(:)
% [1,1,m,n] = data
% ------------------------------------------------
levitus.(name(nv)) = data;
% moves the waitbar cursor
% ------------------------
waitbar( i/length(nv), wb);
end
% close waitbar
% -------------
close(wb);
% close netcdf file
% -----------------
close(nc);
end
% use native toolbox +netcdf since R2008b
% ---------------------------------------
function read_netcdf_native
% Open netCDF file
% ----------------
nc = netcdf_native(file);
for key = keys(nc.VARIABLES)
var = char(key);
data = zeros( size( nc.VARIABLES.(var).data__ ) );
data(:) = nc.VARIABLES.(var).data__;
levitus.(var) = data;
end
end
% check if file exist
%--------------------
function filesfound = checkforfiles( file )
if exist(file,'file');
filesfound = 1;
else
filesfound = 0;
end
end