Class: WrfLibrary::JsonConverter::BaseStationJsonConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_library/json_converter/base_station_converter.rb

Overview

Abstract parent class for the json converter to convert preread weather data into the specified output format. The metadata is identical for all child classes, the weather values can depend on the different attributes.

Direct Known Subclasses

WrfStationJsonConverter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ BaseStationJsonConverter

initialization

Parameters:

  • repository (DataRepository)

    the prefilled repository



17
18
19
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 17

def initialize(repository)
  @data = repository
end

Instance Attribute Details

#dataDataRepository (readonly, private)

Returns the data repository.

Returns:

  • (DataRepository)

    the data repository



42
43
44
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 42

def data
  @data
end

Instance Method Details

#add_additionsObject (private)

abstract method which allows additions to the meta data hash if present method

Raises:

  • (NotImplementedError)

    if the child class does not implement this



73
74
75
76
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 73

def add_additions
  fail NotImplementedError, " Error: the subclass #{self.class} needs " \
       "to implement the method: add_additions from its base class".red
end

#convert(filepath = nil) ⇒ String

method to convert the data of repository into the given json output if nil the output will be printed to stdout

Parameters:

  • filepath (String) (defaults to: nil)

    the filepath where the output should be created,

Returns:

  • (String)

    the json formatted output



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 25

def convert(filepath=nil)
  output = Hash.new()
  output[:meta_data] = generate_meta_hash()
  output[:weather_data] = generate_data_values()

  output_string = JSON.pretty_generate(output)
  if (filepath != nil)
    file = File.open(File.join(filepath,"output.json"), "w")
    file.write(output_string)
    file.close
  end
  return output_string
end

#generate_data_valuesObject (private)

abstract method which adds the weather data to the weather key that will be converted into json via a Hash. If this method implements faulty Hashes the json conversion will fail. method

Raises:

  • (NotImplementedError)

    if the child class does not implement this



83
84
85
86
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 83

def generate_data_values
  fail NotImplementedError, " Error: the subclass #{self.class} needs " \
       "to implement the method: generate_data_values from its base class".red
end

#generate_meta_hashHash (private)

methode to create the meta entry based on the meta data in the repoitory json conversion

Returns:

  • (Hash)

    the prepared key/value hash of the meta data for the



47
48
49
50
51
52
53
54
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 47

def generate_meta_hash
   = @data.
  meta_hash = Hash.new()
  meta_hash[:station] = generate_station_hash(.station)
  meta_hash[:start_date] = .start_date
  meta_hash = meta_hash.merge(add_additions())
  return meta_hash
end

#generate_station_hash(station) ⇒ Hash (private)

method to create the station entry based on the meta data in the repository json conversion

Parameters:

  • station (Station)

    the station data

Returns:

  • (Hash)

    the prepared key/value hash of the station for the



60
61
62
63
64
65
66
67
68
# File 'lib/wrf_library/json_converter/base_station_converter.rb', line 60

def generate_station_hash(station)
  station_hash = Hash.new()
  station_hash[:name] = station.name
  station_hash[:descriptor] = station.descriptor
  station_hash[:elevation] = station.elevation
  station_hash[:coordinate] = { :x => station.coordinate.x, 
                                :y => station.coordinate.y }
  return station_hash
end