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
- .[](key) ⇒ Object
- .each_along_dims_write(gphyses, files, *loopdims, &block) ⇒ Object
- .file2file_class(file) ⇒ Object
- .file2specific_module(file) ⇒ Object
- .file2type(file) ⇒ Object
-
.open(file, varname) ⇒ Object
// module functions to be defined in specific IO modules –>.
-
.open_gturl(gturl) ⇒ Object
def parse_gturl.
- .open_multi(files, varname) ⇒ Object
- .parse_gturl(gturl) ⇒ Object
- .regexp2files(pat) ⇒ Object
-
.str2gphys(str) ⇒ Object
def open_gturl.
-
.use_gphys_grib ⇒ Object
switches for io libraries switch grib library.
- .use_rb_grib ⇒ Object
-
.var_names(file) ⇒ Object
<– file type selctor //.
- .var_names_except_coordinates(file) ⇒ Object
- .write(file, gphys, name = nil) ⇒ Object
- .write_grid(file, grid_or_gphys) ⇒ Object
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
339 340 341 |
# File 'lib/numru/gphys/gphys_io.rb', line 339 def file2file_class(file) @@file_class[ file2type(file) ] end |
.file2specific_module(file) ⇒ Object
335 336 337 |
# File 'lib/numru/gphys/gphys_io.rb', line 335 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 |
# 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 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
434 435 436 437 438 439 440 441 |
# File 'lib/numru/gphys/gphys_io.rb', line 434 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 |
.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 |
.parse_gturl(gturl) ⇒ Object
378 379 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 |
# File 'lib/numru/gphys/gphys_io.rb', line 378 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 file = Dir[file] 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 |
.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_gturl
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 |
# File 'lib/numru/gphys/gphys_io.rb', line 443 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_grib ⇒ Object
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_grib ⇒ Object
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 //
369 370 371 |
# File 'lib/numru/gphys/gphys_io.rb', line 369 def var_names(file) file2specific_module(file).var_names(file) end |
.var_names_except_coordinates(file) ⇒ Object
372 373 374 |
# File 'lib/numru/gphys/gphys_io.rb', line 372 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 |