Skip to content

Commit

Permalink
V4.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
imohamme committed Jan 14, 2024
1 parent e2707e1 commit 946dc00
Show file tree
Hide file tree
Showing 24 changed files with 795 additions and 508 deletions.
16 changes: 8 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Package: NASAaccess
Version: 3.4.3
Date: 2023-04-19
Version: 4.0.0
Date: 2024-01-11
Type: Package
Title: Downloading and Reformatting Tool for NASA Earth Observation Data Products
Authors@R: c(person("Ibrahim", "Mohammed", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-6542-319X"), email = "[email protected]"))
Maintainer: Ibrahim Mohammed <[email protected]>
Depends: R (>= 4.0)
Description: The package assumes that users have already set up a registration account(s) with Earthdata login as well as authorizing NASA GES DISC data access. Please refer to <https://disc.gsfc.nasa.gov/data-access> for further details. Creating the '.netrc' file at the user machine Home directory and storing the user NASA GES DISC logging information in it is done automatically to execute the NASAaccess package commands. The GES DISC user registration access logging information will be processed by masking in the terminal on any major OS. Without providing GES DISC user registration access logging information, the user will receive 'You need to provide your Earthdata GES DISC login to proceed…' message.
Description: Users need to set up a registration account(s) with Earthdata login as well as authorizing NASA GES DISC data access to generate gridded ascii tables of climate and earth observation remote sensing data. Please refer to <https://disc.gsfc.nasa.gov/information/documents?title=Data%20Access> for further details. Creating the '.netrc' file at the user machine Home directory and storing the user NASA GES DISC logging information in it is done automatically to execute the NASAaccess package commands. The GES DISC user registration access logging information will be processed by masking in the terminal on any major OS. Without providing GES DISC user registration access logging information, the user will receive 'You need to provide your Earthdata GES DISC login to proceed…' message.
URL: https://github.com/nasa/NASAaccess
BugReports: https://github.com/nasa/NASAaccess/issues
Imports: ncdf4, stats, shapefiles, rgeos, maptools, httr, stringr, rgdal, XML, sp, utils, raster, methods, getPass
Imports: ncdf4, stats, httr, stringr, XML, utils, methods, getPass, terra
License: file LICENSE
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3
RoxygenNote: 7.3.0
Suggests:
markdown,
rmarkdown,
knitr,
ggmap,
sf,
ggplot2,
codetools
codetools,
leaflet,
tidyterra
VignetteBuilder: knitr
12 changes: 1 addition & 11 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,9 @@ export(NEX_GDDP_CMIP6)
import(XML)
import(getPass)
import(httr)
import(maptools)
import(methods)
import(ncdf4)
import(rgdal)
import(rgeos)
import(shapefiles)
import(sp)
import(stringr)
import(terra, except = c('tail','head'))
import(utils)
importFrom(raster,cellFromPolygon)
importFrom(raster,extract)
importFrom(raster,raster)
importFrom(raster,rowColFromCell)
importFrom(raster,xyFromCell)
importFrom(rgeos,gCentroid)
importFrom(stats,na.exclude)
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# NASAaccess 4.0.0
* Removing raster, rgeos, regal, maptools and transition to terra!
* Fixing CMIP6 server access

# NASAaccess 3.4.3
* Adding DOI (10.5281/zenodo.8422392).

# NASAaccess 3.4.3
* Description edits.

Expand Down
88 changes: 46 additions & 42 deletions R/GLDASpolyCentroid.R

Large diffs are not rendered by default.

102 changes: 71 additions & 31 deletions R/GLDASwat.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
###3/11/19
#' Generate SWAT air temperature input files as well as air temperature stations file from NASA GLDAS remote sensing products.
###12/28/23
#' SWAT air temperature data from NASA GLDAS
#'
#' This function downloads remote sensing data of \acronym{GLDAS} from \acronym{NASA} \acronym{GSFC} servers, extracts air temperature data from grids within a specified watershed shapefile, and then generates tables in a format that \acronym{SWAT} requires for minimum and maximum air temperature data input. The function also generates the air temperature stations file input (file with columns: ID, File NAME, LAT, LONG, and ELEVATION) for those selected grids that fall within the specified watershed.
#' @param Dir A directory name to store gridded air temperature and air temperature stations files.
#' @param watershed A study watershed shapefile spatially describing polygon(s) in a geographic projection sp::CRS('+proj=longlat +datum=WGS84').
#' @param DEM A study watershed digital elevation model raster in a geographic projection sp::CRS('+proj=longlat +datum=WGS84').
#' @param watershed A study watershed shapefile spatially describing polygon(s) in a geographic projection crs='+proj=longlat +datum=WGS84'.
#' @param DEM A study watershed digital elevation model raster in a geographic projection crs='+proj=longlat +datum=WGS84'.
#' @param start Beginning date for gridded air temperature data.
#' @param end Ending date for gridded air temperature data.
#' @details A user should visit \url{https://disc.gsfc.nasa.gov/data-access} to register with the Earth Observing System Data and Information System (\acronym{NASA Earthdata}) and then authorize \acronym{NASA} GESDISC Data Access to successfully work with this function. The function accesses \acronym{NASA} Goddard Space Flight Center server address for \acronym{GLDAS} remote sensing data products at (\url{https://hydro1.gesdisc.eosdis.nasa.gov/data/GLDAS/GLDAS_NOAH025_3H.2.1/}). The function uses variable name ('Tair_f_inst') for air temperature in \acronym{GLDAS} data products. Units for gridded air temperature data are degrees in 'K'. The \command{GLDASwat} function outputs gridded air temperature (maximum and minimum) data in degrees 'C'.
#' @details A user should visit \url{https://disc.gsfc.nasa.gov/information/documents} Data Access document to register with the Earth Observing System Data and Information System (\acronym{NASA Earthdata}) and then authorize \acronym{NASA} GESDISC Data Access to successfully work with this function. The function accesses \acronym{NASA} Goddard Space Flight Center server address for \acronym{GLDAS} remote sensing data products at (\url{https://hydro1.gesdisc.eosdis.nasa.gov/data/GLDAS/GLDAS_NOAH025_3H.2.1/}). The function uses variable name ('Tair_f_inst') for air temperature in \acronym{GLDAS} data products. Units for gridded air temperature data are degrees in 'K'. The \command{GLDASwat} function outputs gridded air temperature (maximum and minimum) data in degrees 'C'.
#'
#' The goal of the Global Land Data Assimilation System \acronym{GLDAS} is to ingest satellite and ground-based observational data products, using advanced land surface modeling and data assimilation techniques, in order to generate optimal fields of land surface states and fluxes (Rodell et al., 2004). \acronym{GLDAS} dataset used in this function is the \acronym{GLDAS} Noah Land Surface Model L4 3 hourly 0.25 x 0.25 degree V2.1. The full suite of \acronym{GLDAS} datasets is available at \url{https://hydro1.gesdisc.eosdis.nasa.gov/dods/}. The \command{GLDASwat} finds the minimum and maximum air temperatures for each day at each grid within the study watershed by searching for minima and maxima over the three hours air temperature data values available for each day and grid.
#'
Expand All @@ -20,19 +20,18 @@
#' @keywords NASA GLDAS Air Temperature
#' @return A table that includes points ID, Point file name, Lat, Long, and Elevation information formatted to be read with \acronym{SWAT}, and
#' a scalar of maximum and minimum air temperature gridded data values at each point within the study watershed in ascii format needed by \acronym{SWAT} model weather inputs will be stored at \code{Dir}.
#' @references Derber, J. C., D. F. Parrish, and S. J. Lord (1991), The New Global Operational Analysis System at the National Meteorological Center, Weather Forecast, 6, 538-547, \cr
#' doi:10.1175/1520-0434(1991)006<0538:tngoas>2.0.co;2.
#' @references Adler, R. F., G. J. Huffman, A. Chang, R. Ferraro, P.-P. Xie, J. Janowiak, B. Rudolf, U. Schneider, S. Curtis, D. Bolvin, A. Gruber, J. Susskind, P. Arkin, and E. Nelkin (2003), The Version-2 Global Precipitation Climatology Project (GPCP) Monthly Precipitation Analysis (1979–Present), J. Hydrometeorol., 4, 1147-1167, doi:10.1175/1525-7541(2003)004<1147:tvgpcp>2.0.co;2.
#' @references Derber, J. C., D. F. Parrish, and S. J. Lord (1991), The New Global Operational Analysis System at the National Meteorological Center, Weather Forecast, 6, 538-547, doi:10.1175/1520-0434(1991)006<0538:tngoas>2.0.co;2.
#' @references Rodell, M., P. R. Houser, U. Jambor, J. Gottschalck, K. Mitchell, C.-J. Meng, K. Arsenault, B. Cosgrove, J. Radakovich, M. Bosilovich, J. K. Entin*, J. P. Walker, D. Lohmann, and D. Toll (2004), The Global Land Data Assimilation System, B. Am. Meteorol. Soc., 85, 381-394, doi:10.1175/bams-85-3-381.
#' @examples
#' #Lower Mekong basin example
#' \dontrun{GLDASwat(Dir = "./SWAT_INPUT/", watershed = "LowerMekong.shp",
#' DEM = "LowerMekong_dem.tif", start = "2015-12-1", end = "2015-12-3")}
#' @import ncdf4 httr stringr rgdal XML utils sp getPass
#' @import ncdf4 httr stringr utils XML getPass
#' @importFrom stats na.exclude
#' @importFrom raster raster cellFromPolygon xyFromCell rowColFromCell extract
#' @export


GLDASwat=function(Dir='./SWAT_INPUT/', watershed ='LowerMekong.shp', DEM = 'LowerMekong_dem.tif', start = '2015-12-1', end = '2015-12-3')
{
if(file.exists('~/.netrc')==FALSE)
Expand All @@ -54,10 +53,11 @@ GLDASwat=function(Dir='./SWAT_INPUT/', watershed ='LowerMekong.shp', DEM = 'Lowe
time_period <- seq.Date(from = as.Date(start), to = as.Date(end), by = 'day')

# Reading cell elevation data (DEM should be in geographic projection)
watershed.elevation <- raster::raster(DEM)
watershed.elevation <- terra::rast(DEM)

# Reading the study Watershed shapefile
suppressWarnings(polys <- rgdal::readOGR(dsn=watershed,verbose = F))

polys <- terra::vect(watershed)

# SWAT climate 'air temperature' master file name
filenametableKEY<-paste(Dir,'temp_Master.txt',sep='')
Expand Down Expand Up @@ -95,22 +95,45 @@ GLDASwat=function(Dir='./SWAT_INPUT/', watershed ='LowerMekong.shp', DEM = 'Lowe
nc.long<-ncdf4::ncvar_get(nc,nc$dim[[lon.dim.number]])
####getting the x values (latitudes in degrees north)
nc.lat<-ncdf4::ncvar_get(nc,nc$dim[[lat.dim.number]])
#extract data
data<-matrix(as.vector(ncdf4::ncvar_get(nc,nc$var[[33]])),nrow=length(nc.lat),ncol=length(nc.long),byrow=T)
#reorder the rows
data<-data[ nrow(data):1, ]
# create a raster
GLDAS<-terra::rast(nrows=length(nc.lat),
ncols=length(nc.long),
xmin=nc.long[1],
xmax=nc.long[NROW(nc.long)],
ymin=nc.lat[1],
ymax=nc.lat[NROW(nc.lat)],
crs='+proj=longlat +datum=WGS84')

#fill raster with dummy values

values(GLDAS) <- 1:ncell(GLDAS)
# Convert raster to points
GLDAS.points <- terra::as.points(GLDAS, na.rm = TRUE)

# Intersect to keep only points on the shape
GLDAS.points <- GLDAS.points[polys]



ncdf4::nc_close(nc)
###save the daily climate data values in a raster
GLDAS<-raster::raster(x=data,xmn=nc.long[1],xmx=nc.long[NROW(nc.long)],ymn=nc.lat[1],ymx=nc.lat[NROW(nc.lat)],crs=sp::CRS('+proj=longlat +datum=WGS84'))

#obtain cell numbers within the GLDAS raster
cell.no<-raster::cellFromPolygon(GLDAS, polys)

cell.no <- terra::cells(GLDAS, GLDAS.points)[,2]

#obtain lat/long values corresponding to watershed cells
cell.longlat<-raster::xyFromCell(GLDAS,unlist(cell.no))
cell.rowCol <- raster::rowColFromCell(GLDAS,unlist(cell.no))
points_elevation<-raster::extract(x=watershed.elevation,y=cell.longlat,method='simple')
study_area_records<-data.frame(ID=unlist(cell.no),cell.longlat,cell.rowCol,Elevation=points_elevation)
#sp::coordinates (study_area_records)<- ~x+y
rm(data,GLDAS)

cell.longlat<-terra::xyFromCell(GLDAS,cell.no)


cell.rowCol <- terra::rowColFromCell(GLDAS,cell.no)


points_elevation<-terra::extract(x=watershed.elevation,y=cell.longlat,method='simple')

study_area_records<-data.frame(ID=unlist(cell.no),cell.longlat,cell.rowCol,Elevation=points_elevation[,])

rm(GLDAS)
unlink(x='./temp', recursive = TRUE)


Expand Down Expand Up @@ -160,15 +183,32 @@ GLDASwat=function(Dir='./SWAT_INPUT/', watershed ='LowerMekong.shp', DEM = 'Lowe
utils::download.file(quiet = T,method='curl',url=paste(myurl,filenames[ll],sep = ''),destfile = paste('./temp/',filenames[ll],sep = ''), mode = 'wb', extra = '-n -c ~/.urs_cookies -b ~/.urs_cookies -L')
# Reading the ncdf file
nc<-ncdf4::nc_open( paste('./temp/',filenames[ll],sep = '') )
data<-matrix(as.vector(ncdf4::ncvar_get(nc,nc$var[[33]])),nrow=length(nc.lat),ncol=length(nc.long),byrow=T)
if(ll==1)
{
GLDAS<-terra::rast(nrows=length(nc.lat),
ncols=length(nc.long),
nlyrs=length(filenames),
xmin=nc.long[1],
xmax=nc.long[NROW(nc.long)],
ymin=nc.lat[1],
ymax=nc.lat[NROW(nc.lat)],
crs='+proj=longlat +datum=WGS84')
}
### Save the subdaily climate data values in a raster

GLDAS[[ll]] <- as.vector(ncdf4::ncvar_get(nc,nc$var[[33]]))


# Reorder the rows
data<-data[ nrow(data):1, ]
GLDAS[[ll]] <- terra::flip(GLDAS[[ll]],direction="v")


ncdf4::nc_close(nc)
### Save the subdaily climate data values in a raster
GLDAS<-raster::raster(x=data,xmn=nc.long[1],xmx=nc.long[NROW(nc.long)],ymn=nc.lat[1],ymx=nc.lat[NROW(nc.lat)],crs=sp::CRS('+proj=longlat +datum=WGS84'))

### Obtaining subdaily climate values at GLDAS grids that has been defined and explained earlier
cell.values<-as.vector(GLDAS)[study_area_records$ID]
SubdailyTemp[[ll]]<-cell.values

cell.values<-extract(GLDAS[[ll]],study_area_records$ID)
SubdailyTemp[[ll]]<-cell.values[,]

}

Expand All @@ -194,7 +234,7 @@ GLDASwat=function(Dir='./SWAT_INPUT/', watershed ='LowerMekong.shp', DEM = 'Lowe
#empty memory and getting ready for next day!
cell.temp.values<-list()
SubdailyTemp<- list()
rm(data,GLDAS)
rm(GLDAS)
unlink(x='./temp', recursive = TRUE)
}

Expand Down
Loading

0 comments on commit 946dc00

Please sign in to comment.