cdp 0.0.6 → 0.0.7
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.
- data/gemspec +1 -1
- data/lib/cdp.rb +1 -140
- data/test/test_cdp.rb +4 -33
- 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 = "cdp"
|
6
|
-
s.version = '0.0.
|
6
|
+
s.version = '0.0.7'
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.files = ["lib/cdp.rb"] + ["gemspec"]
|
9
9
|
s.test_file = "test/test_cdp.rb"
|
data/lib/cdp.rb
CHANGED
@@ -35,7 +35,6 @@ module Cdp
|
|
35
35
|
|
36
36
|
# compute the experiments from the data directories and link the corresponding files
|
37
37
|
def Cdp.splitFilesIntoExperiments(files,exp='exp')
|
38
|
-
gridFile = files.pop
|
39
38
|
experiments = files.map {|f| File.basename(File.dirname(f))}.uniq.sort_by {|f| f.length}.reverse
|
40
39
|
# take the larges part of the filenames as experiment name if the files are in
|
41
40
|
# the current directory
|
@@ -60,7 +59,7 @@ module Cdp
|
|
60
59
|
experimentAnalyzedData[experiment] = []
|
61
60
|
}
|
62
61
|
|
63
|
-
[
|
62
|
+
[experimentFiles,experimentAnalyzedData]
|
64
63
|
end
|
65
64
|
|
66
65
|
# compute area weights from an area variable
|
@@ -94,142 +93,4 @@ module Cdp
|
|
94
93
|
end
|
95
94
|
end
|
96
95
|
end
|
97
|
-
|
98
|
-
# split data file with global grid into 2 separate files with northern and
|
99
|
-
# southern hemisphere grid
|
100
|
-
# TODO: currently ICON cell variable are supported only
|
101
|
-
def Cdp.splitHemisphere(iFilename,varname,lon,lat,dim='2d')
|
102
|
-
iFile = NetCDF.open(iFilename)
|
103
|
-
unless iFile.var_names.include?(varname)
|
104
|
-
warn "Could not find #{varname} in #{iFilename}"
|
105
|
-
raise IOError
|
106
|
-
end
|
107
|
-
lats = iFile.var(lat).get
|
108
|
-
var = iFile.var(varname)
|
109
|
-
varDims = var.dim_names
|
110
|
-
varValues = var.get_with_miss
|
111
|
-
splittedDim = varDims[0]
|
112
|
-
|
113
|
-
# compute location indices and corresponding values
|
114
|
-
nhIndeces = (lats>0.0).where
|
115
|
-
shIndeces = (lats<0.0).where
|
116
|
-
|
117
|
-
case varValues.shape.size
|
118
|
-
when 3
|
119
|
-
varValuesNH = varValues[nhIndeces,true,true]
|
120
|
-
varValuesSH = varValues[shIndeces,true,true]
|
121
|
-
when 2
|
122
|
-
varValuesNH = varValues[nhIndeces,true]
|
123
|
-
varValuesSH = varValues[shIndeces,true]
|
124
|
-
when 1
|
125
|
-
varValuesNH = varValues[nhIndeces]
|
126
|
-
varValuesSH = varValues[shIndeces]
|
127
|
-
end
|
128
|
-
|
129
|
-
# create output
|
130
|
-
iBaseFilename = File.basename(iFilename)
|
131
|
-
nhFile,shFile = "nh_#{iBaseFilename}","sh_#{iBaseFilename}"
|
132
|
-
[nhFile,shFile].each_with_index {|file,i|
|
133
|
-
puts "Creating '#{file}' ...."
|
134
|
-
indeces = [nhIndeces,shIndeces][i]
|
135
|
-
oFile = NetCDF.create(file)
|
136
|
-
|
137
|
-
# create data definitions
|
138
|
-
iFile.each_dim {|dim|
|
139
|
-
pp dim.name
|
140
|
-
if splittedDim == dim.name
|
141
|
-
oFile.def_dim(dim.name,indeces.size)
|
142
|
-
else
|
143
|
-
oFile.def_dim(dim.name,dim.length)
|
144
|
-
end
|
145
|
-
}
|
146
|
-
|
147
|
-
# copy attributes
|
148
|
-
iFile.each_var {|var|
|
149
|
-
next unless ([varname] + varDims + [lon,lat] + [lon,lat].map {|c| c+"_vertices"}).flatten.include?(var.name)
|
150
|
-
newvar = oFile.def_var( var.name, var.ntype, var.dim_names )
|
151
|
-
var.each_att{|att| newvar.put_att( att.name, att.get )}
|
152
|
-
}
|
153
|
-
oFile.enddef
|
154
|
-
iFile.each_var {|var|
|
155
|
-
next unless ([varname] + varDims + [lon,lat] + [lon,lat].map {|c| c+"_vertices"}).flatten.include?(var.name)
|
156
|
-
case var.name
|
157
|
-
when varname
|
158
|
-
case varDims.size
|
159
|
-
when 3
|
160
|
-
oFile.var(var.name).put(var.get[indeces,true,true])
|
161
|
-
when 2
|
162
|
-
oFile.var(var.name).put(var.get[indeces,true])
|
163
|
-
end
|
164
|
-
when 'p_ice_concSum'
|
165
|
-
oFile.var(var.name).put(var.get[indeces,true])
|
166
|
-
when 'cell_area'
|
167
|
-
oFile.var(var.name).put(var.get[indeces])
|
168
|
-
when lon,lat
|
169
|
-
oFile.var(var.name).put(var.get[indeces])
|
170
|
-
when lon+'_vertices',lat+'_vertices'
|
171
|
-
oFile.var(var.name).put(var.get[true,indeces])
|
172
|
-
else
|
173
|
-
oFile.var(var.name).put(var.get)
|
174
|
-
end
|
175
|
-
}
|
176
|
-
oFile.close
|
177
|
-
}
|
178
|
-
|
179
|
-
[nhFile,shFile]
|
180
|
-
end
|
181
|
-
def Cdp.splitICONHemisphere(iFilename,varname,lon,lat)
|
182
|
-
iFile = NetCDF.open(iFilename)
|
183
|
-
lats = iFile.var(lat).get
|
184
|
-
iceValues = iFile.var(varname).get_with_miss
|
185
|
-
|
186
|
-
# compute location indices and corresponding values
|
187
|
-
nhIndeces = (lats>0.0).where
|
188
|
-
shIndeces = (lats<0.0).where
|
189
|
-
iceValuesNH = iceValues[nhIndeces,true,1..-1]
|
190
|
-
iceValuesSH = iceValues[shIndeces,true,1..-1]
|
191
|
-
|
192
|
-
|
193
|
-
# create output
|
194
|
-
nhFile,shFile = "nh_#{iFilename}","sh_#{iFilename}"
|
195
|
-
[nhFile,shFile].each_with_index {|file,i|
|
196
|
-
puts "Creating '#{file}' ...."
|
197
|
-
indeces = [nhIndeces,shIndeces][i]
|
198
|
-
f = NetCDF.create(file)
|
199
|
-
iFile.each_dim {|dim|
|
200
|
-
next if ['clon','clat','ncells'].include?(dim.name) or
|
201
|
-
f.def_dim(dim.name,dim.length)
|
202
|
-
}
|
203
|
-
["clon","clat","ncells"].each {|hdim|
|
204
|
-
f.def_dim(hdim,indeces.size)
|
205
|
-
}
|
206
|
-
|
207
|
-
iFile.each_var{|var|
|
208
|
-
a = {var.name => var.dim_names}
|
209
|
-
newvar = f.def_var( var.name, var.ntype, var.dim_names )
|
210
|
-
var.each_att{|att| newvar.put_att( att.name, att.get )}
|
211
|
-
}
|
212
|
-
f.enddef
|
213
|
-
iFile.each_var{|var|
|
214
|
-
#puts var.name
|
215
|
-
case var.name
|
216
|
-
when varname
|
217
|
-
f.var(var.name).put(var.get[indeces,true,true])
|
218
|
-
when 'p_ice_concSum'
|
219
|
-
f.var(var.name).put(var.get[indeces,true])
|
220
|
-
when 'cell_area'
|
221
|
-
f.var(var.name).put(var.get[indeces])
|
222
|
-
when lon,lat
|
223
|
-
f.var(var.name).put(var.get[indeces])
|
224
|
-
when lon+'_vertices',lat+'_vertices'
|
225
|
-
f.var(var.name).put(var.get[true,indeces])
|
226
|
-
else
|
227
|
-
f.var(var.name).put(var.get)
|
228
|
-
end
|
229
|
-
}
|
230
|
-
f.close
|
231
|
-
}
|
232
|
-
|
233
|
-
[nhFile,shFile]
|
234
|
-
end
|
235
96
|
end
|
data/test/test_cdp.rb
CHANGED
@@ -35,15 +35,14 @@ class TestCdp < Test::Unit::TestCase
|
|
35
35
|
|
36
36
|
def test_splitFiles
|
37
37
|
files = []
|
38
|
-
files << '../A/a' << '../A/aa' << '../B/b' << '../B/bb'
|
39
|
-
|
40
|
-
assert_equal('gridfile',grid)
|
38
|
+
files << '../A/a' << '../A/aa' << '../B/b' << '../B/bb'
|
39
|
+
exp, exp4ana = Cdp.splitFilesIntoExperiments(files)
|
41
40
|
assert_equal(exp['A'],['../A/a','../A/aa'])
|
42
41
|
assert_equal(exp['B'],['../B/b','../B/bb'])
|
43
|
-
files << 'a' << 'aa' << 'b' << 'bb'
|
42
|
+
files << 'a' << 'aa' << 'b' << 'bb'
|
44
43
|
grid, exp, exp4ana = Cdp.splitFilesIntoExperiments(files)
|
45
44
|
assert_equal('exp',exp.keys[0])
|
46
|
-
files << './a' << './aa' << './b' << './bb'
|
45
|
+
files << './a' << './aa' << './b' << './bb'
|
47
46
|
grid, exp, exp4ana = Cdp.splitFilesIntoExperiments(files)
|
48
47
|
assert_equal('exp',exp.keys[0])
|
49
48
|
grid, exp, exp4ana = Cdp.splitFilesIntoExperiments(files,'test')
|
@@ -65,32 +64,4 @@ class TestCdp < Test::Unit::TestCase
|
|
65
64
|
weights = Cdp.maskedAreaWeights("cell_area",ICONGRID,"wet_c",ICONMASK,WEIGHTS,true)
|
66
65
|
assert_equal(WEIGHTS,weights)
|
67
66
|
end
|
68
|
-
def test_split_sphere
|
69
|
-
# one timesteps
|
70
|
-
Cdp.splitHemisphere(ICONOCE,"ELEV",'clon','clat')
|
71
|
-
return
|
72
|
-
# with missing values
|
73
|
-
Cdo.debug = true
|
74
|
-
maskedOce = Cdo.div(:input => "-selname,T #{ICONOCE} -selname,wet_c #{ICONOCE}")
|
75
|
-
Cdp.splitHemisphere(maskedOce,"T",'clon','clat')
|
76
|
-
maskedOce = Cdo.div(:input => "-selname,ELEV #{ICONOCE} -sellevidx,1 -selname,wet_c #{ICONOCE}")
|
77
|
-
Cdp.splitHemisphere(maskedOce,"ELEV",'clon','clat')
|
78
|
-
|
79
|
-
# 10 timesteps
|
80
|
-
Cdp.splitHemisphere(ICONOCEL,"ELEV",'clon','clat')
|
81
|
-
Cdp.splitHemisphere(ICONOCEL,"T",'clon','clat')
|
82
|
-
# with missing values
|
83
|
-
Cdo.debug = true
|
84
|
-
maskedOce = Cdo.div(:input => "-selname,T #{ICONOCEL} -selname,wet_c #{ICONOCEL}")
|
85
|
-
Cdp.splitHemisphere(maskedOce,"T",'clon','clat')
|
86
|
-
maskedOce = Cdo.div(:input => "-selname,ELEV #{ICONOCEL} -sellevidx,1 -selname,wet_c #{ICONOCEL}")
|
87
|
-
Cdp.splitHemisphere(maskedOce,"ELEV",'clon','clat')
|
88
|
-
end
|
89
|
-
def test_split_icon_sphere
|
90
|
-
|
91
|
-
# one timesteps
|
92
|
-
elev = Cdo.selname("ELEV",:input => ICONOCEL)
|
93
|
-
pp Cdo.infov(:input => elev)
|
94
|
-
Cdp.splitICONHemisphere(elev,"ELEV",'clon','clat')
|
95
|
-
end
|
96
67
|
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.
|
4
|
+
version: 0.0.7
|
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:
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cdo
|