Skip to content

Commit

Permalink
Add option to configure forecast frequency (#6)
Browse files Browse the repository at this point in the history
* Add option to configure forecast frequency

* Fix failing test
  • Loading branch information
vinc authored Aug 16, 2023
1 parent 27bf41f commit 98146e2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/forecaster/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
module Forecaster
# Configure how to fetch and read from a forecast file.
class Configuration
attr_accessor :server, :cache_dir, :curl_path, :wgrib2_path, :records
attr_accessor :server, :frequency, :cache_dir, :curl_path, :wgrib2_path, :records

def initialize
#@server = "https://nomads.ncep.noaa.gov/pub/data/nccf/com/gfs/prod"
#@frequency = 1
@server = "https://ftp.ncep.noaa.gov/data/nccf/com/gfs/prod"
@frequency = 3
@cache_dir = "/tmp/forecaster"
@wgrib2_path = "wgrib2"

Expand Down
7 changes: 5 additions & 2 deletions lib/forecaster/forecast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def self.last_run_at

def self.at(time)
# There is a forecast every 3 hours after a run for 384 hours.
# On NOMADS server there also an hourly forecast up to 120 hours.
t = time.utc
fct = Time.utc(t.year, t.month, t.day, (t.hour / 3) * 3)
n = Forecaster.configuration.frequency # 1 or 3
fct = Time.utc(t.year, t.month, t.day, (t.hour / n) * n)
run = Time.utc(t.year, t.month, t.day, (t.hour / 6) * 6)
run -= 6 * 3600 if run == fct

Expand All @@ -29,7 +31,8 @@ def self.at(time)

fct_hour = (fct - run) / 3600

raise "Time too far in the future" if fct_hour > 384
max = n == 1 ? 120 : 384
raise "Time too far in the future" if fct_hour > max

Forecast.new(run.year, run.month, run.day, run.hour, fct_hour)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/forecaster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
wgrib2_path = Forecaster.configuration.wgrib2_path
out = `#{wgrib2_path} -version`

expect(out).to start_with("v0.2")
expect(out).to start_with("v3.1")
end

it "fetches a forecast" do
Expand Down

0 comments on commit 98146e2

Please sign in to comment.