cdo 1.0.0 → 1.0.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 +2 -1
  2. data/lib/cdo.rb +2 -2
  3. data/test/test_cdo.rb +146 -0
  4. metadata +5 -3
data/gemspec CHANGED
@@ -2,9 +2,10 @@ require 'rubygems'
2
2
 
3
3
  spec = Gem::Specification.new do |s|
4
4
  s.name = "cdo"
5
- s.version = '1.0.0'
5
+ s.version = '1.0.1'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.files = ["lib/cdo.rb"] + ["gemspec","COPYING","README.rdoc"]
8
+ s.test_file = "test/test_cdo.rb"
8
9
  s.description = "Easy access to the Climate Data operators"
9
10
  s.summary = "Easy access to the Climate Data operators"
10
11
  s.author = "Ralf Mueller"
data/lib/cdo.rb CHANGED
@@ -61,9 +61,9 @@ module Cdo
61
61
  case ofile
62
62
  when $stdout
63
63
  cmd << " 2>/dev/null"
64
- return IO.popen(cmd).read.split
64
+ return IO.popen(cmd).readlines.map {|l| l.chomp.strip}
65
65
  when nil
66
- ofile = Tempfile.new("Ifs2Icon").path
66
+ ofile = Tempfile.new("Cdo.rb").path
67
67
  end
68
68
  cmd << "#{ofile}"
69
69
  call(cmd)
data/test/test_cdo.rb ADDED
@@ -0,0 +1,146 @@
1
+ $:.unshift File.join(File.dirname(__FILE__),"..","lib")
2
+ require 'test/unit'
3
+ require 'cdo'
4
+
5
+ class TestJobQueue < Test::Unit::TestCase
6
+
7
+ DEFAULT_CDO_PATH = '/usr/bin/cdo'
8
+ def setup
9
+ if ENV['CDO'].nil?
10
+ if File.exists?(DEFAULT_CDO_PATH)
11
+ Cdo.setCdo(DEFAULT_CDO_PATH)
12
+ else
13
+ stop(DEFAULT_CDO_PATH)
14
+ end
15
+ else
16
+ # Check user given path
17
+ unless File.exists?(ENV['CDO'])
18
+ stop(ENV['CDO'])
19
+ else
20
+ Cdo.setCdo(ENV['CDO'])
21
+ end
22
+ end
23
+ end
24
+ def stop(path)
25
+ warn "Could not find CDO binary (#{path})! Abort tests"
26
+ exit
27
+ end
28
+
29
+ def test_getOperators
30
+ %w[for random stdatm info showlevel sinfo remap mask topo thicknessOfLevels].each {|op|
31
+ assert(Cdo.getOperators.include?(op),"Operator '#{op}' not found") unless op == "thicknessOfLevels"
32
+ assert(Cdo.respond_to?(op),"Operator '#{op}' not found") if op == "thicknessOfLevels"
33
+ }
34
+ end
35
+ def test_info
36
+ levels = Cdo.showlevel(:in => "-stdatm,0")
37
+ assert_equal([0,0].map(&:to_s),levels)
38
+
39
+ info = Cdo.sinfo(:in => "-stdatm,0")
40
+ assert_equal("File format: GRIB",info[0])
41
+
42
+ end
43
+ def test_operator_options
44
+ ofile = MyTempfile.path
45
+ targetLevels = [0,10,50,100,200,400,1000]
46
+ Cdo.stdatm(targetLevels,:out => ofile)
47
+ levels = Cdo.showlevel(:in => ofile)
48
+ [0,1].each {|i| assert_equal(targetLevels.map(&:to_s),levels[i].split)}
49
+ end
50
+ def test_CDO_options
51
+ names = Cdo.showname(:in => "-stdatm,0",:options => "-f nc")
52
+ assert_equal(["P T"],names)
53
+
54
+ ofile = MyTempfile.path
55
+ Cdo.topo(:out => ofile,:options => "-z szip")
56
+ assert_equal(["GRIB SZIP"],Cdo.showformat(:in => ofile))
57
+ end
58
+ def test_chain
59
+ ofile = MyTempfile.path
60
+ #Cdo.Debug = true
61
+ Cdo.chainCall("-setname,veloc -copy",:in => "-random,r1x1",:out => ofile,:options => "-f nc")
62
+ assert_equal(["veloc"],Cdo.showname(:in => ofile))
63
+ end
64
+ end
65
+
66
+ # # Calling simple operators
67
+ # #
68
+ # # merge:
69
+ # # let files be an erray of valid filenames and ofile is a string
70
+ # Cdo.merge(:in => outvars.join(" "),:out => ofile)
71
+ # # or with multiple arrays:
72
+ # Cdo.merge(:in => [ifiles0,ifiles1].flatten.join(' '),:out => ofile)
73
+ # # selname:
74
+ # # lets grep out some variables from ifile:
75
+ # ["T","U","V"].each {|varname|
76
+ # varfile = varname+".nc"
77
+ # Cdo.selname(varname,:in => ifile,:out => varfile)
78
+ # }
79
+ # # a threaded version of this could look like:
80
+ # ths = []
81
+ # ["T","U","V"].each {|outvar|
82
+ # ths << Thread.new(outvar) {|ovar|
83
+ # varfile = varname+".nc"
84
+ # Cdo.selname(varname,:in => ifile,:out => varfile)
85
+ # }
86
+ # }
87
+ # ths.each {|th| th.join}
88
+ # # another example with sub:
89
+ # Cdo.sub(:in => [oldfile,newfile].join(' '), :out => diff)
90
+ #
91
+ # # It is possible too use the 'send' method
92
+ # operator = /grb/.match(File.extname(ifile)) ? :showcode : :showname
93
+ # inputVars = Cdo.send(operator,:in => ifile)
94
+ # # show and info operators are writing to stdout. cdo.rb tries to collects this into arrays
95
+ # #
96
+ # # Same stuff with other operators:
97
+ # operator = case var
98
+ # when Fixnum then 'selcode'
99
+ # when String then 'selname'
100
+ # else
101
+ # warn "Wrong usage of variable identifier for '#{var}' (class #{var.class})!"
102
+ # end
103
+ # Cdo.send(operator,var,:in => @ifile, :out => varfile)
104
+ #
105
+ # # For chaining operators, there is a special method:
106
+ # Cdo.chainCall("-setname,veloc -copy",:in => ifile,:out => ofile,:options => "-f nc")
107
+ # # another example with 3 operators and a different hash syntax
108
+ # C_R = 287.05
109
+ # Cdo.chainCall("setname,#{rho} -divc,#{C_R} -div",in: [pressureFile,temperatureFile].join(' '), out: densityFile)
110
+ #
111
+ # # Pass an array for operators with multiple options:
112
+ # # Perform conservative remapping with pregenerated weights
113
+ # Cdo.remap([gridfile,weightfile],:in => copyfile,:out => outfile)
114
+ # # Create vertical height levels out of hybrid model levels
115
+ # Cdo.ml2hl([0,20,50,100,200,400,800,1200].join(','),:in => hybridlayerfile, :out => reallayerfile)
116
+ # # or use multiple arguments directly
117
+ # Cdo.remapeta(vctfile,orofile,:in => ifile,:out => hybridlayerfile)
118
+ #
119
+ # # the powerfull expr operator:
120
+ # # taken from the tutorial in https://code.zmaw.de/projects/cdo/wiki/Tutorial#The-_expr_-Operator
121
+ # SCALEHEIGHT = 10000.0
122
+ # C_EARTH_GRAV = 9.80665
123
+ # # function for later computation of hydrostatic atmosphere pressure
124
+ # PRES_EXPR = lambda {|height| "101325.0*exp((-1)*(1.602769777072154)*log((exp(#{height}/#{SCALEHEIGHT})*213.15+75.0)/288.15))"}
125
+ # TEMP_EXPR = lambda {|height| "213.0+75.0*exp(-#{height}/#{SCALEHEIGHT})"}
126
+ #
127
+ # # Create Pressure and Temperature out of a height field 'geopotheight' from ifile
128
+ # Cdo.expr("'p=#{PRES_EXPR['geopotheight']}'", :in => ifile, :out => presFile)
129
+ # Cdo.expr("'t=#{TEMP_EXPR['geopotheight']}'", :in => ifile, :out => tempFile)
130
+ #
131
+ #
132
+ # # TIPS: I often work with temporary files and for getting rid of handling them manually the MyTempfile module can be used:
133
+ # # Simply include the following methods into you scripts and use tfile for any temporary variable
134
+ # def tfile
135
+ # MyTempfile.path
136
+ # end
137
+ # # As an example, the computation of simple atmospherric density could look like
138
+ # presFile, tempFile = tfile, tfile
139
+ # Cdo.expr("'p=#{PRES_EXPR['geopotheight']}'", :in => ifile, :out => presFile)
140
+ # Cdo.expr("'t=#{TEMP_EXPR['geopotheight']}'", :in => ifile, :out => tempFile)
141
+ # Cdo.chainCall("setname,#{rho} -divc,#{C_R} -div",in: [presFile,tempFile].join(' '), out: densityFile)
142
+ #
143
+ # # For debugging, it is helpfull, to avoid the automatic cleanup at the end of the scripts:
144
+ # MyTempfile.setPersist(true)
145
+ # # creates randomly names files. Switch on debugging with
146
+ # Cdo.Debug = true
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.0.0
4
+ version: 1.0.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: 2011-12-14 00:00:00.000000000 Z
12
+ date: 2011-12-15 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
@@ -23,6 +23,7 @@ files:
23
23
  - gemspec
24
24
  - COPYING
25
25
  - README.rdoc
26
+ - test/test_cdo.rb
26
27
  homepage: http://code.zmaw.de/projects/cdo
27
28
  licenses:
28
29
  - GPLv2
@@ -48,4 +49,5 @@ rubygems_version: 1.8.11
48
49
  signing_key:
49
50
  specification_version: 3
50
51
  summary: Easy access to the Climate Data operators
51
- test_files: []
52
+ test_files:
53
+ - test/test_cdo.rb