Class: CodeRunner::Gs2::NetcdfSmartReader

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

Direct Known Subclasses

OldNetcdfSmartReader

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ NetcdfSmartReader

Returns a new instance of NetcdfSmartReader.



41
42
43
# File 'lib/gs2crmod/read_netcdf.rb', line 41

def initialize(file)
	@file = file
end

Instance Method Details

#axiskit(variable, options) ⇒ Object



103
104
105
# File 'lib/gs2crmod/read_netcdf.rb', line 103

def axiskit(variable, options)
	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/, ''))
end

#check_no_r(non_flat_dims) ⇒ Object



130
131
132
# File 'lib/gs2crmod/read_netcdf.rb', line 130

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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/gs2crmod/read_netcdf.rb', line 90

def dim_end(name, options)
	sym = name.to_sym
	if i=options[sym + :_index]
		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



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gs2crmod/read_netcdf.rb', line 75

def dim_start(name, options)
	sym = name.to_sym
	if i=options[sym + :_index]
		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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gs2crmod/read_netcdf.rb', line 106

def dimension_variable_name(n)
	case n
	when 'X'
		'kx'
	when 'Y'
		'ky'
	when 'z'
		'kz'
	when 'e'
		'energy'
	when 'l'
		'lambda'
	when 't'
		n
	when 'm'
		'hermite'
	when 'p'
		'hankel'
	when 's'
		's'
	else
		raise "Unknown dimension #{n}"
	end
end

#dimensions(varname) ⇒ Object



44
45
46
47
# File 'lib/gs2crmod/read_netcdf.rb', line 44

def dimensions(varname)
	#p 'varname', varname
	@file.var(varname).dims
end

#ends(dims, options) ⇒ Object



87
88
89
# File 'lib/gs2crmod/read_netcdf.rb', line 87

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

#graphkit(variable, options) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gs2crmod/read_netcdf.rb', line 133

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)/, '')
	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
71
# File 'lib/gs2crmod/read_netcdf.rb', line 48

def read_variable(varname, options)
	#start = get_start(dims, options)
	if varname == 's' # dummy response for species index
		return NArray.float(@nspec||1)
	end
	dims = dimensions(varname)
	narray = @file.var(varname).get('start'=>starts(dims, options), 'end'=>ends(dims, options))
	if options[:modify_variable]
		dimhash = dims.inject({}){|hash, dim| 
			opts = options.dup
			opts[:modify_variable] = nil
			dimval = read_variable(dimension_variable_name(dim.name), opts)
			hash[dim.name] = dimval
			hash
		}
		narray = options[:modify_variable].call(varname, narray, hash)
	end
	shape = narray.shape
	shape.delete_if{|i| i==1}
	#p 'shape', shape; STDIN.gets
	narray.reshape!(*shape)
	narray

end

#starts(dims, options) ⇒ Object



72
73
74
# File 'lib/gs2crmod/read_netcdf.rb', line 72

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