Module: Cdp

Defined in:
lib/cdp.rb

Constant Summary collapse

@@debug =
false

Class Method Summary collapse

Class Method Details

.areaWeights(areaVarname, areaFile, ofile = nil, force = false) ⇒ Object

compute area weights from an area variable



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cdp.rb', line 67

def Cdp.areaWeights(areaVarname,areaFile,ofile=nil,force=false)
  if ofile.nil?
    area = Cdo.selname(areaVarname,:input => areaFile)
    Cdo.setname("area_weight",:input => " -div #{area} -enlarge,#{area} -fldsum #{area}")
  else
    unless File.exist?(ofile) and not force
      puts "Resuse existing file #{ofile}" if @@debug
      return ofile
    else
      area = Cdo.selname(areaVarname,:input => areaFile)
      Cdo.setname("area_weight",:input => " -div #{area} -enlarge,#{area} -fldsum #{area}", :output => ofile)
    end
  end
end

.debugObject



32
33
34
# File 'lib/cdp.rb', line 32

def Cdp.debug
  @@debug
end

.maskedAreaWeights(areaVarname, areaFile, maskVarname, maskFile, ofile = nil, force = false) ⇒ Object

compute area weights from an area variable with a mask from another file



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/cdp.rb', line 83

def Cdp.maskedAreaWeights(areaVarname,areaFile,maskVarname,maskFile,ofile=nil,force=false)
  if ofile.nil?
    maskedArea = Cdo.div(:input => " -selname,#{areaVarname} #{areaFile} -selname,#{maskVarname} #{maskFile}")
    Cdo.setname("area_weight", :input => " -div  #{maskedArea}  -enlarge,#{maskedArea} -fldsum #{maskedArea}")
  else
    if File.exist?(ofile) and not force
      puts "Resuse existing file #{ofile}" if @@debug
      return ofile
    else
      maskedArea = Cdo.div(:input => " -selname,#{areaVarname} #{areaFile} -selname,#{maskVarname} #{maskFile}")
      Cdo.setname("area_weight", :input => " -div  #{maskedArea}  -enlarge,#{maskedArea} -fldsum #{maskedArea}", :output => ofile)
    end
  end
end

.setCDOObject

setup of CDO on different machines based on the hostname



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cdp.rb', line 11

def Cdp.setCDO
  hostname = Socket.gethostname
  case hostname
  when /thingol/
    Cdo.setCdo(ENV['HOME']+"/local/bin/cdo-dev")
  when /lizard/
    Cdo.setCdo(ENV['HOME']+"/local/rhel55-x64/bin/cdo")
  when /blizzard/
    Cdo.setCdo(ENV['HOME']+"/local/bin/cdo")
  when /thunder/
    Cdo.setCdo(ENV['HOME']+"/local/squeeze-x64/bin/cdo")
  else
    puts "Use default Cdo:#{Cdo.getCdo} (version:#{Cdo.version})"
  end
end

.setDebug(value = true) ⇒ Object



27
28
29
30
31
# File 'lib/cdp.rb', line 27

def Cdp.setDebug(value=true)
  Cdo.checkCdo
  Cdo.debug = true unless ENV['DEBUG'].nil?
  @@debug   = value
end

.splitFilesIntoExperiments(files, exp = 'exp') ⇒ Object

compute the experiments from the data directories and link the corresponding files



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
# File 'lib/cdp.rb', line 37

def Cdp.splitFilesIntoExperiments(files,exp='exp')
  gridFile = files.pop
  experiments = files.map {|f| File.basename(File.dirname(f))}.uniq.sort_by {|f| f.length}.reverse
  # take the larges part of the filenames as experiment name if the files are in
  # the current directory
  if experiments == ["."] then
    n = files.map(&:size).min.times.map {|i|
      if files.map {|f| f[0,i-1]}.uniq.size == 1
        1
      else
        nil
      end
    }.find_all {|v| not v.nil?}.size-1
    uniqName = files[0][0,n]
    uniqName = exp if uniqName.empty?
    experiments = [uniqName]
  end
  experimentFiles, experimentAnalyzedData = {},{}
  experiments.each {|experiment|
    experimentFiles[experiment] = files.grep(/#{experiment}/)
      experimentFiles[experiment].each {|file| files.delete(file)}
    experimentFiles[experiment].sort!

    experimentAnalyzedData[experiment] = []
  }

  [gridFile,experimentFiles,experimentAnalyzedData]
end

.splitHemisphere(iFilename, varname, lon, lat, dim = '2d') ⇒ Object

split data file with global grid into 2 separate files with northern and southern hemisphere grid TODO: currently ICON cell variable are supported only



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
# File 'lib/cdp.rb', line 101

def Cdp.splitHemisphere(iFilename,varname,lon,lat,dim='2d')
  iFile  = NetCDF.open(iFilename)
  unless iFile.var_names.include?(varname)
    warn "Could not find #{varname} in #{iFilename}"
    raise IOError
  end
  lats      = iFile.var(lat).get
  var       = iFile.var(varname)
  varDims   = var.dim_names
  varValues = var.get_with_miss
  splittedDim = varDims[0]

  # compute location indices and corresponding values
  nhIndeces = (lats>0.0).where
  shIndeces = (lats<0.0).where

  case varValues.shape.size 
  when 3 
    varValuesNH = varValues[nhIndeces,true,true]
    varValuesSH = varValues[shIndeces,true,true]
  when 2
    varValuesNH = varValues[nhIndeces,true]
    varValuesSH = varValues[shIndeces,true]
  when 1
    varValuesNH = varValues[nhIndeces]
    varValuesSH = varValues[shIndeces]
  end

  # create output
  iBaseFilename = File.basename(iFilename)
  nhFile,shFile = "nh_#{iBaseFilename}","sh_#{iBaseFilename}"
  [nhFile,shFile].each_with_index {|file,i|
    puts "Creating '#{file}' ...."
    indeces = [nhIndeces,shIndeces][i]
    oFile   = NetCDF.create(file)

    # create data definitions
    iFile.each_dim {|dim| 
      pp dim.name
      if splittedDim == dim.name
        oFile.def_dim(dim.name,indeces.size)
      else
        oFile.def_dim(dim.name,dim.length)
      end
    }

    #  copy attributes
    iFile.each_var {|var|
      next unless ([varname] + varDims + [lon,lat] + [lon,lat].map {|c| c+"_vertices"}).flatten.include?(var.name)
      newvar = oFile.def_var( var.name, var.ntype, var.dim_names )
      var.each_att{|att| newvar.put_att( att.name, att.get )}
    }
    oFile.enddef
    iFile.each_var {|var| 
      next unless ([varname] + varDims + [lon,lat] + [lon,lat].map {|c| c+"_vertices"}).flatten.include?(var.name)
      case var.name 
      when varname
        case varDims.size
        when 3
          oFile.var(var.name).put(var.get[indeces,true,true])
        when 2
          oFile.var(var.name).put(var.get[indeces,true])
        end
      when 'p_ice_concSum'
        oFile.var(var.name).put(var.get[indeces,true])
      when 'cell_area'
        oFile.var(var.name).put(var.get[indeces])
      when lon,lat
        oFile.var(var.name).put(var.get[indeces])
      when lon+'_vertices',lat+'_vertices'
        oFile.var(var.name).put(var.get[true,indeces])
      else
        oFile.var(var.name).put(var.get)
      end
    }
    oFile.close
  }

  [nhFile,shFile]
end

.splitICONHemisphere(iFilename, varname, lon, lat) ⇒ Object



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
# File 'lib/cdp.rb', line 181

def Cdp.splitICONHemisphere(iFilename,varname,lon,lat)
  iFile     = NetCDF.open(iFilename)
  lats      = iFile.var(lat).get
  iceValues = iFile.var(varname).get_with_miss

  # compute location indices and corresponding values
  nhIndeces   = (lats>0.0).where
  shIndeces   = (lats<0.0).where
  iceValuesNH = iceValues[nhIndeces,true,1..-1]
  iceValuesSH = iceValues[shIndeces,true,1..-1]


  # create output
  nhFile,shFile = "nh_#{iFilename}","sh_#{iFilename}"
  [nhFile,shFile].each_with_index {|file,i|
    puts "Creating '#{file}' ...."
    indeces = [nhIndeces,shIndeces][i]
    f = NetCDF.create(file)
    iFile.each_dim {|dim| 
      next if ['clon','clat','ncells'].include?(dim.name) or 
      f.def_dim(dim.name,dim.length)
    }
    ["clon","clat","ncells"].each {|hdim| 
      f.def_dim(hdim,indeces.size)
    }

    iFile.each_var{|var|
      a      = {var.name => var.dim_names}
      newvar = f.def_var( var.name, var.ntype, var.dim_names )
      var.each_att{|att| newvar.put_att( att.name, att.get )}
    }
    f.enddef
    iFile.each_var{|var| 
      #puts var.name
      case var.name 
      when varname
        f.var(var.name).put(var.get[indeces,true,true])
      when 'p_ice_concSum'
        f.var(var.name).put(var.get[indeces,true])
      when 'cell_area'
        f.var(var.name).put(var.get[indeces])
      when lon,lat
        f.var(var.name).put(var.get[indeces])
      when lon+'_vertices',lat+'_vertices'
        f.var(var.name).put(var.get[true,indeces])
      else
        f.var(var.name).put(var.get)
      end
    }
    f.close
  }

  [nhFile,shFile]
end