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

Avoid reading empty Json objects [no version bump] #546

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions DataCleaningScripts/NDVI.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ def get_scenes(dataset="landsat_ot_c2_l2", latitude=31.9279, longitude=-109.0929
scene_path = os.path.normpath(scene_file)

with open(scene_path, mode='w') as rd:
headers = list(scenes[0].keys())
writer = csv.DictWriter(rd, fieldnames=headers)
writer.writeheader()
for scene in scenes:
writer.writerow(scene)
entity_ids.append(scene['display_id'].strip())
if scenes:
headers = list(scenes[0].keys())
writer = csv.DictWriter(rd, fieldnames=headers)
writer.writeheader()
for scene in scenes:
writer.writerow(scene)
entity_ids.append(scene['display_id'].strip())

api.logout()
print(entity_ids)
Expand Down
65 changes: 42 additions & 23 deletions DataCleaningScripts/get_regional_weather.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
library(httr)
library(jsonlite)
library(dplyr)
library(lubridate)

# Explicitly assign magrittr pipe operator
`%>%` <- magrittr::`%>%`

#' Compiles all regional weather data with the data from
Expand All @@ -6,43 +12,56 @@
#' @example regional_wunder(stationid)

regional_wunder <- function(stationid) {
tryCatch({
# Construct the URL
url <- paste('https://api.weather.com/v2/pws/observations/hourly/7day?stationId=', stationid, '&format=json&units=m&apiKey=', Sys.getenv("WU_API_KEY"), sep="")

# Make the GET request
response <- httr::GET(url)

# Check for status 204 No Content
if (httr::status_code(response) == 204) {
message(paste('No content available for station', stationid))
return(NULL)
}

# Parse the JSON content
rodeo <- jsonlite::fromJSON(httr::content(response, as = "text"))$observations

tryCatch(
{
rodeo <- jsonlite::fromJSON(
paste('https://api.weather.com/v2/pws/observations/hourly/7day?stationId=',stationid,'&format=json&units=m&apiKey=',Sys.getenv("WU_API_KEY"), sep=""))$observations
# Process the data
rodeo <- dplyr::bind_cols(rodeo[,1:14], rodeo$metric) %>%
dplyr::rename_all(.funs = tolower) %>%
dplyr::select(-epoch,-obstimeutc) %>%
dplyr::slice(1:(dplyr::n()-2)) %>%
dplyr::select(-epoch, -obstimeutc) %>%
dplyr::slice(1:(dplyr::n() - 2)) %>%
dplyr::rename(timestamp = obstimelocal, latitude = lat, longitude = lon) %>%
dplyr::mutate_all(as.character) %>%
dplyr::mutate_all(as.character) %>%
dplyr::mutate_at(tail(names(.), 32), as.numeric) %>%
dplyr::mutate(timestamp = round(lubridate::ymd_hms(timestamp), units="hours")) %>%
dplyr::mutate(day = lubridate::day(timestamp),
month = lubridate::month(timestamp),
dplyr::mutate(timestamp = round(lubridate::ymd_hms(timestamp), units = "hours")) %>%
dplyr::mutate(day = lubridate::day(timestamp),
month = lubridate::month(timestamp),
year = lubridate::year(timestamp),
hour = lubridate::hour(timestamp)) %>%
dplyr::select(year, month, day, hour, timestamp, dplyr::everything())
#Fix hour and day so midnight=2400
rodeo$hour[rodeo$hour==0] = 24 ; rodeo$hour = 100*rodeo$hour
rodeo$day[rodeo$hour==2400] = rodeo$day[which(rodeo$hour==2400)-1]
rodeo$month[rodeo$hour==2400] = rodeo$month[which(rodeo$hour==2400)-1]
rodeo$year[rodeo$hour==2400] = rodeo$year[which(rodeo$hour==2400)-1]

return(rodeo)
},
error=function(e) {

# Fix hour and day so midnight = 2400
rodeo$hour[rodeo$hour == 0] <- 24
rodeo$hour <- 100 * rodeo$hour
rodeo$day[rodeo$hour == 2400] <- rodeo$day[which(rodeo$hour == 2400) - 1]
rodeo$month[rodeo$hour == 2400] <- rodeo$month[which(rodeo$hour == 2400) - 1]
rodeo$year[rodeo$hour == 2400] <- rodeo$year[which(rodeo$hour == 2400) - 1]

return(rodeo)
},
error = function(e) {
message(paste('error in', stationid))
print(e)
return(NULL)
},
warning=function(w) {
warning = function(w) {
message(paste('warning in', stationid))
print(w)
return(NULL)
}
)
})
}

#' Reports regional weather data with the data from Wunderground (www.wunderground.com) and
Expand Down Expand Up @@ -101,7 +120,7 @@ get_regional_weather <- function() {
new_rodeo <- dplyr::anti_join(rodeo, all_rodeo, by = c("year","month","day","hour","timestamp","stationid"))

return(list(new_4sw=new_4sw, new_sansimon=new_sansimon, new_rodeo=new_rodeo))

}

#' Appends new regional weather data
Expand Down
2 changes: 1 addition & 1 deletion install-packages.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Install pacman if it isn't already installed

install.packages(c("curl", "devtools", "dplyr", "EML", "git2r", "Hmisc", "htmltab", "jsonlite", "lubridate", "lunar", "lwgeom", "openxlsx", "raster", "Rcpp", "RCurl", "remotes", "rgdal", "semver", "sf", "shiny", "sp", "sqldf", "stringi", "stringr", "terra", "testthat", "tidyr", "units", "yaml", "zoo"))
install.packages(c("curl", "devtools", "dplyr", "EML", "git2r", "Hmisc", "httr", "htmltab", "jsonlite", "lubridate", "lunar", "lwgeom", "openxlsx", "raster", "Rcpp", "RCurl", "remotes", "rgdal", "semver", "sf", "shiny", "sp", "sqldf", "stringi", "stringr", "terra", "testthat", "tidyr", "units", "yaml", "zoo"))
remotes::install_github("htmltab/htmltab")