Module: KNMI

Defined in:
lib/knmi.rb,
lib/knmi/station.rb,
lib/knmi/parameters.rb,
lib/knmi/httpservice.rb,
lib/knmi/calculations.rb

Defined Under Namespace

Classes: Calculations, HttpService, Parameters, Station

Class Method Summary collapse

Class Method Details

.convert(parameters, request) ⇒ Hash

Input data request object and return array of hashes converted from storage to operable units.

Parameters:

  • parameters (KNMI::Stations)
    • Parameters object

  • request (KNMI:HttpService)
    • Data object from HttpRequest

Returns:

  • (Hash)
    • Hash containing climate data in operable units



122
123
124
125
126
127
128
129
130
131
# File 'lib/knmi.rb', line 122

def convert(parameters, request)
  
  if parameters[0].period == "daily"        
    KNMI::Calculations.convert_daily(request.data)
    
  elsif parameters[0].period == "hourly"
    KNMI::Calculations.convert_hourly(request.data)
    
  end    
end

.get_data(station, parameters, starts = nil, ends = nil, seasonal = false) ⇒ KNMI::HttpService

Retrieve climate data from a weather station

Parameters:

  • station (KNMI::Station)
    • station object retrieved with KNMI.station_by_id() or KNMI.station_by_location()

  • parameters (Array<KNMI::Parameters>)
    • parameters object retrieved with KNMI.parameters()

  • starts (Time) (defaults to: nil)
    • Time object e.g. Time.utc(2010, 6, 28)

  • ends (Time) (defaults to: nil)
    • Time object e.g. Time.utc(2010, 6, 29)

  • seasonal (Boolean) (defaults to: false)

Returns:



105
106
107
108
109
110
111
112
113
114
# File 'lib/knmi.rb', line 105

def get_data(station, parameters, starts = nil, ends = nil, seasonal = false)
  
  if parameters[0].period == "daily"        
    HttpService.get_daily(station, parameters, starts, ends, seasonal)
    
  elsif parameters[0].period == "hourly"        
    HttpService.get_hourly(station, parameters, starts, ends, seasonal)
    
  end
end

.parameters(period, params = nil, categories = nil) ⇒ Array<KNMI::Parameters>

Get an array of parameter objects

All details in daily_data_key.yml and hourly_data_key.yml

Examples:

Get daily station data and a single parameter

KNMI.parameters("daily", "TX")

Get hourly station data and a multiple parameters

KNMI.parameters("hourly", ["TX", "TG"])

Get hourly station data and a multiple categories

KNMI.parameters("daily", nil, ["WIND", "TEMP"])

Parameters:

  • period (string)
    • “daily” or “hourly”

  • params (Array<string>) (defaults to: nil)
    • array of parameters e.g [“TX”, “TG”]

  • categories (Array<string>) (defaults to: nil)
    • array of categories e.g. [“WIND”, “TEMP”]

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/knmi.rb', line 67

def parameters(period, params = nil, categories = nil)
  if params.nil? and categories.nil?
    
    list = Parameters.all(period)
  
  elsif categories.nil?
    
    # Parameters by name
    list = []
    params = unique_params(params, list) #ensure params are unique to list 
    list << Parameters.find(period, params)        
    list.flatten!
    list.compact!
  else 

    # Parameters by category
    list = []
    list << Parameters.category(period, categories)
    list.flatten!
    
    # Parameters by name
    params = unique_params(params, list) #ensure params are unique to list 
    list << Parameters.find(period, params)        
    list.flatten!
    list.compact!
  end
  return list
end

.station_by_id(station_id) ⇒ KNMI::Station

Get station object by station ID

Examples:

Get station by ID

KNMI.station_by_id(210)

Parameters:

  • station_id (String, Numeric)
    • number of station of interest, station_ids can be found in

Returns:

  • (KNMI::Station)
    • Object with details about the requested station



46
47
48
# File 'lib/knmi.rb', line 46

def station_by_id(station_id)
  Station.find(station_id)
end

.station_by_location(lat, lng) ⇒ KNMI::Station

Get nearest station by lat lng

Examples:

Get nearest station by lat, lng

KNMI.station_by_location(52.165, 52.165)

Get nearest station by [lat, lng]

KNMI.station_by_location([52.165, 52.165])

Get nearest station with GeoKit object

KNMI.station_by_location(GeoKit::LatLng.new(52.165, 4.419))

Parameters:

  • lat (Numeric)
    • latitude e.g. 52.165

  • lng (Numeric)
    • longitude e.g. 52.165

  • [] (Array)
    • lat, lng

      e.g. [52.165, 4,419] *eqivalent to (lat, lng)*

  • - (GeoKit::LatLng)

    Geokit object e.g. GeoKit::LatLng.new(52.165, 4.419) *eqivalent to (lat, lng)*

Returns:

  • (KNMI::Station)
    • Object with details about the station nearest to the requested lat/lng



34
35
36
# File 'lib/knmi.rb', line 34

def station_by_location(lat, lng)
  Station.closest_to(lat, lng)
end