Class: CodeRunner::Veritas::NetcdfSmartReader

Inherits:
Object
  • Object
show all
Defined in:
lib/veritascrmod/read_netcdf.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ NetcdfSmartReader

Returns a new instance of NetcdfSmartReader.



12
13
14
# File 'lib/veritascrmod/read_netcdf.rb', line 12

def initialize(file)
  @file = file
end

Instance Method Details

#axiskit(variable, options) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/veritascrmod/read_netcdf.rb', line 77

def axiskit(variable, options)
  case variable
  when *file_dimensions
    return GraphKit::AxisKit.autocreate(data: GSL::Vector.linspace(1, sz=@file.dim(variable).length, sz), title: variable)
  end
  GraphKit::AxisKit.autocreate(data: read_variable(variable, options), units: @file.var(variable).att('units').get, title: @file.var(variable).att('description').get.sub(/(,|summed|average).*$/, '').sub(/[vV]alues of (the )?/, '').sub(/ coordinate/, '').sub(/.*rho.*The definition.*/, 'rho'))
end

#check_no_r(non_flat_dims) ⇒ Object



92
93
94
# File 'lib/veritascrmod/read_netcdf.rb', line 92

def check_no_r(non_flat_dims)
  raise "Please specify the r index for real or imaginary" if non_flat_dims.include? @file.dim('r')
end

#dim_end(name, options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/veritascrmod/read_netcdf.rb', line 61

def dim_end(name, options)
  sym = name.to_sym
  if i=options[sym + :_index] or i=options[sym]
    return i-1
  elsif i=options[sym + :_element]
    return i
  elsif i=options[sym + :max]
    return i
  else
    return -1
  end
end

#dim_start(name, options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/veritascrmod/read_netcdf.rb', line 46

def dim_start(name, options)
  sym = name.to_sym
  if i=options[sym + :_index] or i = options[sym]
    return i-1
  elsif i=options[sym + :_element]
    return i
  elsif i=options[sym + :min]
    return i
  else
    return 0
  end
end

#dimension_variable_name(n) ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/veritascrmod/read_netcdf.rb', line 84

def dimension_variable_name(n)
  case n
  when *file_dimensions
    n
  else
    raise "Unknown dimension #{n}"
  end
end

#dimensions(varname) ⇒ Object



15
16
17
18
19
# File 'lib/veritascrmod/read_netcdf.rb', line 15

def dimensions(varname)
  #p 'varname', varname
  raise "Unknown variable #{varname}" unless @file.var(varname)
  @file.var(varname).dims
end

#ends(dims, options) ⇒ Object



58
59
60
# File 'lib/veritascrmod/read_netcdf.rb', line 58

def ends(dims, options)
  dims.map{|d| dim_end(d.name, options)}
end

#file_dimensionsObject



73
74
75
# File 'lib/veritascrmod/read_netcdf.rb', line 73

def file_dimensions
  @file.dims.map{|d| d.name}
end

#graphkit(variable, options) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/veritascrmod/read_netcdf.rb', line 95

def graphkit(variable, options)
  non_flat_dims=dimensions(variable).find_all{|dim| dim_start(dim.name, options) != dim_end(dim.name, options) and dim.length != 1}
  check_no_r(non_flat_dims)
  axiskits = non_flat_dims.map{|dim| dimvar = dimension_variable_name(dim.name); axiskit(dimvar, options)} + [axiskit(variable, options)]
  hash = {}
  axes = [:x, :y, :z, :f]
  axiskits.each_with_index{|ax, i| hash[axes[i]] = ax}
  kit = GraphKit.autocreate(hash)
  opts = options.dup
  opts.delete(:modify_variable)
  opts.delete(:graphkit_name)
  #kit.data[0].title += " with options: " + opts.to_s
  kit.data[0].title += " " + opts.to_s.gsub(/_(index|element)/, '')
  kit.data[0].gp.with = "lp"
  if kit.zlabel
    kit.zlabel = "'#{kit.zlabel}' rotate by 90"
    #kit.zlabel = nil
  end
  kit
end

#read_variable(varname, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/veritascrmod/read_netcdf.rb', line 20

def read_variable(varname, options)
  #start = get_start(dims, options)
  dims = dimensions(varname)
  narray = @file.var(varname).get('start'=>starts(dims, options), 'end'=>ends(dims, options))
  if options[:modify_variable]
    myhsh = dims.inject({}){|hsh, dim|
      opts = options.dup
      opts[:modify_variable] = nil
      dimval = read_variable(dimension_variable_name(dim.name), opts)
      hsh[dim.name] = dimval
      hsh
    }
    narray = options[:modify_variable].call(varname, narray, myhsh)
  elsif options[:invert_matrix]
    narray = GSL::Linalg::LU.invert(narray.to_gm_view).to_na_ref
  end
  shape = narray.shape
  shape.delete_if{|i| i==1}
  #p 'shape', shape; STDIN.gets
  narray.reshape!(*shape)
  narray

end

#starts(dims, options) ⇒ Object



43
44
45
# File 'lib/veritascrmod/read_netcdf.rb', line 43

def starts(dims, options)
  dims.map{|d| dim_start(d.name, options)}
end