Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ramarty committed Dec 6, 2023
1 parent 9f257b6 commit e0ffa1a
Showing 1 changed file with 3 additions and 53 deletions.
56 changes: 3 additions & 53 deletions R/blackmarbler.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
# TODO
# 1. Cloud mask: https://ladsweb.modaps.eosdis.nasa.gov/api/v2/content/archives/Document%20Archive/Science%20Data%20Product%20Documentation/VIIRS_Black_Marble_UG_v1.1_July_2020.pdf
# file:///Users/robmarty/Downloads/Thesis_Zihao_Zheng.pdf

if(F){
library(readr)
library(purrr)
library(stringr)
library(hdf5r)
library(raster)
library(dplyr)
library(sf)
library(lubridate)
library(exactextractr)
library(httr)
library(progress)
}

#' Black Marble Tile Grid Shapefile
#' A dataset containing black marble grid tiles.
#'
Expand Down Expand Up @@ -93,17 +75,7 @@ file_to_raster <- function(f,
# ARGS
# --f: Filepath to h5 file

## Boundaries
# Only works on later years
#spInfo <- h5readAttributes(f,"/")

#xMin<-spInfo$WestBoundingCoord
#yMin<-spInfo$SouthBoundingCoord
#yMax<-spInfo$NorthBoundingCoord
#xMax<-spInfo$EastBoundingCoord

## Data
#h5_data <- H5Fopen(f)
h5_data <- h5file(f, "r+")

#### Daily
Expand Down Expand Up @@ -131,9 +103,6 @@ file_to_raster <- function(f,
))
}

#out <- h5_data$HDFEOS$GRIDS$VNP_Grid_DNB$`Data Fields`[[variable]]
#qf <- h5_data$HDFEOS$GRIDS$VNP_Grid_DNB$`Data Fields`$Mandatory_Quality_Flag

out <- h5_data[[paste0("HDFEOS/GRIDS/VNP_Grid_DNB/Data Fields/", variable)]][,]
qf <- h5_data[["HDFEOS/GRIDS/VNP_Grid_DNB/Data Fields/Mandatory_Quality_Flag"]][,]

Expand All @@ -149,9 +118,7 @@ file_to_raster <- function(f,

#### Monthly/Annually
} else{
#lat <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`$lat
#lon <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`$lon


lat <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lat"]][]
lon <- h5_data[["HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/lon"]][]

Expand All @@ -164,7 +131,6 @@ file_to_raster <- function(f,
))
}

#out <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`[[variable]]
out <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", variable)]][,]

if(length(quality_flag_rm) > 0){
Expand All @@ -177,7 +143,6 @@ file_to_raster <- function(f,

if(qf_name %in% var_names){

#qf <- h5_data$HDFEOS$GRIDS$VIIRS_Grid_DNB_2d$`Data Fields`[[paste0(variable, "_Quality")]]
qf <- h5_data[[paste0("HDFEOS/GRIDS/VIIRS_Grid_DNB_2d/Data Fields/", qf_name)]][,]

for(val in quality_flag_rm){ # out[qf %in% quality_flag_rm] doesn't work, so loop
Expand All @@ -189,7 +154,7 @@ file_to_raster <- function(f,
}

if(class(out[1,1])[1] != "numeric"){
out <- matrix(as.numeric(out), # Convert to numeric matrix
out <- matrix(as.numeric(out), # Convert to numeric matrix
ncol = ncol(out))
}

Expand Down Expand Up @@ -341,34 +306,19 @@ download_raster <- function(file_name,
day <- file_name %>% substring(14,16)
product_id <- file_name %>% substring(1,7)

#wget_command <- paste0('wget -e robots=off -m -np .html,.tmp -nH --cut-dirs=3 ',
# '"https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5000/',product_id,'/', year, '/', day, '/', file_name,'"',
# ' --header "Authorization: Bearer ',
# bearer,
# '" -P ',
# temp_dir)

url <- paste0('https://ladsweb.modaps.eosdis.nasa.gov/archive/allData/5000/',
product_id, '/', year, '/', day, '/', file_name)

headers <- c('Authorization' = paste('Bearer', bearer))
download_path <- file.path(temp_dir, file_name)

if(quiet == FALSE) message(paste0("Processing: ", file_name))

# progress_bar <- progress_bar$new(
# format = "[:bar] :percent ETA: :eta",
# total = 1,
# clear = FALSE
# )


response <- GET(url,
add_headers(headers),
write_disk(download_path, overwrite = TRUE),
progress())

#progress_bar$close()

if(response$status_code != 200){
message("Error in downloading data")
message(response)
Expand Down

0 comments on commit e0ffa1a

Please sign in to comment.