Class: NumRu::GrADSVar

Inherits:
Object
  • Object
show all
Defined in:
lib/numru/gphys/grads_gridded.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, varname, varonly = false, dimonly = false) ⇒ GrADSVar

Returns a new instance of GrADSVar.



1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
# File 'lib/numru/gphys/grads_gridded.rb', line 1221

def initialize(file,varname,varonly=false,dimonly=false)
  @varname = varname

  if file.is_a?(String)
    file = GrADS_Gridded.open(file)
  elsif !file.is_a?(GrADS_Gridded)
    raise ArgumentError, "1st arg must be a GrADS_Gridded or a file name"
  end
  @ctl = file  # control file name

  @attr = NumRu::Attribute.new

  found = false
  if !dimonly
    @dimensions = @ctl.dimensions
    @rank = @dimensions.length
    @ctl.variables.each do |var|
      if @varname == var[:name]
        @idim = -1
        @attr[:long_name] = var[:description]
        @attr[:nlev] = var[:nlev].to_s

        @shape = [@dimensions[0][:len],@dimensions[1][:len],
                  @dimensions[2][:len],@dimensions[3][:len]]
        @shape[2] = var[:nlev].to_i if( var[:nlev] )

        @attr[:missing_value] = NArray.sfloat(1).fill!(@ctl.undef)
        found = true
      end
    end
  end

  if !found && !varonly
    @ctl.dimensions.each_with_index do |dim,i|
      if @varname == dim[:name]
        @idim = i
        @dimensions = [ dim ]
        @rank = 1
        @attr[:name] = dim[:name]
        @attr[:long_name] = dim[:description]
        @attr[:units] = dim[:units]
        if @attr[:long_name] == "time" && @ctl.get_att("365_day_calendar") == true
          @attr[:calendar] = "365_day"
        end

        @shape = [dim[:len]]
        found = true
      end
    end
  end

  raise "variable #{@varname} is not found" if !found
end

Instance Method Details

#[](*a) ⇒ Object

copied from NetCDFVar class



1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
# File 'lib/numru/gphys/grads_gridded.rb', line 1475

def [](*a)  # copied from NetCDFVar class
  a = __rubber_expansion(a)
  first = Array.new
  last = Array.new
  stride = Array.new
  set_stride = false
  a.each{|i|
    if(i.is_a?(Fixnum))
      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?(Fixnum) 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 Fixnum, 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?(Fixnum)
   }
   na.reshape!( *shape )
  na
end

#[]=(*a) ⇒ Object

copied from NetCDFVar class



1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
# File 'lib/numru/gphys/grads_gridded.rb', line 1543

def []=(*a)  # copied from NetCDFVar class
  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?(Fixnum))
      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?(Fixnum)}.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?(Fixnum) 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 Fixnum, 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(name) ⇒ Object



1326
1327
1328
# File 'lib/numru/gphys/grads_gridded.rb', line 1326

def att(name)
  raise "use get_att instead of att"
end

#att_namesObject



1338
1339
1340
# File 'lib/numru/gphys/grads_gridded.rb', line 1338

def att_names
  @attr.keys
end

#attrObject

obsolete



1350
1351
1352
# File 'lib/numru/gphys/grads_gridded.rb', line 1350

def attr # obsolete
  @attr
end

#dim_namesObject

def dim(name)

def dims(names)


1281
1282
1283
1284
1285
# File 'lib/numru/gphys/grads_gridded.rb', line 1281

def dim_names
  ary = Array.new()
  @dimensions.each{|dim| ary.push(dim[:name])}
  ary
end

#each_attObject



1342
1343
1344
# File 'lib/numru/gphys/grads_gridded.rb', line 1342

def each_att
  raise "each_att not supported"
end

#fileObject



1354
1355
1356
# File 'lib/numru/gphys/grads_gridded.rb', line 1354

def file
  @ctl.ctlfile
end

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



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
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
# File 'lib/numru/gphys/grads_gridded.rb', line 1393

def get(hash=nil)
  if @idim != -1
    na = @ctl.get_dim(@dimensions[0])
    if hash == nil
      
    elsif hash.key?("start")==true
      h_sta = hash["start"]
      endq = hash.key?("end")
      strq = hash.key?("stride")
      if endq == false && strq == false
        na = na[h_sta[0]..-1]
      elsif endq == true && strq == false
        h_end = hash["end"]
        na = na[h_sta[0]..h_end[0]]
      elsif endq == false && strq == true
        h_str = hash["stride"]
        raise "sorry, stride is not supported yet."
      else endq == true && strq == true
        h_end = hash["end"]
        h_str = hash["stride"]
        raise "sorry, stride is not supported yet."
      end
    end
  else
    h_sta = [0, 0, 0, 0]
    h_end = [-1, -1, -1, -1]
    if hash == nil
#      elsif hash.key?("index")==true
    else
      if hash.key?("start")==true
        h_sta = hash["start"]
        if hash.key?("end")==true
          h_end = hash["end"]
        end
      end
      if hash.key?("stride")==true
        raise "sorry, stride is not supported yet."
      end
    end

    idx_x = NArray.int(@shape[0]).indgen!
    idx_y = NArray.int(@shape[1]).indgen!
    idx_z = NArray.int(@shape[2]).indgen!
    idx_t = NArray.int(@shape[3]).indgen!
    str_x = idx_x[h_sta[0]]; end_x = idx_x[h_end[0]]
    str_y = idx_y[h_sta[1]]; end_y = idx_y[h_end[1]]
    str_z = idx_z[h_sta[2]]; end_z = idx_z[h_end[2]]
    str_t = idx_t[h_sta[3]]; end_t = idx_t[h_end[3]]

    na = NArrayMiss.new(vartype,end_x-str_x+1,end_y-str_y+1,end_z-str_z+1,end_t-str_t+1)
    for t in str_t..end_t
      for z in str_z..end_z
        na_xy = @ctl.get(@varname,z,t,[str_x,end_x,str_y,end_y])
        mask = ( na_xy.ne @ctl.undef )
        na[true,true,z-str_z,t-str_t] = NArrayMiss.to_nam(na_xy,mask)
      end
    end
  end
    na
end

#get_att(name) ⇒ Object



1330
1331
1332
# File 'lib/numru/gphys/grads_gridded.rb', line 1330

def get_att(name)
  @attr[name]
end

#nameObject



1287
1288
1289
# File 'lib/numru/gphys/grads_gridded.rb', line 1287

def name
  @varname
end

#name=Object



1291
1292
1293
# File 'lib/numru/gphys/grads_gridded.rb', line 1291

def name=
  raise "name= not supported"
end

#nattsObject



1334
1335
1336
# File 'lib/numru/gphys/grads_gridded.rb', line 1334

def natts
  @attr.length
end

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



1389
1390
1391
# File 'lib/numru/gphys/grads_gridded.rb', line 1389

def put(var,hash=nil)
  raise "put not supported"
end

#put_att(name, value) ⇒ Object



1346
1347
1348
# File 'lib/numru/gphys/grads_gridded.rb', line 1346

def put_att(name,value)
  @attr[name] = value
end

#scaled_get(hash = nil) ⇒ Object



1385
1386
1387
# File 'lib/numru/gphys/grads_gridded.rb', line 1385

def scaled_get(hash=nil)
  raise "scaled_get not supported"
end

#scaled_put(var, hash = nil) ⇒ Object



1381
1382
1383
# File 'lib/numru/gphys/grads_gridded.rb', line 1381

def scaled_put(var,hash=nil)
  raise "scaled_put not supported"
end

#shape_currentObject Also known as: shape_ul0

def shape_ul0

  sh = []
  @dimensions.each{|dim|
    if d.unlimited? then
      sh.push(0)
    else
      sh.push(dim[:len])
    end
  }
  sh
end


1370
1371
1372
1373
1374
1375
1376
1377
# File 'lib/numru/gphys/grads_gridded.rb', line 1370

def shape_current
#    sh = []
#    @dimensions.each{|dim|
#      sh.push(dim[:len])
#    }
#    sh
  @shape
end

#typecodeObject



1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
# File 'lib/numru/gphys/grads_gridded.rb', line 1313

def typecode
  if( @idim == -1 )
    for i in 0...@ctl.variables.length
      if @varname == @ctl.variables[i][:name]
        byte = @ctl.variables[i][:byte]
      end
    end
  else
    byte = 4
  end
  byte
end

#vartypeObject Also known as: ntype



1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
# File 'lib/numru/gphys/grads_gridded.rb', line 1299

def vartype
  if( @idim == -1 )
    for i in 0...@ctl.variables.length
      if @varname == @ctl.variables[i][:name]
        type = @ctl.variables[i][:type]
      end
    end
  else
    type = "float"
  end
  type
end