Class: NumRu::NetCDFVar

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/netcdf.rb,
lib/numru/netcdf_miss.rb,
ext/numru/netcdfraw.c

Constant Summary collapse

@@unpack_type =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(file, varname, mode = "r", share = false) ⇒ Object Also known as: open



277
278
279
280
281
282
283
284
# File 'lib/numru/netcdf.rb', line 277

def new(file,varname,mode="r",share=false)
  if(file.is_a?(String))
    file = NetCDF.open(file,mode,share)
  elsif(!file.is_a?(NetCDF))
    raise TypeError, "1st arg must be a NetCDF (file object) or a String (path)"
  end
  file.var(varname)
end

.unpack_typeObject



383
384
385
# File 'lib/numru/netcdf.rb', line 383

def unpack_type
  @@unpack_type
end

.unpack_type=(na_type) ⇒ Object



386
387
388
389
390
391
392
393
# File 'lib/numru/netcdf.rb', line 386

def unpack_type=(na_type)
  if [NArray::BYTE, NArray::SINT, NArray::INT,
      NArray::SFLOAT, NArray::FLOAT, nil].include?(na_type)
    @@unpack_type = na_type
  else
    raise ArgumentError, "Arg must be one of NArray::BYTE, NArray::SINT, NArray::INT, NArray::SFLOAT, NArray::FLOAT"
  end
end

Instance Method Details

#==(Varb) ⇒ Object



1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
# File 'ext/numru/netcdfraw.c', line 1990

VALUE
NetCDF_var_eql(VALUE Vara,VALUE Varb)
{
  struct NetCDFVar *Netcdf_vara;
  struct NetCDFVar *Netcdf_varb;

  if( rb_obj_is_kind_of(Varb, cNetCDFVar) ){
      Data_Get_Struct(Vara,struct NetCDFVar,Netcdf_vara);
      Data_Get_Struct(Varb,struct NetCDFVar,Netcdf_varb);

      if(Netcdf_vara->ncid == Netcdf_varb->ncid &&
	 Netcdf_vara->varid == Netcdf_varb->varid){
	  return Qtrue;
      } else {
	  return Qfalse;
      }
  } else {
      return Qfalse;
  }
}

#[](*a) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/numru/netcdf.rb', line 635

def [](*a)
  if a.length == 0
    return self.get
  end
  a = __rubber_expansion(a)
  first = Array.new
  last = Array.new
  stride = Array.new
  set_stride = false
  a.each{|i|
    if(i.is_a?(Integer))
      first.push(i)
      last.push(i)
      stride.push(1)
    elsif(i.is_a?(Range))
      first.push(i.first)
      last.push(i.exclude_end? ? i.last-1 : i.last)
      stride.push(1)
    elsif(i.is_a?(Hash))
      r = (i.to_a[0])[0]
      s = (i.to_a[0])[1]
      if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
        raise TypeError, "Hash argument must be {a_Range, step}"
      end
      first.push(r.first)
      last.push(r.exclude_end? ? r.last-1 : r.last)
      stride.push(s)
      set_stride = true
    elsif(i.is_a?(TrueClass))
      first.push(0)
      last.push(-1)
      stride.push(1)
    elsif( i.is_a?(Array) || i.is_a?(NArray))
      a_new = a.dup
      at = a.index(i)
      i = NArray.to_na(i) if i.is_a?(Array)
      for n in 0..i.length-1
        a_new[at] = i[n]..i[n]
        na_tmp = self[*a_new]
        if n==0 then
          k = at
          if at > 0
            a[0..at-1].each{|x| if x.is_a?(Integer) then k -= 1 end}
          end
          shape_tmp = na_tmp.shape
          shape_tmp[k] = i.length
          na = na_tmp.class.new(na_tmp.typecode,*shape_tmp)
          index_tmp = Array.new(shape_tmp.length,true)
        end
        index_tmp[k] = n..n
        na[*index_tmp] = na_tmp
      end
      return na
    else
      raise TypeError, "argument must be Integer, Range, Hash, TrueClass, Array, or NArray"
    end
  }

  if(set_stride)
    na = self.get({"start"=>first, "end"=>last, "stride"=>stride})
  else
    na = self.get({"start"=>first, "end"=>last})
  end
  shape = na.shape
  (a.length-1).downto(0){ |i|
    shape.delete_at(i) if a[i].is_a?(Integer)
  }
  na.reshape!( *shape )
  na
end

#[]=(*a) ⇒ Object



706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# File 'lib/numru/netcdf.rb', line 706

def []=(*a)
  val = a.pop
  a = __rubber_expansion(a)
  first = Array.new
  last = Array.new
  stride = Array.new
  set_stride = false
  a.each{|i|
    if(i.is_a?(Integer))
      first.push(i)
      last.push(i)
      stride.push(1)
    elsif(i.is_a?(Range))
      first.push(i.first)
      last.push(i.exclude_end? ? i.last-1 : i.last)
      stride.push(1)
    elsif(i.is_a?(Hash))
      r = (i.to_a[0])[0]
      s = (i.to_a[0])[1]
      if ( !( r.is_a?(Range) ) || ! ( s.is_a?(Integer) ) )
        raise ArgumentError, "Hash argument must be {first..last, step}"
      end
      first.push(r.first)
      last.push(r.exclude_end? ? r.last-1 : r.last)
      stride.push(s)
      set_stride = true
    elsif(i.is_a?(TrueClass))
      first.push(0)
      last.push(-1)
      stride.push(1)
    elsif(i.is_a?(Array) || i.is_a?(NArray))
      a_new = a.dup
      at = a.index(i)
      i = NArray.to_na(i) if i.is_a?(Array)
      val = NArray.to_na(val) if val.is_a?(Array)
      rank_of_subset = a.dup.delete_if{|v| v.is_a?(Integer)}.length
      if val.rank != rank_of_subset
        raise "rank of the rhs (#{val.rank}) is not equal to the rank "+
              "of the subset specified by #{a.inspect} (#{rank_of_subset})"
      end
      k = at
      a[0..at-1].each{|x| if x.is_a?(Integer) then k -= 1 end}
      if i.length != val.shape[k]
        raise "length of the #{k+1}-th dim of rhs is incorrect "+
              "(#{i.length} for #{val.shape[k]})"
      end
      index_tmp = Array.new(val.rank,true) if !val.is_a?(Numeric) #==>Array-like
      for n in 0..i.length-1
        a_new[at] = i[n]..i[n]
        if !val.is_a?(Numeric) then
          index_tmp[k] = n..n
          self[*a_new] = val[*index_tmp]
        else
          self[*a_new] = val
        end
      end
      return self
    else
      raise TypeError, "argument must be Integer, Range, Hash, TrueClass, Array, or NArray"
    end
  }

  if(set_stride)
    self.put(val, {"start"=>first, "end"=>last, "stride"=>stride})
  else
    self.put(val, {"start"=>first, "end"=>last})
  end
end

#att(att_name) ⇒ Object



1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
# File 'ext/numru/netcdfraw.c', line 1932

VALUE
NetCDF_var_att(VALUE Var,VALUE att_name)
{
  int ncid;
  int status;
  int varid;
  char *c_att_name;
  int c_attnump;
  struct NetCDFVar *Netcdf_var;
  struct NetCDFAtt *Netcdf_att;
  VALUE Att;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Check_Type(att_name,T_STRING);
  c_att_name=StringValueCStr(att_name);

  status = nc_inq_attid(ncid,varid,c_att_name,&c_attnump);
  if(status == NC_NOERR){
    Netcdf_att=NetCDF_att_init(ncid,varid,c_att_name);
    Att=Data_Wrap_Struct(cNetCDFAtt,0,Netcdf_att_free,Netcdf_att);
    return Att;
  }
  else if(status == NC_ENOTATT){
    return Qnil;
  }
  else{
    NC_RAISE(status);
    return Qnil;
  }
}

#att_namesObject



305
306
307
308
309
310
311
312
313
# File 'lib/numru/netcdf.rb', line 305

def att_names
  num_att=natts()
  names=[]
  for attnum in 0..num_att-1
    obj_Att=id2att(attnum)
    names=names+[obj_Att.name]
  end
  return names
end

#cloneObject



550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/numru/netcdfraw.c', line 550

VALUE
NetCDF_var_clone(VALUE var)
{
    VALUE clone;
    struct NetCDFVar *nv1, *nv2;

    Data_Get_Struct(var, struct NetCDFVar, nv1);
    nv2 = NetCDF_var_init(nv1->ncid, nv1->varid, nv1->file);
    clone = Data_Wrap_Struct(cNetCDFVar, nc_mark_obj, NetCDF_var_free, nv2);
    CLONESETUP(clone, var);
    return clone;
}

#deflate(*args) ⇒ Object

USAGE

NetCDFVar#deflate(deflate_level, shuffle=false)


1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
# File 'ext/numru/netcdfraw.c', line 1385

VALUE
NetCDF_var_deflate(int argc, VALUE *argv, VALUE Var)
{
  int ncid, varid, status;
  struct NetCDFVar *Netcdf_var;

  int shuffle;
    /* If non-zero, turn on the shuffle filter.

       http://www.unidata.ucar.edu/software/netcdf/papers/AMS_2008.pdf :
       The shuffle algorithm changes the byte order in the data stream;
       when used with integers that are all close together, this
       results in a better compression ratio. There is no benefit
       from using the shuffle filter without also using
       compression.

       MEMO by horinouchi: shuffling filter was also effective for float
       variables in some test (demo5-netcdf4.rb).
     */
  int deflate_level;
  int deflate=1;
        /* Always set to non-zero:
           See https://www.unidata.ucar.edu/software/netcdf/docs/netcdf-c/nc_005fdef_005fvar_005fdeflate.html#nc_005fdef_005fvar_005fdeflate
           If non-zero, turn on the deflate filter at the
           level specified by the deflate_level parameter.
         */

  if (argc>2 || argc<1) rb_raise(rb_eArgError,
		         "wrong # of arguments (%d). It must be 1 or 2", argc);

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  deflate_level = NUM2INT(argv[0]);

  if (argc==1) {
      shuffle = 0;  /* default: false */
  } else {
      if ( argv[1] == Qnil || argv[1] == Qfalse ) {
	  shuffle = 0;
      } else {
	  shuffle = 1;
      }
  }

  status = nc_def_var_deflate(ncid, varid, shuffle, deflate, deflate_level);
  if(status != NC_NOERR) NC_RAISE(status);

  return(Var);
}

#deflate_paramsObject



1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
# File 'ext/numru/netcdfraw.c', line 1437

VALUE
NetCDF_var_deflate_params(VALUE Var)
{
  int ncid, varid, status;
  struct NetCDFVar *Netcdf_var;
  int shufflep, deflatep, deflate_levelp;
  VALUE sh, df, params;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_var_deflate(ncid, varid, &shufflep, &deflatep,
			      &deflate_levelp);
  if(status != NC_NOERR) NC_RAISE(status);
  if (shufflep==0) {sh=Qfalse;} else {sh=Qtrue;}
  if (deflatep==0) {df=Qfalse;} else {df=Qtrue;}
  params = rb_ary_new3(3, sh, df, INT2NUM(deflate_levelp) );
  return(params);
}

#dim(ith) ⇒ Object



1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
# File 'ext/numru/netcdfraw.c', line 1542

VALUE
NetCDF_var_dim(VALUE Var, VALUE ith)
{
  int ncid, *dimids, ndims, varid, status, c_ith;
  struct NetCDFVar *Netcdf_var;
  struct NetCDFDim *Netcdf_dim;
  VALUE Dim;

  Check_Type(ith,T_FIXNUM);
  c_ith=NUM2INT(ith);
  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(c_ith < 0 || c_ith >= ndims) {
      rb_raise(rb_eNetcdfError, "dimension count less than zero or greater than ndims-1");
  }
  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);
  Netcdf_dim = NetCDF_dim_init(ncid,dimids[ndims-1-c_ith]);
  Dim = Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim);
  return(Dim);
}

#dim_namesObject



299
300
301
302
303
# File 'lib/numru/netcdf.rb', line 299

def dim_names
  ary = Array.new()
  dims.each{|dim| ary.push(dim.name)}
  ary
end

#dimsObject



1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
# File 'ext/numru/netcdfraw.c', line 1517

VALUE
NetCDF_var_dims(VALUE Var)
{
  int ncid, *dimids, ndims, varid, i, status;
  struct NetCDFVar *Netcdf_var;
  struct NetCDFDim *Netcdf_dim;
  VALUE Dims;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);
  Dims = rb_ary_new();
  for(i=0;i<ndims;i++){
      Netcdf_dim = NetCDF_dim_init(ncid,dimids[ndims-1-i]);
      rb_ary_push(Dims,
		  Data_Wrap_Struct(cNetCDFDim,0,NetCDF_dim_free,Netcdf_dim));
  }
  return(Dims);
}

#each_attObject



291
292
293
294
295
296
297
# File 'lib/numru/netcdf.rb', line 291

def each_att
  num_att=natts()
  for attnum in 0..num_att-1
    obj_Att=id2att(attnum)
    yield(obj_Att)
  end
end

#endianObject



1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
# File 'ext/numru/netcdfraw.c', line 1471

VALUE
NetCDF_var_endian(VALUE Var)
{
  int ncid, varid, status;
  struct NetCDFVar *Netcdf_var;
  int endian;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_var_endian(ncid, varid, &endian);
  if(status != NC_NOERR) NC_RAISE(status);
  return(INT2FIX(endian));
}

#endian=(endian) ⇒ Object



1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
# File 'ext/numru/netcdfraw.c', line 1457

VALUE
NetCDF_var_set_endian(VALUE Var, VALUE endian)
{
  int ncid, varid, status;
  struct NetCDFVar *Netcdf_var;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_def_var_endian(ncid, varid, NUM2INT(endian));
  if(status != NC_NOERR) NC_RAISE(status);
  return(Var);
}

#fileObject



1900
1901
1902
1903
1904
1905
1906
1907
1908
# File 'ext/numru/netcdfraw.c', line 1900

VALUE
NetCDF_var_file(VALUE Var)
{
  struct NetCDFVar *Netcdf_var;
  /* VALUE file; */
  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  return (Netcdf_var->file);
}

#get_var1_byte(start) ⇒ Object



2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
# File 'ext/numru/netcdfraw.c', line 2379

VALUE
NetCDF_get_var1_byte(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;


  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR)NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

    c_count[i]=1;
  }




  Cbyte_to_NArray(NArray,ndims,c_count,ptr);
  status = nc_get_var1_uchar(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var1_char(start) ⇒ Object



2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
# File 'ext/numru/netcdfraw.c', line 2319

VALUE
NetCDF_get_var1_char(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;


  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR)NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

    c_count[i]=1;
  }




  Cbyte_to_NArray(NArray,ndims,c_count,ptr);
  status = nc_get_var1_text(ncid,varid,c_start,(char *)ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var1_float(start) ⇒ Object



2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
# File 'ext/numru/netcdfraw.c', line 2607

VALUE
NetCDF_get_var1_double(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  double *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR)NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status !=NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
    c_count[i]=1;
  }

  Cdouble_to_NArray(NArray,ndims,c_count,ptr);

  status = nc_get_var1_double(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var1_int(start) ⇒ Object



2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
# File 'ext/numru/netcdfraw.c', line 2495

VALUE
NetCDF_get_var1_int(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  int *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR)NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
    c_count[i]=1;
  }

  Clint_to_NArray(NArray,ndims,c_count,ptr);

  status = nc_get_var1_int(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var1_sfloat(start) ⇒ Object



2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
# File 'ext/numru/netcdfraw.c', line 2551

VALUE
NetCDF_get_var1_float(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  float *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR)NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid, varid, dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
    c_count[i]=1;
  }

  Cfloat_to_NArray(NArray,ndims,c_count,ptr);

  status = nc_get_var1_float(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var1_sint(start) ⇒ Object



2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
# File 'ext/numru/netcdfraw.c', line 2439

VALUE
NetCDF_get_var1_sint(VALUE Var,VALUE start)
{
  int ncid;
  int varid;
  int status;
  short *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int dimids[NC_MAX_DIMS];
  size_t dimlen;
  na_shape_t *c_count;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  c_count=ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    l_start = NUM2INT(RARRAY_PTR(start)[ndims-1-i]);
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
    c_count[i]=1;
  }

  Csint_to_NArray(NArray,ndims,c_count,ptr);

  status = nc_get_var1_short(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;

}

#get_var_byteObject



2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
# File 'ext/numru/netcdfraw.c', line 2099

VALUE
NetCDF_get_var_byte(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Cbyte_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_uchar(ncid,varid,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_var_charObject

Follow methods is to connect “NArray” with “Netcdf”



2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
# File 'ext/numru/netcdfraw.c', line 2055

VALUE
NetCDF_get_var_char(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Cbyte_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_text(ncid,varid,(char *)ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_var_floatObject



2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
# File 'ext/numru/netcdfraw.c', line 2275

VALUE
NetCDF_get_var_double(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  double *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Cdouble_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_double(ncid,varid,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_var_intObject



2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
# File 'ext/numru/netcdfraw.c', line 2187

VALUE
NetCDF_get_var_int(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  int *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Clint_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_int(ncid,varid,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_var_sfloatObject



2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
# File 'ext/numru/netcdfraw.c', line 2231

VALUE
NetCDF_get_var_float(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  float *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Cfloat_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_float(ncid,varid,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_var_sintObject



2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
# File 'ext/numru/netcdfraw.c', line 2143

VALUE
NetCDF_get_var_sint(VALUE Var)
{
  int ncid;
  int varid;
  int status;
  short *ptr;
  struct NetCDFVar *Netcdf_var;
  int i=0;
  int ndimsp;
  int *dimids;
  size_t lengthp;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  dimids = ALLOCA_N(int,ndimsp);
  if (ndimsp != 0){
      shape = ALLOCA_N(na_shape_t,ndimsp);
      for(i=0;i<ndimsp;i++){
	  status = nc_inq_vardimid(ncid,varid,dimids);
	  if(status != NC_NOERR) NC_RAISE(status);
	  nc_inq_dimlen(ncid,dimids[i],&lengthp);
	  shape[ndimsp-1-i]=lengthp;
      }
  } else {
      ndimsp = 1;
      shape = ALLOCA_N(na_shape_t,1);
      shape[0]=1;
  }

  Csint_to_NArray(NArray,ndimsp,shape,ptr);

  status = nc_get_var_short(ncid,varid,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_byte(start, end, stride) ⇒ Object



2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
# File 'ext/numru/netcdfraw.c', line 2771

VALUE
NetCDF_get_vars_byte(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Cbyte_to_NArray(NArray,ndims,shape,ptr);

  status = nc_get_vars_uchar(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_char(start, end, stride) ⇒ Object



2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
# File 'ext/numru/netcdfraw.c', line 2663

VALUE
NetCDF_get_vars_char(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Cbyte_to_NArray(NArray,ndims,shape,ptr);

  status = nc_get_vars_text(ncid,varid,c_start,c_count,c_stride,(char *)ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_float(start, end, stride) ⇒ Object



3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
# File 'ext/numru/netcdfraw.c', line 3206

VALUE
NetCDF_get_vars_double(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  double *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Cdouble_to_NArray(NArray,ndims,shape,ptr);

  status = nc_get_vars_double(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_int(start, end, stride) ⇒ Object



2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
# File 'ext/numru/netcdfraw.c', line 2988

VALUE
NetCDF_get_vars_int(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  int *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Clint_to_NArray(NArray,ndims,shape,ptr);


  status = nc_get_vars_int(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_sfloat(start, end, stride) ⇒ Object



3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
# File 'ext/numru/netcdfraw.c', line 3097

VALUE
NetCDF_get_vars_float(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  float *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Cfloat_to_NArray(NArray,ndims,shape,ptr);


  status = nc_get_vars_float(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_vars_sint(start, end, stride) ⇒ Object



2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
# File 'ext/numru/netcdfraw.c', line 2879

VALUE
NetCDF_get_vars_sint(VALUE Var,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  short *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  na_shape_t *shape;    /* NArray uses int instead of size_t */
  int ndims;
  int *dimids;
  size_t dimlen;
  VALUE NArray;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid = Netcdf_var->ncid;
  varid = Netcdf_var->varid;

  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);
  if(ndims == 0) {
    rb_raise(rb_eNetcdfError,"Cannot specify a subset of a rank-0 scalar\n");
  }

  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims){
    rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start = ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      c_stride[i]=1;
    }
    break;
  default:
    Check_Type(stride,T_ARRAY);
    if(RARRAY_LEN(stride) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'stride is too short\n");
    }
    for(i=0;i<ndims; i++){
      c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
      if(c_stride[i]==0){
	rb_raise(rb_eNetcdfError,"stride cannot be zero\n");
      }
    }
  }

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
    for(i=0; i<ndims; i++){
      nc_inq_dimlen(ncid,dimids[i],&dimlen);
      c_count[i]=(dimlen-c_start[i]-1)/c_stride[i]+1;
    }
    break;
  default:
    Check_Type(end,T_ARRAY);
    if(RARRAY_LEN(end) <ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
    }
    for(i=0; i<ndims; i++){
      l_end= NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
      if(l_end < 0) {
	status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_end +=dimlen;
      }
      c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
    }
  }


  shape = ALLOCA_N(na_shape_t,ndims);
  for(i=0;i<ndims;i++){
    shape[ndims-1-i]=c_count[i];
  }

  Csint_to_NArray(NArray,ndims,shape,ptr);


  status = nc_get_vars_short(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);

  OBJ_TAINT(NArray);
  return NArray;
}

#get_with_miss(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/numru/netcdf_miss.rb', line 8

def get_with_miss(*args)
  __interpret_missing_params if !defined?(@missval)
  data = simple_get(*args)
  if @vmin || @vmax
	if @vmin
	  mask = (data >= @vmin) 
	  mask = mask.and(data <= @vmax) if @vmax
	else
	  mask = (data <= @vmax)
	end
	data = NArrayMiss.to_nam(data, mask)
  elsif @missval	# only missing_value is present.
	mask = (data.ne(@missval)) 
	data = NArrayMiss.to_nam(data, mask)
  end
  data
end

#get_with_miss_and_scaling(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/numru/netcdf_miss.rb', line 26

def get_with_miss_and_scaling(*args)
  __interpret_missing_params if !defined?(@missval)
  data = simple_get(*args)
  if @vmin || @vmax
	if @vmin
	  mask = (data >= @vmin) 
	  mask = mask.and(data <= @vmax) if @vmax
	else
	  mask = (data <= @vmax)
	end
	data = NArrayMiss.to_nam(data, mask)
  elsif @missval	# only missing_value is present.
	mask = (data.ne(@missval))
	data = NArrayMiss.to_nam(data, mask)
  end
  data = unpack( data )
  data
end

#inspectObject



775
776
777
# File 'lib/numru/netcdf.rb', line 775

def inspect
  'NetCDFVar:'+file.path+'?var='+name
end

#nameObject



1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
# File 'ext/numru/netcdfraw.c', line 1792

VALUE
NetCDF_var_inq_name(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  char c_var_name[NC_MAX_NAME];
  struct NetCDFVar *Netcdf_var;
  VALUE Var_name;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varname(ncid,varid,c_var_name);
  if(status != NC_NOERR) NC_RAISE(status);

  Var_name=rb_str_new2(c_var_name);
  OBJ_TAINT(Var_name);
  return Var_name;
}

#name=(var_new_name) ⇒ Object



1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
# File 'ext/numru/netcdfraw.c', line 1910

VALUE
NetCDF_var_rename(VALUE Var,VALUE var_new_name)
{
  int ncid;
  int status;
  int varid;
  char *c_var_new_name;
  struct NetCDFVar *Netcdf_var;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Check_Type(var_new_name,T_STRING);
  c_var_new_name=StringValueCStr(var_new_name);

  status = nc_rename_var(ncid,varid,c_var_new_name);
  if(status !=NC_NOERR) NC_RAISE(status);

  return Qnil;
}

#nattsObject



1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
# File 'ext/numru/netcdfraw.c', line 1877

VALUE
NetCDF_var_natts(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  int nattsp;
  struct NetCDFVar *Netcdf_var;
  VALUE Var_natts;


  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  status= nc_inq_varnatts(ncid,varid,&nattsp);
  if(status !=NC_NOERR) NC_RAISE(status);

  Var_natts=INT2FIX(nattsp);
  return Var_natts;
}

#ndimsObject Also known as: rank



1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
# File 'ext/numru/netcdfraw.c', line 1814

VALUE
NetCDF_var_ndims(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  int ndimsp;
  struct NetCDFVar *Netcdf_var;
  VALUE Var_ndims;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  Var_ndims=INT2FIX(ndimsp);
  return Var_ndims;
}

#ntypeObject



1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'ext/numru/netcdfraw.c', line 1834

VALUE
NetCDF_var_vartype(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  nc_type xtypep;
  struct NetCDFVar *Netcdf_var;
  const char *Vartype;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  status = nc_inq_vartype(ncid,varid,&xtypep);
  if(status != NC_NOERR) NC_RAISE(status);

  Vartype=nctype2natype(xtypep);
  return(rb_str_new2(Vartype));
}

#pack(na) ⇒ Object

The put and get methods in the NetCDFVar class



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/numru/netcdf.rb', line 341

def pack(na)
  sf = att('scale_factor')
  ao = att('add_offset')
  if ( sf == nil && ao == nil ) then
    na
  else
    na = NArray.to_na(na) if na.is_a?(Array)
    if sf
      csf = sf.get
      raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String)
      raise NetcdfError, "scale_factor is not unique" if csf.length != 1
      raise NetcdfError, "zero scale_factor" if csf[0] == 0
    else
      csf = nil
    end
    if ao
      cao = ao.get
      raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String)
      raise NetcdfError, "add_offset is not unique" if cao.length != 1
    else
      cao = nil
    end
    if csf and cao
      packed = (na - cao) / csf
    elsif csf
      packed = na / csf
    elsif cao
      packed = na - cao
    end
    if self.typecode <= NArray::LINT
      packed = packed.round
    end
    packed
  end
end

#put_att(attname, val, atttype = nil) ⇒ Object



315
316
317
# File 'lib/numru/netcdf.rb', line 315

def put_att(attname,val,atttype=nil)
  put_attraw(attname,val,atttype)
end

#put_attrawObject

atttype: nil or a String (“string”,“int”,etc). If nil,

the type of attribute is determined from the type of value


752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'ext/numru/netcdfraw.c', line 752

VALUE
NetCDF_put_att_var(VALUE var,VALUE att_name,VALUE value,VALUE atttype)
     /*
      * atttype: nil or a String ("string","int",etc). If nil,
      *          the type of attribute is determined from the type of value
      */
{
    struct NetCDFVar *ncvar;
    char *name;

    Data_Get_Struct(var,struct NetCDFVar,ncvar);
    Check_Type(att_name,T_STRING);
    name = RSTRING_PTR(att_name);

    return( NetCDF_put_att__(ncvar->ncid, name, value, atttype, ncvar->varid));
}

#put_var1_byte(NArray, start) ⇒ Object



3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
# File 'ext/numru/netcdfraw.c', line 3636

VALUE
NetCDF_put_var1_byte(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Cbyte(NArray,ptr);

  status = nc_put_var1_uchar(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var1_char(NArray, start) ⇒ Object



3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
# File 'ext/numru/netcdfraw.c', line 3586

VALUE
NetCDF_put_var1_char(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Cbyte(NArray,ptr);

  status = nc_put_var1_text(ncid,varid,c_start,(char *)ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var1_float(NArray, start) ⇒ Object



3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
# File 'ext/numru/netcdfraw.c', line 3835

VALUE
NetCDF_put_var1_double(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  double *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Cdouble(NArray,ptr);

  status = nc_put_var1_double(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var1_int(NArray, start) ⇒ Object



3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
# File 'ext/numru/netcdfraw.c', line 3735

VALUE
NetCDF_put_var1_int(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  int *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Clint(NArray,ptr);

  status = nc_put_var1_int(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var1_sfloat(NArray, start) ⇒ Object



3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
# File 'ext/numru/netcdfraw.c', line 3785

VALUE
NetCDF_put_var1_float(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  float *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Cfloat(NArray,ptr);

  status = nc_put_var1_float(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var1_sint(NArray, start) ⇒ Object



3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
# File 'ext/numru/netcdfraw.c', line 3686

VALUE
NetCDF_put_var1_sint(VALUE Var,VALUE NArray,VALUE start)
{
  int ncid;
  int varid;
  int status;
  short *ptr;
  int i;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start;
  size_t *c_start;
  int ndims;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid,varid,&ndims);
  if(status != NC_NOERR) NC_RAISE(status);


  dimids = ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid,varid,dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) <ndims) {
    rb_raise(rb_eNetcdfError,"Length of 'start' is too short\n");
  }

  c_start=ALLOCA_N(size_t,ndims);
  for(i=0;i<ndims;i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
      status = nc_inq_dimlen(ncid,dimids[i],&dimlen);
      if(status != NC_NOERR) NC_RAISE(status);
      l_start += dimlen;
    }
    c_start[i]=l_start;

  }
  Array_to_Csint(NArray,ptr);

  status = nc_put_var1_short(ncid,varid,c_start,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_byte(NArray) ⇒ Object



3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
# File 'ext/numru/netcdfraw.c', line 3359

VALUE
NetCDF_put_var_byte(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Array_to_Cbyte_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(unsigned char,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status );
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array in the '%s'\n",var_name);
  }
  status = nc_put_var_uchar(ncid,varid,ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_char(NArray) ⇒ Object

The “get*” or “put*” methods in the NetCDFVar class



3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
# File 'ext/numru/netcdfraw.c', line 3315

VALUE
NetCDF_put_var_char(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Array_to_Cbyte_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(unsigned char,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status );
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array in the '%s'\n",var_name);
  }
  status = nc_put_var_text(ncid,varid,(char *)ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_float(NArray) ⇒ Object



3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
# File 'ext/numru/netcdfraw.c', line 3540

VALUE
NetCDF_put_var_double(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  double *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;


  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Array_to_Cdouble_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(double,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status);
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
  }

  status = nc_put_var_double(ncid,varid,ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_int(NArray) ⇒ Object



3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
# File 'ext/numru/netcdfraw.c', line 3447

VALUE
NetCDF_put_var_int(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  int *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Array_to_Clint_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(int,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status);
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
  }


  status = nc_put_var_int(ncid,varid,ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_sfloat(NArray) ⇒ Object



3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
# File 'ext/numru/netcdfraw.c', line 3494

VALUE
NetCDF_put_var_float(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  float *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;


  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  Array_to_Cfloat_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(float,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status);
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
  }

  status = nc_put_var_float(ncid,varid,ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_var_sint(NArray) ⇒ Object



3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
# File 'ext/numru/netcdfraw.c', line 3403

VALUE
NetCDF_put_var_short(VALUE Var,VALUE NArray)
{
  int ncid;
  int varid;
  int status;
  short *ptr,scalar;
  na_shape_t len,i=0;
  struct NetCDFVar *Netcdf_var;
  na_shape_t nc_tlen=1;
  int ndimsp;
  int dimids[NC_MAX_DIMS];
  size_t lengthp;
  char *var_name;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  Array_to_Csint_len(NArray,ptr,len);

  status = nc_inq_varndims(ncid,varid,&ndimsp);
  if(status != NC_NOERR) NC_RAISE(status);
  for(i=0;i<ndimsp;i++){
    status = nc_inq_vardimid(ncid,varid,dimids);
    if(status != NC_NOERR) NC_RAISE(status);
    nc_inq_dimlen(ncid,dimids[i],&lengthp);
    nc_tlen=lengthp*nc_tlen;
  }
  if(len == 1 && len != nc_tlen){
    scalar = *ptr;
    ptr = ALLOCA_N(short,nc_tlen);
    for(i=0;i<nc_tlen;i++){ptr[i]=scalar;}
  } else if(len != nc_tlen){
    var_name=ALLOCA_N(char,NC_MAX_NAME);
    status = nc_inq_varname(ncid,varid,var_name);
    if(status != NC_NOERR) NC_RAISE(status);
    rb_raise(rb_eNetcdfError,"Length of NArray don't equal to length of total array length in the '%s'\n",var_name);
  }

  status = nc_put_var_short(ncid,varid,ptr);
  if(status !=NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_byte(NArray, start, end, stride) ⇒ Object



3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
# File 'ext/numru/netcdfraw.c', line 3991

VALUE
NetCDF_put_vars_byte(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Cbyte_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(unsigned char,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	rb_raise(rb_eNetcdfError,
		 "lengh of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_uchar(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_char(NArray, start, end, stride) ⇒ Object



3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
# File 'ext/numru/netcdfraw.c', line 3886

VALUE
NetCDF_put_vars_char(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  unsigned char *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Cbyte_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(unsigned char,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	rb_raise(rb_eNetcdfError,
		 "lengh of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_text(ncid,varid,c_start,c_count,c_stride,(char *)ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_float(NArray, start, end, stride) ⇒ Object



4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
# File 'ext/numru/netcdfraw.c', line 4414

VALUE
NetCDF_put_vars_double(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  double *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Cdouble_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(double,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	  rb_raise(rb_eNetcdfError,
              "lengh of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_double(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_int(NArray, start, end, stride) ⇒ Object



4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
# File 'ext/numru/netcdfraw.c', line 4202

VALUE
NetCDF_put_vars_int(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  int *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Clint_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(int,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	  rb_raise(rb_eNetcdfError,
              "length of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_int(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_sfloat(NArray, start, end, stride) ⇒ Object



4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
# File 'ext/numru/netcdfraw.c', line 4308

VALUE
NetCDF_put_vars_float(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  float *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Cfloat_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(float,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	  rb_raise(rb_eNetcdfError,
              "lengh of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_float(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_vars_sint(NArray, start, end, stride) ⇒ Object



4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
# File 'ext/numru/netcdfraw.c', line 4096

VALUE
NetCDF_put_vars_sint(VALUE Var,VALUE NArray,VALUE start,VALUE end,VALUE stride)
{
  int ncid;
  int varid;
  int status;
  short *ptr,scalar;
  na_shape_t len;
  int c_count_all=1;
  struct NetCDFVar *Netcdf_var;
  na_shape_t l_start, l_end;
  size_t *c_start;
  size_t *c_count;
  ptrdiff_t *c_stride;
  int ndims,i;
  na_shape_t *shape;
  int   *dimids;
  size_t dimlen;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);
  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;
  status = nc_inq_varndims(ncid, varid, &ndims);
  if(status != NC_NOERR) NC_RAISE(status);

  dimids=ALLOCA_N(int,ndims);
  status = nc_inq_vardimid(ncid, varid, dimids);
  if(status != NC_NOERR) NC_RAISE(status);

  Check_Type(start,T_ARRAY);
  if(RARRAY_LEN(start) < ndims) {
      rb_raise(rb_eNetcdfError, "Length of 'start' is too short\n");
  }
  c_start=ALLOCA_N(size_t,ndims);
  for(i=0; i<ndims; i++){
    l_start=NUM2INT(RARRAY_PTR(start)[ndims-1-i]);

    if(l_start < 0) {
	status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	if(status != NC_NOERR) NC_RAISE(status);
	l_start += dimlen;
    }
    c_start[i]=l_start;
  }

  c_stride=ALLOCA_N(ptrdiff_t,ndims);
  switch(TYPE(stride)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_stride[i]=1;
      }
      break;
  default:
      Check_Type(stride,T_ARRAY);
      if(RARRAY_LEN(stride) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'stride' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  c_stride[i]=NUM2INT(RARRAY_PTR(stride)[ndims-1-i]);
	  if(c_stride[i]==0) {
	      rb_raise(rb_eNetcdfError, "stride cannot be zero\n");
	  }
      }
  }

  Array_to_Csint_len_shape(NArray,ptr,len,shape);

  c_count=ALLOCA_N(size_t,ndims);
  switch(TYPE(end)){
  case T_NIL:
      for(i=0; i<ndims; i++){
	  c_count[i]=shape[i];
      }
      c_count_all=len;
      break;
  default:
      Check_Type(end,T_ARRAY);
      if(RARRAY_LEN(end) < ndims) {
	  rb_raise(rb_eNetcdfError, "Length of 'end' is too short\n");
      }
      for(i=0; i<ndims; i++){
	  l_end=NUM2INT(RARRAY_PTR(end)[ndims-1-i]);
	  if(l_end < 0) {
	      status = nc_inq_dimlen(ncid, dimids[i], &dimlen);
	      if(status != NC_NOERR) NC_RAISE(status);
	      l_end += dimlen;
	  }
	  c_count[i]=(l_end-c_start[i])/c_stride[i]+1;
	  c_count_all=c_count[i]*c_count_all;
      }
      if(len == 1 && len != c_count_all){
	  scalar = *ptr;
	  ptr = ALLOCA_N(short,c_count_all);
	  for(i=0;i<c_count_all;i++){ptr[i]=scalar;}
      } else if(len != c_count_all) {
	  rb_raise(rb_eNetcdfError,
              "lengh of the array does not agree with that of the subset\n");
      }
  }

  status = nc_put_vars_short(ncid,varid,c_start,c_count,c_stride,ptr);
  if(status != NC_NOERR) NC_RAISE(status);
  return Qnil;
}

#put_with_miss(data, *args) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/numru/netcdf_miss.rb', line 45

def put_with_miss(data, *args)
  if data.is_a?( NArrayMiss )
	__interpret_missing_params if !defined?(@missval)
	if @missval
	  simple_put(data.to_na(@missval), *args)
	else
	  simple_put(data.to_na, *args)
	end
  else
	simple_put(data, *args)
  end
end

#put_with_miss_and_scaling(data, *args) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/numru/netcdf_miss.rb', line 58

def put_with_miss_and_scaling(data, *args)
  if data.is_a?( NArrayMiss )
	__interpret_missing_params if !defined?(@missval)
	if @missval
	  data = pack( data )
	  data = data.to_na(@missval)
	else
	  data = pack( data )
	  data = data.to_na
	end
	simple_put(data, *args)
  else
	scaled_put(data, *args)
  end
end

#scaled_get(hash = nil) ⇒ Object



430
431
432
# File 'lib/numru/netcdf.rb', line 430

def scaled_get(hash=nil)
  unpack( simple_get(hash) )
end

#scaled_put(var, hash = nil) ⇒ Object



377
378
379
# File 'lib/numru/netcdf.rb', line 377

def scaled_put(var,hash=nil)
  simple_put( pack(var), hash)
end

#shape_currentObject



331
332
333
334
335
336
337
# File 'lib/numru/netcdf.rb', line 331

def shape_current
  sh = []
  dims.each{|d|
    sh.push(d.length)
  }
  sh
end

#shape_ul0Object



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/numru/netcdf.rb', line 319

def shape_ul0
  sh = []
  dims.each{|d|
    if d.unlimited? then
      sh.push(0)
    else
      sh.push(d.length)
    end
  }
  sh
end

#simple_get(hash = nil) ⇒ Object Also known as: get



540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/numru/netcdf.rb', line 540

def simple_get(hash=nil)
  t_var = self.vartype
  if hash == nil
    if t_var == "char"
      get_var_char
    elsif t_var == "byte"
      get_var_byte
    elsif t_var == "sint"
      get_var_sint
    elsif t_var == "int"
      get_var_int
    elsif t_var == "sfloat"
      get_var_sfloat
    elsif t_var == "float"
      get_var_float
    else
      raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
    end
  elsif hash.key?("index")==true
    ind = hash["index"]
    if t_var == "char"
      get_var1_char(ind)
    elsif t_var == "byte"
      get_var1_byte(ind)
    elsif t_var == "sint"
      get_var1_sint(ind)
    elsif t_var == "int"
      get_var1_int(ind)
    elsif t_var == "sfloat"
      get_var1_sfloat(ind)
    elsif t_var == "float"
      get_var1_float(ind)
    else
      raise NetcdfError,"variable type #{t_var} isn't supported in netCDF"
    end
  elsif hash.key?("start")==true
    h_sta = hash["start"]
    h_end = hash["end"]     # can be nill
    h_str = hash["stride"]  # can be nill
    if NetCDF.nc4? && h_str && ((xstr=h_str[0]) != 1)
      # Tentative treatment for the very slow netcdf-4 reading with step.
      # Reading with step is generally slow with NetCDF 4, but it is
      # particularly so for the first dimension.
      # Ref: http://www.unidata.ucar.edu/mailing_lists/archives/netcdfgroup/2013/msg00311.html
      h_str[0] = 1
      nc4remedy = true
    else
      nc4remedy = false
    end
    if t_var == "char"
      v = get_vars_char(h_sta,h_end,h_str)
    elsif t_var == "byte"
      v = get_vars_byte(h_sta,h_end,h_str)
    elsif t_var == "sint"
      v = get_vars_sint(h_sta,h_end,h_str)
    elsif t_var == "int"
      v = get_vars_int(h_sta,h_end,h_str)
    elsif t_var == "sfloat"
      v = get_vars_sfloat(h_sta,h_end,h_str)
    elsif t_var == "float"
      v = get_vars_float(h_sta,h_end,h_str)
    else
      raise NetcdfError, "variable type #{t_var} isn't supported in netCDF"
    end
    if nc4remedy
      idx = []
      (0...v.shape[0]).step(xstr){|k| idx.push(k)}
      v = v[idx,false]
    end
    v
  else
    raise ArgumentError,"{'start'}=>{ARRAY} or {'index'}=>{ARRAY} is needed"
  end
end

#simple_put(var, hash = nil) ⇒ Object Also known as: put



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/numru/netcdf.rb', line 434

def simple_put(var,hash=nil)
  if hash==nil
    if self.vartype == "char"
      put_var_char(var)
    elsif self.vartype == "byte"
      put_var_byte(var)
    elsif self.vartype == "sint"
      put_var_sint(var)
    elsif self.vartype == "int"
      put_var_int(var)
    elsif self.vartype == "sfloat"
      put_var_sfloat(var)
    elsif self.vartype == "float"
      put_var_float(var)
    else
      raise NetcdfError,"variable type isn't supported in netCDF"
    end
  elsif hash.key?("index")==true
    if self.vartype == "char"
      put_var1_char(var,hash["index"])
    elsif self.vartype=="byte"
      put_var1_byte(var,hash["index"])
    elsif self.vartype=="sint"
      put_var1_sint(var,hash["index"])
    elsif self.vartype == "int"
      put_var1_int(var,hash["index"])
    elsif self.vartype == "sfloat"
      put_var1_sfloat(var,hash["index"])
    elsif self.vartype == "float"
      put_var1_float(var,hash["index"])
    else
      raise NetcdfError,"variable type isn't supported in netCDF"
    end
  elsif hash.key?("start")==true
    if hash.key?("end")==false && hash.key?("stride")==false
      if self.vartype == "char"
        put_vars_char(var,hash["start"],nil,nil)
      elsif self.vartype=="byte"
        put_vars_byte(var,hash["start"],nil,nil)
      elsif self.vartype=="sint"
        put_vars_sint(var,hash["start"],nil,nil)
      elsif self.vartype=="int"
        put_vars_int(var,hash["start"],nil,nil)
      elsif self.vartype=="sfloat"
        put_vars_sfloat(var,hash["start"],nil,nil)
      elsif self.vartype=="float"
        put_vars_float(var,hash["start"],nil,nil)
      else
        raise NetcdfError, "variable type isn't supported in netCDF"
      end
    elsif hash.key?("end")==true && hash.key?("stride") == false
      if self.vartype == "char"
        put_vars_char(var,hash["start"],hash["end"],nil)
      elsif self.vartype=="byte"
        put_vars_byte(var,hash["start"],hash["end"],nil)
      elsif self.vartype=="sint"
        put_vars_sint(var,hash["start"],hash["end"],nil)
      elsif self.vartype=="int"
        put_vars_int(var,hash["start"],hash["end"],nil)
      elsif self.vartype == "sfloat"
        put_vars_sfloat(var,hash["start"],hash["end"],nil)
      elsif self.vartype =="float"
        put_vars_float(var,hash["start"],hash["end"],nil)
      else
        raise NetcdfError, "variable type isn't supported in netCDF"
      end
    elsif hash.key?("end")==false && hash.key?("stride")==true
      if self.vartype == "char"
        put_vars_char(var,hash["start"],nil,hash["stride"])
      elsif self.vartype=="byte"
        put_vars_byte(var,hash["start"],nil,hash["stride"])
      elsif self.vartype=="sint"
        put_vars_sint(var,hash["start"],nil,hash["stride"])
      elsif self.vartype=="int"
        put_vars_int(var,hash["start"],nil,hash["stride"])
      elsif self.vartype=="sfloat"
        put_vars_sfloat(var,hash["start"],nil,hash["stride"])
      elsif self.vartype=="float"
        put_vars_float(var,hash["start"],nil,hash["stride"])
      else
        raise NetcdfError, "variable type isn't supported in netCDF"
      end
    else hash.key?("end")==true && hash.key?("stride")==true
      if self.vartype == "char"
        put_vars_char(var,hash["start"],hash["end"],hash["stride"])
      elsif self.vartype=="byte"
        put_vars_byte(var,hash["start"],hash["end"],hash["stride"])
      elsif self.vartype=="sint"
        put_vars_sint(var,hash["start"],hash["end"],hash["stride"])
      elsif self.vartype=="int"
        put_vars_int(var,hash["start"],hash["end"],hash["stride"])
      elsif self.vartype=="sfloat"
        put_vars_sfloat(var,hash["start"],hash["end"],hash["stride"])
      elsif self.vartype=="float"
        put_vars_float(var,hash["start"],hash["end"],hash["stride"])
      else
        raise NetcdfError, "variable type isn't supported in netCDF"
      end
    end
  else
    raise ArgumentError,"{'start'}=>[ARRAY] or {'index'}=>[ARRAY] is needed"
  end
end

#typecodeObject



1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
# File 'ext/numru/netcdfraw.c', line 1856

VALUE
NetCDF_var_typecode(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  nc_type xtypep;
  struct NetCDFVar *Netcdf_var;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  status = nc_inq_vartype(ncid,varid,&xtypep);
  if(status != NC_NOERR) NC_RAISE(status);

  return(INT2NUM(nctype2natypecode(xtypep)));
}

#unpack(na) ⇒ Object



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/numru/netcdf.rb', line 397

def unpack(na)
  sf = att('scale_factor')
  ao = att('add_offset')
  if ( sf == nil && ao == nil ) then
    na
  else
    if sf
      csf = sf.get
      raise NetcdfError,"scale_factor is not a numeric" if csf.is_a?(String)
      raise NetcdfError, "scale_factor is not unique" if csf.length != 1
      raise NetcdfError, "zero scale_factor" if csf[0] == 0
    else
      csf =nil
    end
    if ao
      cao = ao.get
      raise NetcdfError, "add_offset is not a numeric" if cao.is_a?(String)
      raise NetcdfError, "add_offset is not unique" if cao.length != 1
    else
      cao = nil
    end
    if csf and cao
      una = na * csf + cao  # csf & cao are NArray -> coerced to their types
    elsif csf
      una = na * csf
    elsif cao
      una = na + cao
    end
    una = una.to_type(@@unpack_type) if @@unpack_type
    una
  end
end

#vartypeObject



1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
# File 'ext/numru/netcdfraw.c', line 1834

VALUE
NetCDF_var_vartype(VALUE Var)
{
  int ncid;
  int status;
  int varid;
  nc_type xtypep;
  struct NetCDFVar *Netcdf_var;
  const char *Vartype;

  Data_Get_Struct(Var,struct NetCDFVar,Netcdf_var);

  ncid=Netcdf_var->ncid;
  varid=Netcdf_var->varid;

  status = nc_inq_vartype(ncid,varid,&xtypep);
  if(status != NC_NOERR) NC_RAISE(status);

  Vartype=nctype2natype(xtypep);
  return(rb_str_new2(Vartype));
}