ruby-netcdf-updated 0.7.0

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