Module: NumRu::GPhys::IO

Defined in:
lib/numru/gphys/gphys_io.rb

Constant Summary collapse

NETCDF =

// file type selctor –>

"NETCDF"
GRADS =
"GRADS"
GRIB =
"GRIB"
NUSDAS =
"NUSDAS"
He5 =
"He5"
GTOOL3 =
"GTOOL3"
GTURLfmt =
"path[@|/]varname[,dimname=pos1[:pos2[:thinning_intv]][,dimname=...]]"
@@iomdl =
{NETCDF => GPhys::NetCDF_IO,
GRADS => GPhys::GrADS_IO,
GRIB => GPhys::Grib_IO,
NUSDAS => GPhys::NuSDaS_IO,
GTOOL3 => GPhys::Gtool3_IO}
@@file_class =
{NETCDF => NetCDF,
GRADS => GrADS_Gridded,
#GRIB => VArrayGrib.grib, # Hash is not dynamic: see below
NUSDAS => NuSDaS,
GTOOL3 => Gtool3}
@@nc_pattern =
[/\.nc$/]
@@grad_pattern =
[/\.ctl$/]
@@grib_pattern =
[/\.grib$/, /\.grb$/]
@@nus_pattern =
[/\.nus$/]
@@has_he5 =
defined?(HE5)
@@he5_pattern =
[/\.he5$/]

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/numru/gphys/gphys_io.rb', line 272

def @@file_class.[](key)
  if key == GRIB
    VArrayGrib.grib
  else
    super
  end
end

.each_along_dims_write(gphyses, files, *loopdims, &block) ⇒ Object



241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/numru/gphys/gphys_io.rb', line 241

def each_along_dims_write(gphyses, files, *loopdims, &block)

	files = [files] if !files.is_a?(Array)
	files.each do |fl|
	  if fl.is_a?(NetCDF)
	    NetCDF_Conventions.add_history(fl, "#{File.basename($0)}")
	  end
	end

	IO_Common::each_along_dims_write(gphyses, files, loopdims, 
file2specific_module(files), &block)
end

.file2file_class(file) ⇒ Object



341
342
343
# File 'lib/numru/gphys/gphys_io.rb', line 341

def file2file_class(file)
	@@file_class[ file2type(file) ]
end

.file2specific_module(file) ⇒ Object



337
338
339
# File 'lib/numru/gphys/gphys_io.rb', line 337

def file2specific_module(file)
	@@iomdl[ file2type(file) ]
end

.file2type(file) ⇒ Object



291
292
293
294
295
296
297
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
333
334
335
# File 'lib/numru/gphys/gphys_io.rb', line 291

def file2type(file)
 	case file
	when Array, NArray
	  return file2type(file[0])   # inspect the first element (ignoring the rest)
	when NetCDF
	  return NETCDF
	when GrADS_Gridded
	  return GRADS
	when VArrayGrib.grib
	  return GRIB
  when NuSDaS
    return NUSDAS
  when Gtool3
    return GTOOL3
	when *@@nc_pattern
	  return NETCDF
	when *@@grad_pattern
	  return GRADS
	when *@@grib_pattern
    return GRIB
  when *@@nus_pattern
    return NUSDAS
	when String
	  return NETCDF if /^http:\/\// =~ file   # assume a DODS URL
    raise ArgumentError, "File not found: #{file}" unless File.exist?(file)
    return NETCDF if NetCDF_IO.is_a_NetCDF?(file)
    return GRADS if GrADS_IO.is_a_GrADS?(file)
    return GRIB if Grib_IO.is_a_Grib?(file)
    return NUSDAS if NuSDaS_IO.is_a_NuSDaS?(file)
    return GTOOL3 if Gtool3_IO.is_a_Gtool3?(file)
	end
  if @@has_he5
    case file
    when HE5, HE5Sw
      return He5
    when *@@he5_pattern
      return He5
    when String
      return He5   if HE5_IO.is_a_HE5?(file)
    end
  end
  raise "cannot specify the file type of \""+ file + \
        "\"; maybe unsupported format."
  return nil
end

.open(file, varname) ⇒ Object

// module functions to be defined in specific IO modules –>



186
187
188
189
190
191
192
193
# File 'lib/numru/gphys/gphys_io.rb', line 186

def open(file, varname)
  case file
  when Array, NArray, Regexp
    open_multi(file, varname)
  else
    file2specific_module(file)::open(file, varname)
  end
end

.open_gturl(gturl) ⇒ Object

def parse_gturl



458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/numru/gphys/gphys_io.rb', line 458

def open_gturl(gturl)
	file, var, slice, cut_slice, thinning = GPhys::IO.parse_gturl(gturl)
  if var.is_a?(Array)
    raise "This method treats a gturl of a single GPhys object. " +
       "Use open_multi_gturl to treat a gturl of multiple objects."
  end
  gp = GPhys::IO.open(file,var)
	gp = gp[slice] if slice
	gp = gp.cut(cut_slice) if cut_slice
	gp = gp[thinning] if thinning
	gp
end

.open_multi(files, varname) ⇒ Object



195
196
197
198
199
200
201
202
203
204
# File 'lib/numru/gphys/gphys_io.rb', line 195

def open_multi(files, varname)
  case files
  when Array
    GPhys.join( files.collect{|f| open(f,varname)} )
  when NArray
    GPhys.join_md( files.collect{|f| open(f,varname)} )
  when Regexp
    GPhys.join_md( regexp2files(files).collect{|f| open(f,varname)} )
  end
end

.open_multi_gturl(gturl) ⇒ Object

def open_gturl



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
# File 'lib/numru/gphys/gphys_io.rb', line 471

def open_multi_gturl(gturl)
	files, vars, slice, cut_slice, thinning = GPhys::IO.parse_gturl(gturl)
  vars = [vars] unless vars.is_a?(Array)
  gp_array = Array.new
  vars.each do |v|
    begin 
      gps = [ GPhys::IO.open(files,v) ]   # opened as a single GPhys
    rescue ArgumentError 
      if files.is_a?(Array)
        # gturl may designate multiple GPhys objects
        gps = files.collect{|f| gp = GPhys::IO.open(f,v) }
      else
        raise $!    # it must have been a real error
      end
    end
    gp_array += gps
  end
  gp_array.map!{|gp|
    gp = gp[slice] if slice
    gp = gp.cut(cut_slice) if cut_slice
    gp = gp[thinning] if thinning
    gp
  }
  gp_array
end

.parse_gturl(gturl) ⇒ Object



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/numru/gphys/gphys_io.rb', line 380

def parse_gturl(gturl)
	if /(.*)@(.*)/ =~ gturl
	  file = $1
	  var = $2
  elsif /(.*)\/(.*)/ =~ gturl
    file = $1
    var = $2
	else
	  raise "invalid URL: '[@|/]' between path & variable is not found\n\n" + 
    "URL format: " + GTURLfmt
	end
  if /[\*\?]/ =~ file ## match file names if wildcard expression included.
    raise "\n Any files did not match the given expression: \""+file+\
          "\" \n" if Dir[file].empty?
    file = Dir[file].sort
  elsif (!File.exist?(file))
    raise "File specified by gturl \"#{gturl}\" was not found"
  end
	if /,/ =~ var
	  slice = Hash.new
	  cut_slice = Hash.new
	  thinning = Hash.new
	  var_descr = var.split(/,/)
	  var = var_descr.shift
	  var_descr.each do |s|
	    if /(.*)=(.*)/ =~ s
 dimname = $1
 subset = $2
 case subset
 when /\^(.*):(.*):(.*)/
		slice[dimname] = ($1.to_i)..($2.to_i)
		thinning[dimname] = {(0..-1) => $3.to_i}
 when /\^(.*):(.*)/
		slice[dimname] = ($1.to_i)..($2.to_i)
 when /\^(.*)/
		slice[dimname] = $1.to_i
 when /(.*):(.*):(.*)/
		cut_slice[dimname] = ($1.to_f)..($2.to_f)
		thinning[dimname] = {(0..-1) => $3.to_i}
 when /(.*):(.*)/
		cut_slice[dimname] = ($1.to_f)..($2.to_f)
 else
		cut_slice[dimname] = subset.to_f
 end
	    else
 raise "invalid URL: variable subset specification error\n\n" + 
		"URL format: " + GTURLfmt
	    end
	  end
	  slice = nil if slice.length == 0
	  cut_slice = nil if cut_slice.length == 0
	  thinning = nil if thinning.length == 0
	else
	  slice = nil
	  cut_slice = nil
	  thinning = nil
	end
  if /[\*\?]/ =~ var ## match var names if wildcard expression included.
    var_reg = var.gsub("*",".*").gsub("?",".") # convert to regular exp.
    case file
    when String
      vars = GPhys::IO.var_names_except_coordinates(file)
    when Array
      vars_t = file.collect{|f| GPhys::IO.var_names_except_coordinates(f)}
      vars = vars_t.flatten.uniq
    end
    vars_matched = vars.select{|v| v.match(/#{var_reg}/)}
    if (vars_matched.empty?) 
      raise "\n Any variables in \"#{file}\" did not match the given 
      expression: \"#{var}\".\n Included variable(s): #{vars.join(", ")}."
    end
    # put the variable name by String if the number of matched variable is
    # one, otherwise by Array of Strings.
    var = vars_matched.length == 1 ? vars_matched[0] : vars_matched 
  end
	[file, var, slice, cut_slice, thinning]
end

.regexp2files(pat) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/numru/gphys/gphys_io.rb', line 206

def regexp2files( pat )
  if  /^(.*)\\?\/(.*)$/ =~ (pat.source)
    d=$1
    f=$2
    dir = d.gsub(/\\/,'') + '/'
    pat = Regexp.new(f)
  else
    dir = './'
  end
  flstore = MDStorage.new(1)
  lbs = Array.new
  Dir.open(dir).each do |fn| 
    if pat =~ fn
      raise(ArgumentError,"has no capture; need one or more") if $1.nil?
      idx = Array.new
      Regexp.last_match.captures.each_with_index{|lb,i|
        flstore.add_dim if flstore.rank == i   # rank smaller by 1 -> add
        lbs[i] = Array.new if lbs[i].nil?
        idx.push( lbs[i].index(lb) || lbs[i].push(lb) && lbs[i].length-1 )
      }
      flstore[*idx] = NetCDF.open(dir+fn)
    end
  end
  flstore.to_na
end

.str2gphys(str) ⇒ Object

def open_multi_gturl



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
# File 'lib/numru/gphys/gphys_io.rb', line 497

def str2gphys(str)

  case str
  when /^https?:\/\//
    file_tester = Proc.new{|fname| NetCDF.open(fname) rescue false}
  when
    file_tester = Proc.new{|fname| File.exists?(fname)}
  end
  fname = str; vname = nil   # initial value
  while fname
    if file_tester.call(fname)
      break
    else
      if /(.*)\/([^\/]+)/ =~ fname
        fname = $1
        if vname.nil?
          vname = $2
        else
          vname = $2 + "/" + vname
        end
      else
        raise "Not found: #{str}"
      end
    end
  end
  if vname.nil?
    vns = var_names_except_coordinates(fname)
    if vns.length==1
      vname=vns.first 
    else
      raise "#{str} has multiple variables #{vns.inspect}. Specify one."
    end
  end
  open(fname,vname)
end

.use_gphys_gribObject

switches for io libraries switch grib library



178
179
180
# File 'lib/numru/gphys/gphys_io.rb', line 178

def use_gphys_grib
  VArrayGrib.use_gphys_grib
end

.use_rb_gribObject



181
182
183
# File 'lib/numru/gphys/gphys_io.rb', line 181

def use_rb_grib
  VArrayGrib.use_rb_grib
end

.var_names(file) ⇒ Object

<– file type selctor //



371
372
373
# File 'lib/numru/gphys/gphys_io.rb', line 371

def var_names(file)
  file2specific_module(file).var_names(file)
end

.var_names_except_coordinates(file) ⇒ Object



374
375
376
# File 'lib/numru/gphys/gphys_io.rb', line 374

def var_names_except_coordinates(file)
  file2specific_module(file).var_names_except_coordinates(file)
end

.write(file, gphys, name = nil) ⇒ Object



232
233
234
# File 'lib/numru/gphys/gphys_io.rb', line 232

def write(file, gphys, name=nil)
	file2specific_module(file)::write(file, gphys, name)
end

.write_grid(file, grid_or_gphys) ⇒ Object



236
237
238
239
# File 'lib/numru/gphys/gphys_io.rb', line 236

def write_grid(file, grid_or_gphys)
	# usually not needed (internally called by write)
	file2specific_module(file)::write_grid(file, grid_or_gphys)
end