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 }
View Code
1 /// <summary> 2 /// 二維網格數據文件創建、讀、寫 3 /// </summary> 4 class TwoDimDB : IDataBase 5 { 6 public TwoDimDB(string filename) 7 { 8 dbfile = filename; 9 } 10 11 public override bool Create(string[] varname, string xdimname, string ydimname, long[] xdim, long[] ydim) 12 { 13 try 14 { 15 int ndims = 2; 16 int ncid, res, varid; 17 int x_dimid, y_dimid; 18 int[] dimids = new int[ndims]; 19 20 //創建文件 21 res = NetCDF.nc_create(dbfile, NetCDF.CreateMode.NC_NETCDF4, out ncid); 22 if (res != 0) return false; 23 24 int NX = xdim.Length; 25 int NY = ydim.Length; 26 int[,] datas = new int[NX, NY]; 27 28 //定義維度 29 res = NetCDF.nc_def_dim(ncid, xdimname, NX, out x_dimid); 30 if (res != 0) return false; 31 res = NetCDF.nc_def_dim(ncid, ydimname, NY, out y_dimid); 32 if (res != 0) return false; 33 34 res = NetCDF.nc_put_att_longlong(ncid, NetCDF.NC_GLOBAL, xdimname, NetCDF.NcType.NC_INT64, xdim.Length, xdim); 35 if (res != 0) return false; 36 res = NetCDF.nc_put_att_longlong(ncid, NetCDF.NC_GLOBAL, ydimname, NetCDF.NcType.NC_INT64, ydim.Length, ydim); 37 if (res != 0) return false; 38 39 dimids[0] = x_dimid; 40 dimids[1] = y_dimid; 41 42 //定義變量 43 if (varname != null) 44 { 45 foreach (var vn in varname) 46 { 47 res = NetCDF.nc_def_var(ncid, vn, NetCDF.NcType.NC_FLOAT, 2, dimids, out varid); 48 if (res != 0) continue; 49 50 res = NetCDF.nc_enddef(ncid); 51 if (res != 0) continue; 52 53 NetCDF.nc_put_var_int(ncid, varid, datas); 54 if (res != 0) continue; 55 } 56 } 57 58 //關閉文件 59 res = NetCDF.nc_close(ncid); 60 if (res == 0) return true; 61 } 62 catch (Exception ex) 63 { 64 65 } 66 return false; 67 } 68 69 public override bool Write(string[] varName, string dimName, long dimValue, int[][] value, bool isXdim = false) 70 { 71 try 72 { 73 int ncid, res, varid; 74 75 //創建文件 76 res = NetCDF.nc_open(dbfile, NetCDF.CreateMode.NC_WRITE, out ncid); 77 if (res != 0) return false; 78 79 #region GetDimIndex 80 81 int dimIdx = 0; 82 NetCDF.NcType dimXTtype; 83 int dimAttLen = 0; 84 85 res = NetCDF.nc_inq_att(ncid, NetCDF.NC_GLOBAL, dimName, out dimXTtype, out dimAttLen); 86 if (res != 0) return false; 87 long[] xdimValue = new long[dimAttLen]; 88 res = NetCDF.nc_get_att_longlong(ncid, NetCDF.NC_GLOBAL, dimName, xdimValue); 89 dimIdx = Array.IndexOf(xdimValue, dimValue); 90 if (dimIdx == -1) dimIdx = 0; 91 92 #endregion 93 94 int xdimBeginIndex = isXdim ? dimIdx : 0; 95 int ydimBeginIndex = !isXdim ? dimIdx : 0; 96 97 int xdimNumber = !isXdim ? value.Length : 1; 98 int ydimNumber = isXdim ? value.Length : 1; 99 100 //定義變量 101 if (varName != null) 102 { 103 for (int i = 0; i < varName.Length; i++) 104 { 105 res = NetCDF.nc_inq_varid(ncid, varName[i], out varid); 106 if (res != 0) continue; 107 108 var origin = new int[] { xdimBeginIndex, ydimBeginIndex };//第一維的1開始,第二維從0開始 109 var size = new int[] { xdimNumber, ydimNumber };//數量分別為1,2 110 111 NetCDF.nc_put_vara_int(ncid, varid, origin, size, value[i]); 112 if (res != 0) return false; 113 } 114 } 115 116 //關閉文件 117 res = NetCDF.nc_close(ncid); 118 if (res == 0) return true; 119 } 120 catch (Exception ex) 121 { 122 123 } 124 return false; 125 } 126 127 public override int[][] Read(string[] varname, string xdimname, string ydimname, long? xdimValue = null, long? ydimValue = null) 128 { 129 long[] xDimValue; 130 long[] yDimValue; 131 Dictionary<long, int[][]> dic = new Dictionary<long, int[][]>(); 132 try 133 { 134 int ndims = 2; 135 int ncid, res, varid; 136 int[] dimids = new int[ndims]; 137 138 //打開文件 139 res = NetCDF.nc_open(dbfile, NetCDF.CreateMode.NC_NOWRITE, out ncid); 140 if (res != 0) return null; 141 142 #region XDimIndex 143 144 int xDimIdx = 0; 145 NetCDF.NcType xDimxtype; 146 int xDimAttlen = 0; 147 148 res = NetCDF.nc_inq_att(ncid, NetCDF.NC_GLOBAL, xdimname, out xDimxtype, out xDimAttlen); 149 if (res != 0) return null; 150 xDimValue = new long[xDimAttlen]; 151 152 res = NetCDF.nc_get_att_longlong(ncid, NetCDF.NC_GLOBAL, xdimname, xDimValue); 153 if (xdimValue != null) 154 { 155 xDimIdx = Array.IndexOf(xDimValue, xdimValue); 156 if (xDimIdx == -1) xDimIdx = 0; 157 } 158 #endregion 159 160 #region YDimIndex 161 162 int yDimIdx = 0; 163 NetCDF.NcType yDimxtype; 164 int yDimAttlen = 0; 165 166 res = NetCDF.nc_inq_att(ncid, NetCDF.NC_GLOBAL, ydimname, out yDimxtype, out yDimAttlen); 167 if (res != 0) return null; 168 yDimValue = new long[yDimAttlen]; 169 res = NetCDF.nc_get_att_longlong(ncid, NetCDF.NC_GLOBAL, ydimname, yDimValue); 170 if (ydimValue != null) 171 { 172 yDimIdx = Array.IndexOf(yDimValue, ydimValue); 173 if (yDimIdx == -1) yDimIdx = 0; 174 } 175 176 #endregion 177 178 int NX = xDimValue.Length; 179 int NY = yDimValue.Length; 180 181 int xdimCount = NX - xDimIdx; 182 int ydimCount = NY - yDimIdx; 183 int resCount = xdimCount * ydimCount; 184 if (xdimValue == null) 185 { 186 resCount = xdimCount; 187 xdimCount = NX; 188 ydimCount = 1; 189 } 190 if (ydimValue == null) 191 { 192 resCount = ydimCount; 193 xdimCount = 1; 194 ydimCount = NY - yDimIdx; 195 } 196 197 int[] origin = new int[] { xDimIdx, yDimIdx };//第一維的1開始,第二維從0開始 198 int[] size = new int[] { xdimCount, ydimCount };//數量分別為1,2 199 200 int[][] result = new int[varname.Length + 1][]; 201 202 for (int i = 0; i < varname.Length; i++) 203 { 204 res = NetCDF.nc_inq_varid(ncid, varname[i], out varid); 205 if (res != 0) continue; 206 207 result[i+1] = new int[resCount]; 208 NetCDF.nc_get_vara_int(ncid, varid, origin, size, result[i + 1]); 209 if (res != 0) continue; 210 } 211 212 213 res = NetCDF.nc_close(ncid); 214 if (res == 0) return result; 215 } 216 catch (Exception ex) 217 { 218 219 } 220 finally 221 { 222 xDimValue = null; 223 yDimValue = null; 224 GC.Collect(); 225 } 226 return null; 227 } 228 }
