/[MITgcm]/MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO_v2/MITprof_load.m
ViewVC logotype

Annotation of /MITgcm_contrib/gael/profilesMatlabProcessing/profiles_IO_v2/MITprof_load.m

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


Revision 1.3 - (hide annotations) (download)
Wed Jan 5 20:01:31 2011 UTC (14 years, 6 months ago) by gforget
Branch: MAIN
Changes since 1.2: +9 -1 lines
to accomodate old files:
	fix old 'depth' name convention
	fix lon>180

1 gforget 1.1 %function: MITprof_load
2     %object: read netcdf data files in the "MIT format"
3     %author: Gael Forget (gforget@mit.edu)
4     %date: june 21th, 2006
5     %
6     %usage: [MITprof]=MITprof_load(fileIn);
7     % ---> loads full data set
8     % [MITprof]=MITprof_load(fileIn,list_vars);
9     % ---> loads only the files listed in list_vars cell
10     % array (e.g. list_vars={'prof_T','prof_Tweight'})
11     % plus the one dimensional information (prof_lon etc.)
12     %
13     %note: this does the same as MITprof_read, but
14     % - removes empty fields
15     % - replaces missing values with NaN
16     % - adds a couple fields
17     % - replaces prof_descr with a cell form
18     %
19     %inputs: fileIn data file name
20     % list_vars variables list (optional)
21     %
22     %outputs: MITprof structure containing the various fields/vectors
23    
24     function [MITprof]=MITprof_load(fileIn,varargin);
25    
26 gforget 1.2 global useNativeMatlabNetcdf; if isempty(useNativeMatlabNetcdf); useNativeMatlabNetcdf = ~isempty(which('netcdf.open')); end;
27    
28 gforget 1.1 if nargin>1; list_vars=varargin{1};
29     else; list_vars={'prof_T','prof_Tweight','prof_Tmask','prof_Testim','prof_Tflag',...
30     'prof_S','prof_Sweight','prof_Smask','prof_Sestim','prof_Sflag',...
31     'prof_U','prof_Uweight','prof_Umask',...
32     'prof_V','prof_Vweight','prof_Vmask',...
33     'prof_ptr','prof_ptrweight','prof_ptrmask',...
34     'prof_ssh','prof_sshweight','prof_sshmask'};
35     end;
36    
37     list_vars_plus=[{'prof_depth','prof_date','prof_YYYYMMDD','prof_HHMMSS',...
38     'prof_lon','prof_lat','prof_basin','prof_point','prof_descr'}...
39     list_vars];
40    
41     %get directory name:
42     tmp1=strfind(fileIn,'/');
43     if ~isempty(tmp1); dirIn=fileIn(1:tmp1(end)); else; dirIn='./'; end;
44     %check that file exists:
45     tmp1=dir(fileIn);
46     if isempty(tmp1); tmp1=dir([fileIn '.nc']); end;
47     if isempty(tmp1); error([fileIn ' file not found']); end;
48    
49     for ii=1:length(list_vars_plus);
50     ncload([dirIn tmp1.name],list_vars_plus{ii});
51     %nan mask the data:
52     eval(['test0=~isempty(' list_vars_plus{ii} ');']);
53 gforget 1.2 if test0&useNativeMatlabNetcdf;
54 gforget 1.1 f = netcdf.open([dirIn tmp1.name], 'nowrite');
55     varid = netcdf.inqVarId(f,list_vars_plus{ii});
56     test1=~isempty(find(strcmp(ncatts(f,varid),'missing_value')));
57     if test1;
58     spval = double(netcdf.getAtt(f,varid,'missing_value'));
59     eval([list_vars_plus{ii} '(' list_vars_plus{ii} '==spval)=NaN;']);
60     end;
61     netcdf.close(f);
62 gforget 1.2 elseif test0;
63     f = netcdf(fileIn, 'nowrite');
64     spval = f{list_vars_plus{ii}}.missing_value(:);
65     if isempty(spval);
66     spval = f{list_vars_plus{ii}}.FillValue_(:);
67     end
68     if ~isempty(spval);
69     eval([list_vars_plus{ii} '(' list_vars_plus{ii} '==spval)=NaN;']);
70     end;
71     close(f);
72 gforget 1.1 end;
73 gforget 1.3 %fix old name convention:
74     if ii==1&isempty(prof_depth); ncload([dirIn tmp1.name],'depth'); prof_depth=depth; clear depth; end;
75    
76 gforget 1.1 if ii==1; eval(['MITprof=struct(''prof_depth'',prof_depth);']);
77     elseif ~isempty(tmp1); eval(['MITprof.' list_vars_plus{ii} '=' list_vars_plus{ii} ';']);
78     end; eval(['clear ' list_vars_plus{ii} ']);']);
79     end;
80    
81 gforget 1.3 %make sure that lon is -180+180:
82     %-------------------------------
83     tmp_lon=MITprof.prof_lon;
84     tmp_lon(find(tmp_lon>180))=tmp_lon(find(tmp_lon>180))-360;
85     MITprof.prof_lon=tmp_lon;
86    
87 gforget 1.1 %replace prof_descr with cell array:
88     %----------------------------------
89 gforget 1.2 np=length(MITprof.prof_lon); prof_descr=cell(np,1);
90 gforget 1.1 for pp=1:np; prof_descr(pp)={MITprof.prof_descr(pp,:)}; end;
91 gforget 1.2 MITprof.prof_descr=prof_descr;
92 gforget 1.1
93     %get rid of empty variables:
94     %---------------------------
95     fldNames=fieldnames(MITprof);
96     for iFld=1:length(fldNames);
97     eval(['test0=isempty(MITprof.' fldNames{iFld} ');']);
98     if test0; MITprof=rmfield(MITprof,fldNames{iFld}); end;
99     end;
100    
101     %add a couple things:
102     %--------------------
103     MITprof.np=length(MITprof.prof_lon);
104     MITprof.nr=length(MITprof.prof_depth);
105    

  ViewVC Help
Powered by ViewVC 1.1.22