1 public static class NetCDF
2 {
3 [DllImport("netcdf4.dll")]
4 public static extern int nc_put_att_uchar(int ncid, int varid, string name, NcType xtype, int len, byte[] op);
5 [DllImport("netcdf4.dll")]
6 public static extern int nc_get_att_uchar(int ncid, int varid, string name, byte[] op);
7
8 [DllImport("netcdf4.dll")]
9 public static extern int nc_get_var_uchar(int ncid, int varid, byte[] ip);
10 [DllImport("netcdf4.dll")]
11 public static extern int nc_get_var_text(int ncid, int varid, StringBuilder ip);
12
13
14 [DllImport("netcdf4.dll")]
15 public static extern int nc_open(string path, CreateMode mode, out int ncidp);
16 [DllImport("netcdf4.dll")]
17 public static extern int nc_create(string path, CreateMode mode, out int ncidp);
18 [DllImport("netcdf4.dll")]
19 public static extern int nc_close(int ncidp);
20 [DllImport("netcdf4.dll")]
21 public static extern int nc_sync(int ncid);
22 [DllImport("netcdf4.dll")]
23 public static extern int nc_enddef(int ncid);
24 [DllImport("netcdf4.dll")]
25 public static extern int nc_redef(int ncid);
26 [DllImport("netcdf4.dll")]
27 public static extern string nc_strerror(int ncerror);
28
29 [DllImport("netcdf4.dll")]
30 public static extern int nc_inq(int ncid, out int ndims, out int nvars, out int ngatts, out int unlimdimid);
31
32 [DllImport("netcdf4.dll")]
33 public static extern int nc_def_var(int ncid, string name, NcType xtype, int ndims, int[] dimids, out int varidp);
34 [DllImport("netcdf4.dll")]
35 public static extern int nc_inq_var(int ncid, int varid, StringBuilder name, out NcType type, out int ndims, int[] dimids, out int natts);
36 [DllImport("netcdf4.dll")]
37 public static extern int nc_inq_varids(int ncid, out int nvars, int[] varids);
38 [DllImport("netcdf4.dll")]
39 public static extern int nc_inq_vartype(int ncid, int varid, out NcType xtypep);
40 [DllImport("netcdf4.dll")]
41 public static extern int nc_inq_varnatts(int ncid, int varid, out int nattsp);
42 [DllImport("netcdf4.dll")]
43 public static extern int nc_inq_varid(int ncid, string name, out int varidp);
44
45 [DllImport("netcdf4.dll")]
46 public static extern int nc_inq_ndims(int ncid, out int ndims);
47 [DllImport("netcdf4.dll")]
48 public static extern int nc_inq_nvars(int ncid, out int nvars);
49 [DllImport("netcdf4.dll")]
50 public static extern int nc_inq_varname(int ncid, int varid, StringBuilder name);
51 [DllImport("netcdf4.dll")]
52 public static extern int nc_inq_varndims(int ncid, int varid, out int ndims);
53 [DllImport("netcdf4.dll")]
54 public static extern int nc_inq_vardimid(int ncid, int varid, int[] dimids);
55 [DllImport("netcdf4.dll")]
56 public static extern int nc_inq_var_fill(int ncid, int varid, out int no_fill, out object fill_value);
57
58
59 [DllImport("netcdf4.dll")]
60 public static extern int nc_inq_natts(int ncid, out int ngatts);
61 [DllImport("netcdf4.dll")]
62 public static extern int nc_inq_unlimdim(int ncid, out int unlimdimid);
63 [DllImport("netcdf4.dll")]
64 public static extern int nc_inq_format(int ncid, out int format);
65
66 [DllImport("netcdf4.dll")]
67 public static extern int nc_inq_attname(int ncid, int varid, int attnum, StringBuilder name);
68 [DllImport("netcdf4.dll")]
69 public static extern int nc_inq_att(int ncid, int varid, string name, out NcType type, out int length);
70 [DllImport("netcdf4.dll")]
71 public static extern int nc_get_att_text(int ncid, int varid, string name, StringBuilder value);
72 [DllImport("netcdf4.dll")]
73 public static extern int nc_get_att_schar(int ncid, int varid, string name, sbyte[] data);
74 [DllImport("netcdf4.dll")]
75 public static extern int nc_get_att_short(int ncid, int varid, string name, short[] data);
76 [DllImport("netcdf4.dll")]
77 public static extern int nc_get_att_int(int ncid, int varid, string name, int[] data);
78 [DllImport("netcdf4.dll")]
79 public static extern int nc_get_att_float(int ncid, int varid, string name, float[] data);
80 [DllImport("netcdf4.dll")]
81 public static extern int nc_get_att_double(int ncid, int varid, string name, double[] data);
82 [DllImport("netcdf4.dll")]
83 public static extern int nc_get_att_long(int ncid, int varid, string name, long[] data);
84 [DllImport("netcdf4.dll")]
85 public static extern int nc_get_att_longlong(int ncid, int varid, string name, long[] data);
86
87 [DllImport("netcdf4.dll")]
88 public static extern int nc_put_att_text(int ncid, int varid, string name, int len, string tp);
89 [DllImport("netcdf4.dll")]
90 public static extern int nc_put_att_double(int ncid, int varid, string name, NcType type, int len, double[] tp);
91 [DllImport("netcdf4.dll")]
92 public static extern int nc_put_att_int(int ncid, int varid, string name, NcType type, int len, int[] tp);
93 [DllImport("netcdf4.dll")]
94 public static extern int nc_put_att_short(int ncid, int varid, string name, NcType type, int len, short[] tp);
95 [DllImport("netcdf4.dll")]
96 public static extern int nc_put_att_float(int ncid, int varid, string name, NcType type, int len, float[] tp);
97 [DllImport("netcdf4.dll")]
98 public static extern int nc_put_att_byte(int ncid, int varid, string name, NcType type, int len, sbyte[] tp);
99 [DllImport("netcdf4.dll")]
100 public static extern int nc_put_att_long(int ncid, int varid, string name, NcType type, int len, long[] tp);
101 [DllImport("netcdf4.dll")]
102 public static extern int nc_put_att_longlong(int ncid, int varid, string name, NcType type, int len, long[] tp);
103
104 [DllImport("netcdf4.dll")]
105 public static extern int nc_def_dim(int ncid, string name, int len, out int dimidp);
106 [DllImport("netcdf4.dll")]
107 public static extern int nc_inq_dim(int ncid, int dimid, StringBuilder name, out int length);
108 [DllImport("netcdf4.dll")]
109 public static extern int nc_inq_dimname(int ncid, int dimid, StringBuilder name);
110 [DllImport("netcdf4.dll")]
111 public static extern int nc_inq_dimid(int ncid, string name, out int dimid);
112 [DllImport("netcdf4.dll")]
113 public static extern int nc_inq_dimlen(int ncid, int dimid, out int length);
114
115
116 [DllImport("netcdf4.dll")]
117 public static extern int nc_get_var_text(int ncid, int varid, byte[] data);
118 [DllImport("netcdf4.dll")]
119 public static extern int nc_get_var_schar(int ncid, int varid, sbyte[] data);
120 [DllImport("netcdf4.dll")]
121 public static extern int nc_get_var_short(int ncid, int varid, short[] data);
122 [DllImport("netcdf4.dll")]
123 public static extern int nc_get_var_int(int ncid, int varid, int[] data);
124 [DllImport("netcdf4.dll")]
125 public static extern int nc_get_var_long(int ncid, int varid, long[] data);
126 [DllImport("netcdf4.dll")]
127 public static extern int nc_get_var_float(int ncid, int varid, float[,] data);
128 [DllImport("netcdf4.dll")]
129 public static extern int nc_get_var_double(int ncid, int varid, double[] data);
130
131 [DllImport("netcdf4.dll")]
132 public static extern int nc_put_var_ubyte(int ncid, int varid, byte[,] data);
133 [DllImport("netcdf4.dll")]
134 public static extern int nc_put_var_int(int ncid, int varid, int[,] data);
135 [DllImport("netcdf4.dll")]
136 public static extern int nc_put_var_text(int ncid, int varid, string op);
137 [DllImport("netcdf4.dll")]
138 public static extern int nc_put_var_uchar(int ncid, int varid, out byte[] op);
139 [DllImport("netcdf4.dll")]
140 public static extern int nc_put_var_float(int ncid, int varid, float[,] data);
141 [DllImport("netcdf4.dll")]
142 public static extern int nc_put_var_long(int ncid, int varid, long[] data);
143
144
145 [DllImport("netcdf4.dll")]
146 public static extern int nc_put_vara_double(int ncid, int varid, int[] start, int[] count, double[] dp);
147 [DllImport("netcdf4.dll")]
148 public static extern int nc_put_vara_float(int ncid, int varid, int[] start, int[] count, float[] fp);
149 [DllImport("netcdf4.dll")]
150 public static extern int nc_put_vara_short(int ncid, int varid, int[] start, int[] count, short[] sp);
151 [DllImport("netcdf4.dll")]
152 public static extern int nc_put_vara_int(int ncid, int varid, int[] start, int[] count, int[] ip);
153 [DllImport("netcdf4.dll")]
154 public static extern int nc_put_vara_long(int ncid, int varid, int[] start, int[] count, long[] lp);
155 [DllImport("netcdf4.dll")]
156 public static extern int nc_put_vara_ubyte(int ncid, int varid, int[] start, int[] count, byte[] bp);
157 [DllImport("netcdf4.dll")]
158 public static extern int nc_put_vara_schar(int ncid, int varid, int[] start, int[] count, sbyte[] cp);
159 [DllImport("netcdf4.dll")]
160 public static extern int nc_put_vara_string(int ncid, int varid, int[] start, int[] count, string[] sp);
161
162
163 [DllImport("netcdf4.dll")]
164 public static extern int nc_get_vara_text(int ncid, int varid, int[] start, int[] count, byte[] data);
165 [DllImport("netcdf4.dll")]
166 public static extern int nc_get_vara_schar(int ncid, int varid, int[] start, int[] count, sbyte[] data);
167 [DllImport("netcdf4.dll")]
168 public static extern int nc_get_vara_short(int ncid, int varid, int[] start, int[] count, short[] data);
169 [DllImport("netcdf4.dll")]
170 public static extern int nc_get_vara_ubyte(int ncid, int varid, int[] start, int[] count, byte[] data);
171 [DllImport("netcdf4.dll")]
172 public static extern int nc_get_vara_long(int ncid, int varid, int[] start, int[] count, long[] data);
173 [DllImport("netcdf4.dll")]
174 public static extern int nc_get_vara_int(int ncid, int varid, int[] start, int[] count, int[] data);
175 [DllImport("netcdf4.dll")]
176 public static extern int nc_get_vara_float(int ncid, int varid, int[] start, int[] count, float[] data);
177 [DllImport("netcdf4.dll")]
178 public static extern int nc_get_vara_double(int ncid, int varid, int[] start, int[] count, double[] data);
179 [DllImport("netcdf4.dll")]
180 public static extern int nc_get_vara_string(int ncid, int varid, int[] start, int[] count, string[] data);
181
182 ///<summary>
183 ///'size' argument to ncdimdef for an unlimited dimension
184 ///</summary>
185 public const int NC_UNLIMITED = 0;
186
187 ///<summary>
188 ///attribute id to put/get a global attribute
189 ///</summary>
190 public const int NC_GLOBAL = -1;
191
192 ///<summary>
193 ///The netcdf external data types
194 ///</summary>
195 public enum NcType : int
196 {
197 ///<summary>signed 1 byte intege</summary>
198 NC_BYTE = 1,
199 ///<summary>ISO/ASCII character</summary>
200 NC_CHAR = 2,
201 ///<summary>signed 2 byte integer</summary>
202 NC_SHORT = 3,
203 ///<summary>signed 4 byte integer</summary>
204 NC_INT = 4,
205 ///<summary>single precision floating point number</summary>
206 NC_FLOAT = 5,
207 ///<summary>double precision floating point number</summary>
208 NC_DOUBLE = 6,
209 ///<summary>signed 8-byte int</summary>
210 NC_INT64 = 10,
211 ///<summary>string</summary>
212 NC_STRING = 12
213 }
214
215 public static Type GetCLRType(NcType ncType)
216 {
217 switch (ncType)
218 {
219 case NcType.NC_BYTE:
220 return typeof(byte);
221 case NcType.NC_CHAR:
222 return typeof(sbyte);
223 case NcType.NC_SHORT:
224 return typeof(short);
225 case NcType.NC_INT:
226 return typeof(int);
227 case NcType.NC_INT64:
228 return typeof(long);
229 case NcType.NC_FLOAT:
230 return typeof(float);
231 case NcType.NC_DOUBLE:
232 return typeof(double);
233 case NcType.NC_STRING:
234 return typeof(string);
235 default:
236 throw new ApplicationException("Unknown nc type");
237 }
238 }
239
240 public static NcType GetNcType(Type type)
241 {
242 switch (Type.GetTypeCode(type))
243 {
244 case TypeCode.Double:
245 return NcType.NC_DOUBLE;
246
247 case TypeCode.Single:
248 return NcType.NC_FLOAT;
249
250 case TypeCode.Int64:
251 return NcType.NC_INT64;
252
253 case TypeCode.Int32:
254 return NcType.NC_INT;
255
256 case TypeCode.Int16:
257 return NcType.NC_SHORT;
258
259 case TypeCode.Byte:
260 return NcType.NC_BYTE;
261
262 case TypeCode.SByte:
263 return NcType.NC_CHAR;
264
265 case TypeCode.String:
266 return NcType.NC_STRING;
267
268 case TypeCode.DateTime:
269 return NcType.NC_INT64;
270
271
272 default:
273 throw new NotSupportedException("Not supported type of data.");
274 }
275 }
276
277 public enum CreateMode : int
278 {
279 NC_NOWRITE = 0,
280 ///<summary>read & write</summary>
281 NC_WRITE = 0x0001,
282 NC_CLOBBER = 0,
283 ///<summary>Don't destroy existing file on create</summary>
284 NC_NOCLOBBER = 0x0004,
285 ///<summary>argument to ncsetfill to clear NC_NOFILL</summary>
286 NC_FILL = 0,
287 ///<summary>Don't fill data section an records</summary>
288 NC_NOFILL = 0x0100,
289 ///<summary>Use locking if available</summary>
290 NC_LOCK = 0x0400,
291 ///<summary>Share updates, limit cacheing</summary>
292 NC_SHARE = 0x0800,
293 NC_64BIT_OFFSET = 0x0200,
294 ///<summary>Enforce strict netcdf-3 rules</summary>
295 NC_CLASSIC = 0x0100,
296 ///<summary>causes netCDF to create a HDF5/NetCDF-4 file</summary>
297 NC_NETCDF4 = 0x1000
298 }
299
300 public enum ResultCode : int
301 {
302 ///<summary>No Error</summary>
303 NC_NOERR = 0,
304 ///<summary>Invalid dimension id or name</summary>
305 NC_EBADDIM = -46,
306 ///<summary>Attribute not found</summary>
307 NC_ENOTATT = -43,
308 }
309
310 ///<summary>
311 /// Default fill values, used unless _FillValue attribute is set.
312 ///These values are stuffed into newly allocated space as appropriate.
313 ///The hope is that one might use these to notice that a particular datum
314 ///has not been set.
315 ///</summary>
316 public static class FillValues
317 {
318 public const byte NC_FILL_BYTE = 255;
319 public const char NC_FILL_CHAR = (char)0;
320 public const short NC_FILL_SHORT = -32767;
321 public const int NC_FILL_INT = -2147483647;
322 public const float NC_FILL_FLOAT = 9.96921E+36f; /* near 15 * 2^119 */
323 public const double NC_FILL_DOUBLE = 9.969209968386869E+36;
324 }
325
326
327 ///<summary>These maximums are enforced by the interface, to facilitate writing
328 ///applications and utilities. However, nothing is statically allocated to
329 ///these sizes internally.</summary>
330 public enum Limits
331 {
332 ///<summary>max dimensions per file </summary>
333 NC_MAX_DIMS = 10,
334 ///<summary>max global or per variable attributes </summary>
335 NC_MAX_ATTRS = 2000,
336 ///<summary>max variables per file</summary>
337 NC_MAX_VARS = 2000,
338 ///<summary>max length of a name </summary>
339 NC_MAX_NAME = 128,
340 ///<summary>max per variable dimensions </summary>
341 NC_MAX_VAR_DIMS = 10
342 }
343 }
