robwschlegel / heatwaveR

This GitHub repo contains all of the code for the heatwaveR package.

Home Page:https://robwschlegel.github.io/heatwaveR/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when downloading grid data

lucolotto opened this issue · comments

Hi,
First of all thanks for your super useful package.
I was trying to download the temperatures for a subset of the mediterranean basin following the download and prepare OISST data vignette.
The code I use is the following:

# Coordinates:
Latitude=c(42.41, 45.77)
Longitude=c(12.1,17.29)

ExtraSpace=.5
LatitudeYLim=c(Latitude[1]-ExtraSpace, Latitude[2]+ExtraSpace)
LongitudeXLim=c(Longitude[1]-ExtraSpace,Longitude[2]+ExtraSpace)


# Date intervals:
dl_years <- data.frame(date_index = 1:5,
                       start = as.Date(c("1982-01-01", "1990-01-01", 
                                         "1998-01-01", "2006-01-01", "2014-01-01")),
                       end = as.Date(c("1989-12-31", "1997-12-31", 
                                       "2005-12-31", "2013-12-31", "2021-12-31")))

rerddap::info(datasetid = "ncdcOisst21Agg_LonPM180", url = "https://coastwatch.pfeg.noaa.gov/erddap/")

# Note that there is also a version with lon values from 0 yo 360
rerddap::info(datasetid = "ncdcOisst21Agg", url = "https://coastwatch.pfeg.noaa.gov/erddap/")

OISST_sub_dl <- function(time_df){
  OISST_dat <- griddap(x = "ncdcOisst21Agg_LonPM180", 
                       url = "https://coastwatch.pfeg.noaa.gov/erddap/", 
                       time = c(time_df$start, time_df$end), 
                       zlev = c(0, 0),
                       latitude = Latitude,
                       longitude = Longitude,
                       fields = "sst")$data %>% 
    mutate(time = as.Date(stringr::str_remove(time, "T00:00:00Z"))) %>% 
    dplyr::rename(t = time, temp = sst) %>% 
    select(lon, lat, t, temp) %>% 
    na.omit()
}

system.time(
  OISST_data <- dl_years %>% 
    group_by(date_index) %>% 
    group_modify(~OISST_sub_dl(.x)) %>% 
    ungroup() %>% 
    select(lon, lat, t, temp)
)

When I execute the last code line, I get the following error:

Error in curl::curl_fetch_disk(x$url$url, x$disk, handle = x$url$handle) : Empty reply from server

Could you help me figuring out what is causing the error? I should also add that I used to use this code about a year ago and it worked like a charm.
Thanks in advance for your support
All the best
Luca

Hello Luca,
The issue appears to be that you are providing the Latitude and Longitude objects to the OISST_sub_dl() function, rather than the LatitudeYLim and LongitudeXLim objects you created to give the grid dimensions for downloaded. When you change those two lines the code runs as expected:

OISST_sub_dl <- function(time_df){
  OISST_dat <- griddap(x = "ncdcOisst21Agg_LonPM180", 
                       url = "https://coastwatch.pfeg.noaa.gov/erddap/", 
                       time = c(time_df$start, time_df$end), 
                       zlev = c(0, 0),
                       latitude = LatitudeYLim, # Change here
                       longitude = LongitudeXLim, # And here
                       fields = "sst")$data %>% 
    mutate(time = as.Date(stringr::str_remove(time, "T00:00:00Z"))) %>% 
    dplyr::rename(t = time, temp = sst) %>% 
    select(lon, lat, t, temp) %>% 
    na.omit()
}

All the best,
-Robert

Hi Robert,
Thanks, with your suggested edit it works!

Thanks a lot
Luca