Module: Cdo

Defined in:
lib/cdo_lib.rb

Overview

CDO calling mechnism

Constant Summary collapse

VERSION =
"1.2.5"
State =
{
  :debug       => false,
  :returnCdf   => false,
  :operators   => [],
  :forceOutput => true,
  :env         => {},
  :log         => false,
  :logger      => Logger.new(@@file),
}
@@file =
StringIO.new
@@CDO =
@@undocumentedOperators =

Since cdo-1.5.4 undocumented operators are given with the -h option. For earlier version, they have to be provided manually

%w[anomaly beta boxavg change_e5lsm change_e5mask
change_e5slm chisquare chvar cloudlayer cmd com command complextorect
covar0 covar0r daycount daylogs del29feb delday delete deltap deltap_fl
delvar diffv divcoslat dumplogo dumplogs duplicate eca_r1mm enlargegrid
ensrkhistspace ensrkhisttime eof3d eof3dspatial eof3dtime export_e5ml
export_e5res fc2gp fc2sp fillmiss fisher fldcovar fldrms fourier fpressure
gather gengrid geopotheight ggstat ggstats globavg gp2fc gradsdes
gridverify harmonic hourcount hpressure import_e5ml import_e5res
import_obs imtocomplex infos infov interpolate intgrid intgridbil
intgridtraj intpoint isosurface lmavg lmean lmmean lmstd log lsmean
meandiff2test mergegrid mod moncount monlogs mrotuv mrotuvb mulcoslat ncode
ncopy nmltest normal nvar outputbounds outputboundscpt outputcenter
outputcenter2 outputcentercpt outputkey outputtri outputvector outputvrml
pardup parmul pinfo pinfov pressure_fl pressure_hl read_e5ml remapcon1
remapdis1 retocomplex scalllogo scatter seascount select selgridname
seloperator selvar selzaxisname setrcaname setvar showvar sinfov smemlogo
snamelogo sort sortcode sortlevel sortname sorttaxis sorttimestamp sortvar
sp2fc specinfo spectrum sperclogo splitvar stimelogo studentt template1
template2 test test2 testdata thinout timcount timcovar tinfo transxy trms
tstepcount vardes vardup varmul varquot2test varrms vertwind write_e5ml
writegrid writerandom yearcount]
@@outputOperatorsPattern =
/(diff|info|output|griddes|zaxisdes|show|ncode|ndate|nlevel|nmon|nvar|nyear|ntime|npar|gradsdes|pardes)/

Class Method Summary collapse

Class Method Details

.boundaryLevels(args) ⇒ Object

Addional operotors:




324
325
326
327
328
329
330
331
332
# File 'lib/cdo_lib.rb', line 324

def Cdo.boundaryLevels(args)
  ilevels         = Cdo.showlevel(:input => args[:input])[0].split.map(&:to_f)
  bound_levels    = Array.new(ilevels.size+1)
  bound_levels[0] = 0
  (1..ilevels.size).each {|i| 
    bound_levels[i] =bound_levels[i-1] + 2*(ilevels[i-1]-bound_levels[i-1])
  }
  bound_levels
end

.checkCdoObject

test if @@CDO can be used



266
267
268
269
270
271
272
273
274
275
# File 'lib/cdo_lib.rb', line 266

def Cdo.checkCdo
  unless hasCdo?(@@CDO)
    warn "Testing application #@@CDO is not available!"
    exit 1
  else
    puts "Using CDO: #@@CDO"
    puts IO.popen(@@CDO + " -V").readlines
  end
  return true
end

.debugObject



218
219
220
# File 'lib/cdo_lib.rb', line 218

def Cdo.debug
  State[:debug]
end

.debug=(value) ⇒ Object



215
216
217
# File 'lib/cdo_lib.rb', line 215

def Cdo.debug=(value)
  State[:debug] = value
end

.forceOutputObject



224
225
226
# File 'lib/cdo_lib.rb', line 224

def Cdo.forceOutput
  State[:forceOutput]
end

.forceOutput=(value) ⇒ Object



221
222
223
# File 'lib/cdo_lib.rb', line 221

def Cdo.forceOutput=(value)
  State[:forceOutput] = value
end

.getCdoObject



284
285
286
# File 'lib/cdo_lib.rb', line 284

def Cdo.getCdo
  @@CDO
end

.hasCdo?(bin = @@CDO) ⇒ Boolean



258
259
260
261
262
263
# File 'lib/cdo_lib.rb', line 258

def Cdo.hasCdo?(bin=@@CDO)
  return true if File.exists?(@@CDO) and File.executable?(@@CDO)
  ENV['PATH'].split(File::PATH_SEPARATOR).each {|path| 
    return true if File.exists?([path,bin].join(File::SEPARATOR))
  }
end

.hasLib?(lib) ⇒ Boolean



298
299
300
301
# File 'lib/cdo_lib.rb', line 298

def Cdo.hasLib?(lib)
  return Cdo.libs.has_key?(lib)
  return false
end

.help(operator = nil) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/cdo_lib.rb', line 374

def Cdo.help(operator=nil)
  if operator.nil?
    puts Cdo.call([@@CDO,'-h'].join(' '))[:stderr]
  else
    operator = operator.to_s unless String == operator.class
    if Cdo.operators.include?(operator)
      puts Cdo.call([@@CDO,'-h',operator].join(' '))[:stdout]
    else
      puts "Unknown operator #{operator}"
    end
  end
end

.libsObject



293
294
295
296
# File 'lib/cdo_lib.rb', line 293

def Cdo.libs
  getSupportedLibs
  State[:libs]
end

.libsVersion(lib) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/cdo_lib.rb', line 303

def Cdo.libsVersion(lib)
  unless Cdo.hasLib?(lib)
    raise ArgumentError, "Cdo does NOT have support for '#{lib}'"
  else
    if State[:libs][lib].kind_of? String
      return State[:libs][lib]
    else
      warn "No version information available about '#{lib}'"
      return false
    end
  end
end

.logObject



231
232
233
# File 'lib/cdo_lib.rb', line 231

def Cdo.log
  State[:log]
end

.log=(value) ⇒ Object



227
228
229
# File 'lib/cdo_lib.rb', line 227

def Cdo.log=(value)
  State[:log] = value
end

.openCdf(iFile) ⇒ Object



349
350
351
352
# File 'lib/cdo_lib.rb', line 349

def Cdo.openCdf(iFile)
  Cdo.loadCdf unless State[:returnCdf] 
  NetCDF.open(iFile,'r+')
end

.operatorsObject



288
289
290
291
# File 'lib/cdo_lib.rb', line 288

def Cdo.operators
  Cdo.getOperators if State[:operators].empty?
  State[:operators]
end

.readArray(iFile, varname) ⇒ Object



354
355
356
357
358
359
360
361
362
# File 'lib/cdo_lib.rb', line 354

def Cdo.readArray(iFile,varname)
  filehandle = Cdo.readCdf(iFile)
  if filehandle.var_names.include?(varname)
    # return the data array
    filehandle.var(varname).get
  else
    raise ArgumentError, "Cannot find variable '#{varname}'"
  end
end

.readCdf(iFile) ⇒ Object



344
345
346
347
# File 'lib/cdo_lib.rb', line 344

def Cdo.readCdf(iFile)
  Cdo.loadCdf unless State[:returnCdf] 
  NetCDF.open(iFile)
end

.readMaArray(iFile, varname) ⇒ Object



364
365
366
367
368
369
370
371
372
# File 'lib/cdo_lib.rb', line 364

def Cdo.readMaArray(iFile,varname)
  filehandle = Cdo.readCdf(iFile)
  if filehandle.var_names.include?(varname)
    # return the data array
    filehandle.var(varname).get_with_miss
  else
    raise ArgumentError,"Cannot find variable '#{varname}'"
  end
end

.returnCdfObject



254
255
256
# File 'lib/cdo_lib.rb', line 254

def Cdo.returnCdf
  State[:returnCdf]
end

.setCdo(cdo) ⇒ Object



277
278
279
280
281
282
# File 'lib/cdo_lib.rb', line 277

def Cdo.setCdo(cdo)
  puts "Will use #{cdo} instead of #@@CDO" if Cdo.debug
  @@CDO = cdo
  Cdo.getOperators(true)
  Cdo.getSupportedLibs(true)
end

.setReturnCdf(value = true) ⇒ Object



243
244
245
246
247
248
# File 'lib/cdo_lib.rb', line 243

def Cdo.setReturnCdf(value=true)
  if value
    Cdo.loadCdf
  end
  State[:returnCdf] = value
end

.showlogObject



316
317
318
319
# File 'lib/cdo_lib.rb', line 316

def Cdo.showlog
  @@file.rewind
  puts @@file.read
end

.thicknessOfLevels(args) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'lib/cdo_lib.rb', line 334

def Cdo.thicknessOfLevels(args)
  bound_levels = Cdo.boundaryLevels(args)
  delta_levels    = []
  bound_levels.each_with_index {|v,i| 
    next if 0 == i
    delta_levels << v - bound_levels[i-1]
  }
  delta_levels
end

.unsetReturnCdfObject



250
251
252
# File 'lib/cdo_lib.rb', line 250

def Cdo.unsetReturnCdf
  setReturnCdf(false)
end

.versionObject



235
236
237
238
239
240
241
# File 'lib/cdo_lib.rb', line 235

def Cdo.version
  cmd     = @@CDO + ' 2>&1'
  help    = IO.popen(cmd).readlines.map {|l| l.chomp.lstrip}
  regexp  = %r{CDO version (\d.*), Copyright}
  line    = help.find {|v| v =~ regexp}
  version = regexp.match(line)[1]
end