mikejohnson51 / climateR

An R 📦 for getting point and gridded climate data by AOI
https://mikejohnson51.github.io/climateR/
MIT License
170 stars 40 forks source link

getGridMET returns url with class "glue" "character" #89

Closed raruggie closed 9 months ago

raruggie commented 10 months ago

I have 10 watershed shapefiles in New Hampshire that I want to use in getGridMet. Depending on the watershed used, I get either data.frames, lists, or glue objects. Here is the reproducible example using the Rdata file located here on google drive (https://drive.google.com/file/d/1zETjrQ_IzLFHQTJubwRS-yNsyLzJ3Jmz/view?usp=drive_link):


library(terra)
library(dplyr)
# import data:

load("Processed_Data/glue_issue.Rdata")

# create SpatVect and sort:

vect.NH<-vect(df.ws)

vect.NH<-terra::sort(vect.NH, v = 'Site_Key')

# create spatRasters from NLCD raster layers

l.NLCD.2016.rast <- lapply(l.NLCD.2016, rast)

# reproject vector data to match NLCD:

vect.NH.proj<-terra::project(vect.NH, crs(l.NLCD.2016.rast[[1]]))

# look at individual plots of NLCD with watershed vector:

lapply(seq_along(l.NLCD.2016.rast), \(i) plot(l.NLCD.2016.rast[[i]], fun=function()lines(vect.NH.proj[i])))

# set up loop elements: (this workflow would be in a loop to go through all gridmet variables for all sites and dates)

i<-1
j<-1
k<-1

# loop and print classes:

for (i in seq_along(df.ws$Site_Key)){

  df.i<-dfHydroGrab%>%filter(Sample.Name == sort(df.ws$Site_Key)[i])

  rast.k<-getGridMET(vect.NH[i], varname = result_vector[k], startDate = df.i$DATETIME[j]-21, endDate = df.i$DATETIME[j])

  print(class(rast.k))
}

[1] "data.frame"
[1] "list"
[1] "list"
[1] "list"
[1] "data.frame"
[1] "list"
[1] "data.frame"
[1] "glue"      "character"
[1] "glue"      "character"
[1] "list"

I am thinking that the smaller watersheds are returning data.frame because the watershed only spans one GridMet cell? But I am not sure what is happening with the watersheds that are returning class glue.

Any help would be much appreciated. Thank you.

mikejohnson51 commented 9 months ago

Hi there! Thanks for reaching out,

Using the latest code I am not seeing this issue. Can you please confirm?

library(dplyr)
library(terra)
library(climateR)

load("~/Downloads/glue_issue.Rdata")

 j<-1; k<-1

# loop and print classes:

for (i in seq_along(df.ws$Site_Key)){

  df.i <- filter(dfHydroGrab, Sample.Name == df.ws$Site_Key[i])

  rast.k<-getGridMET(terra::vect(df.ws[i,]), 
                     varname = result_vector[k], 
                     startDate = df.i$DATETIME[j]-21, 
                     endDate = df.i$DATETIME[j])

  print(class(rast.k))
}
#> [1] "data.frame"
#> [1] "data.frame"
#> [1] "list"
#> [1] "list"
#> [1] "data.frame"
#> [1] "list"
#> [1] "list"
#> [1] "list"
#> [1] "list"
#> [1] "list"

Created on 2023-12-18 by the reprex package (v2.0.1)

raruggie commented 9 months ago

I needed to load the sf library for it to run but I am still getting glue class:

library(dplyr)
library(terra)
library(sf)
library(climateR)

load("C:/PhD/FOR797/FOR797_git/Processed_Data/glue_issue.Rdata")

j<-1; k<-1

# loop and print classes:

for (i in seq_along(df.ws$Site_Key)){

  df.i <- filter(dfHydroGrab, Sample.Name == df.ws$Site_Key[i])

  rast.k<-getGridMET(terra::vect(df.ws[i,]), 
                     varname = result_vector[k], 
                     startDate = df.i$DATETIME[j]-21, 
                     endDate = df.i$DATETIME[j])

  print(class(rast.k))
}
[1] "data.frame"
[1] "data.frame"
[1] "list"
[1] "glue"      "character"
[1] "data.frame"
[1] "list"
[1] "list"
[1] "glue"      "character"
[1] "list"
[1] "list"
mikejohnson51 commented 9 months ago

Hi - sorry for the continued issue. What does packageVersion("climateR") give you? It should be 0.3.2 if recently installed.

mikejohnson51 commented 9 months ago

I will consider this closed for now - please feel free to re-open!