cdo 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/gemspec +1 -1
  2. data/lib/cdo.rb +58 -20
  3. data/test/test_cdo.rb +58 -0
  4. metadata +2 -2
data/gemspec CHANGED
@@ -3,7 +3,7 @@ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
3
3
 
4
4
  spec = Gem::Specification.new do |s|
5
5
  s.name = "cdo"
6
- s.version = '1.2.0'
6
+ s.version = '1.2.1'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.files = ["lib/cdo.rb"] + ["gemspec","COPYING","README.rdoc","ChangeLog"]
9
9
  s.test_file = "test/test_cdo.rb"
data/lib/cdo.rb CHANGED
@@ -17,7 +17,7 @@ require 'open3'
17
17
  # CDO calling mechnism
18
18
  module Cdo
19
19
 
20
- VERSION = "1.2.0"
20
+ VERSION = "1.2.1"
21
21
 
22
22
  State = {
23
23
  :debug => false,
@@ -73,39 +73,63 @@ module Cdo
73
73
  else
74
74
  help[(help.index("Operators:")+1)..help.index(help.find {|v| v =~ /CDO version/}) - 2].join(' ').split
75
75
  end
76
+ end
76
77
 
77
-
78
+ def Cdo.hasError(cmd,retvals)
79
+ if (State[:debug])
80
+ puts("RETURNCODE: #{retvals[:returncode]}")
81
+ end
82
+ if ( 0 != retvals[:returncode] )
83
+ puts("Error in calling:")
84
+ puts(">>> "+cmd+"<<<")
85
+ puts(retvals[:stderr])
86
+ return true
87
+ else
88
+ return false
89
+ end
78
90
  end
91
+
79
92
  def Cdo.call(cmd)
80
93
  if (State[:debug])
81
94
  puts '# DEBUG ====================================================================='
82
95
  puts cmd
83
96
  puts '# DEBUG ====================================================================='
84
- puts IO.popen(cmd).read
85
- else
86
- system(cmd + ' 1>/dev/null 2>&1 ')
87
97
  end
98
+ stdin, stdout, stderr, wait_thr = Open3.popen3(cmd)
99
+
100
+ {
101
+ :stdout => stdout.read,
102
+ :stderr => stderr.read,
103
+ :returncode => wait_thr.value.exitstatus
104
+ }
88
105
  end
89
- def Cdo.run(cmd,ofile='',options='',returnCdf=false,force=nil,returnArray=nil)
106
+ def Cdo.run(cmd,ofile='',options='',returnCdf=false,force=nil,returnArray=nil,returnMaArray=nil)
90
107
  cmd = "#{@@CDO} -O #{options} #{cmd} "
91
108
  case ofile
92
109
  when $stdout
93
- stdin, stdout, stderr, status = Open3.popen3(cmd)
94
- retval = stdout.read.split($/).map {|l| l.chomp.strip}
95
- pp stderr.read
96
- return retval
110
+ retvals = Cdo.call(cmd)
111
+ unless hasError(cmd,retvals)
112
+ return retvals[:stdout].split($/).map {|l| l.chomp.strip}
113
+ else
114
+ raise ArgumentError,"CDO did NOT run successfully!"
115
+ end
97
116
  else
98
117
  force = State[:forceOutput] if force.nil?
99
118
  if force or not File.exists?(ofile.to_s)
100
119
  ofile = MyTempfile.path if ofile.nil?
101
120
  cmd << "#{ofile}"
102
- call(cmd)
121
+ retvals = call(cmd)
122
+ if hasError(cmd,retvals)
123
+ raise ArgumentError,"CDO did NOT run successfully!"
124
+ end
103
125
  else
104
126
  warn "Use existing file '#{ofile}'" if Cdo.debug
105
127
  end
106
128
  end
107
129
  if not returnArray.nil?
108
130
  Cdo.readArray(ofile,returnArray)
131
+ elsif not returnMaArray.nil?
132
+ Cdo.readMaArray(ofile,returnMaArray)
109
133
  elsif returnCdf or State[:returnCdf]
110
134
  Cdo.readCdf(ofile)
111
135
  else
@@ -130,15 +154,15 @@ module Cdo
130
154
  if @@outputOperatorsPattern.match(sym)
131
155
  run(" -#{sym.to_s}#{opts} #{io[:input]} ",$stdout)
132
156
  else
133
- run(" -#{sym.to_s}#{opts} #{io[:input]} ",io[:output],io[:options],io[:returnCdf],io[:force],io[:returnArray])
157
+ run(" -#{sym.to_s}#{opts} #{io[:input]} ",io[:output],io[:options],io[:returnCdf],io[:force],io[:returnArray],io[:returnMaArray])
134
158
  end
135
159
  else
136
- warn "Operator #{sym.to_s} not found"
160
+ raise ArgumentError,"Operator #{sym.to_s} not found"
137
161
  end
138
162
  end
139
163
  def Cdo.loadCdf
140
164
  begin
141
- require "numru/netcdf"
165
+ require "numru/netcdf_miss"
142
166
  include NumRu
143
167
  rescue LoadError
144
168
  warn "Could not load ruby's netcdf bindings. Please install it."
@@ -244,16 +268,30 @@ module Cdo
244
268
  # return the data array
245
269
  filehandle.var(varname).get
246
270
  else
247
- warn "Cannot find variable '#{varname}'"
248
- raise ArgumentError
271
+ raise ArgumentError, "Cannot find variable '#{varname}'"
249
272
  end
250
273
  end
251
274
 
252
- def Cdo.selindexlist(args)
253
-
275
+ def Cdo.readMaArray(iFile,varname)
276
+ filehandle = Cdo.readCdf(iFile)
277
+ if filehandle.var_names.include?(varname)
278
+ # return the data array
279
+ filehandle.var(varname).get_with_miss
280
+ else
281
+ raise ArgumentError,"Cannot find variable '#{varname}'"
282
+ end
254
283
  end
255
-
256
- def Cdo.plot(args)
284
+ def Cdo.help(operator=nil)
285
+ if operator.nil?
286
+ puts Cdo.call([@@CDO,'-h'].join(' '))[:stderr]
287
+ else
288
+ operator = operator.to_s unless String == operator.class
289
+ if Cdo.operators.include?(operator)
290
+ puts Cdo.call([@@CDO,'-h',operator].join(' '))[:stdout]
291
+ else
292
+ puts "Unknown operator #{operator}"
293
+ end
294
+ end
257
295
  end
258
296
  end
259
297
 
@@ -232,7 +232,55 @@ class TestCdo < Test::Unit::TestCase
232
232
  pressure = Cdo.stdatm(0,1000,:options => '-f nc -b F64',:returnArray => 'P')
233
233
  assert_equal("1013.25 898.543456035875",pressure.flatten.to_a.join(' '))
234
234
  end
235
+ def test_returnMaArray
236
+ Cdo.debug = true
237
+ topo = Cdo.topo(:options => '-f nc',:returnMaArray => 'topo')
238
+ assert_equal(-1890.0,topo.mean.round)
239
+ bathy = Cdo.setrtomiss(0,10000,
240
+ :input => Cdo.topo(:options => '-f nc'),:returnMaArray => 'topo')
241
+ assert_equal(-3386.0,bathy.mean.round)
242
+ oro = Cdo.setrtomiss(-10000,0,
243
+ :input => Cdo.topo(:options => '-f nc'),:returnMaArray => 'topo')
244
+ assert_equal(1142.0,oro.mean.round)
245
+ bathy = Cdo.remapnn('r2x2',:input => Cdo.topo(:options => '-f nc'), :returnMaArray => 'topo')
246
+ assert_equal(-4298.0,bathy[0,0])
247
+ assert_equal(-2669.0,bathy[1,0])
248
+ ta = Cdo.remapnn('r2x2',:input => Cdo.topo(:options => '-f nc'))
249
+ tb = Cdo.subc(-2669.0,:input => ta)
250
+ withMask = Cdo.div(:input => ta+" "+tb,:returnMaArray => 'topo')
251
+ assert(-8.0e+33 > withMask[1,0])
252
+ assert(0 < withMask[0,0])
253
+ assert(0 < withMask[0,1])
254
+ assert(0 < withMask[1,1])
255
+ end
235
256
 
257
+ def test_errorException
258
+ Cdo.debug = true
259
+ # stdout operators get get wrong input
260
+ assert_raise ArgumentError do
261
+ Cdo.showname(:input => '-for,d')
262
+ end
263
+ # non-existing operator
264
+ assert_raise ArgumentError do
265
+ Cdo.neverDefinedOperator()
266
+ end
267
+ # standard opertor get mis-spelled value
268
+ assert_raise ArgumentError do
269
+ Cdo.remapnn('r-10x10')
270
+ end
271
+ # standard operator get unexisting operator as input stream
272
+ assert_raise ArgumentError do
273
+ Cdo.remapnn('r10x10',:input => '-99topo')
274
+ end
275
+ # missing input stream
276
+ assert_raise ArgumentError do
277
+ Cdo.setname('setname')
278
+ end
279
+ # missing input stream for stdout-operator
280
+ assert_raise ArgumentError do
281
+ Cdo.showname
282
+ end
283
+ end
236
284
 
237
285
  if 'thingol' == `hostname`.chomp then
238
286
  def test_readCdf
@@ -248,6 +296,16 @@ class TestCdo < Test::Unit::TestCase
248
296
  ifile = '/home/ram/data/examples/EH5_AMIP_1_TSURF_1991-1995.nc'
249
297
  assert_equal([192, 96, 10],Cdo.readArray(Cdo.seltimestep('1/10',:input => ifile), 'tsurf').shape)
250
298
  end
299
+ def test_doc
300
+ Cdo.debug = true
301
+ Cdo.help(:remap)
302
+ Cdo.help(:infov)
303
+ Cdo.help(:topo)
304
+ Cdo.help(:notDefinedOP)
305
+ Cdo.help
306
+ end
307
+
308
+
251
309
  end
252
310
 
253
311
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-21 00:00:00.000000000 Z
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Easy access to the Climate Data operators
15
15
  email: stark.dreamdetective@gmail.com