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

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

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


Revision 1.2 - (hide annotations) (download)
Mon Apr 11 20:48:54 2011 UTC (14 years, 3 months ago) by roquet
Branch: MAIN
Changes since 1.1: +19 -32 lines
interface for old/new Matlab netcdf toolbox

1 gforget 1.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 roquet 1.2 global useNativeMatlabNetcdf; if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end;
10 gforget 1.1
11 roquet 1.2 f = ncopen(fileIn, 'nowrite');
12     if isempty(f), return, end
13     vars=ncvars(f);
14     if isempty(varargin); varargin = vars; end;
15 gforget 1.1
16     if (useNativeMatlabNetcdf);
17    
18     for i = 1:length(varargin)
19     if sum(strcmp(vars,varargin{i}))>0;
20     %get variable
21 roquet 1.2 varid = netcdf.inqVarID(f,varargin{i});
22 gforget 1.1 aa=netcdf.getVar(f,varid);
23     %inverse the order of dimensions
24 roquet 1.2 bb=length(size(aa)); aa=permute(aa,[bb:-1:1]);
25 gforget 1.1 %replace missing value with NaN
26     [atts]=ncatts(f,varid);
27     if strcmp(atts,'missing_value')&isreal(aa);
28     spval = double(netcdf.getAtt(f,varid,'missing_value'));
29     elseif strcmp(atts,'_FillValue')&isreal(aa);
30     spval = double(netcdf.getAtt(f,varid,'_FillValue'));
31     else;
32     spval=[];
33     end;
34     if ~isempty(spval); aa(aa==spval)=NaN; end;
35     else;
36     aa=[];
37     end;
38     assignin('caller', varargin{i}, aa)
39     end
40    
41    
42    
43    
44     else;%try to use old mex stuff
45    
46    
47     for i = 1:length(varargin)
48     if ~isstr(varargin{i}), varargin{i} = inputname(i+1); end
49     oldfld = f{varargin{i}}(:);
50     spval = f{varargin{i}}.missing_value(:);
51     if isempty(spval);
52     spval = f{varargin{i}}.FillValue_(:);
53     end
54     fld = oldfld;
55     if ~isempty(spval)&~ischar(fld);
56     replace = find(oldfld == spval);
57     nreplace = length(replace);
58     if nreplace>0
59     fld(replace) = NaN*ones(1,nreplace);
60     end %if
61     end %if
62 roquet 1.2 %NaN-substitution messes up backward compatibility so I comment it out
63     % assignin('caller', varargin{i}, fld);
64     %and revert to oldfld
65 gforget 1.1 assignin('caller', varargin{i}, oldfld)
66     end
67    
68     end
69    
70 roquet 1.2 ncclose(f);
71    
72     result = varargin;
73    
74     if nargout > 0
75     theResult = result
76     else
77     ncans(result)
78     end
79 gforget 1.1

  ViewVC Help
Powered by ViewVC 1.1.22