ropensci / rnoaa

R interface to many NOAA data APIs
https://docs.ropensci.org/rnoaa
Other
330 stars 84 forks source link

Precipitation data #413

Open FelipeCarrillo opened 1 year ago

FelipeCarrillo commented 1 year ago

I wonder if this is a bug or data is not available after August 31, 2021. I am trying to download data from 2011 to 2021 but I only get half of 2021. See my script below: library(rnoaa)

create a data frame for Prince William latitude and longitude

lat_lon_df <- data.frame(id = "pw", lat = 60.690545, lon = -147.097055)

find 10 closest monitors to Prince William

mon_near_pw <- meteo_nearby_stations( lat_lon_df = lat_lon_df, lat_colname = "lat", lon_colname = "lon", var = "PRCP", year_min = 2011, year_max = 2021, limit = 20, )

mon_near_pw

3,9,11,14 Get rainfall data from Cannery Creek, Esther Island, Cordova, and Port San Juan

pw_prcp_dat <- meteo_pull_monitors( monitors = mon_near_pw$pw$id[c(1,3,11,14)], date_min = "2011-01-01", date_max = "2021-12-31", var = "PRCP" )

final <- pw_prcp_dat %>% pivot_wider(names_from= id,values_from=prcp) %>% data.frame() head(final) dim(final)

Rename stations with more meaningful names

names(final)[2:5] <- c("cannery_creek","esther_island","cordova_n","sanjuan_chenega") head(final) tail(final)

Get the total rainfall of the sound

final$total_prcp <- rowSums(cbind(final$cannery_creek, final$esther_island, final$cordova_n,final$sanjuan_chenega),na.rm=T) dim(final)

djhocking commented 1 year ago

I am not sure yet but I suspect that it's a change to the API as with the NCDC data I'm working on with issue #412

FelipeCarrillo commented 1 year ago

Would the data pull fix on #412 work for my problem? ncdc2? Or meteo_nearby_stations is completely different?

djhocking commented 1 year ago

I was hoping this was going to be an easier problem to solve but the API has completely changed and it's broken most of the functions in the package. They still work for now, but only for old data as you've found. Essentially, every function needs to be rewritten. So it's a related problem but will require a unique solution. I unfortunately don't have a timeframe for the solution.

FelipeCarrillo commented 1 year ago

Can I pull data with ncdc2 instead?

FelipeCarrillo commented 1 year ago

Thanks for checking into it. Let me know if you find a workaround. These data is very important as I need to update a fish model soon to predict salmon returns to the PWS area for commercial fishing. Thanks

djhocking commented 1 year ago

So I think it should work for you if you since you have a vector of station ID. Give your code a try like this:

lat_lon_df <- data.frame(id = "pw",
                         lat = 60.690545,
                         lon = -147.097055)

mon_near_pw <-
  meteo_nearby_stations(
    lat_lon_df = lat_lon_df,
    lat_colname = "lat",
    lon_colname = "lon",
    var = "PRCP",
    year_min = 2011,
    year_max = 2021,
    limit = 20,
  )

rnoaa:::ncdc2(datasetid='daily-summaries', 
      datatypeid = 'PRCP', 
      stationid = mon_near_pw$pw$id[c(1,3,11,14)], 
      startdate = '2011-01-01', 
      enddate = '2013-12-01')$data |> 
  dplyr::rename(id = station) |> 
  dplyr::mutate(prcp = prcp * 10)

This code renames things to match your old code and converts the precipitation values back to the same units as from the old code I believe.

Very neat project. I actually work for NMFS and not on the weather-side of NOAA.

FelipeCarrillo commented 1 year ago

Trying it. Will let you know if it works. Great!!! I work for USFWS and am a fish head :)

djhocking commented 1 year ago

Yes the three colons allow you to use a non-exported function.

Sent from a handheld device.

On Oct 29, 2022, at 5:50 PM, Felipe @.***> wrote:

 I went ahead and reinstalled rnoaa: @.***_api") But I am getting the following error message: Error: 'ncdc2' is not an exported object from 'namespace:rnoaa'

Did you intentionally are calling ncdc2 like this: rnoaa:::ncdc2() or is a typo?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were assigned.

FelipeCarrillo commented 1 year ago

Works great. I can pull data now up to December 2021.

FelipeCarrillo commented 1 year ago

I also noticed that the NOAA temperature API is not updating. https://www.ndbc.noaa.gov/histsearch.php?station=46061&year=2021&f1=wtmp&t1a=lt&v1a=100&t1b=&v1b=&c1=&f2=&t2a=&v2a=&t2b=&v2b=&c2=&f3=&t3a=&v3a=&t3b=&v3b= It stops at 2021 (see screenshot). Is this related to ncdc or is a total different issue? noaa