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



173
174
175
176
177
178
179
# File 'lib/numru/gphys/gphys_io.rb', line 173

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

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



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/numru/gphys/gphys_io.rb', line 142

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



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

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

.file2specific_module(file) ⇒ Object



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

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

.file2type(file) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
231
232
233
234
235
236
# File 'lib/numru/gphys/gphys_io.rb', line 192

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 Regexp
	  return NETCDF     # So far, only NetCDF_IO supports Regexp. 
	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
    return nil 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
  return nil
end

.open(file, varname) ⇒ Object

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



129
130
131
# File 'lib/numru/gphys/gphys_io.rb', line 129

def open(file, varname)
	file2specific_module(file)::open(file, varname)
end

.open_gturl(gturl) ⇒ Object

def parse_gturl



334
335
336
337
338
339
340
341
# File 'lib/numru/gphys/gphys_io.rb', line 334

def open_gturl(gturl)
	file, var, slice, cut_slice, thinning = GPhys::IO.parse_gturl(gturl)
	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

.parse_gturl(gturl) ⇒ Object



281
282
283
284
285
286
287
288
289
290
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
# File 'lib/numru/gphys/gphys_io.rb', line 281

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 /,/ =~ 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
	[file, var, slice, cut_slice, thinning]
end

.str2gphys(str) ⇒ Object

def open_gturl



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

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



121
122
123
# File 'lib/numru/gphys/gphys_io.rb', line 121

def use_gphys_grib
  VArrayGrib.use_gphys_grib
end

.use_rb_gribObject



124
125
126
# File 'lib/numru/gphys/gphys_io.rb', line 124

def use_rb_grib
  VArrayGrib.use_rb_grib
end

.var_names(file) ⇒ Object

<– file type selctor //



272
273
274
# File 'lib/numru/gphys/gphys_io.rb', line 272

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

.var_names_except_coordinates(file) ⇒ Object



275
276
277
# File 'lib/numru/gphys/gphys_io.rb', line 275

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

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



133
134
135
# File 'lib/numru/gphys/gphys_io.rb', line 133

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

.write_grid(file, grid_or_gphys) ⇒ Object



137
138
139
140
# File 'lib/numru/gphys/gphys_io.rb', line 137

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