cdp 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/LICENSE +27 -0
  2. data/README.textile +23 -0
  3. data/gemspec +3 -3
  4. data/lib/cdp.rb +34 -8
  5. data/test/test_cdp.rb +29 -4
  6. metadata +8 -4
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2009-2012, Ralf Mueller (stark.dreamdetective@googlemail.com)
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice,
8
+ this list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ * The names of its contributors may not be used to endorse or promote
15
+ products derived from this software without specific prior written
16
+ permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,23 @@
1
+ h2. Climate Data Processing
2
+
3
+ The developement target of Cdp is a collection of Ruby methods for easy Climate data file processing.
4
+
5
+ h2. installation
6
+
7
+ <pre>gem install cdp</pre> or
8
+ <pre>gem install cdp --user-install</pre> for non-system-wide installation
9
+
10
+ h2. changelog
11
+
12
+ At this early stage, please look at the commits. The release number are only used for charing the gems.
13
+
14
+ |0.0.x | |
15
+ | ||
16
+
17
+ h2. license
18
+
19
+ cdp uses BSD license, see file: LICENSE
20
+
21
+ h2. contact
22
+
23
+ stark.dreamdetective@gmail.com or via github
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 = "cdp"
6
- s.version = '0.0.2'
6
+ s.version = '0.0.4'
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.files = ["lib/cdp.rb"] + ["gemspec"]
9
9
  s.test_file = "test/test_cdp.rb"
@@ -11,8 +11,8 @@ spec = Gem::Specification.new do |s|
11
11
  s.summary = "---"
12
12
  s.author = "Ralf Mueller"
13
13
  s.email = "stark.dreamdetective@gmail.com"
14
- # s.homepage = "https://code.zmaw.de/projects/cdo/wiki/Cdo%7Brbpy%7D"
15
- # s.extra_rdoc_files = ["README.rdoc","COPYING"]
14
+ s.homepage = "https://github.com/Try2Code/cdp"
15
+ s.extra_rdoc_files = ["README.textile","LICENSE"]
16
16
  s.license = "BSD"
17
17
  s.has_rdoc = false
18
18
  s.required_ruby_version = ">= 1.9"
data/lib/cdp.rb CHANGED
@@ -2,9 +2,11 @@ require 'cdo'
2
2
  require 'socket'
3
3
  module Cdp
4
4
 
5
+ @@debug = false
6
+
7
+
8
+ # setup of CDO on different machines based on the hostname
5
9
  def Cdp.setCDO
6
- #===============================================================================
7
- # setup of CDO on different machines
8
10
  hostname = Socket.gethostname
9
11
  case hostname
10
12
  when /thingol/
@@ -18,12 +20,15 @@ module Cdp
18
20
  end
19
21
  end
20
22
 
21
- def Cdp.setDEBUG
23
+ def Cdp.setDebug(value=true)
22
24
  Cdo.checkCdo
23
25
  Cdo.debug = true unless ENV['DEBUG'].nil?
26
+ @@debug = value
27
+ end
28
+ def Cdp.debug
29
+ @@debug
24
30
  end
25
31
 
26
- #===============================================================================
27
32
  # compute the experiments from the data directories and link the corresponding files
28
33
  def Cdp.splitFilesIntoExperiments(files)
29
34
  gridFile = files.pop
@@ -53,21 +58,42 @@ module Cdp
53
58
  [gridFile,experimentFiles,experimentAnalyzedData]
54
59
  end
55
60
 
56
- def Cdp.manualAreaWeights(areaVarname,areaFile,ofile=nil)
61
+ # compute area weights from an area variable
62
+ def Cdp.areaWeights(areaVarname,areaFile,ofile=nil,force=false)
57
63
  area = Cdo.selname(areaVarname,:in => areaFile)
58
64
  areaSum = Cdo.enlarge(area,:in => "-fldsum #{area}")
59
65
  if ofile.nil?
60
66
  weights = Cdo.setname("area_weight",:in => " -div #{area} #{areaSum}")
61
67
  else
62
- Cdo.setname("area_weight",:in => " -div #{area} #{areaSum}", :out => ofile)
68
+ unless force
69
+ unless File.exist?(ofile)
70
+ Cdo.setname("area_weight",:in => " -div #{area} #{areaSum}", :out => ofile)
71
+ else
72
+ puts "Resuse existing file #{ofile}" if @@debug
73
+ return ofile
74
+ end
75
+ else
76
+ Cdo.setname("area_weight",:in => " -div #{area} #{areaSum}", :out => ofile)
77
+ end
63
78
  end
64
79
  end
65
- def Cdp.manualMaskedAreaWeights(areaVarname,areaFile,maskVarname,maskFile,ofile=nil)
80
+
81
+ # compute area weights from an area variable with a mask from another file
82
+ def Cdp.maskedAreaWeights(areaVarname,areaFile,maskVarname,maskFile,ofile=nil,force=false)
66
83
  maskedArea = Cdo.div(:in => " -selname,#{areaVarname} #{areaFile} -selname,#{maskVarname} #{maskFile}")
67
84
  if ofile.nil?
68
85
  maskedAreaSum = Cdo.setname("area_weight", :in => " -div #{maskedArea} -enlarge,#{maskedArea} -fldsum #{maskedArea}")
69
86
  else
70
- Cdo.setname("area_weight", :in => " -div #{maskedArea} -enlarge,#{maskedArea} -fldsum #{maskedArea}", :out => ofile)
87
+ unless force
88
+ unless File.exist?(ofile)
89
+ Cdo.setname("area_weight", :in => " -div #{maskedArea} -enlarge,#{maskedArea} -fldsum #{maskedArea}", :out => ofile)
90
+ else
91
+ puts "Resuse existing file #{ofile}" if @@debug
92
+ return ofile
93
+ end
94
+ else
95
+ Cdo.setname("area_weight", :in => " -div #{maskedArea} -enlarge,#{maskedArea} -fldsum #{maskedArea}", :out => ofile)
96
+ end
71
97
  end
72
98
  end
73
99
  end
@@ -6,17 +6,42 @@ require 'pp'
6
6
  DATADIR = ENV['HOME'] + "/data/icon"
7
7
  ICONGRID = DATADIR + "/icon-R2B04-grid-etopo.nc"
8
8
  ICONMASK = DATADIR + "/mask.nc"
9
+ WEIGHTS = File.dirname(__FILE__) + "/weights.nc"
9
10
 
10
11
  class TestCdp < Test::Unit::TestCase
12
+ def teardown
13
+ FileUtils.rm(WEIGHTS) if File.exist?(WEIGHTS)
14
+ end
15
+ def test_set_debug
16
+ Cdp.setDebug
17
+ assert_equal(Cdp::debug,true)
18
+ Cdp.setDebug(false)
19
+ assert_equal(Cdp::debug,false)
20
+ end
11
21
  def test_area_weight
12
- Cdo.debug = true
13
- weights = Cdp.manualAreaWeights("cell_area",ICONGRID)
22
+ Cdp.setDebug
23
+ weights = Cdp.areaWeights("cell_area",ICONGRID)
24
+ # global sum over all weights should be 1
14
25
  assert_equal("1",Cdo.outputkey("value", :in => Cdo.fldsum(:in => weights)).first)
26
+ weights = Cdp.areaWeights("cell_area",ICONGRID,weights)
27
+ weights = Cdp.areaWeights("cell_area",ICONGRID,WEIGHTS)
28
+ assert_equal(WEIGHTS,weights)
29
+ weights = Cdp.areaWeights("cell_area",ICONGRID,WEIGHTS)
30
+ assert_equal(WEIGHTS,weights)
15
31
  end
16
32
  def test_masked_area_weight
17
- Cdo.debug = true
33
+ Cdo.debug = false
18
34
  Cdp.setCDO
19
- weights = Cdp.manualMaskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK)
35
+ Cdp.setDebug
36
+ weights = Cdp.maskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK)
37
+ # global sum over all weights should be 1
20
38
  assert_equal("1",Cdo.outputkey("value", :in => Cdo.fldsum(:in => weights)).uniq.first)
39
+
40
+ weights = Cdp.maskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK,WEIGHTS)
41
+ assert_equal(WEIGHTS,weights)
42
+ weights = Cdp.maskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK,WEIGHTS)
43
+ assert_equal(WEIGHTS,weights)
44
+ weights = Cdp.maskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK,WEIGHTS,true)
45
+ assert_equal(WEIGHTS,weights)
21
46
  end
22
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cdp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
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-10-29 00:00:00.000000000 Z
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cdo
@@ -31,12 +31,16 @@ description: Climate Data Processing helpers
31
31
  email: stark.dreamdetective@gmail.com
32
32
  executables: []
33
33
  extensions: []
34
- extra_rdoc_files: []
34
+ extra_rdoc_files:
35
+ - README.textile
36
+ - LICENSE
35
37
  files:
36
38
  - lib/cdp.rb
37
39
  - gemspec
40
+ - README.textile
41
+ - LICENSE
38
42
  - test/test_cdp.rb
39
- homepage:
43
+ homepage: https://github.com/Try2Code/cdp
40
44
  licenses:
41
45
  - BSD
42
46
  post_install_message: