Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow UKAQ to guess source of site #392

Merged
merged 8 commits into from
Oct 22, 2024
Merged

Conversation

jack-davison
Copy link
Collaborator

This PR extends the capabilities introduced in #350.

In short, the default source in importUKAQ() is now NULL. This allows importUKAQ() to work out the source on its own, where possible.

See examples:

devtools::load_all()
#> ℹ Loading openair

set.seed(123)

meta_sample <-
  importMeta(year = 2023, source = "ukaq") |>
  dplyr::slice_sample(n = 1, by = source)

meta_sample$code
#> [1] "TALL"  "WL1"   "NL11"  "NPT4A" "BEL5"  "WA7"
meta_sample$source
#> [1] "aurn"  "aqe"   "saqn"  "waqn"  "ni"    "local"

aq <-
  importUKAQ(
    year = 2023,
    site = meta_sample$code
  )

dplyr::count(aq, source, site, code)
#> # A tibble: 5 × 4
#>   source site                            code      n
#>   <fct>  <chr>                           <chr> <int>
#> 1 aurn   Tallington                      TALL    504
#> 2 saqn   N Lanarkshire Kirkshaws         NL11   8760
#> 3 aqe    Waltham Forest Dawlish Rd       WL1    8760
#> 4 ni     Belfast Newtownards Road        BEL5   8760
#> 5 local  Wandsworth - Putney High Street WA7    8760

Created on 2024-08-12 with reprex v2.1.1

Caveats:

  • Will error if the code is unknown.

  • Will error if the code is ambiguous ("AD1").

devtools::load_all()
#> ℹ Loading openair

# ambiguous codes
importUKAQ(c("my1", "kc1", "ad1"))
#> Error in `guess_source()` at openair/R/importUKAQ.R:215:7:
#> ✖ Ambiguous site codes detected. Please specify source in
#>   `importUKAQ()`.
#> ℹ Ambiguous codes: AD1 (saqn, local)

# non existant codes
importUKAQ(c("my1", "kc1", "foo", "bar", "falk"))
#> Error in `guess_source()` at openair/R/importUKAQ.R:215:7:
#> ✖ Unknown site codes detected. Please ensure all site codes can be found
#>   in `importMeta()`.
#> ℹ Unknown site codes: FOO and BAR

Created on 2024-08-12 with reprex v2.1.1

Ambiguous codes are few and far between, so it may just have to be an unfortunate feature of the function for those local to "AD1" for now! Unless you can think of a better treatment of them.

@jack-davison
Copy link
Collaborator Author

Update - now just prefers AURN to LMAM data.

devtools::load_all()
#> ℹ Loading openair

importUKAQ(c("ad1", "el3"))
#> Warning: ✖ Ambiguous site codes detected. National networks are imported preferentially
#>   to locally managed networks.
#> ! AD1 (could also be 'Adur - Shoreham-by-Sea' from the source: 'local'.)
#> ! EL3 (could also be 'Elmbridge Hampton Court Parade' from the source:
#>   'local'.)
#> ℹ Specify source to import sites from specific monitoring networks.
#> # A tibble: 17,520 × 12
#>    source site     code  date                  nox   no2    no  pm10 pm2.5    ws
#>    <fct>  <chr>    <chr> <dttm>              <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 saqn   Aberdee… AD1   2022-01-01 00:00:00    NA    NA    NA  19.0 14.2    4.7
#>  2 saqn   Aberdee… AD1   2022-01-01 01:00:00    NA    NA    NA  14.6  8.76   4.6
#>  3 saqn   Aberdee… AD1   2022-01-01 02:00:00    NA    NA    NA  19.6  9.44   5.9
#>  4 saqn   Aberdee… AD1   2022-01-01 03:00:00    NA    NA    NA  14.6  7.86   6.4
#>  5 saqn   Aberdee… AD1   2022-01-01 04:00:00    NA    NA    NA  13.6  7.74   8  
#>  6 saqn   Aberdee… AD1   2022-01-01 05:00:00    NA    NA    NA  15.9  8.93   9.5
#>  7 saqn   Aberdee… AD1   2022-01-01 06:00:00    NA    NA    NA  21.1 11.9   10.6
#>  8 saqn   Aberdee… AD1   2022-01-01 07:00:00    NA    NA    NA  22.8 12.6   10  
#>  9 saqn   Aberdee… AD1   2022-01-01 08:00:00    NA    NA    NA  23.5 13.3    8.9
#> 10 saqn   Aberdee… AD1   2022-01-01 09:00:00    NA    NA    NA  25.6 13.3   10.8
#> # ℹ 17,510 more rows
#> # ℹ 2 more variables: wd <dbl>, air_temp <dbl>

Created on 2024-08-12 with reprex v2.1.1
12 with reprex v2.1.1

@jack-davison jack-davison merged commit dbe9e89 into master Oct 22, 2024
6 checks passed
@jack-davison jack-davison deleted the feat/ukaq-guess-source branch October 22, 2024 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant