Class: Observation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/observation.rb

Overview

Respresents a series of measurements taken by a weather station. Take extreme care when querying this table as it contrains a lot of rows!

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#calibratedObject

/@todo


21
22
23
# File 'app/models/observation.rb', line 21

def calibrated
  @calibrated
end

#timezoneObject

Returns the value of attribute timezone


6
7
8
# File 'app/models/observation.rb', line 6

def timezone
  @timezone
end

Class Method Details

.pluck_from_each_station(limit = 1) ⇒ Array

Note:

requires Postgres 9.3

Plucks the IDs of the N latest observations from each station

Parameters:

  • limit (Integer) (defaults to: 1)

Returns:

  • (Array)

See Also:


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/observation.rb', line 102

def self.pluck_from_each_station(limit = 1)
  ActiveRecord::Base.connection.execute(%Q{
    SELECT o.id
    FROM   stations s
    JOIN   LATERAL (
       SELECT id, created_at
       FROM   observations
       WHERE  station_id = s.id  -- lateral reference
       ORDER  BY created_at DESC
       LIMIT  #{limit}
       ) o ON TRUE
    ORDER  BY s.id, o.created_at DESC;
  }).field_values('id')
end

Instance Method Details

#b=Object

TODO:
  • CLEANUP

56
57
58
# File 'app/models/observation.rb', line 56

def b=
  write_attribute(:min_wind_speed, val.to_f / 100)
end

#calibrate!Object


64
65
66
67
68
69
70
71
72
73
# File 'app/models/observation.rb', line 64

def calibrate!
  unless self.calibrated
    unless self.speed_calibration.nil?
      self.speed            = (self.speed * self.speed_calibration).round(1)
      self.min_wind_speed   = (self.min_wind_speed * self.speed_calibration).round(1)
      self.max_wind_speed   = (self.max_wind_speed * self.speed_calibration).round(1)
      self.calibrated = true
    end
  end
end

#calibrated?Boolean

Returns:

  • (Boolean)

60
61
62
# File 'app/models/observation.rb', line 60

def calibrated?
  self.calibrated == true
end

#compass_pointObject Also known as: cardinal

degrees to cardinal


92
93
94
# File 'app/models/observation.rb', line 92

def compass_point
  Geocoder::Calculations.compass_point(self.direction)
end

#created_at_localObject


87
88
89
# File 'app/models/observation.rb', line 87

def created_at_local
  station.time_to_local(created_at)
end

#d=(val) ⇒ Object

TODO:
  • CLEANUP

when writing from the ardiuno params short form


39
40
41
# File 'app/models/observation.rb', line 39

def d= val
  write_attribute(:direction, (val.to_f / 10).round(0))
end

#max=(val) ⇒ Object

TODO:
  • CLEANUP

when writing from the ardiuno params short form


45
46
47
# File 'app/models/observation.rb', line 45

def max= val
  write_attribute(:max_wind_speed, val.to_f / 100)
end

#min=(val) ⇒ Object

TODO:
  • CLEANUP

when writing from the ardiuno params short form


51
52
53
# File 'app/models/observation.rb', line 51

def min= val
  write_attribute(:min_wind_speed, val.to_f / 100)
end

#observation_cannot_be_calibratedObject


75
76
77
78
79
# File 'app/models/observation.rb', line 75

def observation_cannot_be_calibrated
  if self.calibrated
    errors.add(:speed_calibration, "Calibrated observations cannot be saved!")
  end
end

#s=(val) ⇒ Object

TODO:
  • CLEANUP

when writing from the ardiuno params short form


33
34
35
# File 'app/models/observation.rb', line 33

def s= val
  write_attribute(:speed, val.to_f / 100)
end

#set_calibration_value!Object


81
82
83
84
85
# File 'app/models/observation.rb', line 81

def set_calibration_value!
  if self.station.present?
    self.speed_calibration = station.speed_calibration
  end
end