data-raw/data-index.R

library(dplyr)

## Get names expected from stations data download

province <- c("ALBERTA" = "AB",
              "BRITISH COLUMBIA" = "BC",
              "MANITOBA" = "MB",
              "NEW BRUNSWICK" = "NB",
              "NEWFOUNDLAND" = "NL",
              "NORTHWEST TERRITORIES" = "NT",
              "NOVA SCOTIA" = "NS",
              "NUNAVUT" = "NU",
              "ONTARIO" = "ON",
              "PRINCE EDWARD ISLAND" = "PE",
              "QUEBEC" = "QC",
              "SASKATCHEWAN" = "SK",
              "YUKON TERRITORY" = "YT")

m_names <- c("station_name" = "Station Name", "station_id" = "ID",
             "station_operator" = "Current Station Operator",
             "prov" = "Province", "lat" = "Latitude", "lon" = "Longitude",
             "elev" = "Elevation", "climate_id" = "Climate Identifier",
             "WMO_id" = "WMO Identifier", "TC_id" = "TC Identifier")

w_names <- list(
  "hour" = c("time" = "Date/Time (LST)", "year" = "Year", "month" = "Month",
             "day" = "Day", "hour" = "Time (LST)",
             "qual" = "Data Quality",
             "temp" = "Temp (C)", "temp_flag" = "Temp Flag",
             "temp_dew" = "Dew Point Temp (C)",
             "temp_dew_flag" = "Dew Point Temp Flag",
             "rel_hum" = "Rel Hum (%)", "rel_hum_flag" = "Rel Hum Flag",
             "precip_amt" = "Precip. Amount (mm)", "precip_amt_flag" = "Precip. Amount Flag",
             "wind_dir" = "Wind Dir (10s deg)",
             "wind_dir_flag" = "Wind Dir Flag",
             "wind_spd" = "Wind Spd (km/h)", "wind_spd_flag" = "Wind Spd Flag",
             "visib" = "Visibility (km)", "visib_flag" = "Visibility Flag",
             "pressure" = "Stn Press (kPa)", "pressure_flag" = "Stn Press Flag",
             "hmdx" = "Hmdx", "hmdx_flag" = "Hmdx Flag",
             "wind_chill" = "Wind Chill", "wind_chill_flag" = "Wind Chill Flag",
             "weather" = "Weather"),
  "day" = c("date" = "Date/Time", "year" = "Year",
            "month" = "Month", "day" = "Day",
            "qual" = "Data Quality",
            "max_temp" = "Max Temp (C)", "max_temp_flag" = "Max Temp Flag",
            "min_temp" = "Min Temp (C)", "min_temp_flag" = "Min Temp Flag",
            "mean_temp" = "Mean Temp (C)", "mean_temp_flag" = "Mean Temp Flag",
            "heat_deg_days" = "Heat Deg Days (C)",
            "heat_deg_days_flag" = "Heat Deg Days Flag",
            "cool_deg_days" = "Cool Deg Days (C)",
            "cool_deg_days_flag" = "Cool Deg Days Flag",
            "total_rain" = "Total Rain (mm)",
            "total_rain_flag" = "Total Rain Flag",
            "total_snow" = "Total Snow (cm)",
            "total_snow_flag" = "Total Snow Flag",
            "total_precip" = "Total Precip (mm)",
            "total_precip_flag" = "Total Precip Flag",
            "snow_grnd" = "Snow on Grnd (cm)",
            "snow_grnd_flag" = "Snow on Grnd Flag",
            "dir_max_gust" = "Dir of Max Gust (10s deg)",
            "dir_max_gust_flag" = "Dir of Max Gust Flag",
            "spd_max_gust" = "Spd of Max Gust (km/h)",
            "spd_max_gust_flag" = "Spd of Max Gust Flag"),
  "month" = c("date" = "Date/Time", "year" = "Year", "month" = "Month",
              "mean_max_temp" = "Mean Max Temp (C)",
              "mean_max_temp_flag" = "Mean Max Temp Flag",
              "mean_min_temp" = "Mean Min Temp (C)",
              "mean_min_temp_flag" = "Mean Min Temp Flag",
              "mean_temp" = "Mean Temp (C)",
              "mean_temp_flag" = "Mean Temp Flag",
              "extr_max_temp" = "Extr Max Temp (C)",
              "extr_max_temp_flag" = "Extr Max Temp Flag",
              "extr_min_temp" = "Extr Min Temp (C)",
              "extr_min_temp_flag" = "Extr Min Temp Flag",
              "total_rain" = "Total Rain (mm)",
              "total_rain_flag" = "Total Rain Flag",
              "total_snow" = "Total Snow (cm)",
              "total_snow_flag" = "Total Snow Flag",
              "total_precip" = "Total Precip (mm)",
              "total_precip_flag" = "Total Precip Flag",
              "snow_grnd_last_day" = "Snow Grnd Last Day (cm)",
              "snow_grnd_last_day_flag" = "Snow Grnd Last Day Flag",
              "dir_max_gust" = "Dir of Max Gust (10's deg)",
              "dir_max_gust_flag" = "Dir of Max Gust Flag",
              "spd_max_gust" = "Spd of Max Gust (km/h)",
              "spd_max_gust_flag" = "Spd of Max Gust Flag")
)

# w_names <- list(
#   "hour" = c("time", "year", "month", "day", "hour", "qual", "temp", "temp_flag", "temp_dew", "temp_dew_flag", "rel_hum", "rel_hum_flag", "wind_dir", "wind_dir_flag", "wind_spd", "wind_spd_flag", "visib", "visib_flag", "pressure", "pressure_flag", "hmdx", "hmdx_flag", "wind_chill", "wind_chill_flag", "weather"),
#
#   "day" = c("date", "year", "month", "day", "qual", "max_temp", "max_temp_flag", "min_temp", "min_temp_flag", "mean_temp", "mean_temp_flag", "heat_deg_days", "heat_deg_days_flag", "cool_deg_days", "cool_deg_days_flag", "total_rain", "total_rain_flag", "total_snow", "total_snow_flag", "total_precip", "total_precip_flag", "snow_grnd", "snow_grnd_flag", "dir_max_gust", "dir_max_gust_flag","spd_max_gust", "spd_max_gust_flag"),
#
#   "month" = c("date", "year", "month", "mean_max_temp", "mean_max_temp_flag", "mean_min_temp", "mean_min_temp_flag", "mean_temp", "mean_temp_flag", "extr_max_temp", "extr_max_temp_flag", "extr_min_temp", "extr_min_temp_flag", "total_rain", "total_rain_flag", "total_snow", "total_snow_flag", "total_precip", "total_precip_flag", "snow_grnd_last_day", "snow_grnd_last_day_flag", "dir_max_gust", "dir_max_gust_flag","spd_max_gust", "spd_max_gust_flag")
# )


n_names <- tribble(
  ~new_var, ~variable, ~type,
  "title_temp", "Temperature", "title",
  "temp_daily_average", "Daily Average (C)", "unique",
  "temp_sd", "Standard Deviation", "unique",
  "temp_daily_max", "Daily Maximum (C)", "unique",
  "temp_daily_min", "Daily Minimum (C)", "unique",
  "temp_extreme_max", "Extreme Maximum (C)", "unique",
  "temp_extreme_max_date", "Date (yyyy/dd)", "sub",
  "temp_extreme_min", "Extreme Minimum (C)", "unique",
  "temp_extreme_min_date", "Date (yyyy/dd)", "sub",
  "title_precip", "Precipitation", "title",
  "rain", "Rainfall (mm)", "unique",
  "snow", "Snowfall (cm)", "unique",
  "precip", "Precipitation (mm)", "unique",
  "snow_mean_depth", "Average Snow Depth (cm)", "unique",
  "snow_median_depth", "Median Snow Depth (cm)", "unique",
  "snow_depth_month_end", "Snow Depth at Month-end (cm)", "unique",
  "rain_extreme_daily", "Extreme Daily Rainfall (mm)", "unique",
  "rain_extreme_daily_date", "Date (yyyy/dd)", "sub",
  "snow_extreme_daily", "Extreme Daily Snowfall (cm)", "unique",
  "snow_extreme_daily_date", "Date (yyyy/dd)", "sub",
  "precip_extreme_daily", "Extreme Daily Precipitation (mm)", "unique",
  "precip_extreme_daily_date", "Date (yyyy/dd)", "sub",
  "snow_extreme_depth", "Extreme Snow Depth (cm)", "unique",
  "snow_extreme_depth_date", "Date (yyyy/dd)", "sub",
  "title_temp_max", "Days with Maximum Temperature", "title",
  "temp_max_days_<=0", "<= 0 C", "sub",
  "temp_max_days_>0", "> 0 C", "sub",
  "temp_max_days_>10", "> 10 C", "sub",
  "temp_max_days_>20", "> 20 C", "sub",
  "temp_max_days_>30", "> 30 C", "sub",
  "temp_max_days_>35", "> 35 C", "sub",
  "title_temp_min", "Days with Minimum Temperature", "title",
  "temp_min_days_>0", "> 0 C", "sub",
  "temp_min_days_<=2", "<= 2 C", "sub",
  "temp_min_days_<=0", "<= 0 C", "sub",
  "temp_min_days_<-2", "< -2 C", "sub",
  "temp_min_days_<-10", "< -10 C", "sub",
  "temp_min_days_<-20", "< -20 C", "sub",
  "temp_min_days_<-30", "< - 30 C", "sub",
  "title_rain_days", "Days with Rainfall", "title",
  "rain_days_>=0.2", ">= 0.2 mm", "sub",
  "rain_days_>=5", ">= 5 mm", "sub",
  "rain_days_>=10", ">= 10 mm", "sub",
  "rain_days_>=25", ">= 25 mm", "sub",
  "title_snow_days", "Days With Snowfall", "title",
  "snow_days_>=0.2", ">= 0.2 cm", "sub",
  "snow_days_>=5", ">= 5 cm", "sub",
  "snow_days_>=10", ">= 10 cm", "sub",
  "snow_days_>=25", ">= 25 cm", "sub",
  "title_precip_days", "Days with Precipitation", "title",
  "precip_days_>=0.2", ">= 0.2 mm", "sub",
  "precip_days_>=5", ">= 5 mm", "sub",
  "precip_days_>=10", ">= 10 mm", "sub",
  "precip_days_>=25", ">= 25 mm", "sub",
  "title_snowdepth_days", "Days with Snow Depth", "title",
  "snow_depth_days_>=1", ">= 1 cm", "sub",
  "snow_depth_days_>=5", ">= 5 cm", "sub",
  "snow_depth_days_>=10", ">= 10 cm", "sub",
  "snow_depth_days_>=20", ">= 20 cm", "sub",
  "title_wind", "Wind", "title",
  "wind_speed", "Speed (km/h)", "unique",
  "wind_dir", "Most Frequent Direction", "unique",
  "wind_max_speed", "Maximum Hourly Speed (km/h)", "unique",
  "wind_max_speed_date", "Date (yyyy/dd)", "sub",
  "wind_max_speed_dir", "Direction of Maximum Hourly Speed", "unique",
  "wind_max_gust", "Maximum Gust Speed (km/h)", "unique",
  "wind_max_gust_date", "Date (yyyy/dd)", "sub",
  "wind_max_gust_dir", "Direction of Maximum Gust", "unique",
  "wind_days_>=52", "Days with Winds >= 52 km/h", "unique",
  "wind_days_>=63", "Days with Winds >= 63 km/h", "unique",
  "title_dd", "Degree Days", "title",
  "dd_above_24", "Above 24 C", "sub",
  "dd_above_18", "Above 18 C", "sub",
  "dd_above_15", "Above 15 C", "sub",
  "dd_above_10", "Above 10 C", "sub",
  "dd_above_5", "Above 5 C", "sub",
  "dd_above_0", "Above 0 C", "sub",
  "dd_below_0", "Below 0 C", "sub",
  "dd_below_5", "Below 5 C", "sub",
  "dd_below_10", "Below 10 C", "sub",
  "dd_below_15", "Below 15 C", "sub",
  "dd_below_18", "Below 18 C", "sub",
  "title_soil_temp", "Soil Temperature", "title",
  "soil_temp_am_5", "at 5 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_5", "at 5 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_10", "at 10 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_10", "at 10 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_20", "at 20 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_20", "at 20 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_50", "at 50 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_50", "at 50 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_100", "at 100 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_100", "at 100 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_150", "at 150 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_150", "at 150 cm depth (PM obs) (C)", "unique",
  "soil_temp_am_300", "at 300 cm depth (AM obs) (C)", "unique",
  "soil_temp_pm_300", "at 300 cm depth (PM obs) (C)", "unique",
  "title_evaporation", "Evaporation", "title",
  "lake_evaporation", "Lake Evaporation (mm)", "unique",
  "title_sunshine", "Bright Sunshine", "title",
  "sun_hours", "Total Hours", "unique",
  "sun_measurable_days", "Days with measurable", "unique",
  "sun_perc_daylight_hours", "% of possible daylight hours", "unique",
  "sun_extreme_daily", "Extreme Daily", "unique",
  "sun_extreme_daily_date", "Date (yyyy/dd)", "sub",
  "title_hmdx", "Humidex", "title",
  "hmdx_extreme", "Extreme Humidex", "unique",
  "hmdx_extreme_date", "Date (yyyy/dd)", "sub",
  "hmdx_days_>=30", "Days with Humidex >= 30", "unique",
  "hmdx_days_>=35", "Days with Humidex >= 35", "unique",
  "hmdx_days_>=40", "Days with Humidex >= 40", "unique",
  "title_wind_chill", "Wind Chill", "title",
  "wind_chill_extreme", "Extreme Wind Chill (C)", "unique",
  "wind_chill_extreme_date", "Date (yyyy/dd)", "sub",
  "wind_chill_days_<-20", "Days with Wind Chill < -20", "unique",
  "wind_chill_days_<-30", "Days with Wind Chill < -30", "unique",
  "wind_chill_days_<-40", "Days with Wind Chill < -40", "unique",
  "title_humidity", "Humidity", "title",
  "humidity_mean_pressure", "Average Vapour Pressure (kPa)", "unique",
  "humidity_mean_0600LST", "Average Relative Humidity - 0600LST (%)", "unique",
  "humidity_mean_1500LST", "Average Relative Humidity - 1500LST (%)", "unique",
  "title_pressure", "Pressure", "title",
  "pressure_stn_mean", "Average Station Pressure (kPa)", "unique",
  "pressure_sea_mean", "Average Sea Level Pressure (kPa)", "unique",
  "title_radiation", "Radiation", "title",
  "rad_global_rf1", "Global - RF1 (MJ/m2)", "unique",
  "rad_extreme_global_rf1", "Extreme Global - RF1 (MJ/m2)", "unique",
  "rad_extreme_global_rf1_date", "Date (yyyy/dd)", "sub",
  "rad_diffuse_rf2", "Diffuse - RF2 (MJ/m2)", "unique",
  "rad_extreme_diffuse_rf2", "Extreme Diffuse - RF2 (MJ/m2)", "unique",
  "rad_extreme_diffuse_rf2_date", "Date (yyyy/dd)", "sub",
  "rad_reflected_rf3", "Reflected - RF3 (MJ/m2)", "unique",
  "rad_extreme_reflected_rf3", "Extreme Reflected - RF3 (MJ/m2)", "unique",
  "rad_extreme_reflected_rf3_date", "Date (yyyy/dd)", "sub",
  "rad_net_rf4", "Net - RF4 (MJ/m2)", "unique",
  "rad_extreme_net_rf4", "Extreme Net - RF4 (MJ/m2)", "unique",
  "rad_extreme_net_rf4_date", "Date (yyyy/dd)", "sub",
  "title_visibility", "Visibility (hours with)", "title",
  "visibility_<1", "< 1 km", "sub",
  "visibility_1_9", "1 to 9 km", "sub",
  "visibility_>9", "> 9 km", "sub",
  "title_cloud", "Cloud Amount (hours with)", "title",
  "cloud_0_2", "0 to 2 tenths", "sub",
  "cloud_3_7", "3 to 7 tenths", "sub",
  "cloud_8_10", "8 to 10 tenths", "sub"
) %>%
  mutate(variable = tolower(variable),
         group = stringr::str_detect(new_var, "title"),
         group = cumsum(group),
         subgroup = type != "sub",
         subgroup = cumsum(subgroup),
         variable_sub = paste0(variable, "_", subgroup))

f_names <- tribble(
  ~new_var, ~variable, ~group,
  "date_last_spring_frost", "Average Date of Last Spring Frost", 1,
  "date_first_fall_frost", "Average Date of First Fall Frost", 1,
  "length_frost_free", "Average Length of Frost-Free Period", 1,
  "prob_last_spring_temp_below_0_on_date",
  paste0("Probability of last temperature in spring of 0 ",
         "C or lower on or after indicated dates"), 2,
  "prob_first_fall_temp_below_0_on_date",
  paste0("Probability of first temperature in fall of 0 ",
         "C or lower on or before indicated dates"), 2,
  "prob_length_frost_free",
  paste0("Probability of frost-free period equal ",
         "to or less than indicated period (Days)"), 2,
  "probability", "probability", 3)

n_formats <- select(n_names, "new_var") %>%
  filter(!stringr::str_detect(new_var, "title")) %>%
  mutate(format = case_when(
    stringr::str_detect(new_var, "date") ~ "date",
    stringr::str_detect(new_var, "dir") ~ "character",
    TRUE ~ "numeric"))

n_formats <- bind_rows(n_formats,
                       tibble(new_var = paste0(n_formats[["new_var"]], "_code"),
                              format = "character"))

f_formats <- select(f_names, "new_var") %>%
  mutate(format = case_when(
    stringr::str_detect(new_var, "on_date") ~ "date",
    TRUE ~ "numeric")) %>%
  bind_rows(tibble(new_var = "frost_code", format = "character"))

usethis::use_data(province, w_names, m_names, n_names, f_names,
                  n_formats, f_formats,
                  overwrite = TRUE, internal = TRUE)

# Technical documentation: ftp://ftp.tor.ec.gc.ca/Pub/Documentation_Technical/Technical_Documentation.pdf
steffilazerte/weathercan documentation built on Sept. 23, 2023, 7:13 p.m.