require_relative "label" require_relative "interpolator" class Ecu class Kennlinie < Label attr_reader :xdim, :xvalue, :xunit attr_reader :value, :unit def initialize(name:, xdim:, xvalue:, value:, xunit: nil, unit: nil, function: nil, description: nil) @name = name @xdim = xdim @xvalue = xvalue @value = value @xunit = xunit @unit = unit @function = function @description = description init_error "Dimensions for value don't match xdim" if value.size != xdim init_error "Dimensions for xvalue don't match xdim" if xvalue.size != xdim end def value_at(x) Interpolator.interp1(xvalue, value, x) end def round_to(n) hsh = Hash.new hsh[:xvalue] = xvalue.map { |x| x.round(n) } if xvalue.all? { |x| x.is_a?(Numeric) } hsh[:value] = value.map { |x| x.round(n) } if value.all? { |x| x.is_a?(Numeric) } self.with hsh end def reinterp(new_xvalue) self.with \ xvalue: new_xvalue, value: new_xvalue.map { |x| value_at(x) } end def bytesize (1 + 1) * (xdim) * BYTESIZE[:number] end def to_s(detail: false) if detail == :value "#{name}:\n#{to_s(detail: :justvalue)}" elsif detail == :justvalue ValuePrinter.call(self) elsif detail == :onelinefull "#{name} #{to_s(detail: :oneline)}" elsif detail == :oneline "(#{xdim}x1) X: #{valuestats(xvalue)}, Z: #{valuestats(value, show_avg: true)}" else "#{name}: #{@xdim}x1 #{type}".tap do |str| if detail str << "\n" str << " x-Unit: \"#{xunit}\"\n" str << " y-Unit: \"#{unit}\"\n" str << " Value:\n" str << ValuePrinter.call(self).indent(4) str << "\n" end end end end def properties [:name, :xdim, :xvalue, :value, :xunit, :unit, :function, :description] end def equality_properties [:name, :xvalue, :value, :type] end end end