0
|
1 |
#======================================================================
|
|
2 |
# A N T S N C . P L
|
|
3 |
# doc: Mon Jul 17 11:59:37 2006
|
47
|
4 |
# dlm: Fri Apr 22 08:10:25 2022
|
0
|
5 |
# (c) 2006 A.M. Thurnherr
|
47
|
6 |
# uE-Info: 381 63 NIL 0 0 70 2 2 4 NIL ofnI
|
0
|
7 |
#======================================================================
|
|
8 |
|
|
9 |
# ANTS netcdf library
|
|
10 |
|
|
11 |
# HISTORY:
|
|
12 |
# Jul 17, 2006: - created
|
|
13 |
# Jul 21, 2006: - documented
|
|
14 |
# - added NC-encoding routines
|
|
15 |
# Jul 22: 2006: - BUG: pseudo %PARAMs were written as well
|
|
16 |
# - BUG: var ATTRs were not enconded correctly
|
|
17 |
# - added type support
|
|
18 |
# Jul 23, 2006: - improved type magic
|
|
19 |
# Sep 1, 2006: - BUG: removing trainling 0s had not worked
|
|
20 |
# Sep 23, 2006: - fiddled
|
|
21 |
# Jul 11, 2008: - adapted to new pseudo %PARAMs
|
|
22 |
# Jul 16, 2008: - remove \0s from strings in NC_stringify
|
|
23 |
# Mar 20, 2008: - added progress output to NC_stringify
|
|
24 |
# Jul 21, 2009: - allowed for suppression of %PARAMs
|
25
|
25 |
# Jan 15, 2016: - BUG: %DEPS pseudo-%PARAM was encoded
|
47
|
26 |
# Apr 5, 2022: - added NC_writeMDataMulti()
|
|
27 |
# - implemented disappeared NetCDF::DOUBLE etc.
|
|
28 |
# - enabled fill value handling (library bug has gone away)
|
|
29 |
# Apr 22, 2022: - added "_coordinate" to abscissa dimension to
|
|
30 |
# allow reading with python xr
|
|
31 |
# HISTORY END
|
0
|
32 |
|
|
33 |
# NOTES:
|
|
34 |
# - multi-valued attribs are not loaded by getInfo()
|
|
35 |
# - spaces in NC strings are replaced by underscores
|
|
36 |
|
47
|
37 |
# NetCDF Library Bug: NO LONGER ACTIVE AS OF APR 2022
|
0
|
38 |
# The library appears to have incorrect default _FillValue types for
|
|
39 |
# integer data types. The error appears if the "setfill" line is commented
|
|
40 |
# out and the following command is run:
|
|
41 |
# listNC -ct dbk100.nc | NCode -o TEMP.nc time
|
|
42 |
# NB: The error occurs when the 1st variable value is written, NOT when
|
|
43 |
# the first Q_time value is written. However, when all the Q_ fields
|
|
44 |
# are ommitted, the error disappears.
|
|
45 |
|
|
46 |
use NetCDF;
|
|
47 |
|
47
|
48 |
#----------------------------------------------------------------------
|
|
49 |
# NetCDF Constants
|
|
50 |
# - as of April, 2022 these are no longer part of NetCDF???
|
|
51 |
# - manually encoded from .h file
|
|
52 |
#----------------------------------------------------------------------
|
|
53 |
|
|
54 |
sub NetCDF::NAT { return 0; }
|
|
55 |
sub NetCDF::BYTE { return 1; }
|
|
56 |
sub NetCDF::CHAR { return 2; }
|
|
57 |
sub NetCDF::SHORT { return 3; }
|
|
58 |
sub NetCDF::INT { return 4; }
|
|
59 |
sub NetCDF::LONG { return 4; }
|
|
60 |
sub NetCDF::FLOAT { return 5; }
|
|
61 |
sub NetCDF::DOUBLE { return 6; }
|
|
62 |
sub NetCDF::UBYTE { return 7; }
|
|
63 |
sub NetCDF::USHORT { return 8; }
|
|
64 |
sub NetCDF::UINT { return 9; }
|
|
65 |
sub NetCDF::INT64 { return 10; }
|
|
66 |
sub NetCDF::UINT64 { return 11; }
|
|
67 |
sub NetCDF::STRING { return 12; }
|
|
68 |
|
|
69 |
sub NetCDF::FILL_BYTE { return -127; }
|
|
70 |
sub NetCDF::FILL_CHAR { return "\0"; }
|
|
71 |
sub NetCDF::FILL_SHORT { return -32767; }
|
|
72 |
sub NetCDF::FILL_INT { return -2147483647; }
|
|
73 |
sub NetCDF::FILL_LONG { return -2147483647; }
|
|
74 |
sub NetCDF::FILL_FLOAT { return 9.9692099683868690e+36; }
|
|
75 |
sub NetCDF::FILL_DOUBLE { return 9.9692099683868690e+36; }
|
|
76 |
sub NetCDF::FILL_UBYTE { return 255; }
|
|
77 |
sub NetCDF::FILL_USHORT { return 65535; }
|
|
78 |
sub NetCDF::FILL_UINT { return 4294967295; }
|
|
79 |
sub NetCDF::FILL_INT64 { return -9223372036854775806; }
|
|
80 |
sub NetCDF::FILL_UINT64 { return 18446744073709551614; }
|
|
81 |
sub NetCDF::FILL_STRING { return ''; }
|
|
82 |
|
|
83 |
sub NetCDF::NOWRITE { return 0x0000; }
|
|
84 |
sub NetCDF::CLOBBER { return 0x0000; }
|
|
85 |
sub NetCDF::NOFILL { return 0x100; }
|
|
86 |
sub NetCDF::GLOBAL { return -1; }
|
|
87 |
sub NetCDF::UNLIMITED { return 0; }
|
|
88 |
|
|
89 |
|
0
|
90 |
#----------------------------------
|
|
91 |
# string representation of NC types
|
|
92 |
#----------------------------------
|
|
93 |
|
|
94 |
sub NC_typeName($)
|
|
95 |
{
|
|
96 |
my($tp) = @_;
|
|
97 |
|
|
98 |
return 'byte' if ($tp == NetCDF::BYTE);
|
|
99 |
return 'char' if ($tp == NetCDF::CHAR);
|
|
100 |
return 'short' if ($tp == NetCDF::SHORT);
|
|
101 |
return 'long' if ($tp == NetCDF::LONG);
|
|
102 |
return 'float' if ($tp == NetCDF::FLOAT);
|
|
103 |
return 'double' if ($tp == NetCDF::DOUBLE);
|
|
104 |
croak("$0: unknown NetCDF type #$tp\n");
|
|
105 |
}
|
|
106 |
|
|
107 |
sub NC_type($)
|
|
108 |
{
|
|
109 |
my($tn) = lc($_[0]);
|
|
110 |
|
|
111 |
return NetCDF::BYTE if ($tn eq 'byte');
|
|
112 |
return NetCDF::CHAR if ($tn eq 'char');
|
|
113 |
return NetCDF::SHORT if ($tn eq 'short');
|
|
114 |
return NetCDF::LONG if ($tn eq 'long');
|
|
115 |
return NetCDF::FLOAT if ($tn eq 'float');
|
|
116 |
return NetCDF::DOUBLE if ($tn eq 'double');
|
|
117 |
croak("$0: unknown NetCDF type <$tn>\n");
|
|
118 |
}
|
|
119 |
|
|
120 |
#--------------------------------------
|
|
121 |
# test whether given NC type is numeric
|
|
122 |
#--------------------------------------
|
|
123 |
|
|
124 |
sub NC_isNumeric($)
|
|
125 |
{
|
|
126 |
my($tp) = @_;
|
|
127 |
|
|
128 |
return 1 if ($tp == NetCDF::BYTE);
|
|
129 |
return 1 if ($tp == NetCDF::SHORT);
|
|
130 |
return 1 if ($tp == NetCDF::LONG);
|
|
131 |
return 1 if ($tp == NetCDF::FLOAT);
|
|
132 |
return 1 if ($tp == NetCDF::DOUBLE);
|
|
133 |
return 0;
|
|
134 |
}
|
|
135 |
|
|
136 |
#----------------------------------------
|
|
137 |
# test whether given NC type is character
|
|
138 |
#----------------------------------------
|
|
139 |
|
|
140 |
sub NC_isChar($)
|
|
141 |
{
|
|
142 |
return $_[0] == NetCDF::CHAR;
|
|
143 |
}
|
|
144 |
|
|
145 |
#-----------------------------------
|
|
146 |
# convert character- to string array
|
|
147 |
#-----------------------------------
|
|
148 |
|
|
149 |
sub NC_stringify($@)
|
|
150 |
{
|
|
151 |
my($len,@chars) = @_;
|
|
152 |
my(@strings);
|
|
153 |
my($nStrings) = @chars/$len;
|
|
154 |
|
|
155 |
print(STDERR "$0: extracting $nStrings strings")
|
|
156 |
if ($nStrings > 1000);
|
|
157 |
|
|
158 |
while (@chars) {
|
|
159 |
print(STDERR ".") if ($nStrings>1000 && $n++%1000 == 0);
|
|
160 |
push(@strings,pack("c$len",@chars));
|
|
161 |
$strings[$#strings] =~ s/ /_/g;
|
|
162 |
$strings[$#strings] =~ s/\0//g;
|
|
163 |
splice(@chars,0,$len);
|
|
164 |
}
|
|
165 |
print(STDERR "\n") if ($nStrings > 1000);
|
|
166 |
return @strings;
|
|
167 |
}
|
|
168 |
|
|
169 |
#----------------------------------------------------------------------
|
|
170 |
# open netcdf file and read (most) metadata into hash
|
|
171 |
#
|
|
172 |
# INPUT:
|
|
173 |
# <filename>
|
|
174 |
#
|
|
175 |
# OUTPUT:
|
|
176 |
# $NC{id} netcdf id
|
|
177 |
#
|
|
178 |
# @NC{attrName}[] names of global attrs
|
|
179 |
# %NC{AttrType}{$aName} types of global attrs
|
|
180 |
# %NC{AttrLen}{$aName} # of elts in global attrs
|
|
181 |
# %NC{Attr}{$aName} vals of scalar global attrs
|
|
182 |
#
|
|
183 |
# $NC{unlim_dimId} dim id of unlimited dim
|
|
184 |
# @NC{dimName}[$dimId] dim names
|
|
185 |
# %NC{dimID}{$dName} dim ids
|
|
186 |
# %NC{dimLen}{$dName} # elts in dim
|
|
187 |
#
|
|
188 |
# @NC{varName}[$varId] var names
|
|
189 |
# %NC{varType}{$vName} var types
|
|
190 |
# %NC{varId}{$vName} var ids
|
|
191 |
# @%NC{varDimIDs}{$vName}[] dims of vars, e.g. u(lon,lat)
|
|
192 |
# @%NC{varAttrName}{$vName}[] names of var attrs
|
|
193 |
# %%NC{varAttrType}{$vName}{$aName} types of var attrs
|
|
194 |
# %%NC{varAttrLen}{$vName}{$aName} # of elts in var attrs
|
|
195 |
# %%NC{varAttr}{$vName}{$aName} vals of scalar var attrs
|
|
196 |
#
|
|
197 |
#----------------------------------------------------------------------
|
|
198 |
|
|
199 |
sub NC_readMData($)
|
|
200 |
{
|
|
201 |
my($fn) = @_;
|
|
202 |
my(%NC);
|
|
203 |
|
47
|
204 |
$NC{id} = NetCDF::ncopen($ARGV[0],NetCDF::NOWRITE); # open
|
0
|
205 |
|
|
206 |
my($nd,$nv,$nga,$udi); # get nelts
|
47
|
207 |
NetCDF::ncinquire($NC{id},$nd,$nv,$nga,$udi);
|
0
|
208 |
$NC{unlim_dimId} = $udi;
|
|
209 |
|
|
210 |
for (my($d)=0; $d<$nd; $d++) { # dimensions
|
|
211 |
my($dnm,$ln);
|
47
|
212 |
NetCDF::ncdiminq($NC{id},$d,$dnm,$ln);
|
0
|
213 |
$NC{dimName}[$d] = $dnm;
|
|
214 |
$NC{dimId}{$dnm} = $d;
|
|
215 |
$NC{dimLen}{$dnm} = $ln;
|
|
216 |
}
|
|
217 |
|
|
218 |
for (my($v)=0; $v<$nv; $v++) { # vars & var-attribs
|
|
219 |
my($vnm,$vtp,$nvd,$nva);
|
|
220 |
my(@dids) = ();
|
47
|
221 |
NetCDF::ncvarinq($NC{id},$v,$vnm,$vtp,$nvd,\@dids,$nva);
|
0
|
222 |
$NC{varName}[$v] = $vnm;
|
|
223 |
$NC{varId}{$vnm} = $v;
|
|
224 |
$NC{varType}{$vnm} = $vtp;
|
|
225 |
@{$NC{varDimIds}{$vnm}} = @dids[0..$nvd-1];
|
|
226 |
|
|
227 |
for (my($a)=0; $a<$nva; $a++) { # var-attribs
|
|
228 |
my($anm,$atp,$aln);
|
47
|
229 |
NetCDF::ncattname($NC{id},$v,$a,$anm);
|
0
|
230 |
$NC{varAttrName}{$vnm}[$a] = $anm;
|
47
|
231 |
NetCDF::ncattinq($NC{id},$v,$anm,$atp,$aln);
|
0
|
232 |
$NC{varAttrType}{$vnm}{$anm} = $atp;
|
|
233 |
$NC{varAttrLen}{$vnm}{$anm} = $aln;
|
|
234 |
if ($atp == NetCDF::BYTE || $atp == NetCDF::CHAR || $aln == 1) {
|
|
235 |
my($val) = "";
|
47
|
236 |
NetCDF::ncattget($NC{id},$v,$anm,\$val);
|
0
|
237 |
$val =~ s{\0+$}{} if ($atp == NetCDF::CHAR); # trailing \0
|
|
238 |
$NC{varAttr}{$vnm}{$anm} = $val;
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
for (my($a)=0; $a<$nga; $a++) { # global attribs
|
|
244 |
my($anm,$atp,$aln);
|
47
|
245 |
NetCDF::ncattname($NC{id},NetCDF::GLOBAL,$a,$anm);
|
0
|
246 |
$NC{attrName}[$a] = $anm;
|
47
|
247 |
NetCDF::ncattinq($NC{id},NetCDF::GLOBAL,$anm,$atp,$aln);
|
0
|
248 |
$NC{attrType}{$anm} = $atp;
|
|
249 |
$NC{attrLen}{$anm} = $aln;
|
|
250 |
if ($atp == NetCDF::BYTE || $atp == NetCDF::CHAR || $aln == 1) {
|
|
251 |
my($val) = "";
|
47
|
252 |
NetCDF::ncattget($NC{id},NetCDF::GLOBAL,$anm,\$val);
|
0
|
253 |
$val =~ s{\0+$}{} if ($atp == NetCDF::CHAR);
|
|
254 |
$NC{attr}{$anm} = $val;
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
|
258 |
return %NC;
|
|
259 |
}
|
|
260 |
|
|
261 |
#----------------------------------------------------------------------
|
|
262 |
# create new nc file and write metadata
|
|
263 |
#
|
|
264 |
# INPUT:
|
|
265 |
# <filename>
|
|
266 |
# <abscissa> name of unlimited dimension
|
|
267 |
# <suppress-params> if true, don't write %PARAMs
|
|
268 |
#
|
|
269 |
# OUTPUT:
|
|
270 |
# <netcdf id>
|
|
271 |
#
|
|
272 |
# NOTES:
|
|
273 |
# - netcdf types can be set with %<var>:NC_type to
|
|
274 |
# byte, long, short, double
|
|
275 |
# - string types are as in old PASCAL convention (e.g. string80)
|
|
276 |
# - default type is NetCDF::DOUBLE
|
|
277 |
# - %<var>:NC_type are not added to ATTRIBs
|
|
278 |
#----------------------------------------------------------------------
|
|
279 |
|
|
280 |
sub NC_writeMData($$$)
|
|
281 |
{
|
|
282 |
my($fn,$abscissa,$suppress_params) = @_;
|
|
283 |
my(%attrDone,@slDim,@NCtype);
|
|
284 |
|
47
|
285 |
my($ncId) = NetCDF::nccreate($fn,NetCDF::CLOBBER);
|
|
286 |
# NetCDF::ncsetfill($ncId,NetCDF::NOFILL); # NetCDF library bug
|
0
|
287 |
|
|
288 |
# DIMENSIONS
|
47
|
289 |
my($aid) = NetCDF::ncdimdef($ncId,$abscissa . '_coordinate',NetCDF::UNLIMITED);
|
0
|
290 |
|
|
291 |
for (my($f)=0; $f<=$#antsLayout; $f++) { # types
|
|
292 |
my($tpa) = $antsLayout[$f] . ':NC_type';
|
|
293 |
my($sl) = ($P{$tpa} =~ m{^string(\d+)$});
|
|
294 |
if ($sl > 0) { # string
|
47
|
295 |
$slDim[$f] = NetCDF::ncdimdef($ncId,"$antsLayout[$f]:strlen",$sl);
|
0
|
296 |
$NCtype[$f] = NetCDF::CHAR;
|
|
297 |
} elsif (defined($P{$tpa})) { # custom
|
|
298 |
$NCtype[$f] = NC_type($P{$tpa});
|
|
299 |
} else { # default
|
|
300 |
$NCtype[$f] = NetCDF::DOUBLE;
|
|
301 |
}
|
|
302 |
# printf(STDERR "type %s set to %s\n",$antsLayout[$f],NC_typeName($NCtype[$f]));
|
|
303 |
undef($P{$tpa}); # do not add to ATTRIBs
|
|
304 |
}
|
|
305 |
|
|
306 |
for (my($f)=0; $f<=$#antsLayout; $f++) { # VARIABLES
|
|
307 |
my($vid);
|
|
308 |
if (defined($slDim[$f])) {
|
47
|
309 |
$vid = NetCDF::ncvardef($ncId,$antsLayout[$f],$NCtype[$f],[$aid,$slDim[$f]]);
|
0
|
310 |
} else {
|
47
|
311 |
$vid = NetCDF::ncvardef($ncId,$antsLayout[$f],$NCtype[$f],[$aid]);
|
0
|
312 |
}
|
|
313 |
croak("$0: varid != fnr (implementation restriction)")
|
|
314 |
unless ($vid == $f);
|
47
|
315 |
foreach my $anm (keys(%P)) { # VARIABLE ATTRIBUTES
|
0
|
316 |
next unless defined($P{$anm});
|
|
317 |
my($var,$attr) = ($anm =~ m{([^:]+):(.*)});
|
|
318 |
next unless ($var eq $antsLayout[$f]);
|
|
319 |
$attrDone{$anm} = 1; # mark
|
|
320 |
if (numberp($P{$anm}) || lc($P{$anm}) eq nan) {
|
47
|
321 |
NetCDF::ncattput($ncId,$f,$attr,NetCDF::DOUBLE,$P{$anm});
|
0
|
322 |
} else {
|
47
|
323 |
NetCDF::ncattput($ncId,$f,$attr,NetCDF::CHAR,$P{$anm});
|
0
|
324 |
}
|
|
325 |
}
|
|
326 |
}
|
|
327 |
|
|
328 |
unless ($suppress_params) {
|
|
329 |
foreach my $anm (keys(%P)) { # GLOBAL ATTRIBUTES
|
|
330 |
next unless defined($P{$anm});
|
|
331 |
next if ($anm eq 'FILENAME' || $anm eq 'DIRNAME' || # skip pseudo
|
|
332 |
$anm eq 'BASENAME' || $anm eq 'EXTN' ||
|
25
|
333 |
$anm eq 'PATHNAME' || $anm eq 'DEPS' ||
|
0
|
334 |
$anm eq 'RECNO' || $anm eq 'LINENO');
|
|
335 |
next if $attrDone{$anm};
|
|
336 |
if (numberp($P{$anm}) || lc($P{$anm}) eq nan) {
|
47
|
337 |
NetCDF::ncattput($ncId,NetCDF::GLOBAL,$anm,NetCDF::DOUBLE,$P{$anm});
|
0
|
338 |
} else {
|
47
|
339 |
NetCDF::ncattput($ncId,NetCDF::GLOBAL,$anm,NetCDF::CHAR,$P{$anm});
|
0
|
340 |
}
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
47
|
344 |
NetCDF::ncendef($ncId);
|
|
345 |
|
|
346 |
return $ncId;
|
|
347 |
}
|
|
348 |
|
|
349 |
#----------------------------------------------------------------------
|
|
350 |
# create new nc "multi" file and write metadata
|
|
351 |
#
|
|
352 |
# INPUT:
|
|
353 |
# <filename>
|
|
354 |
# <abscissa> name of unlimited dimension
|
|
355 |
# <suppress-params> flag to suppress params
|
|
356 |
# <file-id> name of dimension enumerating input files (e.g. profile_number, cruise_id, etc.)
|
|
357 |
# <n-files> number of 1-D files to encode in multi file
|
|
358 |
#
|
|
359 |
# OUTPUT:
|
|
360 |
# <netcdf id>
|
|
361 |
# %nc_vid netcdf variable ids for %PARAMs
|
|
362 |
# %nc_varAttr flags for variable (not global) attributes
|
|
363 |
#
|
|
364 |
# NOTES:
|
|
365 |
# - netcdf types can be set with %<var|param>:NC_type to
|
|
366 |
# byte, chat, long, short, double, float (NC_type())
|
|
367 |
# - don't use same-named var and param with different types...
|
|
368 |
# - string types are as in old PASCAL convention (e.g. string80)
|
|
369 |
# - default type is NetCDF::DOUBLE; other types must be set
|
|
370 |
# explicitly
|
|
371 |
# - %<var>:NC_type are not added to ATTRIBs
|
|
372 |
#----------------------------------------------------------------------
|
|
373 |
|
|
374 |
sub NC_writeMDataMulti($$$$)
|
|
375 |
{
|
|
376 |
my($fn,$abscissa,$suppress_params,$file_id,$n_files) = @_;
|
|
377 |
my(@slDim,@NCtype);
|
|
378 |
|
|
379 |
my($ncId) = NetCDF::nccreate($fn,NetCDF::CLOBBER);
|
|
380 |
# DIMENSIONS
|
|
381 |
my($aid) = NetCDF::ncdimdef($ncId,$abscissa . '_coordinate',NetCDF::UNLIMITED);
|
|
382 |
my($mid) = NetCDF::ncdimdef($ncId,$file_id,$n_files);
|
|
383 |
|
|
384 |
for (my($f)=0; $f<=$#antsLayout; $f++) { # TYPES
|
|
385 |
my($tpa) = $antsLayout[$f] . ':NC_type'; # defined explicitly
|
|
386 |
my($sl) = ($P{$tpa} =~ m{^string(\d+)$});
|
|
387 |
if ($sl > 0) { # string
|
|
388 |
$slDim[$f] = NetCDF::ncdimdef($ncId,"$antsLayout[$f]:strlen",$sl);
|
|
389 |
$NCtype[$f] = NetCDF::CHAR;
|
|
390 |
} elsif (defined($P{$tpa})) { # other custom type
|
|
391 |
$NCtype[$f] = NC_type($P{$tpa});
|
|
392 |
} else { # default type
|
|
393 |
$NCtype[$f] = NetCDF::DOUBLE;
|
|
394 |
}
|
|
395 |
# printf(STDERR "type %s set to %s\n",$antsLayout[$f],NC_typeName($NCtype[$f]));
|
|
396 |
undef($P{$tpa}); # do not add to ATTRIBs
|
|
397 |
}
|
|
398 |
|
|
399 |
for (my($f)=0; $f<=$#antsLayout; $f++) { # VARIABLES
|
|
400 |
my($vid);
|
|
401 |
if (defined($slDim[$f])) {
|
|
402 |
$vid = NetCDF::ncvardef($ncId,$antsLayout[$f],$NCtype[$f],[$aid,$mid,$slDim[$f]]);
|
|
403 |
} else {
|
|
404 |
$vid = NetCDF::ncvardef($ncId,$antsLayout[$f],$NCtype[$f],[$aid,$mid]);
|
|
405 |
}
|
|
406 |
croak("$0: varid != fnr (implementation restriction)")
|
|
407 |
unless ($vid == $f);
|
|
408 |
foreach my $anm (keys(%P)) { # VARIABLE ATTRIBUTES
|
|
409 |
next unless defined($P{$anm});
|
|
410 |
my($var,$attr) = ($anm =~ m{([^:]+):(.*)});
|
|
411 |
next unless ($var eq $antsLayout[$f]);
|
|
412 |
$nc_varAttr{$anm} = 1; # mark as done
|
|
413 |
if (numberp($P{$anm}) || lc($P{$anm}) eq nan) {
|
|
414 |
NetCDF::ncattput($ncId,$f,$attr,NetCDF::DOUBLE,$P{$anm});
|
|
415 |
} else {
|
|
416 |
NetCDF::ncattput($ncId,$f,$attr,NetCDF::CHAR,$P{$anm});
|
|
417 |
}
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
unless ($suppress_params) { # PARAM vectors
|
|
422 |
foreach my $anm (keys(%P)) {
|
|
423 |
next if ($anm eq 'FILENAME' || $anm eq 'DIRNAME' || # skip pseudo %PARAMs
|
|
424 |
$anm eq 'BASENAME' || $anm eq 'EXTN' ||
|
|
425 |
$anm eq 'PATHNAME' || $anm eq 'DEPS' ||
|
|
426 |
$anm eq 'RECNO' || $anm eq 'LINENO');
|
|
427 |
next if $nc_varAttr{$anm}; # variable attribute (done already)
|
|
428 |
next if ($anm =~ /:NC_type$/); # this is needed to remove the :NC_type params!?
|
|
429 |
|
|
430 |
my($tpa) = $anm . ':NC_type'; # defined explicitly
|
|
431 |
my($sl) = ($P{$tpa} =~ m{^string(\d+)$});
|
|
432 |
if ($sl > 0) { # string
|
|
433 |
$slDim{$anm} = NetCDF::ncdimdef($ncId,"$anm:strlen",$sl);
|
|
434 |
$NCtype{$anm} = NetCDF::CHAR;
|
|
435 |
} elsif (defined($P{$tpa})) { # other custom type
|
|
436 |
$NCtype{$anm} = NC_type($P{$tpa});
|
|
437 |
} else {
|
|
438 |
$NCtype{$anm} = NetCDF::DOUBLE; # not a NC type %PARAM
|
|
439 |
}
|
|
440 |
if (defined($slDim{$anm})) { # string type
|
|
441 |
$nc_vid{$anm} = NetCDF::ncvardef($ncId,$anm,$NCtype{$anm},[$mid,$slDim{$anm}]);
|
|
442 |
} else { # other type
|
|
443 |
$nc_vid{$anm} = NetCDF::ncvardef($ncId,$anm,$NCtype{$anm},[$mid]);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
}
|
|
447 |
|
|
448 |
NetCDF::ncendef($ncId);
|
0
|
449 |
|
|
450 |
return $ncId;
|
|
451 |
}
|
|
452 |
|
|
453 |
1;
|