1 |
gforget |
1.1 |
%function: MITprof_read |
2 |
roquet |
1.10 |
%object: read netcdf data files in the "MIT format". Low-level function. |
3 |
|
|
% use MITprof_load instead to load data from a MITprof |
4 |
|
|
% netcdf file. |
5 |
gforget |
1.1 |
%author: Gael Forget (gforget@mit.edu) |
6 |
|
|
%date: june 21th, 2006 |
7 |
|
|
% |
8 |
gforget |
1.3 |
%usage: [MITprof]=MITprof_read(fileIn); |
9 |
gforget |
1.1 |
% ---> loads full data set |
10 |
gforget |
1.3 |
% [MITprof]=MITprof_read(fileIn,list_vars); |
11 |
gforget |
1.1 |
% ---> loads only the files listed in list_vars cell |
12 |
|
|
% array (e.g. list_vars={'prof_T','prof_Tweight'}) |
13 |
|
|
% plus the one dimensional information (prof_lon etc.) |
14 |
|
|
% |
15 |
roquet |
1.10 |
%inputs: fileIn data file name |
16 |
|
|
% list_vars variables list (optional) |
17 |
gforget |
1.1 |
% |
18 |
|
|
%outputs: MITprof structure containing the various fields/vectors |
19 |
|
|
|
20 |
gforget |
1.3 |
function [MITprof]=MITprof_read(fileIn,varargin); |
21 |
gforget |
1.1 |
|
22 |
roquet |
1.7 |
|
23 |
|
|
% check that file exists and add prefix and suffix if necessary |
24 |
roquet |
1.8 |
[pathstr, name, ext] = fileparts(fileIn); |
25 |
|
|
if isempty(pathstr) | strcmp(pathstr,'.'), pathstr=pwd; end |
26 |
|
|
if isempty(ext) | ~strcmp(ext,'.nc'), ext='.nc'; end |
27 |
|
|
fileIn=[pathstr '/' name ext]; |
28 |
roquet |
1.9 |
if ~exist(fileIn,'file'), error([fileIn ' : file not found']); end |
29 |
roquet |
1.7 |
|
30 |
|
|
% list of variables to read |
31 |
|
|
list_vars={'prof_depth','prof_date','prof_YYYYMMDD','prof_HHMMSS',... |
32 |
|
|
'prof_lon','prof_lat','prof_basin','prof_point','prof_descr'}; |
33 |
|
|
|
34 |
|
|
% load fields |
35 |
|
|
if nargin>1; |
36 |
|
|
list_vars=unique({list_vars{:},varargin{1}{:}}); |
37 |
|
|
for ii=1:length(list_vars), |
38 |
|
|
ncload(fileIn,list_vars{ii}); |
39 |
|
|
end |
40 |
|
|
else; |
41 |
|
|
f = ncopen(fileIn,'nowrite'); |
42 |
|
|
list_vars=ncvars(f); |
43 |
|
|
ncclose(f); |
44 |
|
|
ncload(fileIn); |
45 |
gforget |
1.1 |
end; |
46 |
|
|
|
47 |
roquet |
1.7 |
% fill the variable MITprof |
48 |
|
|
MITprof=[]; |
49 |
|
|
N=length(prof_depth); |
50 |
|
|
for ii=1:length(list_vars); |
51 |
|
|
var=eval(list_vars{ii}); |
52 |
|
|
if ismember(list_vars{ii},{'prof_depth','prof_HHMMSS','prof_YYYYMMDD',... |
53 |
|
|
'prof_lat ','prof_lon','prof_point','prof_basin','prof_date'}) |
54 |
|
|
MITprof=setfield(MITprof,list_vars{ii},reshape(var,length(var),1)); |
55 |
|
|
else |
56 |
|
|
MITprof=setfield(MITprof,list_vars{ii},var); |
57 |
|
|
end |
58 |
|
|
end; |
59 |
|
|
|
60 |
|
|
%fix old name convention: |
61 |
|
|
if isempty(prof_depth); |
62 |
|
|
ncload(fileIn,'depth'); |
63 |
|
|
MITprof.prof_depth=depth; |
64 |
|
|
clear depth; |
65 |
gforget |
1.1 |
end; |
66 |
|
|
|
67 |
gforget |
1.5 |
%make sure that lon is -180+180: |
68 |
roquet |
1.7 |
%------------------------------- |
69 |
gforget |
1.5 |
tmp_lon=MITprof.prof_lon; |
70 |
|
|
tmp_lon(find(tmp_lon>180))=tmp_lon(find(tmp_lon>180))-360; |
71 |
|
|
MITprof.prof_lon=tmp_lon; |
72 |
|
|
|
73 |
gforget |
1.6 |
%get rid of empty variables: |
74 |
roquet |
1.7 |
%--------------------------- |
75 |
gforget |
1.6 |
fldNames=fieldnames(MITprof); |
76 |
|
|
for iFld=1:length(fldNames); |
77 |
|
|
eval(['test0=isempty(MITprof.' fldNames{iFld} ');']); |
78 |
|
|
if test0; MITprof=rmfield(MITprof,fldNames{iFld}); end; |
79 |
|
|
end; |