author | A.M. Thurnherr <athurnherr@yahoo.com> |
Wed, 17 Jan 2018 12:19:54 -0500 | |
changeset 20 | 61b92f8fb463 |
parent 18 | 6e7c8d592f7f |
permissions | -rw-r--r-- |
18 | 1 |
function [] = ladcp2cdf(fname,dr_struct,da,p,ps,f,att); |
2 |
% function [] = ladcp2cdf(fname,dr_struct,da,p,ps,f,att); |
|
3 |
% |
|
4 |
% function to save LADCP data into a netcdf file for MatLab version 2012a |
|
5 |
% |
|
6 |
% input : fname - output filename |
|
7 |
% dr_struct - main inversion results (velocity profiles) |
|
8 |
% da,p,ps,f,att - arbitrary metadata structures |
|
9 |
% |
|
10 |
% Subroutine : add_struct |
|
11 |
||
12 |
% Created By: Diana Cardoso, Bedford Institute of Oceangraphy |
|
13 |
% Diana.Cardoso@dfo-mpo.gc.ca |
|
14 |
% Description: Based on LDEO software to Process LADCP, version IX.8, |
|
15 |
% script ladcp2cdf.m version 0.1 last change 08.03.2002. |
|
16 |
% maintained by A.M. Thurnherr and downloaded from: |
|
17 |
% http://www.ldeo.columbia.edu/cgi-bin/ladcp-cgi-bin/hgwebdir.cgi |
|
18 |
% The function ladcp2cdf was changed to run with the the Matlab |
|
19 |
% version 2012, which now supports netcdf. |
|
20 |
||
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
21 |
%====================================================================== |
18 | 22 |
% L A D C P 2 C D F . M |
23 |
% doc: Thu Aug 15 10:52:55 2013 |
|
24 |
% dlm: Thu Nov 26 22:01:32 2015 |
|
25 |
% (c) 2013 A.M. Thurnherr, from code contributed by D. Cardoso |
|
26 |
% uE-Info: 183 0 NIL 0 0 72 0 2 8 NIL ofnI |
|
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
27 |
%====================================================================== |
0
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
28 |
|
18 | 29 |
% NOTES: |
30 |
% - This version creates slightly different files than the original version |
|
31 |
% created by Visbeck/Krahmann. In the original version, the contents of the |
|
32 |
% dr structure end up as top-level variables and the contents of |
|
33 |
% the da, p, ps, f and att structures end of as global attributes. In |
|
34 |
% the new version, the latter are saved as sub-structures, with _struct appended |
|
35 |
% to the internal names to avoid conflicts. |
|
36 |
||
17 | 37 |
% HISTORY: |
18 | 38 |
% Aug 15, 2013: - incorporated this code, supplied Diana Cardoso, into IX_10beta |
39 |
% - modified doc in header |
|
40 |
% - renamded struct variable to dr_struct |
|
41 |
% - removed 'cd' in and out of results directory (pathnames work just fine) |
|
42 |
% - delete netcdfile before it is written to (old 'clobber' option) |
|
43 |
% - removed 'l' suffix from all dims |
|
44 |
% - replaced yes/no logical vals by true/false |
|
45 |
% - renamed substructures from st2..st6 to internal names (da,p,ps,f,att) |
|
46 |
% Aug 28, 2013: - incorporated bug fix provided by Diana Cardoso to prevent lat,lon,name and |
|
47 |
% date to be stored 2cd in the nc file, which can make the code |
|
48 |
% bomb if the length of any other var is 6 (or equal to the length of name?) |
|
49 |
% Nov 2015: - new coded provided by Diana Cardoso (with support from Eric Firing) |
|
50 |
% Nov 26, 2015: - BUG: code did not work when bottom-track data were missing |
|
51 |
%-------------------------------------------------------------------------- |
|
52 |
% check arguments and remove existing NetCDF file with fname(output filename) |
|
53 |
%-------------------------------------------------------------------------- |
|
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
54 |
|
18 | 55 |
if nargin<2 |
56 |
error('need two input arguments') |
|
57 |
end |
|
58 |
if ~isstruct(dr_struct) |
|
59 |
error('second argument must be a dr structure') |
|
0
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
60 |
end |
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
61 |
|
18 | 62 |
netcdfile = deblank(fname); %remove any blanks from string end |
63 |
if exist(netcdfile,'file') |
|
64 |
delete(netcdfile) |
|
65 |
end |
|
66 |
%-------------------------------------------------------------------------- |
|
67 |
%Create a classic format NetCDF file with 8 dimension definitions. |
|
68 |
%-------------------------------------------------------------------------- |
|
17 | 69 |
|
18 | 70 |
mySchema.Name = '/'; % indicating the full file as opposed to a group |
71 |
mySchema.Format = 'classic'; %The format of the NetCDF file |
|
72 |
%Create Dimensions |
|
73 |
mySchema.Dimensions(1).Name = 'name'; |
|
74 |
mySchema.Dimensions(1).Length = length(getfield(dr_struct,'name')); |
|
75 |
mySchema.Dimensions(2).Name = 'date'; |
|
76 |
mySchema.Dimensions(2).Length = 6; |
|
77 |
mySchema.Dimensions(3).Name = 'lat'; |
|
78 |
mySchema.Dimensions(3).Length = 1; |
|
79 |
mySchema.Dimensions(4).Name = 'lon'; |
|
80 |
mySchema.Dimensions(4).Length = 1; |
|
17 | 81 |
|
18 | 82 |
if isfield(dr_struct,'z'); |
83 |
lz = length(getfield(dr_struct,'z')); |
|
84 |
else |
|
85 |
lz = 0; |
|
86 |
end |
|
87 |
mySchema.Dimensions(5).Name = 'z'; |
|
88 |
mySchema.Dimensions(5).Length = lz; |
|
89 |
||
90 |
if isfield(dr_struct,'tim'); |
|
91 |
ltim = length(getfield(dr_struct,'tim')); |
|
92 |
else |
|
93 |
ltim = 0; |
|
94 |
end |
|
95 |
mySchema.Dimensions(6).Name = 'tim'; |
|
96 |
mySchema.Dimensions(6).Length = ltim; |
|
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
97 |
|
18 | 98 |
if isfield(dr_struct,'zbot'); |
99 |
lbot = length(getfield(dr_struct,'zbot')); |
|
100 |
else |
|
101 |
lbot = 0; |
|
102 |
end |
|
103 |
mySchema.Dimensions(7).Name = 'zbot'; |
|
104 |
mySchema.Dimensions(7).Length = lbot; |
|
105 |
||
106 |
if isfield(dr_struct,'z_sadcp'); |
|
107 |
lsadcp = length(getfield(dr_struct,'z_sadcp')); |
|
108 |
else |
|
109 |
lsadcp = 0; |
|
110 |
end |
|
111 |
mySchema.Dimensions(8).Name = 'z_sadcp'; |
|
112 |
mySchema.Dimensions(8).Length = lsadcp; |
|
113 |
||
114 |
%-------------------------------------------------------------------------- |
|
115 |
%Add first 4 variables definitions(name, date, lat, lon) to Variable field |
|
116 |
%-------------------------------------------------------------------------- |
|
0
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
117 |
|
18 | 118 |
mySchema.Variables(1).Name = 'name'; |
119 |
mySchema.Variables(1).Dimensions(1) = mySchema.Dimensions(1); |
|
120 |
mySchema.Variables(1).Datatype = 'char'; |
|
121 |
mySchema.Variables(2).Name = 'date'; |
|
122 |
mySchema.Variables(2).Dimensions(1) = mySchema.Dimensions(2); |
|
123 |
mySchema.Variables(2).Datatype = 'int32'; |
|
124 |
mySchema.Variables(3).Name = 'lat'; |
|
125 |
mySchema.Variables(3).Dimensions(1) = mySchema.Dimensions(3); |
|
126 |
mySchema.Variables(3).Datatype = 'single'; |
|
127 |
mySchema.Variables(4).Name = 'lon'; |
|
128 |
mySchema.Variables(4).Dimensions(1) = mySchema.Dimensions(4); |
|
129 |
mySchema.Variables(4).Datatype = 'single'; |
|
0
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
130 |
|
18 | 131 |
dr_struct = orderfields(dr_struct) ; %sort dr_struct by fieldnames |
132 |
||
133 |
fnames = fieldnames(dr_struct); |
|
134 |
% find name, date, lat, lon in fnames that have already been added to the |
|
135 |
% NetCDF file and remove from fnames |
|
136 |
nn=strncmp('name',fnames,6); |
|
137 |
nda=strncmp('date',fnames,4); |
|
138 |
nla=strncmp('lat',fnames,3); |
|
139 |
nlo=strncmp('lon',fnames,3); |
|
140 |
ntot=[nn+nda+nla+nlo]; Ktot = logical(ntot); |
|
141 |
fnames(Ktot,:)=[]; |
|
142 |
||
143 |
%-------------------------------------------------------------------------- |
|
144 |
%Add the remaining variables definitions from dr_struct to Variable field |
|
145 |
%-------------------------------------------------------------------------- |
|
0
0a450563f904
VIX_6: first version for Mercurial release
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff
changeset
|
146 |
|
18 | 147 |
for n=1:size(fnames,1) |
148 |
dummy = getfield(dr_struct,fnames{n}); |
|
149 |
mySchema.Variables(4+n).Name = fnames{n}; |
|
150 |
if ~isempty(strfind(fnames{n},'bot')) |
|
151 |
mySchema.Variables(4+n).Dimensions(1) = mySchema.Dimensions(7); |
|
152 |
elseif ~isempty(strfind(fnames{n},'sadcp')) |
|
153 |
mySchema.Variables(4+n).Dimensions(1) = mySchema.Dimensions(8); |
|
154 |
elseif ~isempty(strfind(fnames{n},'tim')) || ~isempty(strfind(fnames{n},'ship')) ... |
|
155 |
|| ~isempty(strfind(fnames{n},'ctd')) && isempty(strmatch('ctd_t',fnames{n},'exact')) ... |
|
156 |
&& isempty(strmatch('ctd_s',fnames{n})) |
|
157 |
mySchema.Variables(4+n).Dimensions(1) = mySchema.Dimensions(6); |
|
158 |
elseif ~isempty(strfind(fnames{n},'vel')) || ~isempty(strfind(fnames{n},'shear')) ... |
|
159 |
|| ~isempty(strfind(fnames{n},'ctd_')) || ~isempty(strfind(fnames{n},'uerr')) ... |
|
160 |
|| ~isempty(strfind(fnames{n},'range')) || ~isempty(strfind(fnames{n},'ts')) ... |
|
161 |
|| ~isempty(strmatch('p',fnames{n},'exact')) || ~isempty(strmatch('u',fnames{n},'exact')) ... |
|
162 |
|| ~isempty(strmatch('v',fnames{n},'exact'))|| ~isempty(strmatch('z',fnames{n},'exact')) ... |
|
163 |
|| ~isempty(strfind(fnames{n},'u_')) || ~isempty(strfind(fnames{n},'v_')); |
|
164 |
mySchema.Variables(4+n).Dimensions(1) = mySchema.Dimensions(5); |
|
165 |
else |
|
166 |
mySchema.Variables(4+n).Dimensions.Name = fnames{n}; |
|
167 |
mySchema.Variables(4+n).Dimensions.Length = length(dummy); |
|
168 |
end |
|
169 |
if strncmp(fnames{n},'tim',4)==1 || ~isempty(strfind(fnames{n},'vbar')) || ~isempty(strfind(fnames{n},'ubar')) |
|
170 |
mySchema.Variables(4+n).Datatype = 'double'; |
|
171 |
elseif strncmp(fnames{n},'nvel',4)==1 |
|
172 |
mySchema.Variables(4+n).Datatype = 'int16'; |
|
173 |
else |
|
174 |
mySchema.Variables(4+n).Datatype = 'single'; |
|
175 |
end |
|
176 |
% disp(sprintf('%d: %s %d',n,mySchema.Variables(4+n).Dimensions.Name,mySchema.Variables(4+n).Dimensions.Length)); |
|
177 |
end |
|
178 |
||
179 |
%-------------------------------------------------------------------------- |
|
180 |
%Add the attributes definitions from dr_struct to the Variable field |
|
181 |
%-------------------------------------------------------------------------- |
|
182 |
||
183 |
ncwriteschema(fname, mySchema); |
|
184 |
ncwriteatt(fname,'name','long_name',att.name.long_name); |
|
185 |
ncwriteatt(fname,'date','long_name',att.date.long_name); |
|
186 |
ncwriteatt(fname,'date','units',att.date.units); |
|
187 |
ncwriteatt(fname,'lat','long_name',att.lat.long_name); |
|
188 |
ncwriteatt(fname,'lat','units',att.lat.units); |
|
189 |
ncwriteatt(fname,'lon','long_name',att.lon.long_name); |
|
190 |
ncwriteatt(fname,'lon','units',att.lon.units); |
|
191 |
ncwrite(fname,'name',dr_struct.name); |
|
192 |
ncwrite(fname,'date',dr_struct.date); |
|
193 |
ncwrite(fname,'lat',dr_struct.lat); |
|
194 |
ncwrite(fname,'lon',dr_struct.lon); |
|
195 |
||
196 |
for n=1:size(fnames,1) |
|
197 |
dummy = getfield(dr_struct,fnames{n}); |
|
198 |
ncwrite(fname,fnames{n},dummy); |
|
199 |
if isfield(att,fnames{n}); |
|
200 |
ncwriteatt(fname,fnames{n},'long_name',att.(fnames{n}).long_name); |
|
201 |
if isfield(att.(fnames{n}),'units'); |
|
202 |
ncwriteatt(fname,fnames{n},'units',att.(fnames{n}).units); |
|
203 |
end |
|
17 | 204 |
end |
18 | 205 |
end |
206 |
||
207 |
%-------------------------------------------------------------------------- |
|
208 |
%Add the attributes definitions from da, p, f, ps mat structures to |
|
209 |
%Attributes field using subroutine add_struct |
|
210 |
%-------------------------------------------------------------------------- |
|
211 |
||
212 |
% remove duplicate fields from mat structures |
|
213 |
ps = rmfield(ps,'outlier'); |
|
214 |
p = rmfield(p,'checkpoints'); |
|
215 |
f = rmfield(f,'ctd_time_base'); |
|
216 |
f = rmfield(f,'nav_time_base'); |
|
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
217 |
|
18 | 218 |
% combine all mat structures and sort fieldnames |
219 |
names = [fieldnames(da); fieldnames(p)]; |
|
220 |
struct1 = cell2struct([struct2cell(da); struct2cell(p)], names, 1); |
|
221 |
names = [fieldnames(struct1); fieldnames(ps)]; |
|
222 |
struct2 = cell2struct([struct2cell(struct1); struct2cell(ps)], names, 1); |
|
223 |
names = [fieldnames(struct2); fieldnames(f)]; |
|
224 |
struct3 = cell2struct([struct2cell(struct2); struct2cell(f)], names, 1); |
|
225 |
structsorted = orderfields(struct3) ; |
|
226 |
||
227 |
% add attributes to netcdf from the p, ps, da and f structures |
|
228 |
% the slash indicates a global variable, if you change it the value in the |
|
229 |
% structures will be placed in the Variables field of the netcdf |
|
230 |
add_struct(fname,'/',structsorted); |
|
231 |
||
11
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
232 |
end % function |
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
233 |
|
d71acdec556a
10beta to be emailed to Eric Firing on Sep 18
A.M. Thurnherr <athurnherr@yahoo.com>
parents:
0
diff
changeset
|
234 |
%---------------------------------------------------------------------- |