Class: NumRu::Gtool3

Inherits:
Object
  • Object
show all
Includes:
IndexMixin
Defined in:
lib/numru/gphys/gtool3.rb

Constant Summary collapse

FLDLEN =

length of each header element

16
HEADLEN =

length a gtool3 header (64*16)

1024
NFLD =

number of fields

64
RLEN =

length of float types in bytes

{"UR4"=>4,"UR"=>4,"UR8"=>8}
NATYP =
{"UR4"=>NArray::SFLOAT,"UR"=>NArray::SFLOAT,"UR8"=>NArray::DFLOAT}
AXES_IGNORED =

special treatment (must be one-element axes)

["SFC1"]
@@default_calender =
"360_day"
@@max_delimlen =
8
@@gtaxdir =

maximum length of fortran unformated IO delimiter. actual length is found heulistically from files.

@@fmtid =
/(.*)            (9009|9010)/
@@default_endian =
:big
@@hditems =
%w!IDFM DSET ITEM EDIT1 EDIT2 EDIT3 EDIT4 EDIT5 EDIT6 EDIT7 EDIT8 FNUM DNUM TITL1 TITL2 UNIT ETTL1 ETTL2 ETTL3 ETTL4 ETTL5 ETTL6 ETTL7 ETTL8 TIME UTIM DATE TDUR AITM1 ASTR1 AEND1 AITM2 ASTR2 AEND2 AITM3 ASTR3 AEND3 DFMT MISS DMIN DMAX DIVS DIVL STYP OPT1 OPT2 OPT3 MEMO1 MEMO2 MEMO3 MEMO4 MEMO5 MEMO6 MEMO7 MEMO8 MEMO9 MEMO10 MEMO11 MEMO12 CDATE CSIGN MDATE MSIGN SIZE!
@@intitems =
%w!FNUM DNUM TIME TDUR ASTR1 AEND1 ASTR2 AEND2 ASTR3 AEND3 STYP SIZE!
@@fltitems =
%w!MISS DMIN DMAX DIVS DIVL!

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexMixin

#gen_idx, #range_negproc, #rubber_expansion, #slicer2idx, #slicer_len, #slicer_minmax, #slicer_negproc

Constructor Details

#initialize(path, mode = "r") ⇒ Gtool3

Returns a new instance of Gtool3.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/numru/gphys/gtool3.rb', line 227

def initialize(path, mode="r")
  case(mode)
  when /^r/
    (delim=Gtool3.is_a_Gtool3?(path)) or raise("not a Gtool3 file: #{path}")
    @file = File.open(path, "rb")
    @delimlen, @bigendian = interpret_delim(delim)
    @headlen = HEADLEN + @delimlen*2
    @vars = @varh = @timeblock_lenb = nil  # to be set in parse_file
    @calender = @@default_calender  # can be modified with Gtool3#calender
    parse_file
  else
    raise ArgumentError, "Unsupported IO mode: #{mode}"
  end
end

Class Method Details

.default_calenderObject



222
223
224
# File 'lib/numru/gphys/gtool3.rb', line 222

def default_calender
  @@default_calender
end

.default_calender=(str) ⇒ Object



219
220
221
# File 'lib/numru/gphys/gtool3.rb', line 219

def default_calender=(str)
  @@default_calender = str
end

.gtaxdirObject



215
216
217
# File 'lib/numru/gphys/gtool3.rb', line 215

def gtaxdir
  @@gtaxdir
end

.gtaxdir=(path) ⇒ Object



211
212
213
# File 'lib/numru/gphys/gtool3.rb', line 211

def gtaxdir=(path)
  @@gtaxdir = path
end

.is_a_Gtool3?(path) ⇒ Boolean

Returns:

  • (Boolean)


204
205
206
207
208
209
# File 'lib/numru/gphys/gtool3.rb', line 204

def is_a_Gtool3?(path)
  file = File.open(path,"rb")
  head = file.read(FLDLEN + @@max_delimlen)
  file.close
  (@@fmtid=~head) and delimiter=head[0,$1.length]
end

Instance Method Details

#calenderObject



245
246
247
# File 'lib/numru/gphys/gtool3.rb', line 245

def calender
  @calender
end

#calender=(str) ⇒ Object



242
243
244
# File 'lib/numru/gphys/gtool3.rb', line 242

def calender=(str)
  @calender = str
end

#closeObject



249
250
251
# File 'lib/numru/gphys/gtool3.rb', line 249

def close
  @file.close
end

#get(name, *sel) ⇒ Object

to redirect Gtool3Var# (the subsetting method) or Gtool3Var#get (for sel==nil)



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/numru/gphys/gtool3.rb', line 298

def get(name, *sel)
  var = @varh[name] or raise(ArgumentError,"vardiable #{name} is not found")
  rk = var[:rank]
  if sel.empty?
    sel = [true]*rk 
  else
    sel = rubber_expansion( sel, rk )
  end

  sb = var[:start_byte]
  lb = @timeblock_lenb

  if var[:time_seq]
    tidx = slicer2idx( sel[rk-1], var[:shape][rk-1] )
    #val = NArray.new(var[:natyp], *var[:shape])
    val = nil
    selb = sel[0...var[:rankb]]
    if tidx.is_a?(Integer)
      val = read_block(sb+tidx*lb,selb,var[:natyp],var[:rlen],var[:shapeb])
    else
      for i in 0...tidx.length
        it = tidx[i]
        vb = read_block(sb+it*lb,selb,var[:natyp],var[:rlen],var[:shapeb])
        if val.nil?
          shp = vb.shape + [tidx.length]
          val = NArray.new(var[:natyp], *shp)
        end
        val[false,i] = vb
      end
    end
  else
    val = read_block(sb,sel,var[:natyp],var[:rlen],var[:shapeb])
  end
  NArrayMiss.to_nam_no_dup(val,val.ne(var["MISS"]))
end

#has_var?(varname) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/numru/gphys/gtool3.rb', line 269

def has_var?(varname)
  @varh.has_key?(varname)
end

#inspectObject



273
274
275
# File 'lib/numru/gphys/gtool3.rb', line 273

def inspect
  "<#{self.class}:#{path}>"
end

#nvarsObject



257
258
259
# File 'lib/numru/gphys/gtool3.rb', line 257

def nvars
  @variables.length
end

#pathObject



253
254
255
# File 'lib/numru/gphys/gtool3.rb', line 253

def path
  @file.path
end

#read_block(strb, selb, natyp, rlen, shapeb) ⇒ Object



334
335
336
337
338
339
340
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
# File 'lib/numru/gphys/gtool3.rb', line 334

def read_block(strb,selb,natyp,rlen,shapeb)
  ##print "***rb*** #{strb} #{selb.inspect}\n"
  zidx = slicer2idx(selb[-1],shapeb[-1])   # z simbolically represents the last dim of block
  if zidx.is_a?(Integer)
    min = max = zidx
    zidxoff = 0
  else
    min = zidx.min
    max = zidx.max
    zidxoff = zidx.collect{|i| i-min}
  end
  for i in 0...shapeb.length-1
    selb[i] = slicer2idx(selb[i],shapeb[i]) if selb[i].is_a?(Hash)
  end

  #min,max = slicer_minmax(selb[-1])   # first and last of last dim in block
  subblen = product(shapeb[0..-2])
  offs = min*subblen*rlen
  len = (max-min+1)*subblen*rlen   # ie, read by [false,min..max]
  @file.seek(strb+offs+@delimlen)
  buf = @file.read(len)
  zsubsh = shapeb[0..-2] + [max-min+1]
  #p "$$$",len,product(zsubsh)*rlen,rlen,natyp
  zsubsel = selb[0..-2] + [ zidxoff ]
  #p "$$$",zidx, zsubsh,zsubsel
  allint = true; zsubsel.each{|s| allint = false unless s.is_a?(Integer)}
  zsubsel[-1] = [zsubsel[-1]..zsubsel[-1]] if allint
  na = NArray.to_na(buf, natyp, *zsubsh)[*zsubsel]
  if @bigendian
    na.ntoh
  else
    na.vtoh
  end
end

#var(varname) ⇒ Object



261
262
263
# File 'lib/numru/gphys/gtool3.rb', line 261

def var( varname )
  Gtool3Var.new(self,varname)
end

#var_namesObject



265
266
267
# File 'lib/numru/gphys/gtool3.rb', line 265

def var_names
  @vars.collect{|var| var["ITEM"]} # use @vars to reflect the order in file
end

#varinfo(name) ⇒ Object



369
370
371
# File 'lib/numru/gphys/gtool3.rb', line 369

def varinfo(name)
  @varh[name]
end