Class: WrfLibrary::Wrf::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/wrf_library/wrf/handler.rb

Overview

Handler class that holds the data repository and the meta data for a wrf model result The data can be added completely be leaving the optional values of the constructor unchanged. But it also can be read only a subset of the provided data by setting a duration and an offset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, start_date, duration = Float::MAX, offset = 0.0) ⇒ Handler

initialization for data from a file

Parameters:

  • filename (String)

    the given filename

  • start_date (Time)

    the starting date and time of the model results

  • duration (Float) (defaults to: Float::MAX)

    the optional forecast duration

  • offset (Float) (defaults to: 0.0)

    the optional offset when to start with the data



25
26
27
28
29
30
31
32
# File 'lib/wrf_library/wrf/handler.rb', line 25

def initialize(filename, start_date, duration=Float::MAX, offset=0.0)
  data = RubyUtils::FileReader.new(filename, " ").data
  # create meta data from first entry
   = MetaData.new(data[0], start_date)
  data.delete_at(0)
  @data_repository = RubyUtils::DataRepository.new()
  fill_repository(data, duration, offset, start_date)
end

Instance Attribute Details

#data_repositoryDataRepository (readonly)

Returns the data repository for the data.

Returns:

  • (DataRepository)

    the data repository for the data



16
17
18
# File 'lib/wrf_library/wrf/handler.rb', line 16

def data_repository
  @data_repository
end

#durationFloat (readonly)

Returns the amount of time the forecast data comprises.

Returns:

  • (Float)

    the amount of time the forecast data comprises



18
19
20
# File 'lib/wrf_library/wrf/handler.rb', line 18

def duration
  @duration
end

Instance Method Details

#create_wrf_entry(elements, start_date) ⇒ WrfEntry (private)

method to create a new entry that can be put in the repository

Parameters:

  • elements (Array)

    the elements of a single line for an entry

  • start_date (Time)

    the starting date and time of the model results

Returns:

  • (WrfEntry)

    the created entry



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/wrf_library/wrf/handler.rb', line 77

def create_wrf_entry(elements, start_date)
  entry = Entry.new()
  entry.forecast_time = start_date + elements[1].to_f * 3600
  entry.air_temperature = elements[5].to_f
  entry.mixing_ratio = elements[6].to_f
  entry.u_wind = elements[7].to_f
  entry.v_wind = elements[8].to_f
  entry.pressure = elements[9].to_f
  entry.longwave = elements[10].to_f
  entry.shortwave = elements[11].to_f
  entry.sensible_heat = elements[12].to_f
  entry.latent_heat = elements[13].to_f
  entry.skin_temperature = elements[14].to_f
  entry.soil_temperature = elements[15].to_f
  entry.cumulus_rainfall = elements[16].to_f
  entry.explicit_rainfall = elements[17].to_f
  entry.water_vapor = elements[18].to_f
  return entry
end

#fill_repository(data, duration, offset, start_date) ⇒ Object (private)

method to fill the data into the data repository

Parameters:

  • data (Array)

    the unformatted input data

  • duration (Float)

    the forecast duration

  • offset (Float)

    the offset when to start with the data addition

  • start_date (Time)

    the starting date and time of the model results



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wrf_library/wrf/handler.rb', line 58

def fill_repository(data, duration, offset, start_date)
  data.each { |line|
    forecast_hours = line[1].to_f
    next if (forecast_hours < offset)
    entry = create_wrf_entry(line, start_date)
    if (forecast_hours - offset <= duration)
      @data_repository.add_data_entry(entry)
    else
      break
    end
  }
  @duration = @data_repository.repository.last.forecast_time
  nil
end

#retrieve_data_set(symbol) ⇒ Array

method to retrieve the complete data series for a given attribute

Parameters:

  • symbol (Symbol)

    the attribute that is requested

Returns:

  • (Array)

    the data of the requested attribute

Raises:

  • (ArgumentError)

    if an invalid attribute is provided



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wrf_library/wrf/handler.rb', line 38

def retrieve_data_set(symbol)
  values = Array.new()
  begin
    @data_repository.repository.each { |data|
      value = data.send(symbol)
      values << value
    }
  rescue NoMethodError
    raise ArgumentError, "Error: Given symbol #{symbol} does not exist.".red
  end
  return values
end