Module: Usgs::Statistics

Included in:
Client
Defined in:
lib/usgs/statistics.rb

Instance Method Summary collapse

Instance Method Details

#get_stats(sites:, parameter_cd: nil, report_type: :daily, stat_year_type: nil) ⇒ Array<Usgs::Models::Statistic>

Fetch statistics from USGS NWIS

Examples:

Usgs.client.get_stats(sites: "06754000", report_type: :daily)
Usgs.client.get_stats(sites: "06754000", report_type: :monthly, parameter_cd: :discharge)
Usgs.client.get_stats(sites: "06754000", report_type: :annual, stat_year_type: "water")

Parameters:

  • sites (String, Array<String>)

    USGS site ID(s)

  • parameter_cd (Symbol, String, Array) (defaults to: nil)

    e.g. :discharge

  • report_type (Symbol) (defaults to: :daily)

    :daily, :monthly, or :annual (default: :daily)

  • stat_year_type (String, nil) (defaults to: nil)

    “water”, “calendar”, “all” — only for :annual

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/usgs/statistics.rb', line 19

def get_stats(sites:, parameter_cd: nil, report_type: :daily, stat_year_type: nil)
  site_list   = Array(sites).join(",")
  param_list  = resolve_parameter_codes(parameter_cd)
  type_str    = report_type.to_s

  unless %w[daily monthly annual].include?(type_str)
    raise ArgumentError, "report_type must be :daily, :monthly, or :annual"
  end

  if type_str != "annual" && stat_year_type
    raise ArgumentError, "stat_year_type is only valid when report_type: :annual"
  end

  query = {
    format: "rdb,1.0",
    sites: site_list,
    parameterCd: param_list,
    statReportType: type_str,
    statTypeCd: "all"
  }

  query[:statYearType] = stat_year_type if stat_year_type

  raw = api_get("/stat/", query)
  Parser.parse_statistics(raw.body).map { |row| Models::Statistic.new(row) }
end