Module: NumRu::GAnalysis

Defined in:
lib/numru/ganalysis.rb,
lib/numru/ganalysis/qg.rb,
lib/numru/ganalysis/eof.rb,
lib/numru/ganalysis/met.rb,
lib/numru/ganalysis/pde.rb,
lib/numru/ganalysis/log_p.rb,
lib/numru/ganalysis/met_z.rb,
lib/numru/ganalysis/planet.rb,
lib/numru/ganalysis/fitting.rb,
lib/numru/ganalysis/histogram.rb,
lib/numru/ganalysis/beta_plane.rb,
lib/numru/ganalysis/covariance.rb,
lib/numru/ganalysis/sigma_coord.rb,
lib/numru/ganalysis/lomb_scargle.rb,
lib/numru/ganalysis/sph_harmonic_iso_g.rb,
ext/numru/ganalysis/pde_ext.c

Defined Under Namespace

Modules: Fitting, LogP, LombScargle, Met, MetZ, PDE, Planet, QG, QG_common, QG_sphere, QG_sphere_common, QG_sphere_div, SigmaCoord, SphHarmonicIsoG Classes: BetaPlane

Class Method Summary collapse

Class Method Details

.correlation(gphys0, gphys1, *dims) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/numru/ganalysis/covariance.rb', line 60

def correlation(gphys0, gphys1, *dims)
  val0 = gphys0.val
  val1 = gphys1.val
  if val0.is_a?(NArrayMiss)
    mask = val0.get_mask
  else
    mask = NArray.byte(*(val0.shape)).fill!(1)
  end
  if val1.is_a?(NArrayMiss)
    mask2 = val1.get_mask
  else
    mask2 = NArray.byte(*(val1.shape)).fill!(1)
  end
  mask.mul!(mask2)
  val0 = NArrayMiss.to_nam(val0) unless val0.is_a?(NArrayMiss)
  val1 = NArrayMiss.to_nam(val1) unless val1.is_a?(NArrayMiss)
  val0 = val0.set_mask(mask)
  val1 = val1.set_mask(mask)
  p val0,val1 if $DEBUG
  gphys0 = gphys0.copy.replace_val(val0)
  gphys1 = gphys1.copy.replace_val(val1)

  covariance, ndiv = gphys0.covariance(gphys1,*dims)
  dims = dims.map{|dim| gphys0.dim_index(dim) }
  return covariance/(gphys0.stddev(*dims)*gphys1.stddev(*dims)), mask.to_type(NArray::LINT).sum(*dims)
end

.covariance(gphys0, gphys1, *dims) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/numru/ganalysis/covariance.rb', line 8

def covariance(gphys0, gphys1, *dims)
  unless GPhys===gphys0 && GPhys===gphys1
    raise "gphys0 and gphys1 must be GPhys"
  end
  unless gphys0.shape == gphys1.shape
    raise "gphys0 and gphys1 must have the same shape"
  end
  units = gphys0.units*gphys1.units
  if dims.length == 0
    dims = Array.new
    gphys0.rank.times{|i| dims.push i }
  else
    dims = dims.map{|dim| gphys0.dim_index(dim) }
  end
  val0 = gphys0.val
  val1 = gphys1.val
  if val0.is_a?(NArrayMiss)
    if val1.is_a?(NArrayMiss)
      mask = val0.get_mask * val1.get_mask
      ndiv = mask.to_type(NArray::LINT).accum(*dims)
      val0 = val0.set_mask(mask)
      val1 = val1.set_mask(mask)
    else
      ndiv = val0.get_mask.to_type(NArray::LINT).accum(*dims)
      val1 = NArrayMiss.to_nam(val1,val0.get_mask)
    end
  elsif val1.is_a?(NArrayMiss)
    ndiv = val1.get_mask.to_type(NArray::LINT).accum(*dims)
    val0 = NArrayMiss.to_nam(val0,val1.get_mask)
  else
    ndiv = 1
    gphys0.shape.each_with_index{|s,i|
      ndiv *= s if dims.include?(i)
    }
  end
  val0 -= val0.accum(*dims).div!(ndiv)
  val1 -= val1.accum(*dims).div!(ndiv)
  nary = val0.mul_add(val1,*dims)
  if Float === nary
    ndiv = ndiv[0] if ndiv.is_a?(NArray)
    nary /= (ndiv-1)
    return UNumeric.new(nary, units), ndiv
  else
    nary = nary.div!(ndiv-1)
    vary = VArray.new(nary,
                      {"long_name"=>"covariance","units"=>units.to_s},
                      "covariance")
    new_grid = gphys0.grid.delete_axes(dims, "covariance").copy
    return GPhys.new(new_grid,vary), ndiv
  end
end

.eof(gphys, *args) ⇒ Object

Calculate EOF vectors and contribution rate

call-seq

NumRu::GAnalysis.eof(gphys, dim0[, dim1, ..., dimN[, opts]]) => [eof, rate]

Arguments

gphys

GPhys object to be calculated its EOF

dim0, …, dimN

dimension name (String) or number (Ingeter) to calculate variance or covariance, and those dimensions are not contained in the result EOF vectors.

opts

a Hash object whose key is String or Symbol. The following options are available:

* nmodes: Integer, number of EOF modes to be calculate (default all EOF modes)
* weight: GPhys or NArray, weight vector
           +gphys+ is multiplied by the weight vector before calculation of variance covariance matrix
           and the result eigen vectors are divided by the vector.
           If weight vector is not set,
              it is cosine of latitude when the first two axes of the +gphys+ are "lon" and "lat" and +disable_weight+ option is not +true+,
              else 1.
* disable_weight: See weight option.
* no_pc: do not compute primary components

Return values

eof

GPhys object for array of EOF vectors.

rate

GPhys object for array of contribution rate correspoinding to the EOF vectors.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
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
# File 'lib/numru/ganalysis/eof.rb', line 52

def eof(gphys, *args)

  unless defined?(@@EOF_engin)
    raise "SSL2 (Ruby-SSL2) or LAPACK (Ruby-LAPACK) or GSL (Ruby/GSL) must have been installed. (SSL2 or LAPACK is recommended for large computation)"
  end

  if Hash === args[-1]
    dims = args[0..-2]
    opts = args[-1]
  else
    dims = args
    opts = Hash.new
  end
  no_pc = ( opts[:no_pc] || opts["no_pc"] )

  dims = dims.map{|dim| gphys.dim_index(dim) }  # dimensions to vary (~"time")
  n = 1
  n_lost = 1
  dims1 = Array.new     # vector dimensions (~"spatial")
  shape1 = Array.new
  gphys.shape.each_with_index{|s,i|
    if dims.include?(i)
      n_lost *= s
    else
      n *= s
      dims1.push i
      shape1.push s
    end
  }
  new_grid_wo_modes = gphys.instance_variable_get("@grid").delete_axes(dims, "covariance matrix").copy
  new_index = NArray.sint(*new_grid_wo_modes.shape).indgen
  index = NArray.object(gphys.rank)
  index[dims] = true

  wgtun = ""
  if w = (opts[:weight] || opts["weight"])
    if GPhys === w
      w = w.val
      wgtun = w.units
    end
    unless NArray === w
      raise "weight must be NArray of GPhys"
    end
    unless w.shape == new_grid_wo_modes.shape
      raise "shape of weight is invalid"
    end
    w_orgshape = w
    w = w.reshape(n)
  elsif new_grid_wo_modes.rank >= 2
    nc0 = new_grid_wo_modes.coord(0)
    nc1 = new_grid_wo_modes.coord(1)
    if !(opts[:disable_weight]||opts["disable_weight"]) &&
       ( /^lon/ =~ nc0.name || /^degree.*_east/ =~ nc0.units.to_s ) &&
       ( /^lat/ =~ nc1.name || /^degree.*_north/ =~ nc1.units.to_s )
      rad = NumRu::Units.new("radian")
      nlon = nc0.length
      lat = nc1.convert_units(rad).val
      w2 = NMath::cos(lat)
      eps = 1e-10   # a value whose sqrt is negligibly small as cosine
      w2[ w2.le(0) ] = eps
      w = NMath.sqrt(w2)     # sqrt(cos(lat))
      w = NArray.new(lat.typecode,nlon).fill!(1) * w.reshape(1,lat.length)
      w_orgshape = w
      w = w.reshape(w.length)
    else
      w = nil
    end
  else
    w = nil
  end

  val = gphys.val
  ary = val.transpose( *(dims + dims1) ).reshape!(n_lost, n)
  ary -= ary.mean(0).newdim(0)   # remove "temporal" mean
  ary = ary.mul!(w.reshape(1,n)) if w

  nmodes = opts[:nmodes] || opts["nmodes"] || n
  case @@EOF_engin
  when "ssl2"
    print "start calc covariance matrix\n" if $DEBUG
    nary = NArray.new(gphys.typecode,n*(n+1)/2)
    nn = 0
    total_var = 0
    n.times{|n0|
      for n1 in n0...n
        nary[nn] = ary[n0].mul_add(ary[n1],0)/(n_lost-1)
        if n1==n0
          total_var += nary[nn]
        end
        nn += 1
      end
    }
    ary = nil if no_pc # for GC
    print "start calc eigen vector\n" if $DEBUG
    val, vec = SSL2.seig2(nary,nmodes)
  when "lapack"
    print "start calc covariance matrix\n" if $DEBUG
    nary = NArray.new(gphys.typecode,n,n)
    total_var = 0.0
    n.times{|n0|
      nary[n0...n,n0] = (ary[true,n0...n].mul_add(ary[true,n0],0)/(n_lost-1)).get_array!
      total_var += nary[n0,n0]
    }
    ary = nil if no_pc  # for GC
    print "start calc eigen vector\n" if $DEBUG
    case nary.typecode
    when NArray::DFLOAT
    m, val, vec, isuppz, work, iwork, info, = NumRu::Lapack.dsyevr("V", "I", "L", nary, 0, 0, n-nmodes+1, n, 0.0, -1, -1)
    m, val, vec, = NumRu::Lapack.dsyevr("V", "I", "L", nary, 0, 0, n-nmodes+1, n, 0.0, work[0], iwork[0])
    when NArray::SFLOAT
    m, val, vec, isuppz, work, iwork, info, = NumRu::Lapack.ssyevr("V", "I", "L", nary, 0, 0, n-nmodes+1, n, 0.0, -1, -1)
    m, val, vec, = NumRu::Lapack.ssyevr("V", "I", "L", nary, 0, 0, n-nmodes+1, n, 0.0, work[0], iwork[0])
    end
    vec = vec[true,-1..0]
    val = val[0...nmodes][-1..0]
  when "gsl"
    print "start calc covariance matrix\n" if $DEBUG
    nary = NArray.new(gphys.typecode,n,n)
    n.times{|n0|
      nary[n0...n,n0] = (ary[true,n0...n].mul_add(ary[true,n0],0)/(n_lost-1)).get_array!
      nary[n0,n0...n] = nary[n0...n,n0]
    }
    ary = nil if no_pc  # for GC
    print "start calc eigen vector\n" if $DEBUG
    val, vec = GSL::Eigen::symmv(nary.to_gm)
    GSL::Eigen.symmv_sort(val, vec, GSL::Eigen::SORT_VAL_DESC)
    vec = vec.to_na[0...nmodes,true].transpose(1,0)
    val = val.to_na
    total_var = val.sum
    val = val[0...nmodes]
  end

  axes = new_grid_wo_modes.instance_variable_get('@axes')
  axis_order = Axis.new
  axis_order.pos = VArray.new(NArray.sint(nmodes).indgen(1),
                              {}, "mode")
  axes = axes + [ axis_order ]
  new_grid = Grid.new(*axes)

  unless no_pc
    mary = NMatrix.new(ary.typecode, *ary.shape)
    mary[true,true] = ary.to_na
    mvec = NMatrix.new(vec.typecode, *vec.shape)
    mvec[true,true] = vec
    mpc = mvec * mary   # use matrix multiplication
    pc = NArray.new(mpc.typecode, *mpc.shape)
    pc[true,true] = mpc
    pc /= pc.stddev(0).newdim!(0)  # normalization
    vary = VArray.new(pc, {"long_name"=>"Primary component",
                      "units"=>"1"}, "PC")
    grid = Grid.new( *dims.map{|d| gphys.grid.axis(d)} + axes[-1..-1] )
    pc = GPhys.new( grid, vary )
  end

  vec /= w if w
  vec.reshape!(*new_grid.shape)
  vec *= NMath::sqrt( val.reshape( *([1]*(axes.length-1)+[nmodes]) ) )
      # multiply a sqrt(eigen value) -> variance same as in the input data
  va_eof = VArray.new(vec,
                      {"long_name"=>"EOF vector","units"=>gphys.units.to_s },
                      "EOF")
  eof = GPhys.new(new_grid, va_eof)

  va_rate = VArray.new(val.div!(total_var),
                       {"long_name"=>"EOF contribution rate", "units"=>"1" },
                       "rate")
  rate = GPhys.new(Grid.new(axis_order), va_rate)

  if w
    w = w_orgshape
    if w.rank == new_grid_wo_modes.rank
      v = VArray.new(w, {"long_name"=>"weight",
                         "units"=>wgtun}, "weight" )
      w = GPhys.new( new_grid_wo_modes,
                     v )
    end
  end
  
  ret = [eof, rate, w]
  ret.push(pc) unless no_pc
  return ret
end

.eof2(gphys1, gphys2, *args) ⇒ Object

Raises:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/numru/ganalysis/eof.rb', line 235

def eof2(gphys1, gphys2, *args)
  if Hash === args[-1]
    opts = args[-1]
  else
    opts = Hash.new
  end
  raise ArgumentError, "The 1st arg must be a GPhys of rank 1: arg1 = #{gphys1.inspect}" unless gphys1.rank==1
  raise ArgumentError, "The 2nd arg must be a GPhys of rank 1: arg2 = #{gphys2.inspect}" unless gphys2.rank==1
  raise ArgumentError, "The 1st and 2nd args must have the same length: #{gphys1.length}!=#{gphys2.length}" unless gphys2.rank==1
  nam = NArrayMiss.new(gphys1.typecode, 2, gphys1.length)
  nam[0,true] = gphys1.val
  nam[1,true] = gphys2.val
  gphys = GPhys.new(Grid.new(Axis.new.set_pos(VArray.new(NArray[0,1],{},"var")),
                             gphys1.axis(0)),
                    VArray.new(nam,gphys1.data.attr_copy,gphys1.name))
  eof(gphys, 1, opts)
end

.histogram(gphys0, opts = Hash.new) ⇒ Object Also known as: histogram1D



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
65
66
67
68
69
# File 'lib/numru/ganalysis/histogram.rb', line 15

def histogram(gphys0,opts=Hash.new)
  unless HistogramGSL
    raise "gsl is necessary to use this method"
  end
  unless GPhys === gphys0
    raise "gphys0 (1st arg) must be GPhys"
  end
  unless Hash === opts
    raise "opts (2nd arg) must be Hash"
  end
  if opts["bins"]
    bins = opts["bins"]
    unless (bins.is_a?(NArray) || bins.is_a?(Array))
      raise(TypeError, "option 'bins' must be Array or NArray")
    end
    bins = bins.to_gslv if bins.is_a?(NArray)
    hist = GSL::Histogram.alloc(bins)
  else
    nbins = opts["nbins"] || gphys0.total/500
    nbins = 10 if nbins < 10
    min = opts["min"] || gphys0.min.val
    max = opts["max"] || gphys0.max.val
    if log_bins = (opts["log_bins"] && (min > 0))
      min = Math.log10(min)
      max = Math.log10(max)
    end
    hist = GSL::Histogram.alloc(nbins,[min,max])
  end
  val = gphys0.val
  val = val.get_array![val.get_mask!] if NArrayMiss === val
  val = NMath.log10(val) if log_bins
  val.each{|v| hist.increment(v)}

  bounds =  NArray.to_na(hist.range.to_a)
  bounds = 10 ** bounds if log_bins
  center = (bounds[0..-2]+bounds[1..-1])/2
  cell_width = (bounds[1..-1] - bounds[0..-2]) / 2
  name = gphys0.name
  attr = gphys0.data.attr_copy
  bounds = VArray.new(bounds, attr, name)
  center = VArray.new(center, attr, name)
  axis = Axis.new(true)
  axis.set_cell(center, bounds, name)
  axis.set_pos_to_center

  bin = NArray.to_na(hist.bin.to_a)
  bin /= cell_width if opts["log_bins"]
  bin = VArray.new(bin,
                   {"long_name" => (log_bins ? "number per unit bin width" : "number in bins"), "units"=>"1"},
                   "bin")
  new_gphys = GPhys.new(Grid.new(axis), bin)
  new_gphys.set_att("mean",[hist.mean])
  new_gphys.set_att("standard_deviation",[hist.sigma])
  return new_gphys
end

.histogram2D(gphys0, gphys1, opts = Hash.new) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
# File 'lib/numru/ganalysis/histogram.rb', line 72

def histogram2D(gphys0, gphys1, opts=Hash.new)
  unless HistogramGSL
    raise "gsl is necessary to use this method"
  end
  unless GPhys === gphys0
    raise "gphys0 (1st arg) must be GPhys"
  end
  unless GPhys === gphys1
    raise "gphys1 (2nd arg) must be GPhys"
  end
  unless Hash === opts
    raise "opts (3nd arg) must be Hash"
  end

  nbins0 = opts["nbins0"] || gphys0.total/500
  nbins0 = 10 if nbins0 < 10
  nbins1 = opts["nbins1"] || gphys1.total/500
  nbins1 = 10 if nbins1 < 10

  min0 = opts["min0"] || gphys0.min.val
  max0 = opts["max0"] || gphys0.max.val
  min1 = opts["min1"] || gphys1.min.val
  max1 = opts["max1"] || gphys1.max.val

  hist = GSL::Histogram2d.alloc(nbins0,[min0,max0],nbins1,[min1,max1])
  val0 = gphys0.val
  val1 = gphys1.val
  mask = nil
  if NArrayMiss === val0
    mask = val0.get_mask!
    val0 = val0.get_array!
  end
  if NArrayMiss === val1
    if mask
      mask = mask & val1.get_mask!
    else
      mask = val1.get_mask!
    end
    val1 = val1.get_array!
  end
  if mask
    val0 = val0[mask]
    val1 = val1[mask]
  end
  hist.increment(val0.to_gslv, val1.to_gslv)

  bounds0 = hist.xrange.to_na
  center0 = (bounds0[0..-2]+bounds0[1..-1])/2
  name = gphys0.name
  attr = gphys0.data.attr_copy
  bounds0 = VArray.new(bounds0, attr, name)
  center0 = VArray.new(center0, attr, name)
  axis0 = Axis.new(true)
  axis0.set_cell(center0, bounds0, name)
  axis0.set_pos_to_center

  bounds1 = hist.yrange.to_na
  center1 = (bounds1[0..-2]+bounds1[1..-1])/2
  name = gphys1.name
  attr = gphys1.data.attr_copy
  bounds1 = VArray.new(bounds1, attr, name)
  center1 = VArray.new(center1, attr, name)
  axis1 = Axis.new(true)
  axis1.set_cell(center1, bounds1, name)
  axis1.set_pos_to_center

  bin = hist.bin.to_na.reshape!(nbins1,nbins0).transpose(1,0)
  bin = VArray.new(bin,
                   {"long_name"=>"number in bins", "units"=>"1"},
                   "bin")
  new_gphys = GPhys.new(Grid.new(axis0,axis1), bin)
  new_gphys.set_att("mean0",[hist.xmean])
  new_gphys.set_att("standard_deviation0",[hist.xsigma])
  new_gphys.set_att("mean1",[hist.ymean])
  new_gphys.set_att("standard_deviation1",[hist.ysigma])
  new_gphys.set_att("covariance",[hist.cov])
  return new_gphys
end