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