Class: CodeRunner::Trinity::NetcdfSmartReader
- Inherits:
-
Object
- Object
- CodeRunner::Trinity::NetcdfSmartReader
- Defined in:
- lib/trinitycrmod/read_netcdf.rb
Class Method Summary collapse
Instance Method Summary collapse
- #axiskit(variable, options) ⇒ Object
- #check_no_r(non_flat_dims) ⇒ Object
- #dim_end(name, options) ⇒ Object
- #dim_start(name, options) ⇒ Object
- #dimension_variable_name(n) ⇒ Object
- #dimensions(varname) ⇒ Object
- #ends(dims, options) ⇒ Object
- #graphkit(variable, options) ⇒ Object
-
#initialize(file) ⇒ NetcdfSmartReader
constructor
A new instance of NetcdfSmartReader.
- #read_variable(varname, options) ⇒ Object
- #starts(dims, options) ⇒ Object
Constructor Details
#initialize(file) ⇒ NetcdfSmartReader
Returns a new instance of NetcdfSmartReader.
40 41 42 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 40 def initialize(file) @file = file end |
Class Method Details
.dimensions ⇒ Object
103 104 105 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 103 def self.dimensions ['t','tspec', 'iter', 'rad', 'cc', 'mrow', 'mcol', 'ivar', 'jac', 'grad', 'eval', 'cegrid', 'job', 'conv'] end |
Instance Method Details
#axiskit(variable, options) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 107 def axiskit(variable, ) case variable when 'mrow', 'mcol', 'ivar', 'tspec', 'iter', 'jac', 'grad', 'eval', 'job', 'conv' return GraphKit::AxisKit.autocreate( data: GSL::Vector.linspace(1, sz= (de=dim_end(variable,); de<0 ? de+@file.dim(variable).length : de) - (ds=dim_start(variable,); ds<0 ? ds+@file.dim(variable).length : ds) + 1, sz), title: variable) end GraphKit::AxisKit.autocreate(data: read_variable(variable, ), 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
127 128 129 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 127 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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 89 def dim_end(name, ) sym = name.to_sym if i=[sym + :_index] or i=[sym] return i-1 elsif i=[sym + :_element] return i elsif i=[sym + :max] #if sym == :conv #return [i, @file.var('conv_final') return i else return -1 end end |
#dim_start(name, options) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 74 def dim_start(name, ) sym = name.to_sym if i=[sym + :_index] or i = [sym] return i-1 elsif i=[sym + :_element] return i elsif i=[sym + :min] return i else return 0 end end |
#dimension_variable_name(n) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 119 def dimension_variable_name(n) case n when *self.class.dimensions n else raise "Unknown dimension #{n}" end end |
#dimensions(varname) ⇒ Object
43 44 45 46 47 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 43 def dimensions(varname) #p 'varname', varname raise "Unknown variable #{varname}" unless @file.var(varname) @file.var(varname).dims end |
#ends(dims, options) ⇒ Object
86 87 88 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 86 def ends(dims, ) dims.map{|d| dim_end(d.name, )} end |
#graphkit(variable, options) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 130 def graphkit(variable, ) non_flat_dims=dimensions(variable).find_all{|dim| dim_start(dim.name, ) != dim_end(dim.name, ) and dim.length != 1} check_no_r(non_flat_dims) axiskits = non_flat_dims.map{|dim| dimvar = dimension_variable_name(dim.name); axiskit(dimvar, )} + [axiskit(variable, )] hash = {} axes = [:x, :y, :z, :f] axiskits.each_with_index{|ax, i| hash[axes[i]] = ax} kit = GraphKit.autocreate(hash) opts = .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
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 48 def read_variable(varname, ) #start = get_start(dims, options) dims = dimensions(varname) narray = @file.var(varname).get('start'=>starts(dims, ), 'end'=>ends(dims, )) if [:modify_variable] myhsh = dims.inject({}){|hsh, dim| opts = .dup opts[:modify_variable] = nil dimval = read_variable(dimension_variable_name(dim.name), opts) hsh[dim.name] = dimval hsh } narray = [:modify_variable].call(varname, narray, myhsh) elsif [: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
71 72 73 |
# File 'lib/trinitycrmod/read_netcdf.rb', line 71 def starts(dims, ) dims.map{|d| dim_start(d.name, )} end |