Skip to contents

Fetches the parts of one dataset and scenario listed by wet_catalog(), checks each against the manifest's size and md5, and writes them to the Postgres table the dataset ID names: discharge/fwa/v1/segment_monthly becomes discharge.fwa_v1_segment_monthly. The scenario's rows already in the table are replaced, in one transaction, so a reload never duplicates and a failed load leaves the table as it was.

Usage

wet_db_load(
  conn,
  catalog,
  dataset = "discharge/fwa/v1/segment_monthly",
  scenario = "historical",
  months = 0:12,
  pick_only = FALSE,
  cache_dir = tools::R_user_dir("wet", "cache"),
  doys = 1:365,
  groups = NULL
)

Arguments

conn

A DBI::DBIConnection to Postgres (RPostgres).

catalog

The parts, from wet_catalog().

dataset

Dataset ID, e.g. "discharge/fwa/v1/segment_monthly".

scenario

Scenario to load, e.g. "historical".

months

Months to keep, from 0 (annual) to 12, for a monthly dataset.

pick_only

Keep only the default source's rows (pick), about half the segment table. Datasets without pick refuse it.

cache_dir

Where fetched parts are kept between loads. A cached part is reused only while its md5 matches the manifest.

doys

Days of the common year to keep, 1 to 365, for a daily dataset.

groups

NULL, or the watershed group codes to keep.

Value

Invisibly, data.frame(key, rows): the rows loaded from each part.

Details

A load with months, doys, groups or pick_only set still replaces the whole scenario, so it leaves only that subset for every reader of the table; the table comment records it. The daily dataset (discharge/fwa/v1/segment_daily, about 2.9 billion rows) is meant to be loaded for the groups a job needs: its parts hold one watershed group each, and only the named groups' parts are fetched.

The table holds the files' columns plus scenario, forcing and source. Where the dataset has pick, the load is refused unless each feature and month (or day) has at most one picked row in the scenario. On the first load it is created with a primary key on (scenario, forcing, source, <id>, <month or doy>, period), an index on the feature ID and, where a monthly dataset has pick, a partial index on (scenario, <id>) for the annual default, which fresh's join reads. fresh then joins the default annual flow per segment with frs_col_join(from = "(select linear_feature_id, q_m3s as mad_m3s from discharge.fwa_v1_segment_monthly where scenario = 'historical' and month = 0 and pick)").

Which parts a table holds

Each load also writes one row per part it loaded to <schema>.wet_load (discharge.wet_load), replacing the dataset and scenario's rows in the same transaction as the data: dataset, scenario, key, md5, rows, subset (the filter of a partial load, NULL for a full one), manifest, built and wet_commit (the build the parts came from), recipe_id (its receipt's, where the manifest names one) and loaded_at. Unlike the table comment, which names only the scenario loaded last, it keeps every dataset and scenario's load, so a consumer reading the historical rows can key on the historical load: select key, md5 from discharge.wet_load where dataset = 'discharge/fwa/v1/segment_monthly' and scenario = 'historical'.

See also

Examples

if (FALSE) { # \dontrun{
conn <- DBI::dbConnect(RPostgres::Postgres(), dbname = "fwapg")
wet_db_load(conn, wet_catalog())
DBI::dbGetQuery(conn, "
  select linear_feature_id, q_m3s as mad_m3s
  from discharge.fwa_v1_segment_monthly
  where scenario = 'historical' and month = 0 and pick
  limit 5")
} # }