-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathcdo.rb
More file actions
578 lines (489 loc) · 18 KB
/
cdo.rb
File metadata and controls
578 lines (489 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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
333
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
368
369
370
371
372
373
374
375
376
377
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
433
434
435
436
437
438
439
440
441
442
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
require 'pp'
require 'open3'
require 'logger'
require 'stringio'
require 'json'
require 'tempfile'
require 'semverse'
class Hash
alias :include? :has_key?
end
# Copyright 2011-2023 Ralf Mueller, ralf.mueller@dkrz.de
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
class Cdo
# hardcoded fallback list of output operators - from 1.8.0 there is an
# options for this: --operators_no_output
# this list works for cdo-1.6.4
NoOutputOperators = %w[cdiread cmor codetab conv_cmor_table diff diffc diffn
diffp diffv dump_cmor_table dumpmap filedes gmtcells gmtxyz gradsdes griddes
griddes2 gridverify info infoc infon infop infos infov map ncode ndate
ngridpoints ngrids nlevel nmon npar ntime nvar nyear output outputarr
outputbounds outputboundscpt outputcenter outputcenter2 outputcentercpt
outputext outputf outputfld outputint outputkey outputsrv outputtab outputtri
outputts outputvector outputvrml outputxyz pardes partab partab2 seinfo
seinfoc seinfon seinfop showattribute showatts showattsglob showattsvar
showcode showdate showformat showgrid showlevel showltype showmon showname
showparam showstdname showtime showtimestamp showunit showvar showyear sinfo
sinfoc sinfon sinfop sinfov spartab specinfo tinfo vardes vct vct2 verifygrid
vlist xinfon zaxisdes]
TwoOutputOperators = %w[trend samplegridicon mrotuv eoftime
eofspatial eof3dtime eof3dspatial eof3d eof complextorect complextopol]
MoreOutputOperators = %w[distgrid eofcoeff eofcoeff3d intyear scatter splitcode
splitday splitgrid splithour splitlevel splitmon splitname splitparam splitrec
splitseas splitsel splittabnum splitvar splityear splityearmon splitzaxis]
attr_accessor :cdo, :returnCdf, :forceOutput, :env, :debug, :logging, :logFile
attr_reader :operators, :filetypes, :hasNetcdf, :version
def initialize(cdo: 'cdo',
returnFalseOnError: false,
returnNilOnError: false,
forceOutput: true,
env: {},
debug: false,
tempdir: Dir.tmpdir,
logging: false,
logFile: StringIO.new)
# setup path to cdo executable
@cdo = ENV.has_key?('CDO') ? ENV['CDO'] : cdo
@version ||= getVersion
@operators = getOperators(@cdo, @version)
@noOutputOperators = @operators.select {|op,io| 0 == io}.keys
@hasNetcdf = loadOptionalLibs
@forceOutput = forceOutput
@env = env
@debug = ENV.has_key?('DEBUG') ? true : debug
@returnNilOnError = returnNilOnError
@returnFalseOnError = returnFalseOnError
@tempStore = CdoTempfileStore.new(tempdir)
@logging = logging
@logFile = logFile
@logger = Logger.new(@logFile,'daily')
@logger.level = Logger::INFO
@config = getFeatures(@version)
# create methods to descibe what can be done with the binary
@config.each {|k,v|
self.class.send :define_method, k.tr('-','_') do
v
end
}
# ignore return code 1 for diff operators (from 1.9.6 onwards)
@exit_success = lambda {|operatorName|
return 0 if @version < Cdo.version('1.9.6')
return 0 if 'diff' != operatorName[0,4]
return 1
}
end
private # {{{
# split arguments into hash-like args and the rest
def Cdo.parseArgs(args)
operatorArgs = args.reject {|a| a.class == Hash}
opArguments = operatorArgs.empty? ? '' : ',' + operatorArgs.join(',')
io = args.find {|a| a.class == Hash}
io = {} if io.nil?
#args.delete_if {|a| a.class == Hash}
# join input streams together if possible
io[:input] = io[:input].join(' ') if io[:input].respond_to?(:join)
return [io,opArguments]
end
def Cdo.version(str)
Semverse::Version.new(str)
end
# collect the complete list of possible operators
def getOperators(path2cdo, version) #{{{
operators = {}
# little side note: the option --operators_no_output works in 1.8.0 and
# 1.8.2, but not in 1.9.0, from 1.9.1 it works again
case
when version < Cdo.version('1.7.2') then
cmd = path2cdo + ' 2>&1'
help = IO.popen(cmd).readlines.map {|l| l.chomp.lstrip}
if 5 >= help.size
warn "Operators could not get listed by running the CDO binary (#{path2cdo})"
pp help if @debug
exit
end
_operators = help[(help.index("Operators:")+1)..help.index(help.find {|v|
v =~ /CDO version/
}) - 2].join(' ').split
# build up operator inventory
# default is 1 output stream
_operators.each {|op| operators[op] = 1 }
operators.each {|op,_|
operators[op] = 0 if NoOutputOperators.include?(op)
operators[op] = 2 if TwoOutputOperators.include?(op)
operators[op] = -1 if MoreOutputOperators.include?(op)
}
when (version < Cdo.version('1.8.0') or Cdo.version('1.9.0') == version) then
cmd = "#{path2cdo} --operators"
_operators = IO.popen(cmd).readlines.map {|l| l.split(' ').first }
_operators.each {|op| operators[op] = 1 }
operators.each {|op,_|
operators[op] = 0 if NoOutputOperators.include?(op)
operators[op] = 2 if TwoOutputOperators.include?(op)
operators[op] = -1 if MoreOutputOperators.include?(op)
}
when version < Cdo.version('1.9.3') then
cmd = "#{path2cdo} --operators"
_operators = IO.popen(cmd).readlines.map {|l| l.split(' ').first }
cmd = "#{path2cdo} --operators_no_output"
_operatorsNoOutput = IO.popen(cmd).readlines.map {|l| l.split(' ').first }
# build up operator inventory
_operators.each {|op| operators[op] = 1 }
_operatorsNoOutput.each {|op| operators[op] = 0}
operators.each {|op,_|
operators[op] = 0 if _operatorsNoOutput.include?(op)
operators[op] = 2 if TwoOutputOperators.include?(op)
operators[op] = -1 if MoreOutputOperators.include?(op)
}
else
cmd = "#{path2cdo} --operators"
operators = {}
IO.popen(cmd).readlines.map {|line|
lineContent = line.chomp.split(' ')
name = lineContent[0]
iCounter, oCounter = lineContent[-1][1..-1].split('|')
operators[name] = oCounter.to_i
}
end
return operators
end #}}}
# get meta-data about the CDO installation
def getFeatures(version)
config = {}
config.default(false)
if version > Cdo.version('1.9.3') then
config.merge!(JSON.parse(IO.popen(@cdo + " --config all").read.chomp))
config.each {|k,v| config[k] = ('yes' == v) ? true : false}
else
_, _, stderr, _ = Open3.popen3(@cdo + " -V")
supported = stderr.readlines.map(&:chomp)
supported.grep(/(Filetypes)/)[0].split(':')[1].split.map(&:downcase).each {|ftype|
config["has-#{ftype}"] = true
}
end
config
end
def getVersion
info = IO.popen(@cdo+' -V 2>&1').readlines.first
Cdo.version(info.split(' ').grep(%r{\d+\.\d+.*})[0].to_s)
end
# Execute the given cdo call and return all outputs
def _call(cmd,env={})
@logger.info(cmd+"\n") if @logging
stdin, stdout, stderr, wait_thr = Open3.popen3(@env.merge(env),cmd)
status = {
:stdout => stdout.read,
:stderr => stderr.read,
:returncode => wait_thr.value
}
# popen3 does not catch exitcode in case of an abort (128+SIGABRT=134)
st = -1
st = status[:returncode].exitstatus if not status[:returncode].exitstatus.nil?
st = 128 + status[:returncode].termsig if (status[:returncode].signaled? and (status[:returncode].termsig != 0))
status[:returncode] = st
if (@debug)
puts '# DEBUG - start ============================================================='
pp @env unless @env.empty?
pp env unless env.empty?
puts 'CALL:' + cmd
puts 'STDOUT:'
puts status[:stdout] unless status[:stdout].strip.empty?
puts 'STDERR:'
puts status[:stderr] unless status[:stderr].strip.empty?
puts '# DEBUG - end ==============================================================='
end
status
end
# Error handling for the given command
def _hasError(cmd,operatorName,retvals)
if @debug
puts("RETURNCODE: #{retvals[:returncode]}")
end
if ( @exit_success[operatorName] < retvals[:returncode] )
puts("Error in calling:")
puts(">>> "+cmd+"<<<")
puts(retvals[:stderr])
@logger.error("FAILIURE in execution of:"+cmd+"| msg:"+retvals[:stderr]) if @logging
return true
else
return false
end
end
# command execution wrapper, which handles the possible return types
def _run(operatorName,
operatorParameters,
input: nil,
output: nil,
options: '',
returnCdf: false,
force: nil,
returnArray: nil,
returnMaArray: nil,
env: nil,
autoSplit: nil)
# switch netcdf output if data of filehandles are requested as output
options << ' -f nc' if ( \
( returnCdf ) or \
( not returnArray.nil? ) or \
( not returnMaArray.nil?) \
)
# avoid verbose output when using autoSplit
unless operatorName.match(/^verify/) or false then
options << ' -s'
end
# use an empty hash for non-given environment
env = {} if env.nil?
# list of all output streams
outputs = []
# just collect given output(s)
outputs << output unless output.nil?
# setup basic operator execution command
cmd = "#{@cdo} -O #{options} -#{operatorName}#{operatorParameters} #{input} "
case output
when $stdout
retvals = _call(cmd,env)
unless _hasError(cmd,operatorName,retvals)
_output = retvals[:stdout].split($/).map {|l| l.chomp.strip}
unless autoSplit.nil?
_output.map! {|line| line.split(autoSplit)}
_output = _output[0] if 1 == _output.size
end
return _output
else
if @returnNilOnError then
return nil
else
raise ArgumentError,"CDO did NOT run successfully!"
end
end
else
# if operators was not called with output-forcing given, take the global switch
force = @forceOutput if force.nil?
if force or not File.exist?(output.to_s)
# create tempfile(s) according to the number of output streams needed
# if output argument is missing
if output.nil? then
operators[operatorName].times { outputs << @tempStore.newFile}
end
#finalize the execution command
cmd << "#{outputs.join(' ')}"
retvals = _call(cmd,env)
if _hasError(cmd,operatorName,retvals)
if @returnNilOnError then
return nil
else
raise ArgumentError,"CDO did NOT run successfully!"
end
end
else
warn "Use existing file(s) '#{outputs.join(' ')}'" if @debug
end
end
# return data arrays instead - this is for now limitted to fields of the
# first output file. number from the second need only one addition line, so
# I think this is sufficient
if not returnArray.nil?
readArray(outputs[0],returnArray)
elsif not returnMaArray.nil?
readMaArray(outputs[0],returnMaArray)
elsif returnCdf
retval = outputs.map{|f| readCdf(f)}
return 1 == retval.size ? retval[0] : retval
elsif /^split/.match(operatorName)
Dir.glob("#{output}*")
else
return outputs[0] if outputs.size == 1
return outputs
end
end
# Implementation of operator calls using ruby's meta programming skills
#
# args is expected to look like
# [opt1,...,optN,:input => iStream,:output => oStream, :options => ' ']
# where iStream could be another CDO call (timmax(selname(Temp,U,V,ifile.nc))
def method_missing(sym, *args, &block)
operatorName = sym.to_s
puts "Operator #{operatorName} is called" if @debug
# exit eary on missing operator
unless @operators.include?(operatorName)
return false if @returnFalseOnError
raise ArgumentError,"Operator #{operatorName} not found"
end
io, operatorParameters = Cdo.parseArgs(args)
# mark calls for operators without output files
io[:output] = $stdout if @noOutputOperators.include?(operatorName)
_run(operatorName,operatorParameters,**io)
end
# load the netcdf bindings
def loadOptionalLibs
begin
require "numru/netcdf_miss"
return true
rescue
warn "Could not load ruby's netcdf bindings"
return false
end
end
# }}}
public # {{{
# show Cdo's built-in help for given operator
def help(operator=nil)
if operator.nil?
puts _call([@cdo,'-h'].join(' '))[:stderr]
else
operator = operator.to_s
puts _call([@cdo,'-h',operator].join(' ')).values_at(:stdout,:stderr)
end
end
# collect logging messages
def collectLogs
if @logger.instance_variable_get(:'@logdev').filename.nil?
@logFile.rewind
return @logFile.read
else
return File.open(@logFile).readlines
end
end
# print the loggin messaged
def showLog
puts collectLogs
end
# check if the CDO binary is present and works
def hasCdo(path=@cdo)
executable = system("#{path} -V >/dev/null 2>&1")
fullpath = File.exist?(path) and File.executable?(path)
return (executable or fullpath)
end
# check if cdo backend is working
def check
return false unless hasCdo
retval = _call("#{@cdo} -V")
pp retval if @debug
return true
end
# return cdf handle to given file readonly
def readCdf(iFile,mode='r')
if @hasNetcdf then
NumRu::NetCDF.open(iFile,mode)
else
raise LoadError,"Could not load ruby-netcdf"
end
end
# return cdf handle opened in append more
def openCdf(iFile)
readCdf(iFile,'r+')
end
# return narray for given variable name
def readArray(iFile,varname)
filehandle = 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
# return a masked array for given variable name
def readMaArray(iFile,varname)
filehandle = 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
# remove tempfiles created from this or previous runs
def cleanTempDir
@tempStore.cleanTempDir
end
# }}}
# Addional operators: {{{
# compute vertical boundary levels from full levels
def boundaryLevels(args)
ilevels = self.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
# compute level thicknesses from given full levels
def thicknessOfLevels(args)
bound_levels = self.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
# }}}
# helper methods {{{
def getNoOutputOperators
return @noOutputOperators
end
end
#
# Helper module for easy temp file handling {{{
class CdoTempfileStore
# base for persitent temp files - just for debugging
N = 10000000
def initialize(dir=Dir.tmpdir)
@dir = dir
@tag = 'Cdorb'
@persistent_tempfiles = false
# storage for filenames in order to prevent too early removement
@_tempfiles = []
# make sure the tempdir ie really there
Dir.mkdir(@dir) unless Dir.exist?(@dir)
end
def setPersist(value)
@persistent_tempfiles = value
end
def newFile
unless @persistent_tempfiles
t = Tempfile.new(@tag,@dir)
@_tempfiles << t
@_tempfiles << t.path
t.path
else
t = "_"+rand(N).to_s
@_tempfiles << t
t
end
end
def showFiles
@_tempfiles.each {|f| print(f+" ") if f.kind_of? String}
end
def cleanTempDir
# filter by name, realfile and ownership
Dir.entries(@dir).map {|f| "#@dir/#{f}"}.find_all {|file|
File.file?(file) and File.owned?(file) and file.include?(@tag)
}.each {|f| File.unlink(f)}
end
end #}}}
# vim: fdm=marker