ruby-netcdf 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,203 @@
1
+ require "numru/netcdf"
2
+ require "narray_miss"
3
+
4
+ module NumRu
5
+
6
+ class NetCDFVar
7
+
8
+ def get_with_miss(*args)
9
+ __interpret_missing_params if !defined?(@missval)
10
+ data = simple_get(*args)
11
+ if @vmin || @vmax
12
+ if @vmin
13
+ mask = (data >= @vmin)
14
+ mask = mask.and(data <= @vmax) if @vmax
15
+ else
16
+ mask = (data <= @vmax)
17
+ end
18
+ data = NArrayMiss.to_nam(data, mask)
19
+ elsif @missval # only missing_value is present.
20
+ mask = (data.ne(@missval))
21
+ data = NArrayMiss.to_nam(data, mask)
22
+ end
23
+ data
24
+ end
25
+
26
+ def get_with_miss_and_scaling(*args)
27
+ __interpret_missing_params if !defined?(@missval)
28
+ data = simple_get(*args)
29
+ if @vmin || @vmax
30
+ if @vmin
31
+ mask = (data >= @vmin)
32
+ mask = mask.and(data <= @vmax) if @vmax
33
+ else
34
+ mask = (data <= @vmax)
35
+ end
36
+ data = NArrayMiss.to_nam(data, mask)
37
+ elsif @missval # only missing_value is present.
38
+ mask = (data.ne(@missval))
39
+ data = NArrayMiss.to_nam(data, mask)
40
+ end
41
+ data = unpack( data )
42
+ data
43
+ end
44
+
45
+ def put_with_miss(data, *args)
46
+ if data.is_a?( NArrayMiss )
47
+ __interpret_missing_params if !defined?(@missval)
48
+ if @missval
49
+ simple_put(data.to_na(@missval), *args)
50
+ else
51
+ simple_put(data.to_na, *args)
52
+ end
53
+ else
54
+ simple_put(data, *args)
55
+ end
56
+ end
57
+
58
+ def put_with_miss_and_scaling(data, *args)
59
+ if data.is_a?( NArrayMiss )
60
+ __interpret_missing_params if !defined?(@missval)
61
+ if @missval
62
+ data = pack( data )
63
+ data = data.to_na(@missval)
64
+ else
65
+ data = pack( data )
66
+ data = data.to_na
67
+ end
68
+ simple_put(data, *args)
69
+ else
70
+ scaled_put(data, *args)
71
+ end
72
+ end
73
+
74
+ ######### private ##########
75
+
76
+ def __interpret_missing_params
77
+ # Interprets the specification of missing data,
78
+ # either by valid_range, (valid_min and/or valid_max), or missing_value.
79
+ # (unlike the NetCDF User's guide (NUG), missing_value is interpreted,
80
+ # but valid_* has a higher precedence.)
81
+ # Always sets @missval whether missing_value is defined or not,
82
+ # since it will be used as a fill value for data missing.
83
+ #
84
+ @vmin = att('valid_min')
85
+ @vmin = @vmin.get if @vmin # kept in a NArray(size==1) to consv type
86
+ @vmax = att('valid_max')
87
+ @vmax = @vmax.get if @vmax # kept in a NArray(size==1) to consv type
88
+ vrange = att('valid_range')
89
+ vrange = vrange.get if vrange
90
+ if vrange
91
+ vrange.sort!
92
+ @vmin = vrange[0..0] # kept in... (same)
93
+ @vmax = vrange[-1..-1] # kept in... (same)
94
+ end
95
+ @missval = att('missing_value') || att('_FillValue')
96
+ @missval = @missval.get if @missval # kept in... (same)
97
+
98
+ sf = att('scale_factor')
99
+ ao = att('add_offset')
100
+ if ( sf || ao )
101
+ ## Both NUG & CF conventions requires to specify the valid
102
+ ## range with respect to the external (i.e. packed) values.
103
+ ## However, some conventions require specification
104
+ ## with respect to unpacked values. The following
105
+ ## is to support such cases as well:
106
+ thres_tp = [ self.typecode, NArray::LINT ].max
107
+ @missval = pack(@missval) if @missval && @missval.typecode > thres_tp
108
+ @vmin = pack(@vmin) if @vmin && @vmin.typecode > thres_tp
109
+ @vmax = pack(@vmax) if @vmax && @vmax.typecode > thres_tp
110
+ end
111
+
112
+ if @missval
113
+ if @vmin && @vmax
114
+ if @vmin[0] <= @missval[0] && @missval[0] <= @vmax[0]
115
+ warn "WARNING: missing_value #{@missval[0]} is in the valid range #{@vmin[0]}..#{@vmax[0]} --> will be ignored (#{__FILE__}:#{__LINE__})"
116
+ end
117
+ else
118
+ if @vmin && @missval[0] >= @vmin[0]
119
+ warn "WARNING: missing_value #{@missval[0]} >= valid min #{@vmin[0]} --> will be ignored (#{__FILE__}:#{__LINE__})"
120
+ elsif @vmax && @missval[0] <= @vmax[0]
121
+ warn "WARNING: missing_value #{@missval[0]} <= valid min #{@vmin[0]} --> will be ignored (#{__FILE__}:#{__LINE__})"
122
+ end
123
+ end
124
+ else
125
+ realtc = NArray::SFLOAT
126
+ if @vmin
127
+ if @vmin[0] >= 0
128
+ @missval = ( @vmin.typecode>=realtc ? 0.99*@vmin : @vmin-1 )
129
+ else
130
+ @missval = ( @vmin.typecode>=realtc ? 1.01*@vmin : @vmin-1 )
131
+ end
132
+ elsif @vmax
133
+ if @vmax[0] >= 0
134
+ @missval = ( @vmax.typecode>=realtc ? 1.01*@vmax : @vmax+1 )
135
+ else
136
+ @missval = ( @vmax.typecode>=realtc ? 0.99*@vmax : @vmax+1 )
137
+ end
138
+ end
139
+ end
140
+
141
+ end
142
+
143
+ private :__interpret_missing_params
144
+
145
+ end
146
+
147
+ end
148
+
149
+ if $0 == __FILE__
150
+ include NumRu
151
+
152
+ filename = "tmp.nc"
153
+ print "creating ",filename,"...\n"
154
+ file=NetCDF.create(filename)
155
+ nx = 10
156
+ dimx = file.def_dim("x",nx)
157
+ xf = file.def_var("xf","sfloat",[dimx])
158
+ xfn = file.def_var("xfn","sfloat",[dimx])
159
+ xf.put_att("valid_range",[-1e12,1e12])
160
+ f = 10 ** (2*NArray.sfloat(nx).indgen!)
161
+ xr = file.def_var("xr","sint",[dimx])
162
+ xr.put_att("valid_max",[0.5])
163
+ xr.put_att("scale_factor",1e-4)
164
+ xr.put_att("add_offset",0.5)
165
+ xr2 = file.def_var("xr2","sint",[dimx])
166
+ xr2.put_att("valid_max",NArray.sint(1).fill!(1000))
167
+ xr2.put_att("scale_factor",1e-4)
168
+ xr2.put_att("add_offset",0.5)
169
+ r = NArray.sfloat(nx).indgen!/nx
170
+ file.enddef
171
+ xf.put(f)
172
+ xfn.put(f)
173
+ xr.scaled_put(r)
174
+ file.close
175
+
176
+ file = NetCDF.open(filename,'r+')
177
+ xf = file.var('xf')
178
+ xfn = file.var('xfn')
179
+ p "f0"
180
+ xf.get.each{|v| print "#{v} "} ; print "\n"
181
+ p( 'f1', nam = xf.get_with_miss )
182
+ def xf.get(*args); get_with_miss(*args); end
183
+ p( 'f12', xf[2..-3].to_na )
184
+ p( 'fn10', xfn.get_with_miss )
185
+ p( 'fn11', xfn.get_with_miss_and_scaling )
186
+ nam.invalidation([0,1])
187
+ p 'f2', nam
188
+ xf.put_with_miss(nam)
189
+ p( 'f3', xf.get_with_miss )
190
+ xr = file.var('xr')
191
+ p "r0"
192
+ xr.simple_get.each{|v| print "#{v} "} ; print "\n"
193
+ p( 'r1', xr.get_with_miss_and_scaling )
194
+ def xr.get(*args); get_with_miss_and_scaling(*args); end
195
+ def xr.put(*args); put_with_miss_and_scaling(*args); end
196
+ #xr[0..3] = xr[0..3]*10
197
+ p( 'r2', xr.get_with_miss_and_scaling )
198
+ p 'r',r
199
+ xr2.put_with_miss_and_scaling(r)
200
+ p 'xr2',xr2.get_with_miss_and_scaling
201
+ file.close
202
+ print "** ncdump tmp.nc **\n", `ncdump tmp.nc`
203
+ end
data/netcdfraw.c ADDED
@@ -0,0 +1,4613 @@
1
+ #include<stdio.h>
2
+ #include "ruby.h"
3
+ #include "narray.h"
4
+ #include<netcdf.h>
5
+ #include<string.h>
6
+
7
+ /* for compatibility with ruby 1.6 */
8
+ #ifndef RSTRING_PTR
9
+ #define RSTRING_PTR(s) (RSTRING(s)->ptr)
10
+ #endif
11
+ #ifndef RSTRING_LEN
12
+ #define RSTRING_LEN(s) (RSTRING(s)->len)
13
+ #endif
14
+ #ifndef RARRAY_PTR
15
+ #define RARRAY_PTR(a) (RARRAY(a)->ptr)
16
+ #endif
17
+ #ifndef RARRAY_LEN
18
+ #define RARRAY_LEN(a) (RARRAY(a)->len)
19
+ #endif
20
+ #ifndef StringValueCStr
21
+ #define StringValueCStr(s) STR2CSTR(s)
22
+ #endif
23
+ #ifndef SafeStringValue
24
+ #define SafeStringValue(s) Check_SafeStr(s)
25
+ #endif
26
+
27
+ /* Data to NArray */
28
+
29
+ /* memcpy(ary->ptr,nc_ptr,na_sizeof[NA_SINT]*ary->total); \ */
30
+
31
+ #define Cbyte_to_NArray(v, rank, shape, up) \
32
+ { \
33
+ struct NARRAY *ary; \
34
+ v = na_make_object(NA_BYTE, rank, shape, cNArray); \
35
+ GetNArray(v,ary); \
36
+ up = (unsigned char *)ary->ptr; \
37
+ }
38
+
39
+ #define Csint_to_NArray(v, rank, shape, sp) \
40
+ { \
41
+ struct NARRAY *ary; \
42
+ v = na_make_object(NA_SINT, rank, shape, cNArray); \
43
+ GetNArray(v, ary); \
44
+ sp = (short *)ary->ptr; \
45
+ }
46
+
47
+ #define Clint_to_NArray(v, rank, shape, lp) \
48
+ { \
49
+ struct NARRAY *ary; \
50
+ v = na_make_object(NA_LINT, rank, shape, cNArray); \
51
+ GetNArray(v, ary); \
52
+ lp = (int *)ary->ptr; \
53
+ }
54
+ #define Cfloat_to_NArray(v, rank, shape, fp) \
55
+ { \
56
+ struct NARRAY *ary; \
57
+ v = na_make_object(NA_SFLOAT, rank, shape, cNArray); \
58
+ GetNArray(v, ary); \
59
+ fp = (float *)ary->ptr; \
60
+ }
61
+ #define Cdouble_to_NArray(v, rank, shape, dp); \
62
+ { \
63
+ struct NARRAY *ary; \
64
+ v = na_make_object(NA_DFLOAT, rank, shape, cNArray); \
65
+ GetNArray(v, ary); \
66
+ dp = (double *)ary->ptr; \
67
+ }
68
+
69
+ /* Array or NArray to pointer and length (with no new allocation) */
70
+
71
+ #define Array_to_Cfloat_len(obj, ptr, len) \
72
+ { \
73
+ struct NARRAY *na; \
74
+ obj = na_cast_object(obj, NA_SFLOAT); \
75
+ GetNArray(obj, na); \
76
+ ptr = (float *) NA_PTR(na,0); \
77
+ len = na->total; \
78
+ }
79
+
80
+ #define Array_to_Cfloat_len_shape(obj, ptr, len, shape) \
81
+ { \
82
+ struct NARRAY *na; \
83
+ obj = na_cast_object(obj, NA_SFLOAT); \
84
+ GetNArray(obj, na); \
85
+ ptr = (float *) NA_PTR(na,0); \
86
+ len = na->total; \
87
+ shape = na->shape; \
88
+ }
89
+
90
+ #define Array_to_Cdouble_len(obj, ptr, len) \
91
+ { \
92
+ struct NARRAY *na; \
93
+ obj = na_cast_object(obj, NA_DFLOAT); \
94
+ GetNArray(obj, na); \
95
+ ptr = (double *) NA_PTR(na,0); \
96
+ len = na->total; \
97
+ }
98
+ #define Array_to_Cdouble_len_shape(obj, ptr, len, shape) \
99
+ { \
100
+ struct NARRAY *na; \
101
+ obj = na_cast_object(obj, NA_DFLOAT); \
102
+ GetNArray(obj, na); \
103
+ ptr = (double *) NA_PTR(na,0); \
104
+ len = na->total; \
105
+ shape = na->shape; \
106
+ }
107
+
108
+ #define Array_to_Cbyte_len(obj, ptr, len) \
109
+ { \
110
+ struct NARRAY *na; \
111
+ obj = na_cast_object(obj, NA_BYTE); \
112
+ GetNArray(obj, na); \
113
+ ptr = (u_int8_t *) NA_PTR(na,0); \
114
+ len = na->total; \
115
+ }
116
+
117
+ #define Array_to_Cbyte_len_shape(obj, ptr, len, shape) \
118
+ { \
119
+ struct NARRAY *na; \
120
+ obj = na_cast_object(obj, NA_BYTE); \
121
+ GetNArray(obj, na); \
122
+ ptr = (u_int8_t *) NA_PTR(na,0); \
123
+ len = na->total; \
124
+ shape = na->shape; \
125
+ }
126
+
127
+ #define Array_to_Csint_len(obj, ptr, len) \
128
+ { \
129
+ struct NARRAY *na; \
130
+ obj = na_cast_object(obj, NA_SINT); \
131
+ GetNArray(obj, na); \
132
+ ptr = (int16_t *) NA_PTR(na,0); \
133
+ len = na->total; \
134
+ }
135
+
136
+ #define Array_to_Csint_len_shape(obj, ptr, len, shape) \
137
+ { \
138
+ struct NARRAY *na; \
139
+ obj = na_cast_object(obj, NA_SINT); \
140
+ GetNArray(obj, na); \
141
+ ptr = (int16_t *) NA_PTR(na,0); \
142
+ len = na->total; \
143
+ shape = na->shape; \
144
+ }
145
+
146
+
147
+ #define Array_to_Clint_len(obj, ptr, len) \
148
+ { \
149
+ struct NARRAY *na; \
150
+ obj = na_cast_object(obj, NA_LINT); \
151
+ GetNArray(obj, na); \
152
+ ptr = (int32_t *) NA_PTR(na,0); \
153
+ len = na->total; \
154
+ }
155
+
156
+ #define Array_to_Clint_len_shape(obj, ptr, len, shape) \
157
+ { \
158
+ struct NARRAY *na; \
159
+ obj = na_cast_object(obj, NA_LINT); \
160
+ GetNArray(obj, na); \
161
+ ptr = (int32_t *) NA_PTR(na,0); \
162
+ len = na->total; \
163
+ shape = na->shape; \
164
+ }
165
+
166
+
167
+ /* Array or NArray to pointer (with no new allocation) */
168
+
169
+ #define Array_to_Cfloat(obj, ptr) \
170
+ { \
171
+ struct NARRAY *na; \
172
+ obj = na_cast_object(obj, NA_SFLOAT); \
173
+ GetNArray(obj, na); \
174
+ ptr = (float *) NA_PTR(na,0); \
175
+ }
176
+ #define Array_to_Cdouble(obj, ptr) \
177
+ { \
178
+ struct NARRAY *na; \
179
+ obj = na_cast_object(obj, NA_DFLOAT); \
180
+ GetNArray(obj, na); \
181
+ ptr = (double *) NA_PTR(na,0); \
182
+ }
183
+ #define Array_to_Cbyte(obj, ptr) \
184
+ { \
185
+ struct NARRAY *na; \
186
+ obj = na_cast_object(obj, NA_BYTE); \
187
+ GetNArray(obj, na); \
188
+ ptr = (u_int8_t *) NA_PTR(na,0); \
189
+ }
190
+ #define Array_to_Csint(obj, ptr) \
191
+ { \
192
+ struct NARRAY *na; \
193
+ obj = na_cast_object(obj, NA_SINT); \
194
+ GetNArray(obj, na); \
195
+ ptr = (int16_t *) NA_PTR(na,0); \
196
+ }
197
+ #define Array_to_Clint(obj, ptr) \
198
+ { \
199
+ struct NARRAY *na; \
200
+ obj = na_cast_object(obj, NA_LINT); \
201
+ GetNArray(obj, na); \
202
+ ptr = (int32_t *) NA_PTR(na,0); \
203
+ }
204
+
205
+ #define NC_RAISE(status) rb_raise(err_status2class(status),(nc_strerror(status)))
206
+
207
+ static VALUE mNumRu = 0;
208
+ static VALUE cNetCDF;
209
+ static VALUE cNetCDFDim;
210
+ static VALUE cNetCDFAtt;
211
+ static VALUE cNetCDFVar;
212
+
213
+ static VALUE rb_eNetcdfError;
214
+ static VALUE rb_eNetcdfBadid;
215
+ static VALUE rb_eNetcdfNfile;
216
+ static VALUE rb_eNetcdfExist;
217
+ static VALUE rb_eNetcdfInval;
218
+ static VALUE rb_eNetcdfPerm;
219
+ static VALUE rb_eNetcdfNotindefine;
220
+ static VALUE rb_eNetcdfIndefine;
221
+ static VALUE rb_eNetcdfInvalcoords;
222
+ static VALUE rb_eNetcdfMaxdims;
223
+ static VALUE rb_eNetcdfNameinuse;
224
+ static VALUE rb_eNetcdfNotatt;
225
+ static VALUE rb_eNetcdfMaxatts;
226
+ static VALUE rb_eNetcdfBadtype;
227
+ static VALUE rb_eNetcdfBaddim;
228
+ static VALUE rb_eNetcdfUnlimpos;
229
+ static VALUE rb_eNetcdfMaxvars;
230
+ static VALUE rb_eNetcdfNotvar;
231
+ static VALUE rb_eNetcdfGlobal;
232
+ static VALUE rb_eNetcdfNotnc;
233
+ static VALUE rb_eNetcdfSts;
234
+ static VALUE rb_eNetcdfMaxname;
235
+ static VALUE rb_eNetcdfUnlimit;
236
+ static VALUE rb_eNetcdfNorecvars;
237
+ static VALUE rb_eNetcdfChar;
238
+ static VALUE rb_eNetcdfEdge;
239
+ static VALUE rb_eNetcdfStride;
240
+ static VALUE rb_eNetcdfBadname;
241
+ static VALUE rb_eNetcdfRange;
242
+ static VALUE rb_eNetcdfNomem;
243
+
244
+ /* Special Error */
245
+ /* Global error status */
246
+
247
+ static VALUE rb_eNetcdfFatal;
248
+
249
+ /* Global options variable. Used to determine behavior of error handler. */
250
+
251
+ static VALUE rb_eNetcdfEntool;
252
+ static VALUE rb_eNetcdfExdr;
253
+ static VALUE rb_eNetcdfSyserr;
254
+
255
+
256
+ struct Netcdf{
257
+ int ncid;
258
+ char *name;
259
+ int closed;
260
+ };
261
+
262
+ struct NetCDFDim{
263
+ int dimid;
264
+ int ncid;
265
+ };
266
+
267
+ struct NetCDFVar{
268
+ int varid;
269
+ int ncid;
270
+ VALUE file;
271
+ };
272
+
273
+ struct NetCDFAtt{
274
+ int varid;
275
+ int ncid;
276
+ char *name;
277
+ };
278
+
279
+ static struct Netcdf *
280
+ NetCDF_init(int ncid,char *filename)
281
+ {
282
+ struct Netcdf *Netcdffile;
283
+ Netcdffile=xmalloc(sizeof(struct Netcdf));
284
+ Netcdffile->ncid=ncid;
285
+ Netcdffile->closed=0;
286
+ Netcdffile->name=xmalloc((strlen(filename)+1)*sizeof(char));
287
+ strcpy(Netcdffile->name,filename);
288
+ return(Netcdffile);
289
+ }
290
+
291
+ static struct NetCDFDim *
292
+ NetCDF_dim_init(int ncid,int dimid)
293
+ {
294
+ struct NetCDFDim *Netcdf_dim;
295
+ Netcdf_dim=xmalloc(sizeof(struct NetCDFDim));
296
+ Netcdf_dim->dimid=dimid;
297
+ Netcdf_dim->ncid=ncid;
298
+ return(Netcdf_dim);
299
+ }
300
+
301
+ static struct NetCDFVar *
302
+ NetCDF_var_init(int ncid,int varid,VALUE file)
303
+ {
304
+ struct NetCDFVar *Netcdf_var;
305
+ Netcdf_var=xmalloc(sizeof(struct NetCDFVar));
306
+ Netcdf_var->varid=varid;
307
+ Netcdf_var->ncid=ncid;
308
+ Netcdf_var->file=file;
309
+ return(Netcdf_var);
310
+ }
311
+ static struct NetCDFAtt *
312
+ NetCDF_att_init(int ncid,int varid,char *attname)
313
+ {
314
+ struct NetCDFAtt *Netcdf_att;
315
+ Netcdf_att=xmalloc(sizeof(struct NetCDFAtt));
316
+ Netcdf_att->ncid=ncid;
317
+ Netcdf_att->varid=varid;
318
+ Netcdf_att->name=xmalloc((strlen(attname)+1)*sizeof(char));
319
+ strcpy(Netcdf_att->name,attname);
320
+ return(Netcdf_att);
321
+ }
322
+
323
+ void
324
+ Netcdf_att_free(struct NetCDFAtt *Netcdf_att)
325
+ {
326
+ free(Netcdf_att->name);
327
+ free(Netcdf_att);
328
+ }
329
+
330
+ void
331
+ NetCDF_var_free(struct NetCDFVar *Netcdf_var)
332
+ {
333
+ free(Netcdf_var);
334
+ }
335
+
336
+ void
337
+ NetCDF_dim_free(struct NetCDFDim *Netcdf_dim)
338
+ {
339
+ free(Netcdf_dim);
340
+ }
341
+
342
+ void
343
+ NetCDF_free(struct Netcdf *Netcdffile)
344
+ {
345
+ int status;
346
+ if (!Netcdffile->closed){
347
+ status = nc_close(Netcdffile->ncid); /* no error check -- not to stop during GC */
348
+ }
349
+ free(Netcdffile->name);
350
+ free(Netcdffile);
351
+ }
352
+
353
+ static VALUE
354
+ err_status2class(int status)
355
+ {
356
+ if(NC_ISSYSERR(status)){
357
+ return(rb_eNetcdfSyserr);
358
+ }
359
+ switch(status)
360
+ {
361
+ case(NC_EBADID):
362
+ return(rb_eNetcdfBadid);break;
363
+ case(NC_ENFILE):
364
+ return(rb_eNetcdfNfile);break;
365
+ case(NC_EEXIST):
366
+ return(rb_eNetcdfExist);break;
367
+ case(NC_EINVAL):
368
+ return(rb_eNetcdfInval);break;
369
+ case(NC_EPERM):
370
+ return(rb_eNetcdfPerm);break;
371
+ case(NC_ENOTINDEFINE):
372
+ return(rb_eNetcdfNotindefine);break;
373
+ case(NC_EINDEFINE):
374
+ return(rb_eNetcdfIndefine);break;
375
+ case(NC_EINVALCOORDS):
376
+ return(rb_eNetcdfInvalcoords);break;
377
+ case(NC_EMAXDIMS):
378
+ return(rb_eNetcdfMaxdims);break;
379
+ case(NC_ENAMEINUSE):
380
+ return(rb_eNetcdfNameinuse);break;
381
+ case(NC_ENOTATT):
382
+ return(rb_eNetcdfNotatt);break;
383
+ case(NC_EMAXATTS):
384
+ return(rb_eNetcdfMaxatts);break;
385
+ case(NC_EBADTYPE):
386
+ return(rb_eNetcdfBadtype);break;
387
+ case(NC_EBADDIM):
388
+ return(rb_eNetcdfBaddim);break;
389
+ case(NC_EUNLIMPOS):
390
+ return(rb_eNetcdfUnlimpos);break;
391
+ case(NC_EMAXVARS):
392
+ return(rb_eNetcdfMaxvars);break;
393
+ case(NC_ENOTVAR):
394
+ return(rb_eNetcdfNotvar);break;
395
+ case(NC_EGLOBAL):
396
+ return(rb_eNetcdfGlobal);break;
397
+ case(NC_ENOTNC):
398
+ return(rb_eNetcdfNotnc);break;
399
+ case(NC_ESTS):
400
+ return(rb_eNetcdfSts);break;
401
+ case(NC_EMAXNAME):
402
+ return(rb_eNetcdfMaxname);break;
403
+ case(NC_EUNLIMIT):
404
+ return(rb_eNetcdfUnlimit);break;
405
+ case(NC_ENORECVARS):
406
+ return(rb_eNetcdfNorecvars);break;
407
+ case(NC_ECHAR):
408
+ return(rb_eNetcdfChar);break;
409
+ case(NC_EEDGE):
410
+ return(rb_eNetcdfEdge);break;
411
+ case(NC_ESTRIDE):
412
+ return(rb_eNetcdfStride);break;
413
+ case(NC_EBADNAME):
414
+ return(rb_eNetcdfBadname);break;
415
+ case(NC_ERANGE):
416
+ return(rb_eNetcdfRange);break;
417
+ case(NC_ENOMEM):
418
+ return(rb_eNetcdfNomem);break;
419
+ /* case(NC_ENTOOL):
420
+ return(rb_eNetcdfEntool);break; */
421
+ case(NC_EXDR):
422
+ return(rb_eNetcdfExdr);break;
423
+ case(NC_SYSERR):
424
+ return(rb_eNetcdfSyserr);break;
425
+ case(NC_FATAL):
426
+ return(rb_eNetcdfFatal);break;
427
+ }
428
+ }
429
+
430
+ static const char*
431
+ nctype2natype(int nctype){
432
+ switch(nctype){
433
+ case NC_CHAR:
434
+ return("char");
435
+ case NC_BYTE:
436
+ return("byte");
437
+ case NC_SHORT:
438
+ return("sint");
439
+ case NC_INT:
440
+ return("int");
441
+ case NC_FLOAT:
442
+ return("sfloat");
443
+ case NC_DOUBLE:
444
+ return("float");
445
+ default:
446
+ rb_raise(rb_eNetcdfError, "No such netcdf type number %d\n",nctype);
447
+ }
448
+ }
449
+
450
+ static int
451
+ nctype2natypecode(int nctype){
452
+ switch(nctype){
453
+ case NC_CHAR:
454
+ return(NA_BYTE);
455
+ case NC_BYTE:
456
+ return(NA_BYTE);
457
+ case NC_SHORT:
458
+ return(NA_SINT);
459
+ case NC_INT:
460
+ return(NA_LINT);
461
+ case NC_FLOAT:
462
+ return(NA_SFLOAT);
463
+ case NC_DOUBLE:
464
+ return(NA_DFLOAT);
465
+ default:
466
+ rb_raise(rb_eNetcdfError, "No such netcdf type number %d\n",nctype);
467
+ }
468
+ }
469
+
470
+ static int
471
+ natype2nctype(char *natype)
472
+ {
473
+ if(strcmp(natype,"byte")==0) return(NC_BYTE);
474
+ else if(strcmp(natype,"char")==0) return(NC_CHAR);
475
+ else if(strcmp(natype,"text")==0) return(NC_CHAR); /* alias of char */
476
+ else if(strcmp(natype,"string")==0) return(NC_CHAR); /* alias of char */
477
+ else if(strcmp(natype,"sint")==0) return(NC_SHORT);
478
+ else if(strcmp(natype,"int")==0) return(NC_INT);
479
+ else if(strcmp(natype,"sfloat")==0) return(NC_FLOAT);
480
+ else if(strcmp(natype,"float")==0) return(NC_DOUBLE);
481
+ else rb_raise(rb_eNetcdfError, "No such NArray type '%s'",natype);
482
+ }
483
+
484
+ static int
485
+ natypecode2nctype(int natypecode)
486
+ {
487
+ if(natypecode==NA_BYTE) return(NC_BYTE);
488
+ else if(natypecode==NA_SINT) return(NC_SHORT);
489
+ else if(natypecode==NA_LINT) return(NC_INT);
490
+ else if(natypecode==NA_SFLOAT) return(NC_FLOAT);
491
+ else if(natypecode==NA_DFLOAT) return(NC_DOUBLE);
492
+ else rb_raise(rb_eNetcdfError, "No such NArray typecode '%d'",natypecode);
493
+ }
494
+
495
+ static void
496
+ nc_mark_obj(struct NetCDFVar *netcdf_var)
497
+ {
498
+ VALUE ptr;
499
+
500
+ ptr = netcdf_var->file;
501
+ rb_gc_mark(ptr);
502
+ }
503
+
504
+
505
+ VALUE
506
+ NetCDF_clone(VALUE file)
507
+ {
508
+ VALUE clone;
509
+ struct Netcdf *nc1, *nc2;
510
+
511
+ Data_Get_Struct(file, struct Netcdf, nc1);
512
+ nc2 = NetCDF_init(nc1->ncid, nc1->name);
513
+ clone = Data_Wrap_Struct(cNetCDF, 0, NetCDF_free, nc2);
514
+ CLONESETUP(clone, file);
515
+ return clone;
516
+ }
517
+
518
+ VALUE
519
+ NetCDF_dim_clone(VALUE dim)
520
+ {
521
+ VALUE clone;
522
+ struct NetCDFDim *nd1, *nd2;
523
+
524
+ Data_Get_Struct(dim, struct NetCDFDim, nd1);
525
+ nd2 = NetCDF_dim_init(nd1->ncid, nd1->dimid);
526
+ clone = Data_Wrap_Struct(cNetCDFDim, 0, NetCDF_dim_free, nd2);
527
+ CLONESETUP(clone, dim);
528
+ return clone;
529
+ }
530
+
531
+ VALUE
532
+ NetCDF_att_clone(VALUE att)
533
+ {
534
+ VALUE clone;
535
+ struct NetCDFAtt *na1, *na2;
536
+
537
+ Data_Get_Struct(att, struct NetCDFAtt, na1);
538
+ na2 = NetCDF_att_init(na1->ncid, na1->varid, na1->name);
539
+ clone = Data_Wrap_Struct(cNetCDFAtt, 0, Netcdf_att_free, na2);
540
+ CLONESETUP(clone, att);
541
+ return clone;
542
+ }
543
+
544
+ VALUE
545
+ NetCDF_var_clone(VALUE var)
546
+ {
547
+ VALUE clone;
548
+ struct NetCDFVar *nv1, *nv2;
549
+
550
+ Data_Get_Struct(var, struct NetCDFVar, nv1);
551
+ nv2 = NetCDF_var_init(nv1->ncid, nv1->varid, nv1->file);
552
+ clone = Data_Wrap_Struct(cNetCDFVar, nc_mark_obj, NetCDF_var_free, nv2);
553
+ CLONESETUP(clone, var);
554
+ return clone;
555
+ }
556
+
557
+ VALUE
558
+ NetCDF_close(file)
559
+ VALUE file;
560
+ {
561
+ int status;
562
+ int ncid;
563
+ struct Netcdf *Netcdffile;
564
+
565
+ if (rb_safe_level() >= 4 && !OBJ_TAINTED(file)) {
566
+ rb_raise(rb_eSecurityError, "Insecure: can't close");
567
+ }
568
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
569
+ ncid=Netcdffile->ncid;
570
+ if(!Netcdffile->closed){
571
+ status = nc_close(ncid);
572
+ if(status != NC_NOERR) NC_RAISE(status);
573
+ Netcdffile->closed = 1;
574
+ } else {
575
+ rb_warn("file %s is already closed", Netcdffile->name);
576
+ }
577
+ return Qnil;
578
+ }
579
+
580
+ VALUE
581
+ NetCDF_def_dim(VALUE file,VALUE dim_name,VALUE length)
582
+ {
583
+ char* c_dim_name;
584
+ size_t c_length;
585
+ int ncid;
586
+ int dimidp;
587
+ int status;
588
+ struct Netcdf *Netcdffile;
589
+ struct NetCDFDim *Netcdf_dim;
590
+ VALUE Dimension;
591
+
592
+ rb_secure(4);
593
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
594
+
595
+ Check_Type(dim_name,T_STRING);
596
+ c_dim_name=RSTRING_PTR(dim_name);
597
+ c_length=NUM2UINT(length);
598
+ ncid=Netcdffile->ncid;
599
+
600
+ status = nc_def_dim(ncid,c_dim_name,c_length,&dimidp);
601
+ if(status !=NC_NOERR) NC_RAISE(status);
602
+
603
+ Netcdf_dim = NetCDF_dim_init(ncid,dimidp);
604
+
605
+ Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
606
+ return Dimension;
607
+ }
608
+
609
+
610
+ static VALUE
611
+ NetCDF_put_att_char(int ncid, char *name,VALUE value,VALUE atttype, int varid)
612
+ {
613
+ int status;
614
+ struct NetCDFAtt *ncatt;
615
+
616
+ /* check atttype (not necessarily needed but it's better to do it) */
617
+ if (TYPE(atttype) == T_STRING){
618
+ if ( natype2nctype(RSTRING_PTR(atttype)) != NC_CHAR ) {
619
+ rb_raise(rb_eNetcdfError,
620
+ "attribute type must be 'char' (or nil) for a String value");
621
+ }
622
+ } else if (TYPE(atttype) != T_NIL) {
623
+ rb_raise(rb_eNetcdfError,
624
+ "type specfication must be by a string or nil");
625
+ }
626
+ /* put value */
627
+ Check_Type(value,T_STRING);
628
+ status = nc_put_att_text(ncid, varid, name,
629
+ RSTRING_LEN(value), RSTRING_PTR(value));
630
+ if(status != NC_NOERR) NC_RAISE(status);
631
+
632
+ ncatt = NetCDF_att_init(ncid,varid,name);
633
+ return (Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,ncatt));
634
+ }
635
+
636
+ static VALUE
637
+ NetCDF_put_att_numeric(int ncid, char *name,VALUE value,VALUE atttype, int varid)
638
+ {
639
+ VALUE val;
640
+ struct NARRAY *na_val;
641
+ int na_typecode, status, len;
642
+ char *ptr;
643
+ struct NetCDFAtt *ncatt;
644
+
645
+ /* check atttype and cast to an appropriate NArray if needed */
646
+
647
+ if (TYPE(atttype) != T_NIL){
648
+ na_typecode = na_get_typecode(atttype);
649
+ GetNArray( na_cast_object(value, na_typecode), na_val );
650
+ } else {
651
+ if (TYPE(value)==T_ARRAY) {
652
+ val = RARRAY_PTR(value)[0]; /* to check the 1st elemnt if Array */
653
+ } else {
654
+ val = value;
655
+ }
656
+ switch(TYPE(val)){
657
+ case T_FIXNUM:
658
+ case T_BIGNUM:
659
+ na_typecode = NA_LINT;
660
+ GetNArray( na_cast_object(value, na_typecode), na_val );
661
+ break;
662
+ case T_FLOAT:
663
+ na_typecode = NA_DFLOAT;
664
+ GetNArray( na_cast_object(value, na_typecode), na_val );
665
+ break;
666
+ case T_DATA:
667
+ if ( IsNArray(value) ){
668
+ GetNArray(value,na_val);
669
+ na_typecode = na_val->type;
670
+ } else {
671
+ rb_raise(rb_eNetcdfError,"value has a wrong data type");
672
+ }
673
+ break;
674
+ default:
675
+ rb_raise(rb_eNetcdfError,
676
+ "value (or its first element) has a wrong type");
677
+ }
678
+ }
679
+
680
+ /* put value */
681
+
682
+ len = na_val->total;
683
+ ptr = na_val->ptr;
684
+ switch(na_typecode){
685
+ case NA_BYTE:
686
+ status = nc_put_att_uchar(ncid,varid,name,NC_BYTE,len,(unsigned char *)ptr);
687
+ break;
688
+ case NA_SINT:
689
+ status = nc_put_att_short(ncid,varid,name,NC_SHORT,len,(short *)ptr);
690
+ break;
691
+ case NA_LINT:
692
+ status = nc_put_att_int(ncid,varid,name,NC_INT,len,(int *)ptr);
693
+ break;
694
+ case NA_SFLOAT:
695
+ status = nc_put_att_float(ncid,varid,name,NC_FLOAT,len,(float *)ptr);
696
+ break;
697
+ case NA_DFLOAT:
698
+ status = nc_put_att_double(ncid,varid,name,NC_DOUBLE,len,(double*)ptr);
699
+ break;
700
+ default:
701
+ rb_raise(rb_eNetcdfError,
702
+ "unsupported type. code = %d",na_typecode);
703
+ }
704
+ if(status != NC_NOERR) NC_RAISE(status);
705
+
706
+ ncatt = NetCDF_att_init(ncid,varid,name);
707
+ return (Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,ncatt));
708
+ }
709
+
710
+ static VALUE
711
+ NetCDF_put_att__(int ncid, char *name, VALUE value, VALUE atttype, int varid)
712
+ /*
713
+ * atttype: nil or a String ("string","int",etc). If nil,
714
+ * the type of attribute is determined from the type of value
715
+ */
716
+ {
717
+ switch(TYPE(value)){
718
+ case T_STRING:
719
+ return(NetCDF_put_att_char(ncid, name, value, atttype, varid));
720
+ default:
721
+ return(NetCDF_put_att_numeric(ncid, name, value, atttype, varid));
722
+ }
723
+ }
724
+
725
+ VALUE
726
+ NetCDF_put_att(VALUE file,VALUE att_name,VALUE value,VALUE atttype)
727
+ /*
728
+ * atttype: nil or a String ("string","int",etc). If nil,
729
+ * the type of attribute is determined from the type of value
730
+ */
731
+ {
732
+ struct Netcdf *ncfile;
733
+ char *name;
734
+
735
+ rb_secure(4);
736
+ Data_Get_Struct(file,struct Netcdf,ncfile);
737
+ Check_Type(att_name,T_STRING);
738
+ name = RSTRING_PTR(att_name);
739
+
740
+ return( NetCDF_put_att__(ncfile->ncid, name, value, atttype, NC_GLOBAL) );
741
+ }
742
+
743
+ VALUE
744
+ NetCDF_put_att_var(VALUE var,VALUE att_name,VALUE value,VALUE atttype)
745
+ /*
746
+ * atttype: nil or a String ("string","int",etc). If nil,
747
+ * the type of attribute is determined from the type of value
748
+ */
749
+ {
750
+ struct NetCDFVar *ncvar;
751
+ char *name;
752
+
753
+ rb_secure(4);
754
+ Data_Get_Struct(var,struct NetCDFVar,ncvar);
755
+ Check_Type(att_name,T_STRING);
756
+ name = RSTRING_PTR(att_name);
757
+
758
+ return( NetCDF_put_att__(ncvar->ncid, name, value, atttype, ncvar->varid));
759
+ }
760
+
761
+
762
+ VALUE
763
+ NetCDF_def_var(VALUE file,VALUE var_name,VALUE vartype,VALUE dimensions)
764
+ {
765
+ int ncid;
766
+ char *c_var_name;
767
+ static int xtype;
768
+ long c_ndims;
769
+ int varidp;
770
+ int dimidp;
771
+ int i=0;
772
+ int status;
773
+ char *c_dim_name;
774
+ int c_dimids[NC_MAX_DIMS];
775
+ struct Netcdf *Netcdffile;
776
+ struct NetCDFVar *Netcdf_var;
777
+ struct NetCDFDim *Netcdf_dim;
778
+ VALUE Var;
779
+
780
+ rb_secure(4);
781
+ Check_Type(var_name,T_STRING);
782
+ Check_Type(dimensions,T_ARRAY);
783
+
784
+ c_var_name=RSTRING_PTR(var_name);
785
+ c_ndims=RARRAY_LEN(dimensions);
786
+
787
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
788
+ ncid=Netcdffile->ncid;
789
+
790
+ if (TYPE(vartype) == T_STRING){
791
+ xtype = natype2nctype(RSTRING_PTR(vartype));
792
+ } else if (TYPE(vartype) == T_FIXNUM){
793
+ xtype = natypecode2nctype(NUM2INT(vartype));
794
+ } else {
795
+ rb_raise(rb_eNetcdfError,
796
+ "type specfication must be by a string or nil");
797
+ }
798
+
799
+ for(i=0;i<c_ndims;i++){
800
+ switch(TYPE(RARRAY_PTR(dimensions)[c_ndims-1-i])){
801
+ case T_STRING:
802
+ Check_Type(RARRAY_PTR(dimensions)[c_ndims-1-i],T_STRING);
803
+ c_dim_name=StringValueCStr(RARRAY_PTR(dimensions)[c_ndims-1-i]);
804
+ status=nc_inq_dimid(ncid,c_dim_name,&dimidp);
805
+ if(status != NC_NOERR) NC_RAISE(status);
806
+ c_dimids[i]=dimidp;
807
+ break;
808
+ case T_DATA:
809
+ Data_Get_Struct(RARRAY_PTR(dimensions)[c_ndims-1-i],struct NetCDFDim,Netcdf_dim);
810
+ c_dimids[i]=Netcdf_dim->dimid;
811
+ break;
812
+ default:
813
+ rb_raise(rb_eNetcdfError, "No such object of the netCDF dimension class.");
814
+ }
815
+ }
816
+
817
+ status = nc_def_var(ncid,c_var_name,xtype,c_ndims,c_dimids,&varidp);
818
+ if(status != NC_NOERR) NC_RAISE(status);
819
+
820
+ Netcdf_var = NetCDF_var_init(ncid,varidp,file);
821
+
822
+ Var=Data_Wrap_Struct(cNetCDFVar,nc_mark_obj,NetCDF_var_free,Netcdf_var);
823
+ return Var;
824
+ }
825
+
826
+
827
+ VALUE
828
+ NetCDF_dim(VALUE file,VALUE dim_name)
829
+ {
830
+ int ncid;
831
+ char *c_dim_name;
832
+ int dimidp;
833
+ int status;
834
+ struct Netcdf *Netcdffile;
835
+ struct NetCDFDim *Netcdf_dim;
836
+ VALUE Dimension;
837
+
838
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
839
+ ncid=Netcdffile->ncid;
840
+ Check_Type(dim_name,T_STRING);
841
+ c_dim_name=RSTRING_PTR(dim_name);
842
+
843
+ status = nc_inq_dimid(ncid,c_dim_name,&dimidp);
844
+ if(status !=NC_NOERR){
845
+ if(status == NC_EBADDIM){
846
+ return(Qnil); /*2003/08/27 back to orig (from changes on 2003/02/03)*/
847
+ } else{
848
+ NC_RAISE(status);
849
+ }
850
+ }
851
+
852
+ Netcdf_dim=NetCDF_dim_init(ncid,dimidp);
853
+
854
+ Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
855
+ return Dimension;
856
+ }
857
+
858
+ VALUE
859
+ NetCDF_var(VALUE file,VALUE var_name)
860
+ {
861
+ int ncid;
862
+ int status;
863
+ int varidp;
864
+ char *c_var_name;
865
+ struct Netcdf *Netcdffile;
866
+ struct NetCDFVar *Netcdf_var;
867
+ VALUE Variable;
868
+
869
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
870
+ ncid=Netcdffile->ncid;
871
+ Check_Type(var_name,T_STRING);
872
+ c_var_name=RSTRING_PTR(var_name);
873
+
874
+ status=nc_inq_varid(ncid,c_var_name,&varidp);
875
+ if(status != NC_NOERR){
876
+ if(status == NC_ENOTVAR){
877
+ return(Qnil); /*2003/08/27 back to orig (from changes on 2003/02/03)*/
878
+ } else{
879
+ NC_RAISE(status);
880
+ }
881
+ }
882
+
883
+ Netcdf_var = NetCDF_var_init(ncid,varidp,file);
884
+ Variable = Data_Wrap_Struct(cNetCDFVar,nc_mark_obj,NetCDF_var_free,Netcdf_var);
885
+ return Variable;
886
+ }
887
+
888
+ VALUE
889
+ NetCDF_att(VALUE file,VALUE att_name)
890
+ {
891
+ int ncid;
892
+ int status;
893
+ int attnump;
894
+ char *c_att_name;
895
+ struct Netcdf *Netcdffile;
896
+ struct NetCDFAtt *Netcdf_att;
897
+ VALUE Attribute;
898
+
899
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
900
+ ncid=Netcdffile->ncid;
901
+ Check_Type(att_name,T_STRING);
902
+ c_att_name=RSTRING_PTR(att_name);
903
+
904
+
905
+ status = nc_inq_attid(ncid,NC_GLOBAL,c_att_name,&attnump);
906
+ if(status != NC_NOERR){
907
+ if(status == NC_ENOTATT){
908
+ return(Qnil);
909
+ }
910
+ else{
911
+ NC_RAISE(status);
912
+ }
913
+ }
914
+
915
+ Netcdf_att = NetCDF_att_init(ncid,NC_GLOBAL,c_att_name);
916
+
917
+ Attribute = Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
918
+
919
+ return Attribute;
920
+ }
921
+ VALUE
922
+ NetCDF_fill(VALUE file,VALUE mode)
923
+ {
924
+ int ncid;
925
+ int status;
926
+ struct Netcdf *Netcdffile;
927
+ int old_modep;
928
+
929
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
930
+ ncid = Netcdffile->ncid;
931
+ if(mode==Qfalse){
932
+ status = nc_set_fill(ncid,NC_NOFILL,&old_modep);
933
+ if(status != NC_NOERR) NC_RAISE(status);
934
+ }
935
+ else if(mode == Qtrue){
936
+ status = nc_set_fill(ncid,NC_FILL,&old_modep);
937
+ if(status != NC_NOERR) NC_RAISE(status);
938
+ }
939
+ else
940
+ rb_raise(rb_eNetcdfError,"Usage:self.fill(true) or self.fill(false)");
941
+ return Qnil;
942
+ }
943
+
944
+ VALUE
945
+ NetCDF_redef(VALUE file)
946
+ {
947
+ int ncid;
948
+ int status;
949
+ struct Netcdf *Netcdffile;
950
+
951
+ rb_secure(4);
952
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
953
+ ncid=Netcdffile->ncid;
954
+ status = nc_redef(ncid);
955
+ if(status !=NC_NOERR){
956
+ if(status == NC_EINDEFINE){
957
+ return Qnil;
958
+ }
959
+ else{
960
+ NC_RAISE(status);
961
+ }
962
+ }
963
+ return Qtrue;
964
+ }
965
+
966
+ VALUE
967
+ NetCDF_enddef(VALUE file)
968
+ {
969
+ int ncid;
970
+ int status;
971
+ struct Netcdf *Netcdffile;
972
+
973
+ rb_secure(4);
974
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
975
+ ncid=Netcdffile->ncid;
976
+ status = nc_enddef(ncid);
977
+ if(status !=NC_NOERR){
978
+ if(status == NC_ENOTINDEFINE){
979
+ return Qnil;
980
+ }
981
+ else{
982
+ NC_RAISE(status);
983
+ }
984
+ }
985
+ return Qtrue;
986
+ }
987
+
988
+ VALUE
989
+ NetCDF_whether_in_define_mode(VALUE file)
990
+ {
991
+ /* returns true if the NetCDF object is currently in the define mode,
992
+ false if in the data mode, and
993
+ nil if else (possibly the file is read-only, or some other
994
+ error occurred)
995
+ */
996
+ int ncid;
997
+ int status;
998
+ struct Netcdf *Netcdffile;
999
+
1000
+ rb_secure(4);
1001
+ Data_Get_Struct(file,struct Netcdf,Netcdffile);
1002
+ ncid=Netcdffile->ncid;
1003
+ status = nc_redef(ncid);
1004
+ if(status == NC_EINDEFINE){
1005
+ return Qtrue;
1006
+ } else if(status == NC_NOERR) {
1007
+ /* was in the data mode --> recover the data mode and report false */
1008
+ status = nc_enddef(ncid);
1009
+ if(status == NC_NOERR) {
1010
+ return Qfalse;
1011
+ } else {
1012
+ return Qnil;
1013
+ }
1014
+ } else {
1015
+ return Qnil;
1016
+ }
1017
+ }
1018
+
1019
+ VALUE
1020
+ NetCDF_open(VALUE mod,VALUE filename,VALUE omode)
1021
+ {
1022
+ int status;
1023
+ int ncid;
1024
+ char* c_filename;
1025
+ int c_omode;
1026
+ struct Netcdf *ncfile;
1027
+ VALUE retval;
1028
+
1029
+ Check_Type(filename,T_STRING);
1030
+ SafeStringValue(filename);
1031
+ c_filename=RSTRING_PTR(filename);
1032
+ Check_Type(omode,T_FIXNUM);
1033
+ c_omode=NUM2INT(omode);
1034
+
1035
+ status = nc_open(c_filename,c_omode,&ncid);
1036
+ if(status !=NC_NOERR){NC_RAISE(status);}
1037
+
1038
+ ncfile = NetCDF_init(ncid,c_filename);
1039
+ retval = Data_Wrap_Struct(cNetCDF,0,NetCDF_free,ncfile);
1040
+ return( retval );
1041
+ }
1042
+
1043
+ VALUE
1044
+ NetCDF_create(VALUE mod,VALUE filename,VALUE cmode)
1045
+ {
1046
+ int ncid;
1047
+ int status;
1048
+ char* c_filename;
1049
+ int c_cmode;
1050
+ struct Netcdf *ncfile;
1051
+
1052
+ Check_Type(filename,T_STRING);
1053
+ SafeStringValue(filename);
1054
+ c_filename=RSTRING_PTR(filename);
1055
+ Check_Type(cmode,T_FIXNUM);
1056
+ c_cmode=NUM2INT(cmode);
1057
+
1058
+ status = nc_create(c_filename,c_cmode,&ncid);
1059
+ if(status != NC_NOERR) NC_RAISE(status);
1060
+
1061
+ ncfile = NetCDF_init(ncid,c_filename);
1062
+ return( Data_Wrap_Struct(cNetCDF,0,NetCDF_free,ncfile) );
1063
+ }
1064
+
1065
+ VALUE
1066
+ NetCDF_ndims(VALUE file)
1067
+ {
1068
+ int ncid;
1069
+ int ndimsp;
1070
+ VALUE Integer;
1071
+ int status;
1072
+ struct Netcdf *ncfile;
1073
+
1074
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1075
+ ncid=ncfile->ncid;
1076
+ status = nc_inq_ndims(ncid,&ndimsp);
1077
+ if(status != NC_NOERR) NC_RAISE (status);
1078
+ Integer = INT2NUM(ndimsp);
1079
+ return Integer;
1080
+ }
1081
+
1082
+ VALUE
1083
+ NetCDF_nvars(VALUE file)
1084
+ {
1085
+ int ncid;
1086
+ int nvarsp;
1087
+ int status;
1088
+ VALUE Integer;
1089
+ struct Netcdf *ncfile;
1090
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1091
+ ncid=ncfile->ncid;
1092
+ status = nc_inq_nvars(ncid,&nvarsp);
1093
+ if(status != NC_NOERR) NC_RAISE (status);
1094
+ Integer = INT2NUM(nvarsp);
1095
+ return Integer;
1096
+ }
1097
+
1098
+ VALUE
1099
+ NetCDF_natts(VALUE file)
1100
+ {
1101
+ int ncid;
1102
+ int nattsp;
1103
+ int status;
1104
+ VALUE Integer;
1105
+ struct Netcdf *ncfile;
1106
+
1107
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1108
+ ncid=ncfile->ncid;
1109
+ status=nc_inq_natts(ncid,&nattsp);
1110
+ if(status != NC_NOERR) NC_RAISE (status);
1111
+ Integer = INT2NUM(nattsp);
1112
+ return Integer;
1113
+ }
1114
+
1115
+ VALUE
1116
+ NetCDF_unlimited(VALUE file)
1117
+ {
1118
+ int ncid;
1119
+ int unlimdimidp;
1120
+ int status;
1121
+ struct Netcdf *ncfile;
1122
+ struct NetCDFDim *Netcdf_dim;
1123
+ VALUE Dimension;
1124
+
1125
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1126
+ ncid=ncfile->ncid;
1127
+ status=nc_inq_unlimdim(ncid,&unlimdimidp);
1128
+ if(status !=NC_NOERR) NC_RAISE(status);
1129
+
1130
+ Netcdf_dim = NetCDF_dim_init(ncid,unlimdimidp);
1131
+
1132
+ /* If unlimdimidp=-1,No unlimited dimension is defined in the netCDF dataset */
1133
+ if(unlimdimidp != -1)
1134
+ {
1135
+ Dimension = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
1136
+ return Dimension;
1137
+ }
1138
+ else
1139
+ {
1140
+ return Qnil;
1141
+ }
1142
+ }
1143
+
1144
+ VALUE
1145
+ NetCDF_sync(VALUE file)
1146
+ {
1147
+ int ncid;
1148
+ int status;
1149
+ struct Netcdf *ncfile;
1150
+
1151
+ rb_secure(4);
1152
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1153
+ ncid=ncfile->ncid;
1154
+ status = nc_sync(ncid);
1155
+ if(status !=NC_NOERR) NC_RAISE (status);
1156
+ return Qnil;
1157
+ }
1158
+
1159
+ VALUE
1160
+ NetCDF_path(VALUE file)
1161
+ {
1162
+ char *path;
1163
+ struct Netcdf *ncfile;
1164
+
1165
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1166
+ path=ncfile->name;
1167
+ return(rb_str_new2(path));
1168
+ }
1169
+
1170
+ VALUE
1171
+ NetCDF_dim_length(VALUE Dim)
1172
+ {
1173
+ int ncid;
1174
+ int status;
1175
+ int dimid;
1176
+ size_t lengthp;
1177
+ struct NetCDFDim *Netcdf_dim;
1178
+
1179
+ Data_Get_Struct(Dim,struct NetCDFDim,Netcdf_dim);
1180
+ ncid=Netcdf_dim->ncid;
1181
+ dimid=Netcdf_dim->dimid;
1182
+
1183
+ status = nc_inq_dimlen(ncid,dimid,&lengthp);
1184
+ if(status != NC_NOERR) NC_RAISE(status);
1185
+
1186
+ return(INT2NUM(lengthp));
1187
+ }
1188
+
1189
+ VALUE
1190
+ NetCDF_dim_name(VALUE Dim,VALUE dimension_newname)
1191
+ {
1192
+ int ncid;
1193
+ int status;
1194
+ int dimid;
1195
+ char *c_dim_name;
1196
+ struct NetCDFDim *Netcdf_dim;
1197
+
1198
+ rb_secure(4);
1199
+ Data_Get_Struct(Dim,struct NetCDFDim,Netcdf_dim);
1200
+ ncid=Netcdf_dim->ncid;
1201
+ dimid=Netcdf_dim->dimid;
1202
+ Check_Type(dimension_newname,T_STRING);
1203
+ c_dim_name = StringValueCStr(dimension_newname);
1204
+
1205
+ status = nc_rename_dim(ncid,dimid,c_dim_name);
1206
+ if(status !=NC_NOERR) NC_RAISE(status);
1207
+
1208
+ return Qnil;
1209
+ }
1210
+
1211
+ VALUE
1212
+ NetCDF_dim_inqname(VALUE Dim)
1213
+ {
1214
+ int ncid;
1215
+ int status;
1216
+ int dimid;
1217
+ char c_dim_name[NC_MAX_NAME];
1218
+ struct NetCDFDim *Netcdf_dim;
1219
+ VALUE str;
1220
+
1221
+ Data_Get_Struct(Dim,struct NetCDFDim,Netcdf_dim);
1222
+ ncid=Netcdf_dim->ncid;
1223
+ dimid=Netcdf_dim->dimid;
1224
+
1225
+ status = nc_inq_dimname(ncid,dimid,c_dim_name);
1226
+ if(status !=NC_NOERR) NC_RAISE(status);
1227
+
1228
+ str = rb_str_new2(c_dim_name);
1229
+ OBJ_TAINT(str);
1230
+ return(str);
1231
+ }
1232
+
1233
+ VALUE
1234
+ NetCDF_dim_whether_unlimited(VALUE Dim)
1235
+ {
1236
+ int status;
1237
+ int uldid;
1238
+ struct NetCDFDim *Netcdf_dim;
1239
+
1240
+ Data_Get_Struct(Dim,struct NetCDFDim,Netcdf_dim);
1241
+ status=nc_inq_unlimdim(Netcdf_dim->ncid,&uldid);
1242
+ if(status !=NC_NOERR) NC_RAISE(status);
1243
+ if(Netcdf_dim->dimid == uldid){
1244
+ return(Qtrue);
1245
+ } else {
1246
+ return(Qfalse);
1247
+ }
1248
+ }
1249
+
1250
+ VALUE
1251
+ NetCDF_att_inq_name(VALUE Att)
1252
+ {
1253
+ char *c_att_name;
1254
+ struct NetCDFAtt *Netcdf_att;
1255
+ VALUE str;
1256
+
1257
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1258
+ c_att_name=Netcdf_att->name;
1259
+
1260
+ str = rb_str_new2(c_att_name);
1261
+ OBJ_TAINT(str);
1262
+ return(str);
1263
+ }
1264
+
1265
+ VALUE
1266
+ NetCDF_att_rename(VALUE Att,VALUE new_att_name)
1267
+ {
1268
+ int ncid;
1269
+ int status;
1270
+ int varid;
1271
+ char *c_att_name;
1272
+ char *c_new_att_name;
1273
+ struct NetCDFAtt *Netcdf_att;
1274
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1275
+ ncid=Netcdf_att->ncid;
1276
+ varid=Netcdf_att->varid;
1277
+
1278
+ c_att_name=Netcdf_att->name;
1279
+
1280
+ Check_Type(new_att_name,T_STRING);
1281
+ SafeStringValue(new_att_name);
1282
+ c_new_att_name=StringValueCStr(new_att_name);
1283
+
1284
+ status = nc_rename_att(ncid,varid,c_att_name,c_new_att_name);
1285
+ if(status != NC_NOERR) NC_RAISE(status);
1286
+
1287
+ strcpy(Netcdf_att->name,c_new_att_name);
1288
+ return Qnil;
1289
+ }
1290
+
1291
+ VALUE
1292
+ NetCDF_id2dim(VALUE file,VALUE dimid)
1293
+ {
1294
+ int ncid;
1295
+ int c_dimid;
1296
+ struct Netcdf *ncfile;
1297
+ struct NetCDFDim *Netcdf_dim;
1298
+ VALUE Dim;
1299
+
1300
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1301
+ ncid=ncfile->ncid;
1302
+ Check_Type(dimid,T_FIXNUM);
1303
+ c_dimid=NUM2INT(dimid);
1304
+ Netcdf_dim = NetCDF_dim_init(ncid,c_dimid);
1305
+ Dim=Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
1306
+ return(Dim);
1307
+ }
1308
+
1309
+ VALUE
1310
+ NetCDF_id2var(VALUE file,VALUE varid)
1311
+ {
1312
+ int ncid;
1313
+ int c_varid;
1314
+ struct Netcdf *ncfile;
1315
+ struct NetCDFVar *Netcdf_var;
1316
+ VALUE Var;
1317
+
1318
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1319
+ ncid=ncfile->ncid;
1320
+ Check_Type(varid,T_FIXNUM);
1321
+ c_varid=NUM2INT(varid);
1322
+ Netcdf_var = NetCDF_var_init(ncid,c_varid,file);
1323
+ Var=Data_Wrap_Struct(cNetCDFVar,nc_mark_obj,NetCDF_var_free,Netcdf_var);
1324
+ return(Var);
1325
+ }
1326
+
1327
+
1328
+ VALUE
1329
+ NetCDF_id2att(VALUE file,VALUE attnum)
1330
+ {
1331
+ int ncid;
1332
+ int c_attnum;
1333
+ int status;
1334
+ struct Netcdf *ncfile;
1335
+ struct NetCDFAtt *Netcdf_att;
1336
+ char *c_att_name;
1337
+ VALUE Att;
1338
+ c_att_name=ALLOCA_N(char,NC_MAX_NAME);
1339
+
1340
+ Data_Get_Struct(file,struct Netcdf,ncfile);
1341
+ ncid=ncfile->ncid;
1342
+
1343
+ Check_Type(attnum,T_FIXNUM);
1344
+ c_attnum=NUM2INT(attnum);
1345
+
1346
+ status = nc_inq_attname(ncid,NC_GLOBAL,c_attnum,c_att_name);
1347
+ if(status != NC_NOERR) NC_RAISE(status);
1348
+
1349
+ Netcdf_att=NetCDF_att_init(ncid,NC_GLOBAL,c_att_name);
1350
+
1351
+ Att=Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
1352
+ return(Att);
1353
+
1354
+ }
1355
+
1356
+ VALUE
1357
+ NetCDF_var_id2att(VALUE Var,VALUE attnum)
1358
+ {
1359
+ int ncid;
1360
+ int c_attnum;
1361
+ int status;
1362
+ int c_varid;
1363
+ struct NetCDFVar *Netcdf_var;
1364
+ struct NetCDFAtt *Netcdf_att;
1365
+ char *c_att_name;
1366
+ VALUE Att;
1367
+
1368
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1369
+ ncid=Netcdf_var->ncid;
1370
+ c_varid=Netcdf_var->varid;
1371
+
1372
+ Check_Type(attnum,T_FIXNUM);
1373
+ c_attnum=NUM2INT(attnum);
1374
+
1375
+ c_att_name=ALLOCA_N(char,NC_MAX_NAME);
1376
+
1377
+ status = nc_inq_attname(ncid,c_varid,c_attnum,c_att_name);
1378
+ if(status != NC_NOERR) NC_RAISE(status);
1379
+
1380
+ Netcdf_att=NetCDF_att_init(ncid,c_varid,c_att_name);
1381
+ Att=Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
1382
+ return(Att);
1383
+ }
1384
+
1385
+ VALUE
1386
+ NetCDF_var_dims(VALUE Var)
1387
+ {
1388
+ int ncid, *dimids, ndims, varid, i, status;
1389
+ struct NetCDFVar *Netcdf_var;
1390
+ struct NetCDFDim *Netcdf_dim;
1391
+ VALUE Dims;
1392
+
1393
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1394
+ ncid = Netcdf_var->ncid;
1395
+ varid = Netcdf_var->varid;
1396
+ status = nc_inq_varndims(ncid,varid,&ndims);
1397
+ if(status != NC_NOERR) NC_RAISE(status);
1398
+ dimids=ALLOCA_N(int,ndims);
1399
+ status = nc_inq_vardimid(ncid,varid,dimids);
1400
+ if(status != NC_NOERR) NC_RAISE(status);
1401
+ Dims = rb_ary_new();
1402
+ for(i=0;i<ndims;i++){
1403
+ Netcdf_dim = NetCDF_dim_init(ncid,dimids[ndims-1-i]);
1404
+ rb_ary_push(Dims,
1405
+ Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim));
1406
+ }
1407
+ return(Dims);
1408
+ }
1409
+
1410
+ VALUE
1411
+ NetCDF_var_dim(VALUE Var, VALUE ith)
1412
+ {
1413
+ int ncid, *dimids, ndims, varid, status, c_ith;
1414
+ struct NetCDFVar *Netcdf_var;
1415
+ struct NetCDFDim *Netcdf_dim;
1416
+ VALUE Dim;
1417
+
1418
+ Check_Type(ith,T_FIXNUM);
1419
+ c_ith=NUM2INT(ith);
1420
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1421
+ ncid = Netcdf_var->ncid;
1422
+ varid = Netcdf_var->varid;
1423
+ status = nc_inq_varndims(ncid,varid,&ndims);
1424
+ if(status != NC_NOERR) NC_RAISE(status);
1425
+ if(c_ith < 0 || c_ith >= ndims) {
1426
+ rb_raise(rb_eNetcdfError, "dimension count less than zero or greater than ndims-1");
1427
+ }
1428
+ dimids=ALLOCA_N(int,ndims);
1429
+ status = nc_inq_vardimid(ncid,varid,dimids);
1430
+ if(status != NC_NOERR) NC_RAISE(status);
1431
+ Netcdf_dim = NetCDF_dim_init(ncid,dimids[ndims-1-c_ith]);
1432
+ Dim = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
1433
+ return(Dim);
1434
+ }
1435
+
1436
+ VALUE
1437
+ NetCDF_att_copy(VALUE Att,VALUE Var_or_File)
1438
+ {
1439
+ int ncid_in,ncid_out;
1440
+ int status;
1441
+ int varid_in,varid_out;
1442
+ char *att_name;
1443
+ struct NetCDFAtt *Netcdf_att;
1444
+ struct NetCDFVar *Netcdf_var;
1445
+ struct Netcdf *ncfile;
1446
+ struct NetCDFAtt *Netcdf_att_out;
1447
+
1448
+ rb_secure(4);
1449
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1450
+ ncid_in=Netcdf_att->ncid;
1451
+ varid_in=Netcdf_att->varid;
1452
+ att_name=Netcdf_att->name;
1453
+
1454
+ if( rb_obj_is_kind_of(Var_or_File, cNetCDFVar) ){
1455
+ Data_Get_Struct(Var_or_File,struct NetCDFVar, Netcdf_var);
1456
+ ncid_out=Netcdf_var->ncid;
1457
+ varid_out=Netcdf_var->varid;
1458
+ } else if ( rb_obj_is_kind_of(Var_or_File, cNetCDF) ){
1459
+ Data_Get_Struct(Var_or_File,struct Netcdf, ncfile);
1460
+ ncid_out=ncfile->ncid;
1461
+ varid_out=NC_GLOBAL;
1462
+ } else {
1463
+ rb_raise(rb_eNetcdfError,"The argument must be a NetCDFVar or a NetCDF");
1464
+ }
1465
+
1466
+ status = nc_copy_att(ncid_in,varid_in,att_name,ncid_out,varid_out);
1467
+ if(status != NC_NOERR) NC_RAISE(status);
1468
+ Netcdf_att_out = NetCDF_att_init(ncid_out,varid_out,att_name);
1469
+ return (Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att_out));
1470
+ }
1471
+
1472
+ VALUE
1473
+ NetCDF_att_atttype(VALUE Att)
1474
+ {
1475
+ int ncid;
1476
+ int varid;
1477
+ int status;
1478
+ char *att_name;
1479
+ const char *Attname;
1480
+ struct NetCDFAtt *Netcdf_att;
1481
+ nc_type xtypep;
1482
+
1483
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1484
+ ncid = Netcdf_att->ncid;
1485
+ varid = Netcdf_att->varid;
1486
+ att_name = Netcdf_att->name;
1487
+
1488
+ status = nc_inq_atttype(ncid,varid,att_name,&xtypep);
1489
+ if(status != NC_NOERR) NC_RAISE(status);
1490
+
1491
+ Attname = nctype2natype(xtypep);
1492
+ return(rb_str_new2(Attname));
1493
+ }
1494
+
1495
+ VALUE
1496
+ NetCDF_att_typecode(VALUE Att)
1497
+ {
1498
+ int ncid;
1499
+ int varid;
1500
+ int status;
1501
+ char *att_name;
1502
+ struct NetCDFAtt *Netcdf_att;
1503
+ nc_type xtypep;
1504
+
1505
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1506
+ ncid = Netcdf_att->ncid;
1507
+ varid = Netcdf_att->varid;
1508
+ att_name = Netcdf_att->name;
1509
+
1510
+ status = nc_inq_atttype(ncid,varid,att_name,&xtypep);
1511
+ if(status != NC_NOERR) NC_RAISE(status);
1512
+
1513
+ return(INT2NUM(nctype2natypecode(xtypep)));
1514
+ }
1515
+
1516
+ VALUE
1517
+ NetCDF_att_delete(VALUE Att)
1518
+ {
1519
+ int ncid;
1520
+ int status;
1521
+ int varid;
1522
+ char *c_att_name;
1523
+ struct NetCDFAtt *Netcdf_att;
1524
+
1525
+ rb_secure(4);
1526
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1527
+
1528
+ ncid=Netcdf_att->ncid;
1529
+ varid=Netcdf_att->varid;
1530
+ c_att_name=Netcdf_att->name;
1531
+
1532
+ status = nc_del_att(ncid,varid,c_att_name);
1533
+ if(status != NC_NOERR) NC_RAISE(status);
1534
+
1535
+ return Qnil;
1536
+ }
1537
+
1538
+ VALUE
1539
+ NetCDF_att_put(VALUE Att,VALUE value,VALUE atttype)
1540
+ /*
1541
+ * atttype: nil or a String ("string","int",etc). If nil,
1542
+ * the type of attribute is determined from the type of value
1543
+ */
1544
+ {
1545
+ struct NetCDFAtt *ncatt;
1546
+
1547
+ rb_secure(4);
1548
+ Data_Get_Struct(Att,struct NetCDFAtt,ncatt);
1549
+ return( NetCDF_put_att__(ncatt->ncid, ncatt->name, value,
1550
+ atttype, ncatt->varid) );
1551
+ }
1552
+
1553
+ VALUE
1554
+ NetCDF_att_get(VALUE Att)
1555
+ {
1556
+ int ncid;
1557
+ int varid;
1558
+ char *c_attname;
1559
+ int status;
1560
+ struct NetCDFAtt *Netcdf_att;
1561
+ nc_type xtypep;
1562
+ size_t lenp;
1563
+ int attlen[1]; /* NArray uses int instead of size_t */
1564
+ char *tp;
1565
+ unsigned char *up;
1566
+ short *sp;
1567
+ int *ip;
1568
+ float *fp;
1569
+ double *dp;
1570
+ VALUE NArray;
1571
+ VALUE str;
1572
+
1573
+ Data_Get_Struct(Att,struct NetCDFAtt,Netcdf_att);
1574
+ ncid = Netcdf_att->ncid;
1575
+ varid = Netcdf_att->varid;
1576
+ c_attname = Netcdf_att->name;
1577
+
1578
+ status = nc_inq_atttype(ncid,varid,c_attname,&xtypep);
1579
+ if(status != NC_NOERR) NC_RAISE(status);
1580
+
1581
+ switch(xtypep){
1582
+ case NC_CHAR:
1583
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1584
+ if(status != NC_NOERR) NC_RAISE(status);
1585
+ tp = ALLOCA_N(char,lenp+1);
1586
+ tp[lenp]= '\0';
1587
+ status = nc_get_att_text(ncid,varid,c_attname,tp);
1588
+ if(status != NC_NOERR) NC_RAISE(status);
1589
+ str = rb_str_new2(tp);
1590
+ OBJ_TAINT(str);
1591
+ return(str);
1592
+ break;
1593
+ case NC_BYTE:
1594
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1595
+ if(status != NC_NOERR) NC_RAISE(status);
1596
+
1597
+ attlen[0]=lenp;
1598
+ Cbyte_to_NArray(NArray,1,attlen,up);
1599
+
1600
+ status = nc_get_att_uchar(ncid,varid,c_attname,up);
1601
+ if(status != NC_NOERR) NC_RAISE(status);
1602
+
1603
+ OBJ_TAINT(NArray);
1604
+ return NArray;
1605
+ break;
1606
+ case NC_SHORT:
1607
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1608
+ if(status != NC_NOERR) NC_RAISE(status);
1609
+
1610
+ attlen[0]=lenp;
1611
+ Csint_to_NArray(NArray,1,attlen,sp);
1612
+
1613
+ status = nc_get_att_short(ncid,varid,c_attname,sp);
1614
+ if(status != NC_NOERR) NC_RAISE(status);
1615
+ OBJ_TAINT(NArray);
1616
+ return NArray;
1617
+ break;
1618
+ case NC_INT:
1619
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1620
+ if(status != NC_NOERR) NC_RAISE(status);
1621
+
1622
+ attlen[0]=lenp;
1623
+ Clint_to_NArray(NArray,1,attlen,ip);
1624
+
1625
+ status = nc_get_att_int(ncid,varid,c_attname,ip);
1626
+ if(status != NC_NOERR) NC_RAISE(status);
1627
+
1628
+ OBJ_TAINT(NArray);
1629
+ return NArray;
1630
+ break;
1631
+ case NC_FLOAT:
1632
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1633
+ if(status != NC_NOERR) NC_RAISE(status);
1634
+
1635
+ attlen[0]=lenp;
1636
+ Cfloat_to_NArray(NArray,1,attlen,fp);
1637
+
1638
+ status = nc_get_att_float(ncid,varid,c_attname,fp);
1639
+ if(status != NC_NOERR) NC_RAISE(status);
1640
+
1641
+ OBJ_TAINT(NArray);
1642
+ return NArray;
1643
+ break;
1644
+ case NC_DOUBLE:
1645
+ status = nc_inq_attlen(ncid,varid,c_attname,&lenp);
1646
+ if(status != NC_NOERR) NC_RAISE(status);
1647
+
1648
+ attlen[0]=lenp;
1649
+ Cdouble_to_NArray(NArray,1,attlen,dp);
1650
+
1651
+ status = nc_get_att_double(ncid,varid,c_attname,dp);
1652
+ if(status != NC_NOERR) NC_RAISE(status);
1653
+ OBJ_TAINT(NArray);
1654
+ return NArray;
1655
+ break;
1656
+ default:
1657
+ rb_raise(rb_eNetcdfError,"atttype isn't supported in netCDF");
1658
+ }
1659
+ return Qnil;
1660
+ }
1661
+
1662
+
1663
+ VALUE
1664
+ NetCDF_var_inq_name(VALUE Var)
1665
+ {
1666
+ int ncid;
1667
+ int status;
1668
+ int varid;
1669
+ char c_var_name[NC_MAX_NAME];
1670
+ struct NetCDFVar *Netcdf_var;
1671
+ VALUE Var_name;
1672
+
1673
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1674
+
1675
+ ncid=Netcdf_var->ncid;
1676
+ varid=Netcdf_var->varid;
1677
+ status = nc_inq_varname(ncid,varid,c_var_name);
1678
+ if(status != NC_NOERR) NC_RAISE(status);
1679
+
1680
+ Var_name=rb_str_new2(c_var_name);
1681
+ OBJ_TAINT(Var_name);
1682
+ return Var_name;
1683
+ }
1684
+
1685
+ VALUE
1686
+ NetCDF_var_ndims(VALUE Var)
1687
+ {
1688
+ int ncid;
1689
+ int status;
1690
+ int varid;
1691
+ int ndimsp;
1692
+ struct NetCDFVar *Netcdf_var;
1693
+ VALUE Var_ndims;
1694
+
1695
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1696
+
1697
+ ncid=Netcdf_var->ncid;
1698
+ varid=Netcdf_var->varid;
1699
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
1700
+ if(status != NC_NOERR) NC_RAISE(status);
1701
+ Var_ndims=INT2FIX(ndimsp);
1702
+ return Var_ndims;
1703
+ }
1704
+
1705
+ VALUE
1706
+ NetCDF_var_vartype(VALUE Var)
1707
+ {
1708
+ int ncid;
1709
+ int status;
1710
+ int varid;
1711
+ nc_type xtypep;
1712
+ struct NetCDFVar *Netcdf_var;
1713
+ const char *Vartype;
1714
+
1715
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1716
+
1717
+ ncid=Netcdf_var->ncid;
1718
+ varid=Netcdf_var->varid;
1719
+
1720
+ status = nc_inq_vartype(ncid,varid,&xtypep);
1721
+ if(status != NC_NOERR) NC_RAISE(status);
1722
+
1723
+ Vartype=nctype2natype(xtypep);
1724
+ return(rb_str_new2(Vartype));
1725
+ }
1726
+
1727
+ VALUE
1728
+ NetCDF_var_typecode(VALUE Var)
1729
+ {
1730
+ int ncid;
1731
+ int status;
1732
+ int varid;
1733
+ nc_type xtypep;
1734
+ struct NetCDFVar *Netcdf_var;
1735
+
1736
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1737
+
1738
+ ncid=Netcdf_var->ncid;
1739
+ varid=Netcdf_var->varid;
1740
+
1741
+ status = nc_inq_vartype(ncid,varid,&xtypep);
1742
+ if(status != NC_NOERR) NC_RAISE(status);
1743
+
1744
+ return(INT2NUM(nctype2natypecode(xtypep)));
1745
+ }
1746
+
1747
+
1748
+ VALUE
1749
+ NetCDF_var_natts(VALUE Var)
1750
+ {
1751
+ int ncid;
1752
+ int status;
1753
+ int varid;
1754
+ int nattsp;
1755
+ struct NetCDFVar *Netcdf_var;
1756
+ VALUE Var_natts;
1757
+
1758
+
1759
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1760
+
1761
+ ncid=Netcdf_var->ncid;
1762
+ varid=Netcdf_var->varid;
1763
+
1764
+ status= nc_inq_varnatts(ncid,varid,&nattsp);
1765
+ if(status !=NC_NOERR) NC_RAISE(status);
1766
+
1767
+ Var_natts=INT2FIX(nattsp);
1768
+ return Var_natts;
1769
+ }
1770
+
1771
+ VALUE
1772
+ NetCDF_var_file(VALUE Var)
1773
+ {
1774
+ struct NetCDFVar *Netcdf_var;
1775
+ /* VALUE file; */
1776
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1777
+
1778
+ return (Netcdf_var->file);
1779
+ }
1780
+
1781
+ VALUE
1782
+ NetCDF_var_rename(VALUE Var,VALUE var_new_name)
1783
+ {
1784
+ int ncid;
1785
+ int status;
1786
+ int varid;
1787
+ char *c_var_new_name;
1788
+ struct NetCDFVar *Netcdf_var;
1789
+
1790
+ rb_secure(4);
1791
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1792
+ ncid=Netcdf_var->ncid;
1793
+ varid=Netcdf_var->varid;
1794
+
1795
+ Check_Type(var_new_name,T_STRING);
1796
+ c_var_new_name=StringValueCStr(var_new_name);
1797
+
1798
+ status = nc_rename_var(ncid,varid,c_var_new_name);
1799
+ if(status !=NC_NOERR) NC_RAISE(status);
1800
+
1801
+ return Qnil;
1802
+ }
1803
+
1804
+ VALUE
1805
+ NetCDF_var_att(VALUE Var,VALUE att_name)
1806
+ {
1807
+ int ncid;
1808
+ int status;
1809
+ int varid;
1810
+ char *c_att_name;
1811
+ int c_attnump;
1812
+ struct NetCDFVar *Netcdf_var;
1813
+ struct NetCDFAtt *Netcdf_att;
1814
+ VALUE Att;
1815
+
1816
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1817
+
1818
+ ncid=Netcdf_var->ncid;
1819
+ varid=Netcdf_var->varid;
1820
+
1821
+ Check_Type(att_name,T_STRING);
1822
+ c_att_name=StringValueCStr(att_name);
1823
+
1824
+ status = nc_inq_attid(ncid,varid,c_att_name,&c_attnump);
1825
+ if(status == NC_NOERR){
1826
+ Netcdf_att=NetCDF_att_init(ncid,varid,c_att_name);
1827
+ Att=Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
1828
+ return Att;
1829
+ }
1830
+ else if(status == NC_ENOTATT){
1831
+ return Qnil;
1832
+ }
1833
+ else{
1834
+ NC_RAISE(status);
1835
+ return Qnil;
1836
+ }
1837
+ }
1838
+
1839
+ /* Redifinition of the "==" and "eql?" methods */
1840
+
1841
+ VALUE
1842
+ NetCDF_eql(VALUE filea,VALUE fileb)
1843
+ {
1844
+ struct Netcdf *ncfilea;
1845
+ struct Netcdf *ncfileb;
1846
+
1847
+ if( rb_obj_is_kind_of(fileb, cNetCDF) ){
1848
+ Data_Get_Struct(filea,struct Netcdf,ncfilea);
1849
+ Data_Get_Struct(fileb,struct Netcdf,ncfileb);
1850
+
1851
+ if(ncfilea->ncid == ncfileb->ncid &&
1852
+ strcmp(ncfilea->name,ncfileb->name)==0){
1853
+ return Qtrue;
1854
+ } else {
1855
+ return Qfalse;
1856
+ }
1857
+ } else {
1858
+ return Qfalse;
1859
+ }
1860
+ }
1861
+
1862
+ VALUE
1863
+ NetCDF_var_eql(VALUE Vara,VALUE Varb)
1864
+ {
1865
+ struct NetCDFVar *Netcdf_vara;
1866
+ struct NetCDFVar *Netcdf_varb;
1867
+
1868
+ if( rb_obj_is_kind_of(Varb, cNetCDFVar) ){
1869
+ Data_Get_Struct(Vara,struct NetCDFVar,Netcdf_vara);
1870
+ Data_Get_Struct(Varb,struct NetCDFVar,Netcdf_varb);
1871
+
1872
+ if(Netcdf_vara->ncid == Netcdf_varb->ncid &&
1873
+ Netcdf_vara->varid == Netcdf_varb->varid){
1874
+ return Qtrue;
1875
+ } else {
1876
+ return Qfalse;
1877
+ }
1878
+ } else {
1879
+ return Qfalse;
1880
+ }
1881
+ }
1882
+
1883
+ VALUE
1884
+ NetCDF_dim_eql(VALUE Dima,VALUE Dimb)
1885
+ {
1886
+ struct NetCDFDim *Netcdf_dima;
1887
+ struct NetCDFDim *Netcdf_dimb;
1888
+
1889
+ if( rb_obj_is_kind_of(Dimb, cNetCDFDim) ){
1890
+ Data_Get_Struct(Dima,struct NetCDFDim,Netcdf_dima);
1891
+ Data_Get_Struct(Dimb,struct NetCDFDim,Netcdf_dimb);
1892
+
1893
+ if(Netcdf_dima->ncid == Netcdf_dimb->ncid &&
1894
+ Netcdf_dima->dimid == Netcdf_dimb->dimid){
1895
+ return Qtrue;
1896
+ } else {
1897
+ return Qfalse;
1898
+ }
1899
+ } else {
1900
+ return Qfalse;
1901
+ }
1902
+ }
1903
+
1904
+ VALUE
1905
+ NetCDF_att_eql(VALUE Atta,VALUE Attb)
1906
+ {
1907
+ struct NetCDFAtt *Netcdf_atta;
1908
+ struct NetCDFAtt *Netcdf_attb;
1909
+
1910
+ if( rb_obj_is_kind_of(Attb, cNetCDFAtt) ){
1911
+ Data_Get_Struct(Atta,struct NetCDFAtt,Netcdf_atta);
1912
+ Data_Get_Struct(Attb,struct NetCDFAtt,Netcdf_attb);
1913
+
1914
+ if(Netcdf_atta->ncid == Netcdf_atta->ncid &&
1915
+ Netcdf_atta->varid == Netcdf_attb->varid &&
1916
+ strcmp(Netcdf_atta->name,Netcdf_attb->name)==0){
1917
+ return Qtrue;
1918
+ } else {
1919
+ return Qfalse;
1920
+ }
1921
+ } else {
1922
+ return Qfalse;
1923
+ }
1924
+ }
1925
+
1926
+ /* Follow methods is to connect "NArray" with "Netcdf" */
1927
+ VALUE
1928
+ NetCDF_get_var_char(VALUE Var)
1929
+ {
1930
+ int ncid;
1931
+ int varid;
1932
+ int status;
1933
+ unsigned char *ptr;
1934
+ struct NetCDFVar *Netcdf_var;
1935
+ int i=0;
1936
+ int ndimsp;
1937
+ int *dimids;
1938
+ size_t lengthp;
1939
+ int *shape; /* NArray uses int instead of size_t */
1940
+ VALUE NArray;
1941
+
1942
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1943
+ ncid = Netcdf_var->ncid;
1944
+ varid = Netcdf_var->varid;
1945
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
1946
+ if(status != NC_NOERR) NC_RAISE(status);
1947
+ dimids = ALLOCA_N(int,ndimsp);
1948
+ if (ndimsp != 0){
1949
+ shape = ALLOCA_N(int,ndimsp);
1950
+ for(i=0;i<ndimsp;i++){
1951
+ status = nc_inq_vardimid(ncid,varid,dimids);
1952
+ if(status != NC_NOERR) NC_RAISE(status);
1953
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
1954
+ shape[ndimsp-1-i]=lengthp;
1955
+ }
1956
+ } else {
1957
+ ndimsp = 1;
1958
+ shape = ALLOCA_N(int,1);
1959
+ shape[0]=1;
1960
+ }
1961
+
1962
+ Cbyte_to_NArray(NArray,ndimsp,shape,ptr);
1963
+
1964
+ status = nc_get_var_text(ncid,varid,(char *)ptr);
1965
+ if(status != NC_NOERR) NC_RAISE(status);
1966
+
1967
+ OBJ_TAINT(NArray);
1968
+ return NArray;
1969
+ }
1970
+
1971
+ VALUE
1972
+ NetCDF_get_var_byte(VALUE Var)
1973
+ {
1974
+ int ncid;
1975
+ int varid;
1976
+ int status;
1977
+ unsigned char *ptr;
1978
+ struct NetCDFVar *Netcdf_var;
1979
+ int i=0;
1980
+ int ndimsp;
1981
+ int *dimids;
1982
+ size_t lengthp;
1983
+ int *shape; /* NArray uses int instead of size_t */
1984
+ VALUE NArray;
1985
+
1986
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
1987
+ ncid = Netcdf_var->ncid;
1988
+ varid = Netcdf_var->varid;
1989
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
1990
+ if(status != NC_NOERR) NC_RAISE(status);
1991
+ dimids = ALLOCA_N(int,ndimsp);
1992
+ if (ndimsp != 0){
1993
+ shape = ALLOCA_N(int,ndimsp);
1994
+ for(i=0;i<ndimsp;i++){
1995
+ status = nc_inq_vardimid(ncid,varid,dimids);
1996
+ if(status != NC_NOERR) NC_RAISE(status);
1997
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
1998
+ shape[ndimsp-1-i]=lengthp;
1999
+ }
2000
+ } else {
2001
+ ndimsp = 1;
2002
+ shape = ALLOCA_N(int,1);
2003
+ shape[0]=1;
2004
+ }
2005
+
2006
+ Cbyte_to_NArray(NArray,ndimsp,shape,ptr);
2007
+
2008
+ status = nc_get_var_uchar(ncid,varid,ptr);
2009
+ if(status != NC_NOERR) NC_RAISE(status);
2010
+
2011
+ OBJ_TAINT(NArray);
2012
+ return NArray;
2013
+ }
2014
+
2015
+ VALUE
2016
+ NetCDF_get_var_sint(VALUE Var)
2017
+ {
2018
+ int ncid;
2019
+ int varid;
2020
+ int status;
2021
+ short *ptr;
2022
+ struct NetCDFVar *Netcdf_var;
2023
+ int i=0;
2024
+ int ndimsp;
2025
+ int *dimids;
2026
+ size_t lengthp;
2027
+ int *shape; /* NArray uses int instead of size_t */
2028
+ VALUE NArray;
2029
+
2030
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2031
+ ncid = Netcdf_var->ncid;
2032
+ varid = Netcdf_var->varid;
2033
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
2034
+ if(status != NC_NOERR) NC_RAISE(status);
2035
+ dimids = ALLOCA_N(int,ndimsp);
2036
+ if (ndimsp != 0){
2037
+ shape = ALLOCA_N(int,ndimsp);
2038
+ for(i=0;i<ndimsp;i++){
2039
+ status = nc_inq_vardimid(ncid,varid,dimids);
2040
+ if(status != NC_NOERR) NC_RAISE(status);
2041
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
2042
+ shape[ndimsp-1-i]=lengthp;
2043
+ }
2044
+ } else {
2045
+ ndimsp = 1;
2046
+ shape = ALLOCA_N(int,1);
2047
+ shape[0]=1;
2048
+ }
2049
+
2050
+ Csint_to_NArray(NArray,ndimsp,shape,ptr);
2051
+
2052
+ status = nc_get_var_short(ncid,varid,ptr);
2053
+ if(status != NC_NOERR) NC_RAISE(status);
2054
+
2055
+ OBJ_TAINT(NArray);
2056
+ return NArray;
2057
+ }
2058
+
2059
+ VALUE
2060
+ NetCDF_get_var_int(VALUE Var)
2061
+ {
2062
+ int ncid;
2063
+ int varid;
2064
+ int status;
2065
+ int *ptr;
2066
+ struct NetCDFVar *Netcdf_var;
2067
+ int i=0;
2068
+ int ndimsp;
2069
+ int *dimids;
2070
+ size_t lengthp;
2071
+ int *shape; /* NArray uses int instead of size_t */
2072
+ VALUE NArray;
2073
+
2074
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2075
+ ncid = Netcdf_var->ncid;
2076
+ varid = Netcdf_var->varid;
2077
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
2078
+ if(status != NC_NOERR) NC_RAISE(status);
2079
+ dimids = ALLOCA_N(int,ndimsp);
2080
+ if (ndimsp != 0){
2081
+ shape = ALLOCA_N(int,ndimsp);
2082
+ for(i=0;i<ndimsp;i++){
2083
+ status = nc_inq_vardimid(ncid,varid,dimids);
2084
+ if(status != NC_NOERR) NC_RAISE(status);
2085
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
2086
+ shape[ndimsp-1-i]=lengthp;
2087
+ }
2088
+ } else {
2089
+ ndimsp = 1;
2090
+ shape = ALLOCA_N(int,1);
2091
+ shape[0]=1;
2092
+ }
2093
+
2094
+ Clint_to_NArray(NArray,ndimsp,shape,ptr);
2095
+
2096
+ status = nc_get_var_int(ncid,varid,ptr);
2097
+ if(status != NC_NOERR) NC_RAISE(status);
2098
+
2099
+ OBJ_TAINT(NArray);
2100
+ return NArray;
2101
+ }
2102
+
2103
+ VALUE
2104
+ NetCDF_get_var_float(VALUE Var)
2105
+ {
2106
+ int ncid;
2107
+ int varid;
2108
+ int status;
2109
+ float *ptr;
2110
+ struct NetCDFVar *Netcdf_var;
2111
+ int i=0;
2112
+ int ndimsp;
2113
+ int *dimids;
2114
+ size_t lengthp;
2115
+ int *shape; /* NArray uses int instead of size_t */
2116
+ VALUE NArray;
2117
+
2118
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2119
+ ncid = Netcdf_var->ncid;
2120
+ varid = Netcdf_var->varid;
2121
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
2122
+ if(status != NC_NOERR) NC_RAISE(status);
2123
+ dimids = ALLOCA_N(int,ndimsp);
2124
+ if (ndimsp != 0){
2125
+ shape = ALLOCA_N(int,ndimsp);
2126
+ for(i=0;i<ndimsp;i++){
2127
+ status = nc_inq_vardimid(ncid,varid,dimids);
2128
+ if(status != NC_NOERR) NC_RAISE(status);
2129
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
2130
+ shape[ndimsp-1-i]=lengthp;
2131
+ }
2132
+ } else {
2133
+ ndimsp = 1;
2134
+ shape = ALLOCA_N(int,1);
2135
+ shape[0]=1;
2136
+ }
2137
+
2138
+ Cfloat_to_NArray(NArray,ndimsp,shape,ptr);
2139
+
2140
+ status = nc_get_var_float(ncid,varid,ptr);
2141
+ if(status != NC_NOERR) NC_RAISE(status);
2142
+
2143
+ OBJ_TAINT(NArray);
2144
+ return NArray;
2145
+ }
2146
+
2147
+ VALUE
2148
+ NetCDF_get_var_double(VALUE Var)
2149
+ {
2150
+ int ncid;
2151
+ int varid;
2152
+ int status;
2153
+ double *ptr;
2154
+ struct NetCDFVar *Netcdf_var;
2155
+ int i=0;
2156
+ int ndimsp;
2157
+ int *dimids;
2158
+ size_t lengthp;
2159
+ int *shape; /* NArray uses int instead of size_t */
2160
+ VALUE NArray;
2161
+
2162
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2163
+ ncid = Netcdf_var->ncid;
2164
+ varid = Netcdf_var->varid;
2165
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
2166
+ if(status != NC_NOERR) NC_RAISE(status);
2167
+ dimids = ALLOCA_N(int,ndimsp);
2168
+ if (ndimsp != 0){
2169
+ shape = ALLOCA_N(int,ndimsp);
2170
+ for(i=0;i<ndimsp;i++){
2171
+ status = nc_inq_vardimid(ncid,varid,dimids);
2172
+ if(status != NC_NOERR) NC_RAISE(status);
2173
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
2174
+ shape[ndimsp-1-i]=lengthp;
2175
+ }
2176
+ } else {
2177
+ ndimsp = 1;
2178
+ shape = ALLOCA_N(int,1);
2179
+ shape[0]=1;
2180
+ }
2181
+
2182
+ Cdouble_to_NArray(NArray,ndimsp,shape,ptr);
2183
+
2184
+ status = nc_get_var_double(ncid,varid,ptr);
2185
+ if(status != NC_NOERR) NC_RAISE(status);
2186
+
2187
+ OBJ_TAINT(NArray);
2188
+ return NArray;
2189
+ }
2190
+
2191
+ VALUE
2192
+ NetCDF_get_var1_char(VALUE Var,VALUE start)
2193
+ {
2194
+ int ncid;
2195
+ int varid;
2196
+ int status;
2197
+ unsigned char *ptr;
2198
+ int i;
2199
+ struct NetCDFVar *Netcdf_var;
2200
+ long l_start;
2201
+ size_t *c_start;
2202
+ int ndims;
2203
+ int dimids[NC_MAX_DIMS];
2204
+ size_t dimlen;
2205
+ int *c_count;
2206
+ int nc_tlen=0;
2207
+ VALUE NArray;
2208
+
2209
+
2210
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2211
+ ncid = Netcdf_var->ncid;
2212
+ varid = Netcdf_var->varid;
2213
+ status = nc_inq_varndims(ncid, varid, &ndims);
2214
+ if(status != NC_NOERR)NC_RAISE(status);
2215
+ if(ndims == 0) {
2216
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2217
+ }
2218
+
2219
+ Check_Type(start,T_ARRAY);
2220
+ if(RARRAY_LEN(start) < ndims) {
2221
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2222
+ }
2223
+
2224
+ c_start=ALLOCA_N(size_t,ndims);
2225
+ c_count=ALLOCA_N(int,ndims);
2226
+ for(i=0;i<ndims;i++){
2227
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2228
+ status = nc_inq_vardimid(ncid,varid,dimids);
2229
+ if(status != NC_NOERR) NC_RAISE(status);
2230
+ if(l_start < 0) {
2231
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2232
+ if(status != NC_NOERR) NC_RAISE(status);
2233
+ l_start += dimlen;
2234
+ }
2235
+ c_start[i]=l_start;
2236
+
2237
+ c_count[i]=1;
2238
+ nc_tlen = 1+nc_tlen;
2239
+ }
2240
+
2241
+
2242
+
2243
+
2244
+ Cbyte_to_NArray(NArray,ndims,c_count,ptr);
2245
+ status = nc_get_var1_text(ncid,varid,c_start,(char *)ptr);
2246
+ if(status != NC_NOERR) NC_RAISE(status);
2247
+
2248
+ OBJ_TAINT(NArray);
2249
+ return NArray;
2250
+
2251
+ }
2252
+
2253
+ VALUE
2254
+ NetCDF_get_var1_byte(VALUE Var,VALUE start)
2255
+ {
2256
+ int ncid;
2257
+ int varid;
2258
+ int status;
2259
+ unsigned char *ptr;
2260
+ int i;
2261
+ struct NetCDFVar *Netcdf_var;
2262
+ long l_start;
2263
+ size_t *c_start;
2264
+ int ndims;
2265
+ int dimids[NC_MAX_DIMS];
2266
+ size_t dimlen;
2267
+ int *c_count;
2268
+ int nc_tlen=0;
2269
+ VALUE NArray;
2270
+
2271
+
2272
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2273
+ ncid = Netcdf_var->ncid;
2274
+ varid = Netcdf_var->varid;
2275
+ status = nc_inq_varndims(ncid, varid, &ndims);
2276
+ if(status != NC_NOERR)NC_RAISE(status);
2277
+ if(ndims == 0) {
2278
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2279
+ }
2280
+
2281
+ Check_Type(start,T_ARRAY);
2282
+ if(RARRAY_LEN(start) < ndims) {
2283
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2284
+ }
2285
+
2286
+ c_start=ALLOCA_N(size_t,ndims);
2287
+ c_count=ALLOCA_N(int,ndims);
2288
+ for(i=0;i<ndims;i++){
2289
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2290
+ status = nc_inq_vardimid(ncid,varid,dimids);
2291
+ if(status != NC_NOERR) NC_RAISE(status);
2292
+ if(l_start < 0) {
2293
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2294
+ if(status != NC_NOERR) NC_RAISE(status);
2295
+ l_start += dimlen;
2296
+ }
2297
+ c_start[i]=l_start;
2298
+
2299
+ c_count[i]=1;
2300
+ nc_tlen = 1+nc_tlen;
2301
+ }
2302
+
2303
+
2304
+
2305
+
2306
+ Cbyte_to_NArray(NArray,ndims,c_count,ptr);
2307
+ status = nc_get_var1_uchar(ncid,varid,c_start,ptr);
2308
+ if(status != NC_NOERR) NC_RAISE(status);
2309
+
2310
+ OBJ_TAINT(NArray);
2311
+ return NArray;
2312
+
2313
+ }
2314
+
2315
+ VALUE
2316
+ NetCDF_get_var1_sint(VALUE Var,VALUE start)
2317
+ {
2318
+ int ncid;
2319
+ int varid;
2320
+ int status;
2321
+ short *ptr;
2322
+ int i;
2323
+ struct NetCDFVar *Netcdf_var;
2324
+ long l_start;
2325
+ size_t *c_start;
2326
+ int ndims;
2327
+ int dimids[NC_MAX_DIMS];
2328
+ size_t dimlen;
2329
+ int *c_count;
2330
+ int nc_tlen=0;
2331
+ VALUE NArray;
2332
+
2333
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2334
+ ncid = Netcdf_var->ncid;
2335
+ varid = Netcdf_var->varid;
2336
+ status = nc_inq_varndims(ncid, varid, &ndims);
2337
+ if(status != NC_NOERR) NC_RAISE(status);
2338
+ if(ndims == 0) {
2339
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2340
+ }
2341
+
2342
+ Check_Type(start,T_ARRAY);
2343
+ if(RARRAY_LEN(start) < ndims) {
2344
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2345
+ }
2346
+
2347
+ c_start=ALLOCA_N(size_t,ndims);
2348
+ c_count=ALLOCA_N(int,ndims);
2349
+ for(i=0;i<ndims;i++){
2350
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2351
+ status = nc_inq_vardimid(ncid,varid,dimids);
2352
+ if(status != NC_NOERR) NC_RAISE(status);
2353
+ if(l_start < 0) {
2354
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2355
+ if(status != NC_NOERR) NC_RAISE(status);
2356
+ l_start += dimlen;
2357
+ }
2358
+ c_start[i]=l_start;
2359
+ c_count[i]=1;
2360
+ nc_tlen = nc_tlen+1;
2361
+ }
2362
+
2363
+ Csint_to_NArray(NArray,ndims,c_count,ptr);
2364
+
2365
+ status = nc_get_var1_short(ncid,varid,c_start,ptr);
2366
+ if(status != NC_NOERR) NC_RAISE(status);
2367
+
2368
+ OBJ_TAINT(NArray);
2369
+ return NArray;
2370
+
2371
+ }
2372
+
2373
+ VALUE
2374
+ NetCDF_get_var1_int(VALUE Var,VALUE start)
2375
+ {
2376
+ int ncid;
2377
+ int varid;
2378
+ int status;
2379
+ int *ptr;
2380
+ int i;
2381
+ struct NetCDFVar *Netcdf_var;
2382
+ long l_start;
2383
+ size_t *c_start;
2384
+ int ndims;
2385
+ int dimids[NC_MAX_DIMS];
2386
+ size_t dimlen;
2387
+ int *c_count;
2388
+ int nc_tlen=0;
2389
+ VALUE NArray;
2390
+
2391
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2392
+ ncid = Netcdf_var->ncid;
2393
+ varid = Netcdf_var->varid;
2394
+ status = nc_inq_varndims(ncid, varid, &ndims);
2395
+ if(status != NC_NOERR)NC_RAISE(status);
2396
+ if(ndims == 0) {
2397
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2398
+ }
2399
+
2400
+ Check_Type(start,T_ARRAY);
2401
+ if(RARRAY_LEN(start) < ndims) {
2402
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2403
+ }
2404
+
2405
+ c_start=ALLOCA_N(size_t,ndims);
2406
+ c_count=ALLOCA_N(int,ndims);
2407
+ for(i=0;i<ndims;i++){
2408
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2409
+ status = nc_inq_vardimid(ncid,varid,dimids);
2410
+ if(status != NC_NOERR) NC_RAISE(status);
2411
+ if(l_start < 0) {
2412
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2413
+ if(status != NC_NOERR) NC_RAISE(status);
2414
+ l_start += dimlen;
2415
+ }
2416
+ c_start[i]=l_start;
2417
+ c_count[i]=1;
2418
+ nc_tlen= nc_tlen+1;
2419
+ }
2420
+
2421
+ Clint_to_NArray(NArray,ndims,c_count,ptr);
2422
+
2423
+ status = nc_get_var1_int(ncid,varid,c_start,ptr);
2424
+ if(status != NC_NOERR) NC_RAISE(status);
2425
+
2426
+ OBJ_TAINT(NArray);
2427
+ return NArray;
2428
+
2429
+ }
2430
+
2431
+ VALUE
2432
+ NetCDF_get_var1_float(VALUE Var,VALUE start)
2433
+ {
2434
+ int ncid;
2435
+ int varid;
2436
+ int status;
2437
+ float *ptr;
2438
+ int i;
2439
+ struct NetCDFVar *Netcdf_var;
2440
+ long l_start;
2441
+ size_t *c_start;
2442
+ int ndims;
2443
+ int dimids[NC_MAX_DIMS];
2444
+ size_t dimlen;
2445
+ int *c_count;
2446
+ int nc_tlen=0;
2447
+ VALUE NArray;
2448
+
2449
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2450
+ ncid = Netcdf_var->ncid;
2451
+ varid = Netcdf_var->varid;
2452
+ status = nc_inq_varndims(ncid, varid, &ndims);
2453
+ if(status != NC_NOERR)NC_RAISE(status);
2454
+ if(ndims == 0) {
2455
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2456
+ }
2457
+
2458
+ Check_Type(start,T_ARRAY);
2459
+ if(RARRAY_LEN(start) < ndims) {
2460
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2461
+ }
2462
+
2463
+ c_start=ALLOCA_N(size_t,ndims);
2464
+ c_count=ALLOCA_N(int,ndims);
2465
+ for(i=0;i<ndims;i++){
2466
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2467
+ status = nc_inq_vardimid(ncid, varid, dimids);
2468
+ if(status != NC_NOERR) NC_RAISE(status);
2469
+ if(l_start < 0) {
2470
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2471
+ if(status != NC_NOERR) NC_RAISE(status);
2472
+ l_start += dimlen;
2473
+ }
2474
+ c_start[i]=l_start;
2475
+ c_count[i]=1;
2476
+ nc_tlen = nc_tlen+1;
2477
+ }
2478
+
2479
+ Cfloat_to_NArray(NArray,ndims,c_count,ptr);
2480
+
2481
+ status = nc_get_var1_float(ncid,varid,c_start,ptr);
2482
+ if(status != NC_NOERR) NC_RAISE(status);
2483
+
2484
+ OBJ_TAINT(NArray);
2485
+ return NArray;
2486
+
2487
+ }
2488
+
2489
+ VALUE
2490
+ NetCDF_get_var1_double(VALUE Var,VALUE start)
2491
+ {
2492
+ int ncid;
2493
+ int varid;
2494
+ int status;
2495
+ double *ptr;
2496
+ int i;
2497
+ struct NetCDFVar *Netcdf_var;
2498
+ long l_start;
2499
+ size_t *c_start;
2500
+ int ndims;
2501
+ int dimids[NC_MAX_DIMS];
2502
+ size_t dimlen;
2503
+ int *c_count;
2504
+ int nc_tlen=0;
2505
+ VALUE NArray;
2506
+
2507
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2508
+ ncid = Netcdf_var->ncid;
2509
+ varid = Netcdf_var->varid;
2510
+ status = nc_inq_varndims(ncid, varid, &ndims);
2511
+ if(status != NC_NOERR)NC_RAISE(status);
2512
+ if(ndims == 0) {
2513
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2514
+ }
2515
+
2516
+ Check_Type(start,T_ARRAY);
2517
+ if(RARRAY_LEN(start) < ndims) {
2518
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
2519
+ }
2520
+
2521
+ c_start=ALLOCA_N(size_t,ndims);
2522
+ c_count=ALLOCA_N(int,ndims);
2523
+ for(i=0;i<ndims;i++){
2524
+ l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2525
+ status = nc_inq_vardimid(ncid,varid,dimids);
2526
+ if(status !=NC_NOERR) NC_RAISE(status);
2527
+ if(l_start < 0) {
2528
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2529
+ if(status != NC_NOERR) NC_RAISE(status);
2530
+ l_start += dimlen;
2531
+ }
2532
+ c_start[i]=l_start;
2533
+ c_count[i]=1;
2534
+ nc_tlen = nc_tlen+1;
2535
+ }
2536
+
2537
+ Cdouble_to_NArray(NArray,ndims,c_count,ptr);
2538
+
2539
+ status = nc_get_var1_double(ncid,varid,c_start,ptr);
2540
+ if(status != NC_NOERR) NC_RAISE(status);
2541
+
2542
+ OBJ_TAINT(NArray);
2543
+ return NArray;
2544
+
2545
+ }
2546
+
2547
+ VALUE
2548
+ NetCDF_get_vars_char(VALUE Var,VALUE start,VALUE end,VALUE stride)
2549
+ {
2550
+ int ncid;
2551
+ int varid;
2552
+ int status;
2553
+ unsigned char *ptr;
2554
+ int i;
2555
+ struct NetCDFVar *Netcdf_var;
2556
+ long l_start, l_end;
2557
+ size_t *c_start;
2558
+ size_t *c_count;
2559
+ ptrdiff_t *c_stride;
2560
+ int *shape; /* NArray uses int instead of size_t */
2561
+ int ndims;
2562
+ int *dimids;
2563
+ int nc_tlen=1;
2564
+ size_t dimlen;
2565
+ VALUE NArray;
2566
+
2567
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2568
+ ncid = Netcdf_var->ncid;
2569
+ varid = Netcdf_var->varid;
2570
+
2571
+ status = nc_inq_varndims(ncid,varid,&ndims);
2572
+ if(status != NC_NOERR) NC_RAISE(status);
2573
+ if(ndims == 0) {
2574
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2575
+ }
2576
+
2577
+ dimids = ALLOCA_N(int,ndims);
2578
+ status = nc_inq_vardimid(ncid,varid,dimids);
2579
+ if(status != NC_NOERR) NC_RAISE(status);
2580
+
2581
+ Check_Type(start,T_ARRAY);
2582
+ if(RARRAY_LEN(start) < ndims){
2583
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
2584
+ }
2585
+ c_start = ALLOCA_N(size_t,ndims);
2586
+ for(i=0; i<ndims; i++){
2587
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2588
+
2589
+ if(l_start < 0) {
2590
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
2591
+ if(status != NC_NOERR) NC_RAISE(status);
2592
+ l_start += dimlen;
2593
+ }
2594
+ c_start[i]=l_start;
2595
+ }
2596
+
2597
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
2598
+ switch(TYPE(stride)){
2599
+ case T_NIL:
2600
+ for(i=0; i<ndims; i++){
2601
+ c_stride[i]=1;
2602
+ }
2603
+ break;
2604
+ default:
2605
+ Check_Type(stride,T_ARRAY);
2606
+ if(RARRAY_LEN(stride) < ndims) {
2607
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
2608
+ }
2609
+ for(i=0;i<ndims; i++){
2610
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
2611
+ if(c_stride[i]==0){
2612
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
2613
+ }
2614
+ }
2615
+ }
2616
+
2617
+ c_count=ALLOCA_N(size_t,ndims);
2618
+ switch(TYPE(end)){
2619
+ case T_NIL:
2620
+ for(i=0; i<ndims; i++){
2621
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
2622
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
2623
+ }
2624
+ break;
2625
+ default:
2626
+ Check_Type(end,T_ARRAY);
2627
+ if(RARRAY_LEN(end) <ndims) {
2628
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
2629
+ }
2630
+ for(i=0; i<ndims; i++){
2631
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
2632
+ if(l_end < 0) {
2633
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2634
+ if(status != NC_NOERR) NC_RAISE(status);
2635
+ l_end +=dimlen;
2636
+ }
2637
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
2638
+ }
2639
+ }
2640
+ for(i=0;i<ndims;i++){
2641
+ nc_tlen = nc_tlen*c_count[i];
2642
+ }
2643
+
2644
+
2645
+ shape = ALLOCA_N(int,ndims);
2646
+ for(i=0;i<ndims;i++){
2647
+ shape[ndims-1-i]=c_count[i];
2648
+ }
2649
+
2650
+ Cbyte_to_NArray(NArray,ndims,shape,ptr);
2651
+
2652
+ status = nc_get_vars_text(ncid,varid,c_start,c_count,c_stride,(char *)ptr);
2653
+ if(status != NC_NOERR) NC_RAISE(status);
2654
+
2655
+ OBJ_TAINT(NArray);
2656
+ return NArray;
2657
+ }
2658
+
2659
+ VALUE
2660
+ NetCDF_get_vars_byte(VALUE Var,VALUE start,VALUE end,VALUE stride)
2661
+ {
2662
+ int ncid;
2663
+ int varid;
2664
+ int status;
2665
+ unsigned char *ptr;
2666
+ int i;
2667
+ struct NetCDFVar *Netcdf_var;
2668
+ long l_start, l_end;
2669
+ size_t *c_start;
2670
+ size_t *c_count;
2671
+ ptrdiff_t *c_stride;
2672
+ int *shape; /* NArray uses int instead of size_t */
2673
+ int ndims;
2674
+ int *dimids;
2675
+ int nc_tlen=1;
2676
+ size_t dimlen;
2677
+ VALUE NArray;
2678
+
2679
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2680
+ ncid = Netcdf_var->ncid;
2681
+ varid = Netcdf_var->varid;
2682
+
2683
+ status = nc_inq_varndims(ncid,varid,&ndims);
2684
+ if(status != NC_NOERR) NC_RAISE(status);
2685
+ if(ndims == 0) {
2686
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2687
+ }
2688
+
2689
+ dimids = ALLOCA_N(int,ndims);
2690
+ status = nc_inq_vardimid(ncid,varid,dimids);
2691
+ if(status != NC_NOERR) NC_RAISE(status);
2692
+
2693
+ Check_Type(start,T_ARRAY);
2694
+ if(RARRAY_LEN(start) < ndims){
2695
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
2696
+ }
2697
+ c_start = ALLOCA_N(size_t,ndims);
2698
+ for(i=0; i<ndims; i++){
2699
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2700
+
2701
+ if(l_start < 0) {
2702
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
2703
+ if(status != NC_NOERR) NC_RAISE(status);
2704
+ l_start += dimlen;
2705
+ }
2706
+ c_start[i]=l_start;
2707
+ }
2708
+
2709
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
2710
+ switch(TYPE(stride)){
2711
+ case T_NIL:
2712
+ for(i=0; i<ndims; i++){
2713
+ c_stride[i]=1;
2714
+ }
2715
+ break;
2716
+ default:
2717
+ Check_Type(stride,T_ARRAY);
2718
+ if(RARRAY_LEN(stride) < ndims) {
2719
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
2720
+ }
2721
+ for(i=0;i<ndims; i++){
2722
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
2723
+ if(c_stride[i]==0){
2724
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
2725
+ }
2726
+ }
2727
+ }
2728
+
2729
+ c_count=ALLOCA_N(size_t,ndims);
2730
+ switch(TYPE(end)){
2731
+ case T_NIL:
2732
+ for(i=0; i<ndims; i++){
2733
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
2734
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
2735
+ }
2736
+ break;
2737
+ default:
2738
+ Check_Type(end,T_ARRAY);
2739
+ if(RARRAY_LEN(end) <ndims) {
2740
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
2741
+ }
2742
+ for(i=0; i<ndims; i++){
2743
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
2744
+ if(l_end < 0) {
2745
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2746
+ if(status != NC_NOERR) NC_RAISE(status);
2747
+ l_end +=dimlen;
2748
+ }
2749
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
2750
+ }
2751
+ }
2752
+ for(i=0;i<ndims;i++){
2753
+ nc_tlen = nc_tlen*c_count[i];
2754
+ }
2755
+
2756
+
2757
+ shape = ALLOCA_N(int,ndims);
2758
+ for(i=0;i<ndims;i++){
2759
+ shape[ndims-1-i]=c_count[i];
2760
+ }
2761
+
2762
+ Cbyte_to_NArray(NArray,ndims,shape,ptr);
2763
+
2764
+ status = nc_get_vars_uchar(ncid,varid,c_start,c_count,c_stride,ptr);
2765
+ if(status != NC_NOERR) NC_RAISE(status);
2766
+
2767
+ OBJ_TAINT(NArray);
2768
+ return NArray;
2769
+ }
2770
+
2771
+ VALUE
2772
+ NetCDF_get_vars_sint(VALUE Var,VALUE start,VALUE end,VALUE stride)
2773
+ {
2774
+ int ncid;
2775
+ int varid;
2776
+ int status;
2777
+ short *ptr;
2778
+ int i;
2779
+ struct NetCDFVar *Netcdf_var;
2780
+ long l_start, l_end;
2781
+ size_t *c_start;
2782
+ size_t *c_count;
2783
+ ptrdiff_t *c_stride;
2784
+ int *shape; /* NArray uses int instead of size_t */
2785
+ int ndims;
2786
+ int *dimids;
2787
+ int nc_tlen=1;
2788
+ size_t dimlen;
2789
+ VALUE NArray;
2790
+
2791
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2792
+ ncid = Netcdf_var->ncid;
2793
+ varid = Netcdf_var->varid;
2794
+
2795
+ status = nc_inq_varndims(ncid,varid,&ndims);
2796
+ if(status != NC_NOERR) NC_RAISE(status);
2797
+ if(ndims == 0) {
2798
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2799
+ }
2800
+
2801
+ dimids = ALLOCA_N(int,ndims);
2802
+ status = nc_inq_vardimid(ncid,varid,dimids);
2803
+ if(status != NC_NOERR) NC_RAISE(status);
2804
+
2805
+ Check_Type(start,T_ARRAY);
2806
+ if(RARRAY_LEN(start) < ndims){
2807
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
2808
+ }
2809
+ c_start = ALLOCA_N(size_t,ndims);
2810
+ for(i=0; i<ndims; i++){
2811
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2812
+
2813
+ if(l_start < 0) {
2814
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
2815
+ if(status != NC_NOERR) NC_RAISE(status);
2816
+ l_start += dimlen;
2817
+ }
2818
+ c_start[i]=l_start;
2819
+ }
2820
+
2821
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
2822
+ switch(TYPE(stride)){
2823
+ case T_NIL:
2824
+ for(i=0; i<ndims; i++){
2825
+ c_stride[i]=1;
2826
+ }
2827
+ break;
2828
+ default:
2829
+ Check_Type(stride,T_ARRAY);
2830
+ if(RARRAY_LEN(stride) < ndims) {
2831
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
2832
+ }
2833
+ for(i=0;i<ndims; i++){
2834
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
2835
+ if(c_stride[i]==0){
2836
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
2837
+ }
2838
+ }
2839
+ }
2840
+
2841
+ c_count=ALLOCA_N(size_t,ndims);
2842
+ switch(TYPE(end)){
2843
+ case T_NIL:
2844
+ for(i=0; i<ndims; i++){
2845
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
2846
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
2847
+ }
2848
+ break;
2849
+ default:
2850
+ Check_Type(end,T_ARRAY);
2851
+ if(RARRAY_LEN(end) <ndims) {
2852
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
2853
+ }
2854
+ for(i=0; i<ndims; i++){
2855
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
2856
+ if(l_end < 0) {
2857
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2858
+ if(status != NC_NOERR) NC_RAISE(status);
2859
+ l_end +=dimlen;
2860
+ }
2861
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
2862
+ }
2863
+ }
2864
+
2865
+ for(i=0;i<ndims;i++){
2866
+ nc_tlen = nc_tlen*c_count[i];
2867
+ }
2868
+
2869
+ shape = ALLOCA_N(int,ndims);
2870
+ for(i=0;i<ndims;i++){
2871
+ shape[ndims-1-i]=c_count[i];
2872
+ }
2873
+
2874
+ Csint_to_NArray(NArray,ndims,shape,ptr);
2875
+
2876
+
2877
+ status = nc_get_vars_short(ncid,varid,c_start,c_count,c_stride,ptr);
2878
+ if(status != NC_NOERR) NC_RAISE(status);
2879
+
2880
+ OBJ_TAINT(NArray);
2881
+ return NArray;
2882
+ }
2883
+
2884
+ VALUE
2885
+ NetCDF_get_vars_int(VALUE Var,VALUE start,VALUE end,VALUE stride)
2886
+ {
2887
+ int ncid;
2888
+ int varid;
2889
+ int status;
2890
+ int *ptr;
2891
+ int i;
2892
+ struct NetCDFVar *Netcdf_var;
2893
+ long l_start, l_end;
2894
+ size_t *c_start;
2895
+ size_t *c_count;
2896
+ ptrdiff_t *c_stride;
2897
+ int *shape; /* NArray uses int instead of size_t */
2898
+ int ndims;
2899
+ int *dimids;
2900
+ int nc_tlen=1;
2901
+ size_t dimlen;
2902
+ VALUE NArray;
2903
+
2904
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
2905
+ ncid = Netcdf_var->ncid;
2906
+ varid = Netcdf_var->varid;
2907
+
2908
+ status = nc_inq_varndims(ncid,varid,&ndims);
2909
+ if(status != NC_NOERR) NC_RAISE(status);
2910
+ if(ndims == 0) {
2911
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
2912
+ }
2913
+
2914
+ dimids = ALLOCA_N(int,ndims);
2915
+ status = nc_inq_vardimid(ncid,varid,dimids);
2916
+ if(status != NC_NOERR) NC_RAISE(status);
2917
+
2918
+ Check_Type(start,T_ARRAY);
2919
+ if(RARRAY_LEN(start) < ndims){
2920
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
2921
+ }
2922
+ c_start = ALLOCA_N(size_t,ndims);
2923
+ for(i=0; i<ndims; i++){
2924
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
2925
+
2926
+ if(l_start < 0) {
2927
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
2928
+ if(status != NC_NOERR) NC_RAISE(status);
2929
+ l_start += dimlen;
2930
+ }
2931
+ c_start[i]=l_start;
2932
+ }
2933
+
2934
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
2935
+ switch(TYPE(stride)){
2936
+ case T_NIL:
2937
+ for(i=0; i<ndims; i++){
2938
+ c_stride[i]=1;
2939
+ }
2940
+ break;
2941
+ default:
2942
+ Check_Type(stride,T_ARRAY);
2943
+ if(RARRAY_LEN(stride) < ndims) {
2944
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
2945
+ }
2946
+ for(i=0;i<ndims; i++){
2947
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
2948
+ if(c_stride[i]==0){
2949
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
2950
+ }
2951
+ }
2952
+ }
2953
+
2954
+ c_count=ALLOCA_N(size_t,ndims);
2955
+ switch(TYPE(end)){
2956
+ case T_NIL:
2957
+ for(i=0; i<ndims; i++){
2958
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
2959
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
2960
+ }
2961
+ break;
2962
+ default:
2963
+ Check_Type(end,T_ARRAY);
2964
+ if(RARRAY_LEN(end) <ndims) {
2965
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
2966
+ }
2967
+ for(i=0; i<ndims; i++){
2968
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
2969
+ if(l_end < 0) {
2970
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
2971
+ if(status != NC_NOERR) NC_RAISE(status);
2972
+ l_end +=dimlen;
2973
+ }
2974
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
2975
+ }
2976
+ }
2977
+
2978
+ for(i=0;i<ndims;i++){
2979
+ nc_tlen = nc_tlen*c_count[i];
2980
+ }
2981
+
2982
+ shape = ALLOCA_N(int,ndims);
2983
+ for(i=0;i<ndims;i++){
2984
+ shape[ndims-1-i]=c_count[i];
2985
+ }
2986
+
2987
+ Clint_to_NArray(NArray,ndims,shape,ptr);
2988
+
2989
+
2990
+ status = nc_get_vars_int(ncid,varid,c_start,c_count,c_stride,ptr);
2991
+ if(status != NC_NOERR) NC_RAISE(status);
2992
+
2993
+ OBJ_TAINT(NArray);
2994
+ return NArray;
2995
+ }
2996
+
2997
+ VALUE
2998
+ NetCDF_get_vars_float(VALUE Var,VALUE start,VALUE end,VALUE stride)
2999
+ {
3000
+ int ncid;
3001
+ int varid;
3002
+ int status;
3003
+ float *ptr;
3004
+ int i;
3005
+ struct NetCDFVar *Netcdf_var;
3006
+ long l_start, l_end;
3007
+ size_t *c_start;
3008
+ size_t *c_count;
3009
+ ptrdiff_t *c_stride;
3010
+ int *shape; /* NArray uses int instead of size_t */
3011
+ int ndims;
3012
+ int *dimids;
3013
+ int nc_tlen=1;
3014
+ size_t dimlen;
3015
+ VALUE NArray;
3016
+
3017
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3018
+ ncid = Netcdf_var->ncid;
3019
+ varid = Netcdf_var->varid;
3020
+
3021
+ status = nc_inq_varndims(ncid,varid,&ndims);
3022
+ if(status != NC_NOERR) NC_RAISE(status);
3023
+ if(ndims == 0) {
3024
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
3025
+ }
3026
+
3027
+ dimids = ALLOCA_N(int,ndims);
3028
+ status = nc_inq_vardimid(ncid,varid,dimids);
3029
+ if(status != NC_NOERR) NC_RAISE(status);
3030
+
3031
+ Check_Type(start,T_ARRAY);
3032
+ if(RARRAY_LEN(start) < ndims){
3033
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
3034
+ }
3035
+ c_start = ALLOCA_N(size_t,ndims);
3036
+ for(i=0; i<ndims; i++){
3037
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3038
+
3039
+ if(l_start < 0) {
3040
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3041
+ if(status != NC_NOERR) NC_RAISE(status);
3042
+ l_start += dimlen;
3043
+ }
3044
+ c_start[i]=l_start;
3045
+ }
3046
+
3047
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
3048
+ switch(TYPE(stride)){
3049
+ case T_NIL:
3050
+ for(i=0; i<ndims; i++){
3051
+ c_stride[i]=1;
3052
+ }
3053
+ break;
3054
+ default:
3055
+ Check_Type(stride,T_ARRAY);
3056
+ if(RARRAY_LEN(stride) < ndims) {
3057
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
3058
+ }
3059
+ for(i=0;i<ndims; i++){
3060
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
3061
+ if(c_stride[i]==0){
3062
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
3063
+ }
3064
+ }
3065
+ }
3066
+
3067
+ c_count=ALLOCA_N(size_t,ndims);
3068
+ switch(TYPE(end)){
3069
+ case T_NIL:
3070
+ for(i=0; i<ndims; i++){
3071
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
3072
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
3073
+ }
3074
+ break;
3075
+ default:
3076
+ Check_Type(end,T_ARRAY);
3077
+ if(RARRAY_LEN(end) <ndims) {
3078
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
3079
+ }
3080
+ for(i=0; i<ndims; i++){
3081
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
3082
+ if(l_end < 0) {
3083
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3084
+ if(status != NC_NOERR) NC_RAISE(status);
3085
+ l_end +=dimlen;
3086
+ }
3087
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
3088
+ }
3089
+ }
3090
+
3091
+ for(i=0;i<ndims;i++){
3092
+ nc_tlen = nc_tlen*c_count[i];
3093
+ }
3094
+
3095
+ shape = ALLOCA_N(int,ndims);
3096
+ for(i=0;i<ndims;i++){
3097
+ shape[ndims-1-i]=c_count[i];
3098
+ }
3099
+
3100
+ Cfloat_to_NArray(NArray,ndims,shape,ptr);
3101
+
3102
+
3103
+ status = nc_get_vars_float(ncid,varid,c_start,c_count,c_stride,ptr);
3104
+ if(status != NC_NOERR) NC_RAISE(status);
3105
+
3106
+ OBJ_TAINT(NArray);
3107
+ return NArray;
3108
+ }
3109
+
3110
+ VALUE
3111
+ NetCDF_get_vars_double(VALUE Var,VALUE start,VALUE end,VALUE stride)
3112
+ {
3113
+ int ncid;
3114
+ int varid;
3115
+ int status;
3116
+ double *ptr;
3117
+ int i;
3118
+ struct NetCDFVar *Netcdf_var;
3119
+ long l_start, l_end;
3120
+ size_t *c_start;
3121
+ size_t *c_count;
3122
+ ptrdiff_t *c_stride;
3123
+ int *shape; /* NArray uses int instead of size_t */
3124
+ int ndims;
3125
+ int *dimids;
3126
+ int nc_tlen=1;
3127
+ size_t dimlen;
3128
+ VALUE NArray;
3129
+
3130
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3131
+ ncid = Netcdf_var->ncid;
3132
+ varid = Netcdf_var->varid;
3133
+
3134
+ status = nc_inq_varndims(ncid,varid,&ndims);
3135
+ if(status != NC_NOERR) NC_RAISE(status);
3136
+ if(ndims == 0) {
3137
+ rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
3138
+ }
3139
+
3140
+ dimids = ALLOCA_N(int,ndims);
3141
+ status = nc_inq_vardimid(ncid,varid,dimids);
3142
+ if(status != NC_NOERR) NC_RAISE(status);
3143
+
3144
+ Check_Type(start,T_ARRAY);
3145
+ if(RARRAY_LEN(start) < ndims){
3146
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
3147
+ }
3148
+ c_start = ALLOCA_N(size_t,ndims);
3149
+ for(i=0; i<ndims; i++){
3150
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3151
+
3152
+ if(l_start < 0) {
3153
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3154
+ if(status != NC_NOERR) NC_RAISE(status);
3155
+ l_start += dimlen;
3156
+ }
3157
+ c_start[i]=l_start;
3158
+ }
3159
+
3160
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
3161
+ switch(TYPE(stride)){
3162
+ case T_NIL:
3163
+ for(i=0; i<ndims; i++){
3164
+ c_stride[i]=1;
3165
+ }
3166
+ break;
3167
+ default:
3168
+ Check_Type(stride,T_ARRAY);
3169
+ if(RARRAY_LEN(stride) < ndims) {
3170
+ rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
3171
+ }
3172
+ for(i=0;i<ndims; i++){
3173
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
3174
+ if(c_stride[i]==0){
3175
+ rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
3176
+ }
3177
+ }
3178
+ }
3179
+
3180
+ c_count=ALLOCA_N(size_t,ndims);
3181
+ switch(TYPE(end)){
3182
+ case T_NIL:
3183
+ for(i=0; i<ndims; i++){
3184
+ nc_inq_dimlen(ncid,dimids[i],&dimlen);
3185
+ c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
3186
+ }
3187
+ break;
3188
+ default:
3189
+ Check_Type(end,T_ARRAY);
3190
+ if(RARRAY_LEN(end) <ndims) {
3191
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
3192
+ }
3193
+ for(i=0; i<ndims; i++){
3194
+ l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
3195
+ if(l_end < 0) {
3196
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3197
+ if(status != NC_NOERR) NC_RAISE(status);
3198
+ l_end +=dimlen;
3199
+ }
3200
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
3201
+ }
3202
+ }
3203
+
3204
+ for(i=0;i<ndims;i++){
3205
+ nc_tlen = nc_tlen*c_count[i];
3206
+ }
3207
+
3208
+ shape = ALLOCA_N(int,ndims);
3209
+ for(i=0;i<ndims;i++){
3210
+ shape[ndims-1-i]=c_count[i];
3211
+ }
3212
+
3213
+ Cdouble_to_NArray(NArray,ndims,shape,ptr);
3214
+
3215
+ status = nc_get_vars_double(ncid,varid,c_start,c_count,c_stride,ptr);
3216
+ if(status != NC_NOERR) NC_RAISE(status);
3217
+
3218
+ OBJ_TAINT(NArray);
3219
+ return NArray;
3220
+ }
3221
+
3222
+
3223
+ VALUE
3224
+ NetCDF_put_var_char(VALUE Var,VALUE NArray)
3225
+ {
3226
+ int ncid;
3227
+ int varid;
3228
+ int status;
3229
+ unsigned char *ptr,scalar;
3230
+ int len,i=0;
3231
+ struct NetCDFVar *Netcdf_var;
3232
+ int nc_tlen=1;
3233
+ int ndimsp;
3234
+ int dimids[NC_MAX_DIMS];
3235
+ size_t lengthp;
3236
+ char *var_name;
3237
+
3238
+ rb_secure(4);
3239
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3240
+ ncid=Netcdf_var->ncid;
3241
+ varid=Netcdf_var->varid;
3242
+
3243
+ Array_to_Cbyte_len(NArray,ptr,len);
3244
+
3245
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3246
+ if(status != NC_NOERR) NC_RAISE(status);
3247
+ for(i=0;i<ndimsp;i++){
3248
+ status = nc_inq_vardimid(ncid,varid,dimids);
3249
+ if(status != NC_NOERR) NC_RAISE(status);
3250
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3251
+ nc_tlen=lengthp*nc_tlen;
3252
+ }
3253
+ if(len == 1 && len != nc_tlen){
3254
+ scalar = *ptr;
3255
+ ptr = ALLOCA_N(unsigned char,nc_tlen);
3256
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3257
+ } else if(len != nc_tlen){
3258
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3259
+ status = nc_inq_varname(ncid,varid,var_name);
3260
+ if(status != NC_NOERR) NC_RAISE(status );
3261
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array in the '%s'\n",var_name);
3262
+ }
3263
+ status = nc_put_var_text(ncid,varid,(char *)ptr);
3264
+ if(status !=NC_NOERR) NC_RAISE(status);
3265
+ return Qnil;
3266
+ }
3267
+
3268
+ VALUE
3269
+ NetCDF_put_var_byte(VALUE Var,VALUE NArray)
3270
+ {
3271
+ int ncid;
3272
+ int varid;
3273
+ int status;
3274
+ unsigned char *ptr,scalar;
3275
+ int len,i=0;
3276
+ struct NetCDFVar *Netcdf_var;
3277
+ int nc_tlen=1;
3278
+ int ndimsp;
3279
+ int dimids[NC_MAX_DIMS];
3280
+ size_t lengthp;
3281
+ char *var_name;
3282
+
3283
+ rb_secure(4);
3284
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3285
+ ncid=Netcdf_var->ncid;
3286
+ varid=Netcdf_var->varid;
3287
+
3288
+ Array_to_Cbyte_len(NArray,ptr,len);
3289
+
3290
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3291
+ if(status != NC_NOERR) NC_RAISE(status);
3292
+ for(i=0;i<ndimsp;i++){
3293
+ status = nc_inq_vardimid(ncid,varid,dimids);
3294
+ if(status != NC_NOERR) NC_RAISE(status);
3295
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3296
+ nc_tlen=lengthp*nc_tlen;
3297
+ }
3298
+ if(len == 1 && len != nc_tlen){
3299
+ scalar = *ptr;
3300
+ ptr = ALLOCA_N(unsigned char,nc_tlen);
3301
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3302
+ } else if(len != nc_tlen){
3303
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3304
+ status = nc_inq_varname(ncid,varid,var_name);
3305
+ if(status != NC_NOERR) NC_RAISE(status );
3306
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array in the '%s'\n",var_name);
3307
+ }
3308
+ status = nc_put_var_uchar(ncid,varid,ptr);
3309
+ if(status !=NC_NOERR) NC_RAISE(status);
3310
+ return Qnil;
3311
+ }
3312
+
3313
+ VALUE
3314
+ NetCDF_put_var_short(VALUE Var,VALUE NArray)
3315
+ {
3316
+ int ncid;
3317
+ int varid;
3318
+ int status;
3319
+ short *ptr,scalar;
3320
+ int len,i=0;
3321
+ struct NetCDFVar *Netcdf_var;
3322
+ int nc_tlen=1;
3323
+ int ndimsp;
3324
+ int dimids[NC_MAX_DIMS];
3325
+ size_t lengthp;
3326
+ char *var_name;
3327
+
3328
+ rb_secure(4);
3329
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3330
+ ncid=Netcdf_var->ncid;
3331
+ varid=Netcdf_var->varid;
3332
+ Array_to_Csint_len(NArray,ptr,len);
3333
+
3334
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3335
+ if(status != NC_NOERR) NC_RAISE(status);
3336
+ for(i=0;i<ndimsp;i++){
3337
+ status = nc_inq_vardimid(ncid,varid,dimids);
3338
+ if(status != NC_NOERR) NC_RAISE(status);
3339
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3340
+ nc_tlen=lengthp*nc_tlen;
3341
+ }
3342
+ if(len == 1 && len != nc_tlen){
3343
+ scalar = *ptr;
3344
+ ptr = ALLOCA_N(short,nc_tlen);
3345
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3346
+ } else if(len != nc_tlen){
3347
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3348
+ status = nc_inq_varname(ncid,varid,var_name);
3349
+ if(status != NC_NOERR) NC_RAISE(status);
3350
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
3351
+ }
3352
+
3353
+ status = nc_put_var_short(ncid,varid,ptr);
3354
+ if(status !=NC_NOERR) NC_RAISE(status);
3355
+ return Qnil;
3356
+ }
3357
+
3358
+ VALUE
3359
+ NetCDF_put_var_int(VALUE Var,VALUE NArray)
3360
+ {
3361
+ int ncid;
3362
+ int varid;
3363
+ int status;
3364
+ int *ptr,scalar;
3365
+ int len,i=0;
3366
+ struct NetCDFVar *Netcdf_var;
3367
+ int nc_tlen=1;
3368
+ int ndimsp;
3369
+ int dimids[NC_MAX_DIMS];
3370
+ size_t lengthp;
3371
+ char *var_name;
3372
+
3373
+ rb_secure(4);
3374
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3375
+ ncid=Netcdf_var->ncid;
3376
+ varid=Netcdf_var->varid;
3377
+
3378
+ Array_to_Clint_len(NArray,ptr,len);
3379
+
3380
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3381
+ if(status != NC_NOERR) NC_RAISE(status);
3382
+ for(i=0;i<ndimsp;i++){
3383
+ status = nc_inq_vardimid(ncid,varid,dimids);
3384
+ if(status != NC_NOERR) NC_RAISE(status);
3385
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3386
+ nc_tlen=lengthp*nc_tlen;
3387
+ }
3388
+ if(len == 1 && len != nc_tlen){
3389
+ scalar = *ptr;
3390
+ ptr = ALLOCA_N(int,nc_tlen);
3391
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3392
+ } else if(len != nc_tlen){
3393
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3394
+ status = nc_inq_varname(ncid,varid,var_name);
3395
+ if(status != NC_NOERR) NC_RAISE(status);
3396
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
3397
+ }
3398
+
3399
+
3400
+ status = nc_put_var_int(ncid,varid,ptr);
3401
+ if(status !=NC_NOERR) NC_RAISE(status);
3402
+ return Qnil;
3403
+ }
3404
+
3405
+
3406
+ VALUE
3407
+ NetCDF_put_var_float(VALUE Var,VALUE NArray)
3408
+ {
3409
+ int ncid;
3410
+ int varid;
3411
+ int status;
3412
+ float *ptr,scalar;
3413
+ int len,i=0;
3414
+ struct NetCDFVar *Netcdf_var;
3415
+ int nc_tlen=1;
3416
+ int ndimsp;
3417
+ int dimids[NC_MAX_DIMS];
3418
+ size_t lengthp;
3419
+ char *var_name;
3420
+
3421
+
3422
+ rb_secure(4);
3423
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3424
+ ncid=Netcdf_var->ncid;
3425
+ varid=Netcdf_var->varid;
3426
+
3427
+ Array_to_Cfloat_len(NArray,ptr,len);
3428
+
3429
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3430
+ if(status != NC_NOERR) NC_RAISE(status);
3431
+ for(i=0;i<ndimsp;i++){
3432
+ status = nc_inq_vardimid(ncid,varid,dimids);
3433
+ if(status != NC_NOERR) NC_RAISE(status);
3434
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3435
+ nc_tlen=lengthp*nc_tlen;
3436
+ }
3437
+ if(len == 1 && len != nc_tlen){
3438
+ scalar = *ptr;
3439
+ ptr = ALLOCA_N(float,nc_tlen);
3440
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3441
+ } else if(len != nc_tlen){
3442
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3443
+ status = nc_inq_varname(ncid,varid,var_name);
3444
+ if(status != NC_NOERR) NC_RAISE(status);
3445
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
3446
+ }
3447
+
3448
+ status = nc_put_var_float(ncid,varid,ptr);
3449
+ if(status !=NC_NOERR) NC_RAISE(status);
3450
+ return Qnil;
3451
+ }
3452
+
3453
+ VALUE
3454
+ NetCDF_put_var_double(VALUE Var,VALUE NArray)
3455
+ {
3456
+ int ncid;
3457
+ int varid;
3458
+ int status;
3459
+ double *ptr,scalar;
3460
+ int len,i=0;
3461
+ struct NetCDFVar *Netcdf_var;
3462
+ int nc_tlen=1;
3463
+ int ndimsp;
3464
+ int dimids[NC_MAX_DIMS];
3465
+ size_t lengthp;
3466
+ char *var_name;
3467
+
3468
+
3469
+ rb_secure(4);
3470
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3471
+ ncid=Netcdf_var->ncid;
3472
+ varid=Netcdf_var->varid;
3473
+
3474
+ Array_to_Cdouble_len(NArray,ptr,len);
3475
+
3476
+ status = nc_inq_varndims(ncid,varid,&ndimsp);
3477
+ if(status != NC_NOERR) NC_RAISE(status);
3478
+ for(i=0;i<ndimsp;i++){
3479
+ status = nc_inq_vardimid(ncid,varid,dimids);
3480
+ if(status != NC_NOERR) NC_RAISE(status);
3481
+ nc_inq_dimlen(ncid,dimids[i],&lengthp);
3482
+ nc_tlen=lengthp*nc_tlen;
3483
+ }
3484
+ if(len == 1 && len != nc_tlen){
3485
+ scalar = *ptr;
3486
+ ptr = ALLOCA_N(double,nc_tlen);
3487
+ for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
3488
+ } else if(len != nc_tlen){
3489
+ var_name=ALLOCA_N(char,NC_MAX_NAME);
3490
+ status = nc_inq_varname(ncid,varid,var_name);
3491
+ if(status != NC_NOERR) NC_RAISE(status);
3492
+ rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
3493
+ }
3494
+
3495
+ status = nc_put_var_double(ncid,varid,ptr);
3496
+ if(status !=NC_NOERR) NC_RAISE(status);
3497
+ return Qnil;
3498
+ }
3499
+
3500
+ VALUE
3501
+ NetCDF_put_var1_char(VALUE Var,VALUE NArray,VALUE start)
3502
+ {
3503
+ int ncid;
3504
+ int varid;
3505
+ int status;
3506
+ unsigned char *ptr;
3507
+ int i;
3508
+ struct NetCDFVar *Netcdf_var;
3509
+ long l_start;
3510
+ size_t *c_start;
3511
+ int ndims;
3512
+ int *dimids;
3513
+ size_t dimlen;
3514
+
3515
+ rb_secure(4);
3516
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3517
+ ncid=Netcdf_var->ncid;
3518
+ varid=Netcdf_var->varid;
3519
+ status = nc_inq_varndims(ncid,varid,&ndims);
3520
+ if(status != NC_NOERR) NC_RAISE(status);
3521
+
3522
+
3523
+ dimids = ALLOCA_N(int,ndims);
3524
+ status = nc_inq_vardimid(ncid,varid,dimids);
3525
+ if(status != NC_NOERR) NC_RAISE(status);
3526
+
3527
+ Check_Type(start,T_ARRAY);
3528
+ if(RARRAY_LEN(start) <ndims) {
3529
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3530
+ }
3531
+
3532
+ c_start=ALLOCA_N(size_t,ndims);
3533
+ for(i=0;i<ndims;i++){
3534
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3535
+
3536
+ if(l_start < 0) {
3537
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3538
+ if(status != NC_NOERR) NC_RAISE(status);
3539
+ l_start += dimlen;
3540
+ }
3541
+ c_start[i]=l_start;
3542
+
3543
+ }
3544
+ Array_to_Cbyte(NArray,ptr);
3545
+
3546
+ status = nc_put_var1_text(ncid,varid,c_start,(char *)ptr);
3547
+ if(status != NC_NOERR) NC_RAISE(status);
3548
+ return Qnil;
3549
+ }
3550
+
3551
+ VALUE
3552
+ NetCDF_put_var1_byte(VALUE Var,VALUE NArray,VALUE start)
3553
+ {
3554
+ int ncid;
3555
+ int varid;
3556
+ int status;
3557
+ unsigned char *ptr;
3558
+ int i;
3559
+ struct NetCDFVar *Netcdf_var;
3560
+ long l_start;
3561
+ size_t *c_start;
3562
+ int ndims;
3563
+ int *dimids;
3564
+ size_t dimlen;
3565
+
3566
+ rb_secure(4);
3567
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3568
+ ncid=Netcdf_var->ncid;
3569
+ varid=Netcdf_var->varid;
3570
+ status = nc_inq_varndims(ncid,varid,&ndims);
3571
+ if(status != NC_NOERR) NC_RAISE(status);
3572
+
3573
+
3574
+ dimids = ALLOCA_N(int,ndims);
3575
+ status = nc_inq_vardimid(ncid,varid,dimids);
3576
+ if(status != NC_NOERR) NC_RAISE(status);
3577
+
3578
+ Check_Type(start,T_ARRAY);
3579
+ if(RARRAY_LEN(start) <ndims) {
3580
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3581
+ }
3582
+
3583
+ c_start=ALLOCA_N(size_t,ndims);
3584
+ for(i=0;i<ndims;i++){
3585
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3586
+
3587
+ if(l_start < 0) {
3588
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3589
+ if(status != NC_NOERR) NC_RAISE(status);
3590
+ l_start += dimlen;
3591
+ }
3592
+ c_start[i]=l_start;
3593
+
3594
+ }
3595
+ Array_to_Cbyte(NArray,ptr);
3596
+
3597
+ status = nc_put_var1_uchar(ncid,varid,c_start,ptr);
3598
+ if(status != NC_NOERR) NC_RAISE(status);
3599
+ return Qnil;
3600
+ }
3601
+
3602
+ VALUE
3603
+ NetCDF_put_var1_sint(VALUE Var,VALUE NArray,VALUE start)
3604
+ {
3605
+ int ncid;
3606
+ int varid;
3607
+ int status;
3608
+ short *ptr;
3609
+ int i;
3610
+ struct NetCDFVar *Netcdf_var;
3611
+ long l_start;
3612
+ size_t *c_start;
3613
+ int ndims;
3614
+ int *dimids;
3615
+ size_t dimlen;
3616
+
3617
+ rb_secure(4);
3618
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3619
+ ncid=Netcdf_var->ncid;
3620
+ varid=Netcdf_var->varid;
3621
+ status = nc_inq_varndims(ncid,varid,&ndims);
3622
+ if(status != NC_NOERR) NC_RAISE(status);
3623
+
3624
+
3625
+ dimids = ALLOCA_N(int,ndims);
3626
+ status = nc_inq_vardimid(ncid,varid,dimids);
3627
+ if(status != NC_NOERR) NC_RAISE(status);
3628
+
3629
+ Check_Type(start,T_ARRAY);
3630
+ if(RARRAY_LEN(start) <ndims) {
3631
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3632
+ }
3633
+
3634
+ c_start=ALLOCA_N(size_t,ndims);
3635
+ for(i=0;i<ndims;i++){
3636
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3637
+
3638
+ if(l_start < 0) {
3639
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3640
+ if(status != NC_NOERR) NC_RAISE(status);
3641
+ l_start += dimlen;
3642
+ }
3643
+ c_start[i]=l_start;
3644
+
3645
+ }
3646
+ Array_to_Csint(NArray,ptr);
3647
+
3648
+ status = nc_put_var1_short(ncid,varid,c_start,ptr);
3649
+ if(status != NC_NOERR) NC_RAISE(status);
3650
+ return Qnil;
3651
+ }
3652
+ VALUE
3653
+ NetCDF_put_var1_int(VALUE Var,VALUE NArray,VALUE start)
3654
+ {
3655
+ int ncid;
3656
+ int varid;
3657
+ int status;
3658
+ int *ptr;
3659
+ int i;
3660
+ struct NetCDFVar *Netcdf_var;
3661
+ long l_start;
3662
+ size_t *c_start;
3663
+ int ndims;
3664
+ int *dimids;
3665
+ size_t dimlen;
3666
+
3667
+ rb_secure(4);
3668
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3669
+ ncid=Netcdf_var->ncid;
3670
+ varid=Netcdf_var->varid;
3671
+ status = nc_inq_varndims(ncid,varid,&ndims);
3672
+ if(status != NC_NOERR) NC_RAISE(status);
3673
+
3674
+
3675
+ dimids = ALLOCA_N(int,ndims);
3676
+ status = nc_inq_vardimid(ncid,varid,dimids);
3677
+ if(status != NC_NOERR) NC_RAISE(status);
3678
+
3679
+ Check_Type(start,T_ARRAY);
3680
+ if(RARRAY_LEN(start) <ndims) {
3681
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3682
+ }
3683
+
3684
+ c_start=ALLOCA_N(size_t,ndims);
3685
+ for(i=0;i<ndims;i++){
3686
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3687
+
3688
+ if(l_start < 0) {
3689
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3690
+ if(status != NC_NOERR) NC_RAISE(status);
3691
+ l_start += dimlen;
3692
+ }
3693
+ c_start[i]=l_start;
3694
+
3695
+ }
3696
+ Array_to_Clint(NArray,ptr);
3697
+
3698
+ status = nc_put_var1_int(ncid,varid,c_start,ptr);
3699
+ if(status != NC_NOERR) NC_RAISE(status);
3700
+ return Qnil;
3701
+ }
3702
+
3703
+ VALUE
3704
+ NetCDF_put_var1_float(VALUE Var,VALUE NArray,VALUE start)
3705
+ {
3706
+ int ncid;
3707
+ int varid;
3708
+ int status;
3709
+ float *ptr;
3710
+ int i;
3711
+ struct NetCDFVar *Netcdf_var;
3712
+ long l_start;
3713
+ size_t *c_start;
3714
+ int ndims;
3715
+ int *dimids;
3716
+ size_t dimlen;
3717
+
3718
+ rb_secure(4);
3719
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3720
+ ncid=Netcdf_var->ncid;
3721
+ varid=Netcdf_var->varid;
3722
+ status = nc_inq_varndims(ncid,varid,&ndims);
3723
+ if(status != NC_NOERR) NC_RAISE(status);
3724
+
3725
+
3726
+ dimids = ALLOCA_N(int,ndims);
3727
+ status = nc_inq_vardimid(ncid,varid,dimids);
3728
+ if(status != NC_NOERR) NC_RAISE(status);
3729
+
3730
+ Check_Type(start,T_ARRAY);
3731
+ if(RARRAY_LEN(start) <ndims) {
3732
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3733
+ }
3734
+
3735
+ c_start=ALLOCA_N(size_t,ndims);
3736
+ for(i=0;i<ndims;i++){
3737
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3738
+
3739
+ if(l_start < 0) {
3740
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3741
+ if(status != NC_NOERR) NC_RAISE(status);
3742
+ l_start += dimlen;
3743
+ }
3744
+ c_start[i]=l_start;
3745
+
3746
+ }
3747
+ Array_to_Cfloat(NArray,ptr);
3748
+
3749
+ status = nc_put_var1_float(ncid,varid,c_start,ptr);
3750
+ if(status != NC_NOERR) NC_RAISE(status);
3751
+ return Qnil;
3752
+ }
3753
+
3754
+ VALUE
3755
+ NetCDF_put_var1_double(VALUE Var,VALUE NArray,VALUE start)
3756
+ {
3757
+ int ncid;
3758
+ int varid;
3759
+ int status;
3760
+ double *ptr;
3761
+ int i;
3762
+ struct NetCDFVar *Netcdf_var;
3763
+ long l_start;
3764
+ size_t *c_start;
3765
+ int ndims;
3766
+ int *dimids;
3767
+ size_t dimlen;
3768
+
3769
+ rb_secure(4);
3770
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3771
+ ncid=Netcdf_var->ncid;
3772
+ varid=Netcdf_var->varid;
3773
+ status = nc_inq_varndims(ncid,varid,&ndims);
3774
+ if(status != NC_NOERR) NC_RAISE(status);
3775
+
3776
+
3777
+ dimids = ALLOCA_N(int,ndims);
3778
+ status = nc_inq_vardimid(ncid,varid,dimids);
3779
+ if(status != NC_NOERR) NC_RAISE(status);
3780
+
3781
+ Check_Type(start,T_ARRAY);
3782
+ if(RARRAY_LEN(start) <ndims) {
3783
+ rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
3784
+ }
3785
+
3786
+ c_start=ALLOCA_N(size_t,ndims);
3787
+ for(i=0;i<ndims;i++){
3788
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3789
+
3790
+ if(l_start < 0) {
3791
+ status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
3792
+ if(status != NC_NOERR) NC_RAISE(status);
3793
+ l_start += dimlen;
3794
+ }
3795
+ c_start[i]=l_start;
3796
+
3797
+ }
3798
+ Array_to_Cdouble(NArray,ptr);
3799
+
3800
+ status = nc_put_var1_double(ncid,varid,c_start,ptr);
3801
+ if(status != NC_NOERR) NC_RAISE(status);
3802
+ return Qnil;
3803
+ }
3804
+
3805
+
3806
+ VALUE
3807
+ NetCDF_put_vars_char(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
3808
+ {
3809
+ int ncid;
3810
+ int varid;
3811
+ int status;
3812
+ unsigned char *ptr,scalar;
3813
+ int len,i;
3814
+ int c_count_all=1;
3815
+ struct NetCDFVar *Netcdf_var;
3816
+ long l_start, l_end;
3817
+ size_t *c_start;
3818
+ size_t *c_count;
3819
+ ptrdiff_t *c_stride;
3820
+ int ndims;
3821
+ int *shape;
3822
+ int *dimids;
3823
+ size_t dimlen;
3824
+
3825
+ rb_secure(4);
3826
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3827
+ ncid=Netcdf_var->ncid;
3828
+ varid=Netcdf_var->varid;
3829
+ status = nc_inq_varndims(ncid, varid, &ndims);
3830
+ if(status != NC_NOERR) NC_RAISE(status);
3831
+
3832
+ dimids=ALLOCA_N(int,ndims);
3833
+ status = nc_inq_vardimid(ncid, varid, dimids);
3834
+ if(status != NC_NOERR) NC_RAISE(status);
3835
+
3836
+ Check_Type(start,T_ARRAY);
3837
+ if(RARRAY_LEN(start) < ndims) {
3838
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
3839
+ }
3840
+ c_start=ALLOCA_N(size_t,ndims);
3841
+ for(i=0; i<ndims; i++){
3842
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3843
+
3844
+ if(l_start < 0) {
3845
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3846
+ if(status != NC_NOERR) NC_RAISE(status);
3847
+ l_start += dimlen;
3848
+ }
3849
+ c_start[i]=l_start;
3850
+ }
3851
+
3852
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
3853
+ switch(TYPE(stride)){
3854
+ case T_NIL:
3855
+ for(i=0; i<ndims; i++){
3856
+ c_stride[i]=1;
3857
+ }
3858
+ break;
3859
+ default:
3860
+ Check_Type(stride,T_ARRAY);
3861
+ if(RARRAY_LEN(stride) < ndims) {
3862
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
3863
+ }
3864
+ for(i=0; i<ndims; i++){
3865
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
3866
+ if(c_stride[i]==0) {
3867
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
3868
+ }
3869
+ }
3870
+ }
3871
+
3872
+ Array_to_Cbyte_len_shape(NArray,ptr,len,shape);
3873
+
3874
+ c_count=ALLOCA_N(size_t,ndims);
3875
+ switch(TYPE(end)){
3876
+ case T_NIL:
3877
+ for(i=0; i<ndims; i++){
3878
+ c_count[i]=shape[i];
3879
+ }
3880
+ c_count_all=len;
3881
+ break;
3882
+ default:
3883
+ Check_Type(end,T_ARRAY);
3884
+ if(RARRAY_LEN(end) < ndims) {
3885
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
3886
+ }
3887
+ for(i=0; i<ndims; i++){
3888
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
3889
+ if(l_end < 0) {
3890
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3891
+ if(status != NC_NOERR) NC_RAISE(status);
3892
+ l_end += dimlen;
3893
+ }
3894
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
3895
+ c_count_all=c_count[i]*c_count_all;
3896
+ }
3897
+ if(len == 1 && len != c_count_all){
3898
+ scalar = *ptr;
3899
+ ptr = ALLOCA_N(unsigned char,c_count_all);
3900
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
3901
+ } else if(len != c_count_all) {
3902
+ rb_raise(rb_eNetcdfError,
3903
+ "lengh of the array does not agree with that of the subset\n");
3904
+ }
3905
+ }
3906
+
3907
+ status = nc_put_vars_text(ncid,varid,c_start,c_count,c_stride,(char *)ptr);
3908
+ if(status != NC_NOERR) NC_RAISE(status);
3909
+ return Qnil;
3910
+ }
3911
+
3912
+ VALUE
3913
+ NetCDF_put_vars_byte(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
3914
+ {
3915
+ int ncid;
3916
+ int varid;
3917
+ int status;
3918
+ unsigned char *ptr,scalar;
3919
+ int len,i;
3920
+ int c_count_all=1;
3921
+ struct NetCDFVar *Netcdf_var;
3922
+ long l_start, l_end;
3923
+ size_t *c_start;
3924
+ size_t *c_count;
3925
+ ptrdiff_t *c_stride;
3926
+ int ndims;
3927
+ int *shape;
3928
+ int *dimids;
3929
+ size_t dimlen;
3930
+
3931
+ rb_secure(4);
3932
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
3933
+ ncid=Netcdf_var->ncid;
3934
+ varid=Netcdf_var->varid;
3935
+ status = nc_inq_varndims(ncid, varid, &ndims);
3936
+ if(status != NC_NOERR) NC_RAISE(status);
3937
+
3938
+ dimids=ALLOCA_N(int,ndims);
3939
+ status = nc_inq_vardimid(ncid, varid, dimids);
3940
+ if(status != NC_NOERR) NC_RAISE(status);
3941
+
3942
+ Check_Type(start,T_ARRAY);
3943
+ if(RARRAY_LEN(start) < ndims) {
3944
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
3945
+ }
3946
+ c_start=ALLOCA_N(size_t,ndims);
3947
+ for(i=0; i<ndims; i++){
3948
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
3949
+
3950
+ if(l_start < 0) {
3951
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3952
+ if(status != NC_NOERR) NC_RAISE(status);
3953
+ l_start += dimlen;
3954
+ }
3955
+ c_start[i]=l_start;
3956
+ }
3957
+
3958
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
3959
+ switch(TYPE(stride)){
3960
+ case T_NIL:
3961
+ for(i=0; i<ndims; i++){
3962
+ c_stride[i]=1;
3963
+ }
3964
+ break;
3965
+ default:
3966
+ Check_Type(stride,T_ARRAY);
3967
+ if(RARRAY_LEN(stride) < ndims) {
3968
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
3969
+ }
3970
+ for(i=0; i<ndims; i++){
3971
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
3972
+ if(c_stride[i]==0) {
3973
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
3974
+ }
3975
+ }
3976
+ }
3977
+
3978
+ Array_to_Cbyte_len_shape(NArray,ptr,len,shape);
3979
+
3980
+ c_count=ALLOCA_N(size_t,ndims);
3981
+ switch(TYPE(end)){
3982
+ case T_NIL:
3983
+ for(i=0; i<ndims; i++){
3984
+ c_count[i]=shape[i];
3985
+ }
3986
+ c_count_all=len;
3987
+ break;
3988
+ default:
3989
+ Check_Type(end,T_ARRAY);
3990
+ if(RARRAY_LEN(end) < ndims) {
3991
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
3992
+ }
3993
+ for(i=0; i<ndims; i++){
3994
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
3995
+ if(l_end < 0) {
3996
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
3997
+ if(status != NC_NOERR) NC_RAISE(status);
3998
+ l_end += dimlen;
3999
+ }
4000
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
4001
+ c_count_all=c_count[i]*c_count_all;
4002
+ }
4003
+ if(len == 1 && len != c_count_all){
4004
+ scalar = *ptr;
4005
+ ptr = ALLOCA_N(unsigned char,c_count_all);
4006
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
4007
+ } else if(len != c_count_all) {
4008
+ rb_raise(rb_eNetcdfError,
4009
+ "lengh of the array does not agree with that of the subset\n");
4010
+ }
4011
+ }
4012
+
4013
+ status = nc_put_vars_uchar(ncid,varid,c_start,c_count,c_stride,ptr);
4014
+ if(status != NC_NOERR) NC_RAISE(status);
4015
+ return Qnil;
4016
+ }
4017
+
4018
+ VALUE
4019
+ NetCDF_put_vars_sint(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
4020
+ {
4021
+ int ncid;
4022
+ int varid;
4023
+ int status;
4024
+ short *ptr,scalar;
4025
+ int len,i;
4026
+ int c_count_all=1;
4027
+ struct NetCDFVar *Netcdf_var;
4028
+ long l_start, l_end;
4029
+ size_t *c_start;
4030
+ size_t *c_count;
4031
+ ptrdiff_t *c_stride;
4032
+ int ndims;
4033
+ int *shape;
4034
+ int *dimids;
4035
+ size_t dimlen;
4036
+
4037
+ rb_secure(4);
4038
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
4039
+ ncid=Netcdf_var->ncid;
4040
+ varid=Netcdf_var->varid;
4041
+ status = nc_inq_varndims(ncid, varid, &ndims);
4042
+ if(status != NC_NOERR) NC_RAISE(status);
4043
+
4044
+ dimids=ALLOCA_N(int,ndims);
4045
+ status = nc_inq_vardimid(ncid, varid, dimids);
4046
+ if(status != NC_NOERR) NC_RAISE(status);
4047
+
4048
+ Check_Type(start,T_ARRAY);
4049
+ if(RARRAY_LEN(start) < ndims) {
4050
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
4051
+ }
4052
+ c_start=ALLOCA_N(size_t,ndims);
4053
+ for(i=0; i<ndims; i++){
4054
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
4055
+
4056
+ if(l_start < 0) {
4057
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4058
+ if(status != NC_NOERR) NC_RAISE(status);
4059
+ l_start += dimlen;
4060
+ }
4061
+ c_start[i]=l_start;
4062
+ }
4063
+
4064
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
4065
+ switch(TYPE(stride)){
4066
+ case T_NIL:
4067
+ for(i=0; i<ndims; i++){
4068
+ c_stride[i]=1;
4069
+ }
4070
+ break;
4071
+ default:
4072
+ Check_Type(stride,T_ARRAY);
4073
+ if(RARRAY_LEN(stride) < ndims) {
4074
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
4075
+ }
4076
+ for(i=0; i<ndims; i++){
4077
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
4078
+ if(c_stride[i]==0) {
4079
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
4080
+ }
4081
+ }
4082
+ }
4083
+
4084
+ Array_to_Csint_len_shape(NArray,ptr,len,shape);
4085
+
4086
+ c_count=ALLOCA_N(size_t,ndims);
4087
+ switch(TYPE(end)){
4088
+ case T_NIL:
4089
+ for(i=0; i<ndims; i++){
4090
+ c_count[i]=shape[i];
4091
+ }
4092
+ c_count_all=len;
4093
+ break;
4094
+ default:
4095
+ Check_Type(end,T_ARRAY);
4096
+ if(RARRAY_LEN(end) < ndims) {
4097
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
4098
+ }
4099
+ for(i=0; i<ndims; i++){
4100
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
4101
+ if(l_end < 0) {
4102
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4103
+ if(status != NC_NOERR) NC_RAISE(status);
4104
+ l_end += dimlen;
4105
+ }
4106
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
4107
+ c_count_all=c_count[i]*c_count_all;
4108
+ }
4109
+ if(len == 1 && len != c_count_all){
4110
+ scalar = *ptr;
4111
+ ptr = ALLOCA_N(short,c_count_all);
4112
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
4113
+ } else if(len != c_count_all) {
4114
+ rb_raise(rb_eNetcdfError,
4115
+ "lengh of the array does not agree with that of the subset\n");
4116
+ }
4117
+ }
4118
+
4119
+ status = nc_put_vars_short(ncid,varid,c_start,c_count,c_stride,ptr);
4120
+ if(status != NC_NOERR) NC_RAISE(status);
4121
+ return Qnil;
4122
+ }
4123
+
4124
+
4125
+ VALUE
4126
+ NetCDF_put_vars_int(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
4127
+ {
4128
+ int ncid;
4129
+ int varid;
4130
+ int status;
4131
+ int *ptr,scalar;
4132
+ int len,i;
4133
+ int c_count_all=1;
4134
+ struct NetCDFVar *Netcdf_var;
4135
+ long l_start, l_end;
4136
+ size_t *c_start;
4137
+ size_t *c_count;
4138
+ ptrdiff_t *c_stride;
4139
+ int ndims;
4140
+ int *shape;
4141
+ int *dimids;
4142
+ size_t dimlen;
4143
+
4144
+ rb_secure(4);
4145
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
4146
+ ncid=Netcdf_var->ncid;
4147
+ varid=Netcdf_var->varid;
4148
+ status = nc_inq_varndims(ncid, varid, &ndims);
4149
+ if(status != NC_NOERR) NC_RAISE(status);
4150
+
4151
+ dimids=ALLOCA_N(int,ndims);
4152
+ status = nc_inq_vardimid(ncid, varid, dimids);
4153
+ if(status != NC_NOERR) NC_RAISE(status);
4154
+
4155
+ Check_Type(start,T_ARRAY);
4156
+ if(RARRAY_LEN(start) < ndims) {
4157
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
4158
+ }
4159
+ c_start=ALLOCA_N(size_t,ndims);
4160
+ for(i=0; i<ndims; i++){
4161
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
4162
+
4163
+ if(l_start < 0) {
4164
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4165
+ if(status != NC_NOERR) NC_RAISE(status);
4166
+ l_start += dimlen;
4167
+ }
4168
+ c_start[i]=l_start;
4169
+ }
4170
+
4171
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
4172
+ switch(TYPE(stride)){
4173
+ case T_NIL:
4174
+ for(i=0; i<ndims; i++){
4175
+ c_stride[i]=1;
4176
+ }
4177
+ break;
4178
+ default:
4179
+ Check_Type(stride,T_ARRAY);
4180
+ if(RARRAY_LEN(stride) < ndims) {
4181
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
4182
+ }
4183
+ for(i=0; i<ndims; i++){
4184
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
4185
+ if(c_stride[i]==0) {
4186
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
4187
+ }
4188
+ }
4189
+ }
4190
+
4191
+ Array_to_Clint_len_shape(NArray,ptr,len,shape);
4192
+
4193
+ c_count=ALLOCA_N(size_t,ndims);
4194
+ switch(TYPE(end)){
4195
+ case T_NIL:
4196
+ for(i=0; i<ndims; i++){
4197
+ c_count[i]=shape[i];
4198
+ }
4199
+ c_count_all=len;
4200
+ break;
4201
+ default:
4202
+ Check_Type(end,T_ARRAY);
4203
+ if(RARRAY_LEN(end) < ndims) {
4204
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
4205
+ }
4206
+ for(i=0; i<ndims; i++){
4207
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
4208
+ if(l_end < 0) {
4209
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4210
+ if(status != NC_NOERR) NC_RAISE(status);
4211
+ l_end += dimlen;
4212
+ }
4213
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
4214
+ c_count_all=c_count[i]*c_count_all;
4215
+ }
4216
+ if(len == 1 && len != c_count_all){
4217
+ scalar = *ptr;
4218
+ ptr = ALLOCA_N(int,c_count_all);
4219
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
4220
+ } else if(len != c_count_all) {
4221
+ rb_raise(rb_eNetcdfError,
4222
+ "length of the array does not agree with that of the subset\n");
4223
+ }
4224
+ }
4225
+
4226
+ status = nc_put_vars_int(ncid,varid,c_start,c_count,c_stride,ptr);
4227
+ if(status != NC_NOERR) NC_RAISE(status);
4228
+ return Qnil;
4229
+ }
4230
+
4231
+
4232
+ VALUE
4233
+ NetCDF_put_vars_float(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
4234
+ {
4235
+ int ncid;
4236
+ int varid;
4237
+ int status;
4238
+ float *ptr,scalar;
4239
+ int len,i;
4240
+ int c_count_all=1;
4241
+ struct NetCDFVar *Netcdf_var;
4242
+ long l_start, l_end;
4243
+ size_t *c_start;
4244
+ size_t *c_count;
4245
+ ptrdiff_t *c_stride;
4246
+ int ndims;
4247
+ int *shape;
4248
+ int *dimids;
4249
+ size_t dimlen;
4250
+
4251
+ rb_secure(4);
4252
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
4253
+ ncid=Netcdf_var->ncid;
4254
+ varid=Netcdf_var->varid;
4255
+ status = nc_inq_varndims(ncid, varid, &ndims);
4256
+ if(status != NC_NOERR) NC_RAISE(status);
4257
+
4258
+ dimids=ALLOCA_N(int,ndims);
4259
+ status = nc_inq_vardimid(ncid, varid, dimids);
4260
+ if(status != NC_NOERR) NC_RAISE(status);
4261
+
4262
+ Check_Type(start,T_ARRAY);
4263
+ if(RARRAY_LEN(start) < ndims) {
4264
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
4265
+ }
4266
+ c_start=ALLOCA_N(size_t,ndims);
4267
+ for(i=0; i<ndims; i++){
4268
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
4269
+
4270
+ if(l_start < 0) {
4271
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4272
+ if(status != NC_NOERR) NC_RAISE(status);
4273
+ l_start += dimlen;
4274
+ }
4275
+ c_start[i]=l_start;
4276
+ }
4277
+
4278
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
4279
+ switch(TYPE(stride)){
4280
+ case T_NIL:
4281
+ for(i=0; i<ndims; i++){
4282
+ c_stride[i]=1;
4283
+ }
4284
+ break;
4285
+ default:
4286
+ Check_Type(stride,T_ARRAY);
4287
+ if(RARRAY_LEN(stride) < ndims) {
4288
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
4289
+ }
4290
+ for(i=0; i<ndims; i++){
4291
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
4292
+ if(c_stride[i]==0) {
4293
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
4294
+ }
4295
+ }
4296
+ }
4297
+
4298
+ Array_to_Cfloat_len_shape(NArray,ptr,len,shape);
4299
+
4300
+ c_count=ALLOCA_N(size_t,ndims);
4301
+ switch(TYPE(end)){
4302
+ case T_NIL:
4303
+ for(i=0; i<ndims; i++){
4304
+ c_count[i]=shape[i];
4305
+ }
4306
+ c_count_all=len;
4307
+ break;
4308
+ default:
4309
+ Check_Type(end,T_ARRAY);
4310
+ if(RARRAY_LEN(end) < ndims) {
4311
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
4312
+ }
4313
+ for(i=0; i<ndims; i++){
4314
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
4315
+ if(l_end < 0) {
4316
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4317
+ if(status != NC_NOERR) NC_RAISE(status);
4318
+ l_end += dimlen;
4319
+ }
4320
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
4321
+ c_count_all=c_count[i]*c_count_all;
4322
+ }
4323
+ if(len == 1 && len != c_count_all){
4324
+ scalar = *ptr;
4325
+ ptr = ALLOCA_N(float,c_count_all);
4326
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
4327
+ } else if(len != c_count_all) {
4328
+ rb_raise(rb_eNetcdfError,
4329
+ "lengh of the array does not agree with that of the subset\n");
4330
+ }
4331
+ }
4332
+
4333
+ status = nc_put_vars_float(ncid,varid,c_start,c_count,c_stride,ptr);
4334
+ if(status != NC_NOERR) NC_RAISE(status);
4335
+ return Qnil;
4336
+ }
4337
+
4338
+
4339
+ VALUE
4340
+ NetCDF_put_vars_double(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
4341
+ {
4342
+ int ncid;
4343
+ int varid;
4344
+ int status;
4345
+ double *ptr,scalar;
4346
+ int len,i;
4347
+ int c_count_all=1;
4348
+ struct NetCDFVar *Netcdf_var;
4349
+ long l_start, l_end;
4350
+ size_t *c_start;
4351
+ size_t *c_count;
4352
+ ptrdiff_t *c_stride;
4353
+ int ndims;
4354
+ int *shape;
4355
+ int *dimids;
4356
+ size_t dimlen;
4357
+
4358
+ rb_secure(4);
4359
+ Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
4360
+ ncid=Netcdf_var->ncid;
4361
+ varid=Netcdf_var->varid;
4362
+ status = nc_inq_varndims(ncid, varid, &ndims);
4363
+ if(status != NC_NOERR) NC_RAISE(status);
4364
+
4365
+ dimids=ALLOCA_N(int,ndims);
4366
+ status = nc_inq_vardimid(ncid, varid, dimids);
4367
+ if(status != NC_NOERR) NC_RAISE(status);
4368
+
4369
+ Check_Type(start,T_ARRAY);
4370
+ if(RARRAY_LEN(start) < ndims) {
4371
+ rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
4372
+ }
4373
+ c_start=ALLOCA_N(size_t,ndims);
4374
+ for(i=0; i<ndims; i++){
4375
+ l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
4376
+
4377
+ if(l_start < 0) {
4378
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4379
+ if(status != NC_NOERR) NC_RAISE(status);
4380
+ l_start += dimlen;
4381
+ }
4382
+ c_start[i]=l_start;
4383
+ }
4384
+
4385
+ c_stride=ALLOCA_N(ptrdiff_t,ndims);
4386
+ switch(TYPE(stride)){
4387
+ case T_NIL:
4388
+ for(i=0; i<ndims; i++){
4389
+ c_stride[i]=1;
4390
+ }
4391
+ break;
4392
+ default:
4393
+ Check_Type(stride,T_ARRAY);
4394
+ if(RARRAY_LEN(stride) < ndims) {
4395
+ rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
4396
+ }
4397
+ for(i=0; i<ndims; i++){
4398
+ c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
4399
+ if(c_stride[i]==0) {
4400
+ rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
4401
+ }
4402
+ }
4403
+ }
4404
+
4405
+ Array_to_Cdouble_len_shape(NArray,ptr,len,shape);
4406
+
4407
+ c_count=ALLOCA_N(size_t,ndims);
4408
+ switch(TYPE(end)){
4409
+ case T_NIL:
4410
+ for(i=0; i<ndims; i++){
4411
+ c_count[i]=shape[i];
4412
+ }
4413
+ c_count_all=len;
4414
+ break;
4415
+ default:
4416
+ Check_Type(end,T_ARRAY);
4417
+ if(RARRAY_LEN(end) < ndims) {
4418
+ rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
4419
+ }
4420
+ for(i=0; i<ndims; i++){
4421
+ l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
4422
+ if(l_end < 0) {
4423
+ status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
4424
+ if(status != NC_NOERR) NC_RAISE(status);
4425
+ l_end += dimlen;
4426
+ }
4427
+ c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
4428
+ c_count_all=c_count[i]*c_count_all;
4429
+ }
4430
+ if(len == 1 && len != c_count_all){
4431
+ scalar = *ptr;
4432
+ ptr = ALLOCA_N(double,c_count_all);
4433
+ for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
4434
+ } else if(len != c_count_all) {
4435
+ rb_raise(rb_eNetcdfError,
4436
+ "lengh of the array does not agree with that of the subset\n");
4437
+ }
4438
+ }
4439
+
4440
+ status = nc_put_vars_double(ncid,varid,c_start,c_count,c_stride,ptr);
4441
+ if(status != NC_NOERR) NC_RAISE(status);
4442
+ return Qnil;
4443
+ }
4444
+
4445
+ void
4446
+ Init_netcdfraw(void)
4447
+ {
4448
+ mNumRu = rb_define_module("NumRu");
4449
+
4450
+ /* Difinitions of the classes */
4451
+ cNetCDF = rb_define_class_under(mNumRu, "NetCDF", rb_cObject);
4452
+ cNetCDFDim = rb_define_class_under(mNumRu, "NetCDFDim", rb_cObject);
4453
+ cNetCDFAtt = rb_define_class_under(mNumRu, "NetCDFAtt", rb_cObject);
4454
+ cNetCDFVar = rb_define_class_under(mNumRu, "NetCDFVar", rb_cObject);
4455
+
4456
+ rb_eNetcdfError = rb_define_class("NetcdfError",rb_eStandardError);
4457
+ rb_eNetcdfBadid = rb_define_class("NetcdfBadid",rb_eNetcdfError);
4458
+ rb_eNetcdfNfile = rb_define_class("NetcdfNfile",rb_eNetcdfError);
4459
+ rb_eNetcdfExist = rb_define_class("NetcdfExist",rb_eNetcdfError);
4460
+ rb_eNetcdfInval = rb_define_class("NetcdfInval",rb_eNetcdfError);
4461
+ rb_eNetcdfPerm = rb_define_class("NetcdfPerm",rb_eNetcdfError);
4462
+ rb_eNetcdfNotindefine = rb_define_class("NetcdfNotindefine",rb_eNetcdfError);
4463
+ rb_eNetcdfIndefine = rb_define_class("NetcdfIndefine",rb_eNetcdfError);
4464
+ rb_eNetcdfInvalcoords = rb_define_class("NetcdfInvalcoords",rb_eNetcdfError);
4465
+ rb_eNetcdfMaxdims = rb_define_class("NetcdfMaxdims",rb_eNetcdfError);
4466
+ rb_eNetcdfNameinuse = rb_define_class("NetcdfNameinuse",rb_eNetcdfError);
4467
+ rb_eNetcdfNotatt = rb_define_class("NetcdfNotatt",rb_eNetcdfError);
4468
+ rb_eNetcdfMaxatts = rb_define_class("NetcdfMaxatts",rb_eNetcdfError);
4469
+ rb_eNetcdfBadtype = rb_define_class("NetcdfBadtype",rb_eNetcdfError);
4470
+ rb_eNetcdfBaddim = rb_define_class("NetcdfBaddim",rb_eNetcdfError);
4471
+ rb_eNetcdfUnlimpos = rb_define_class("NetcdFUnlimpos",rb_eNetcdfError);
4472
+ rb_eNetcdfMaxvars = rb_define_class("NetcdfMaxvars",rb_eNetcdfError);
4473
+ rb_eNetcdfNotvar = rb_define_class("NetcdfNotvar",rb_eNetcdfError);
4474
+ rb_eNetcdfGlobal = rb_define_class("NetcdfGlobal",rb_eNetcdfError);
4475
+ rb_eNetcdfNotnc = rb_define_class("NetcdfNotnc",rb_eNetcdfError);
4476
+ rb_eNetcdfSts = rb_define_class("NetcdfSts",rb_eNetcdfError);
4477
+ rb_eNetcdfMaxname = rb_define_class("NetcdfMaxname",rb_eNetcdfError);
4478
+ rb_eNetcdfUnlimit = rb_define_class("NetcdfUnlimit",rb_eNetcdfError);
4479
+ rb_eNetcdfNorecvars = rb_define_class("NetcdfNorecvars",rb_eNetcdfError);
4480
+ rb_eNetcdfChar = rb_define_class("NetcdfChar",rb_eNetcdfError);
4481
+ rb_eNetcdfEdge = rb_define_class("NetcdfEdge",rb_eNetcdfError);
4482
+ rb_eNetcdfStride = rb_define_class("NetcdfStride",rb_eNetcdfError);
4483
+ rb_eNetcdfBadname = rb_define_class("NetcdfBadname",rb_eNetcdfError);
4484
+ /* N.B. following must match value in ncx.h */
4485
+ rb_eNetcdfRange = rb_define_class("NetcdfRange",rb_eNetcdfError);
4486
+ rb_eNetcdfNomem = rb_define_class("NetcdfNomem",rb_eNetcdfError);
4487
+ /* Global error status */
4488
+ rb_eNetcdfEntool = rb_define_class("NetcdfEntool",rb_eNetcdfError);
4489
+ rb_eNetcdfExdr = rb_define_class("NetcdfExdr",rb_eNetcdfError);
4490
+ rb_eNetcdfSyserr = rb_define_class("NetcdfSyserr",rb_eNetcdfError);
4491
+ /* Global options variable. Used to determine behavior of error handler. */
4492
+ rb_eNetcdfFatal = rb_define_class("NetcdfFatal",rb_eNetcdfError);
4493
+
4494
+ /* Class Constants Definition */
4495
+ rb_define_const(cNetCDF, "NC_NOWRITE", INT2FIX(NC_NOWRITE));
4496
+ rb_define_const(cNetCDF, "NC_WRITE", INT2FIX(NC_WRITE));
4497
+ rb_define_const(cNetCDF, "NC_SHARE", INT2FIX(NC_SHARE));
4498
+ rb_define_const(cNetCDF, "NC_CLOBBER", INT2FIX(NC_CLOBBER));
4499
+ rb_define_const(cNetCDF, "NC_NOCLOBBER", INT2FIX(NC_NOCLOBBER));
4500
+
4501
+ /* Difinitions of the ruby methods */
4502
+ /* The methods of the NetCDF class */
4503
+ rb_define_singleton_method(cNetCDF,"nc_open",NetCDF_open,2);
4504
+ rb_define_method(cNetCDF,"clone",NetCDF_clone,0);
4505
+ rb_define_method(cNetCDF,"close",NetCDF_close,0);
4506
+ /* rb_define_singleton_method(cNetCDF,"new",NetCDF_open,2); */
4507
+ rb_define_singleton_method(cNetCDF,"nc_create",NetCDF_create,2);
4508
+ rb_define_method(cNetCDF,"def_dim",NetCDF_def_dim,2);
4509
+ rb_define_method(cNetCDF,"def_var",NetCDF_def_var,3);
4510
+ rb_define_method(cNetCDF,"put_attraw",NetCDF_put_att,3);
4511
+ rb_define_method(cNetCDF,"redef",NetCDF_redef,0);
4512
+ rb_define_method(cNetCDF,"enddef",NetCDF_enddef,0);
4513
+ rb_define_method(cNetCDF,"define_mode?",NetCDF_whether_in_define_mode,0);
4514
+ rb_define_method(cNetCDF,"fill",NetCDF_fill,1);
4515
+ rb_define_method(cNetCDF,"ndims",NetCDF_ndims,0);
4516
+ rb_define_method(cNetCDF,"nvars",NetCDF_nvars,0);
4517
+ rb_define_method(cNetCDF,"natts",NetCDF_natts,0);
4518
+ rb_define_method(cNetCDF,"sync",NetCDF_sync,0);
4519
+ rb_define_method(cNetCDF,"path",NetCDF_path,0);
4520
+ rb_define_method(cNetCDF,"dim",NetCDF_dim,1);
4521
+ rb_define_method(cNetCDF,"var",NetCDF_var,1);
4522
+ rb_define_method(cNetCDF,"att",NetCDF_att,1);
4523
+ rb_define_method(cNetCDF,"unlimited",NetCDF_unlimited,0);
4524
+ rb_define_private_method(cNetCDF,"id2var",NetCDF_id2var,1);
4525
+ rb_define_private_method(cNetCDF,"id2dim",NetCDF_id2dim,1);
4526
+ rb_define_private_method(cNetCDF,"id2att",NetCDF_id2att,1);
4527
+ rb_define_method(cNetCDF,"==",NetCDF_eql,1);
4528
+ /* rb_define_method(cNetCDF,"eql?",NetCDF_eql,1); */
4529
+
4530
+ /* The methods of the NetCDFDim class */
4531
+ rb_define_method(cNetCDFDim,"clone",NetCDF_dim_clone,0);
4532
+ rb_define_method(cNetCDFDim,"length",NetCDF_dim_length,0);
4533
+ rb_define_method(cNetCDFDim,"name=",NetCDF_dim_name,1);
4534
+ rb_define_method(cNetCDFDim,"name",NetCDF_dim_inqname,0);
4535
+ rb_define_method(cNetCDFDim,"unlimited?",NetCDF_dim_whether_unlimited,0);
4536
+ rb_define_method(cNetCDFDim,"==",NetCDF_dim_eql,1);
4537
+ /* rb_define_method(cNetCDFDim,"eql?",NetCDF_dim_eql,1); */
4538
+
4539
+ /* The methods of the NetCDFAtt class */
4540
+ rb_define_method(cNetCDFAtt,"clone",NetCDF_att_clone,0);
4541
+ rb_define_method(cNetCDFAtt,"name",NetCDF_att_inq_name,0);
4542
+ rb_define_method(cNetCDFAtt,"name=",NetCDF_att_rename,1);
4543
+ rb_define_method(cNetCDFAtt,"delete",NetCDF_att_delete,0);
4544
+ rb_define_method(cNetCDFAtt,"copy",NetCDF_att_copy,1);
4545
+ rb_define_method(cNetCDFAtt,"atttype",NetCDF_att_atttype,0);
4546
+ rb_define_method(cNetCDFAtt,"typecode",NetCDF_att_typecode,0);
4547
+ rb_define_method(cNetCDFAtt,"==",NetCDF_att_eql,1);
4548
+ /* rb_define_method(cNetCDFAtt,"eql?",NetCDF_att_eql,1); */
4549
+ rb_define_method(cNetCDFAtt,"putraw",NetCDF_att_put,2);
4550
+ rb_define_method(cNetCDFAtt,"get",NetCDF_att_get,0);
4551
+
4552
+ /* The methods of the NetCDFVar class */
4553
+ rb_define_method(cNetCDFVar,"clone",NetCDF_var_clone,0);
4554
+ rb_define_method(cNetCDFVar,"name",NetCDF_var_inq_name,0);
4555
+ rb_define_method(cNetCDFVar,"ndims",NetCDF_var_ndims,0);
4556
+ rb_define_method(cNetCDFVar,"vartype",NetCDF_var_vartype,0);
4557
+ rb_define_method(cNetCDFVar,"typecode",NetCDF_var_typecode,0);
4558
+ rb_define_method(cNetCDFVar,"ntype",NetCDF_var_vartype,0);
4559
+ rb_define_method(cNetCDFVar,"natts",NetCDF_var_natts,0);
4560
+ rb_define_method(cNetCDFVar,"file",NetCDF_var_file,0);
4561
+ rb_define_method(cNetCDFVar,"name=",NetCDF_var_rename,1);
4562
+ rb_define_method(cNetCDFVar,"att",NetCDF_var_att,1);
4563
+ rb_define_method(cNetCDFVar,"put_attraw",NetCDF_put_att_var,3);
4564
+ rb_define_method(cNetCDFVar,"dims",NetCDF_var_dims,0);
4565
+ rb_define_method(cNetCDFVar,"dim",NetCDF_var_dim,1);
4566
+ /*rb_define_private_method(cNetCDFVar,"id2dim",NetCDF_var_id2dim,1); */
4567
+ rb_define_private_method(cNetCDFVar,"id2att",NetCDF_var_id2att,1);
4568
+ rb_define_method(cNetCDFVar,"==",NetCDF_var_eql,1);
4569
+ /* rb_define_method(cNetCDFVar,"eql?",NetCDF_var_eql,1); */
4570
+
4571
+ /* The "get*" or "put*" methods in the NetCDFVar class */
4572
+ rb_define_method(cNetCDFVar,"put_var_char",NetCDF_put_var_char,1);
4573
+ rb_define_method(cNetCDFVar,"put_var_byte",NetCDF_put_var_byte,1);
4574
+ rb_define_method(cNetCDFVar,"put_var_sint",NetCDF_put_var_short,1);
4575
+ rb_define_method(cNetCDFVar,"put_var_int",NetCDF_put_var_int,1);
4576
+ rb_define_method(cNetCDFVar,"put_var_sfloat",NetCDF_put_var_float,1);
4577
+ rb_define_method(cNetCDFVar,"put_var_float",NetCDF_put_var_double,1);
4578
+
4579
+ rb_define_method(cNetCDFVar,"put_vars_char",NetCDF_put_vars_char,4);
4580
+ rb_define_method(cNetCDFVar,"put_vars_byte",NetCDF_put_vars_byte,4);
4581
+ rb_define_method(cNetCDFVar,"put_vars_sint",NetCDF_put_vars_sint,4);
4582
+ rb_define_method(cNetCDFVar,"put_vars_int",NetCDF_put_vars_int,4);
4583
+ rb_define_method(cNetCDFVar,"put_vars_sfloat",NetCDF_put_vars_float,4);
4584
+ rb_define_method(cNetCDFVar,"put_vars_float",NetCDF_put_vars_double,4);
4585
+
4586
+ rb_define_method(cNetCDFVar,"put_var1_char",NetCDF_put_var1_char,2);
4587
+ rb_define_method(cNetCDFVar,"put_var1_byte",NetCDF_put_var1_byte,2);
4588
+ rb_define_method(cNetCDFVar,"put_var1_sint",NetCDF_put_var1_sint,2);
4589
+ rb_define_method(cNetCDFVar,"put_var1_int",NetCDF_put_var1_int,2);
4590
+ rb_define_method(cNetCDFVar,"put_var1_sfloat",NetCDF_put_var1_float,2);
4591
+ rb_define_method(cNetCDFVar,"put_var1_float",NetCDF_put_var1_double,2);
4592
+
4593
+ rb_define_method(cNetCDFVar,"get_var_char",NetCDF_get_var_char,0);
4594
+ rb_define_method(cNetCDFVar,"get_var_byte",NetCDF_get_var_byte,0);
4595
+ rb_define_method(cNetCDFVar,"get_var_sint",NetCDF_get_var_sint,0);
4596
+ rb_define_method(cNetCDFVar,"get_var_int",NetCDF_get_var_int,0);
4597
+ rb_define_method(cNetCDFVar,"get_var_sfloat",NetCDF_get_var_float,0);
4598
+ rb_define_method(cNetCDFVar,"get_var_float",NetCDF_get_var_double,0);
4599
+
4600
+ rb_define_method(cNetCDFVar,"get_vars_char",NetCDF_get_vars_char,3);
4601
+ rb_define_method(cNetCDFVar,"get_vars_byte",NetCDF_get_vars_byte,3);
4602
+ rb_define_method(cNetCDFVar,"get_vars_sint",NetCDF_get_vars_sint,3);
4603
+ rb_define_method(cNetCDFVar,"get_vars_int",NetCDF_get_vars_int,3);
4604
+ rb_define_method(cNetCDFVar,"get_vars_sfloat",NetCDF_get_vars_float,3);
4605
+ rb_define_method(cNetCDFVar,"get_vars_float",NetCDF_get_vars_double,3);
4606
+
4607
+ rb_define_method(cNetCDFVar,"get_var1_char",NetCDF_get_var1_char,1);
4608
+ rb_define_method(cNetCDFVar,"get_var1_byte",NetCDF_get_var1_byte,1);
4609
+ rb_define_method(cNetCDFVar,"get_var1_sint",NetCDF_get_var1_sint,1);
4610
+ rb_define_method(cNetCDFVar,"get_var1_int",NetCDF_get_var1_int,1);
4611
+ rb_define_method(cNetCDFVar,"get_var1_sfloat",NetCDF_get_var1_float,1);
4612
+ rb_define_method(cNetCDFVar,"get_var1_float",NetCDF_get_var1_double,1);
4613
+ }