/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/profiles_misc/ncload.m
ViewVC logotype

Contents of /MITgcm_contrib/gael/profilesMatlabProcessing/profiles_misc/ncload.m

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations) (download)
Fri Nov 5 21:41:02 2010 UTC (14 years, 8 months ago) by gforget
Branch: MAIN
- make this work both with the legacy mexnc stuff (as before)
  and with the modern matlab native netcdf implementation.

1 function theResult = ncload(fileIn, varargin);
2
3 % ncload -- Load NetCDF variables.
4 % ncload('fileIn', 'var1', 'var2', ...) loads the
5 % given variables of 'fileIn' into the Matlab
6 % workspace of the "caller" of this routine. If no names
7 % are given, all variables are loaded.
8
9 if nargin < 1, help(mfilename), return, end
10
11 result = [];
12 if nargout > 0, theResult = result; end
13
14 global useNativeMatlabNetcdf; if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end;
15
16 if (useNativeMatlabNetcdf);
17 f = netcdf.open(fileIn, 'nowrite');
18
19 vars=ncvars(f);
20 if isempty(varargin); varargin = vars; end;
21
22 for i = 1:length(varargin)
23 if sum(strcmp(vars,varargin{i}))>0;
24 %get variable
25 varid = netcdf.inqVarId(f,varargin{i});
26 aa=netcdf.getVar(f,varid);
27 %inverse the order of dimensions
28 if length(size(aa))>2|size(aa,2)~=1;
29 bb=length(size(aa)); aa=permute(aa,[bb:-1:1]);
30 end;
31 %replace missing value with NaN
32 [atts]=ncatts(f,varid);
33 if strcmp(atts,'missing_value')&isreal(aa);
34 spval = double(netcdf.getAtt(f,varid,'missing_value'));
35 elseif strcmp(atts,'_FillValue')&isreal(aa);
36 spval = double(netcdf.getAtt(f,varid,'_FillValue'));
37 else;
38 spval=[];
39 end;
40 if ~isempty(spval); aa(aa==spval)=NaN; end;
41 else;
42 aa=[];
43 end;
44 assignin('caller', varargin{i}, aa)
45 end
46
47 result = varargin;
48
49 netcdf.close(f);
50
51
52 else;%try to use old mex stuff
53
54 f = netcdf(fileIn, 'nowrite');
55 if isempty(f), return, end
56
57 if isempty(varargin), varargin = ncnames(var(f)); end
58
59 for i = 1:length(varargin)
60 if ~isstr(varargin{i}), varargin{i} = inputname(i+1); end
61 oldfld = f{varargin{i}}(:);
62 spval = f{varargin{i}}.missing_value(:);
63 if isempty(spval);
64 spval = f{varargin{i}}.FillValue_(:);
65 end
66 fld = oldfld;
67 if ~isempty(spval)&~ischar(fld);
68 replace = find(oldfld == spval);
69 nreplace = length(replace);
70 if nreplace>0
71 fld(replace) = NaN*ones(1,nreplace);
72 end %if
73 end %if
74 %NaN-substitution messes up backward compatibility so I comment it out
75 % assignin('caller', varargin{i}, fld);
76 %and revert to oldfld
77 assignin('caller', varargin{i}, oldfld)
78 end
79
80 result = varargin;
81
82 close(f)
83
84 if nargout > 0
85 theResult = result
86 else
87 ncans(result)
88 end
89
90 end
91
92

  ViewVC Help
Powered by ViewVC 1.1.22