Skip to content

Commit

Permalink
nctoclimarray function removed as mcera5 packge forked and pull reque…
Browse files Browse the repository at this point in the history
…st issued to enable mcera5 package to provide data in correct format for microclimf
  • Loading branch information
ilyamaclean committed Sep 21, 2024
1 parent 1a3b810 commit ac2c69e
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 118 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export(clumpestimate)
export(leafrfromalb)
export(modelin)
export(mosaicblend)
export(nctoclimarray)
export(runbioclim)
export(runmicro)
export(runmicro_big)
Expand Down
78 changes: 0 additions & 78 deletions R/dataprep.R
Original file line number Diff line number Diff line change
Expand Up @@ -1024,84 +1024,6 @@ leafrfromalb<-function(pai, x, alb, ltrr = 0.5) {
lref<-mask(lref,pai)
return(list(leafr=lref,leaft=ltrr*lref,gref=.rast(gref,pai)))
}
#' Create climate arrays for inputting to microclimate model from a netCDF4 file
#'
#' @description The function `nctoclimarray` converts data in a netCDF4 file returned
#' by [mcera5::request_era5()] to the correct formal required for running [modelina()] or
#' [modelina_dy()].
#'
#' @param ncfile character vector containing the path and filename of the nc file
#' @param dtm a SpatRaster object of elevations covering the extent of the study area (see details)
#' @param dtr_cor_fac numeric value to be used in the diurnal temperature range
#' correction of coastal grid cells. Default = 1.285, based on calibration against UK Met Office
#' observations. If set to zero, no correction is applied.
#' @return a list of the following:
#' \describe{
#' \item{tme}{POSIXlt object of times corresponding to climate observations}
#' \item{climarray}{a list of arrays of hourly weather variables - same format as for [modelina()].}
#' \item{precarray}{an array of daily precipitation values - same format as for [modelina()].}
#' \item{dtmc}{a coarse resolution digital elevation dataset matching the resolution of input
#' climate data, but with a coordinate reference system and extent matching `dtm`}
#' }
#' @export
#' @import ncdf4
#' @details the model requires that input climate data are projected using a coordinate reference
#' system in which x and y are in metres. Since values returned by [mcera5::request_era5()]
#' are in lat long, the output data are reprojected using the coordinate reference system and
#' extent of dtm (but retain the approximate original grid resolution of the input climate data).
#' Returned climate data match the resolution, coordinate reference system and extent of `dtmc`.
nctoclimarray <- function(ncfile, dtm, dtr_cor_fac = 1.285) {
t2m<-rast(ncfile,subds = "t2m") # Air temperature (K)
d2m<-rast(ncfile,subds = "d2m") # Dewpoint temperature (K)
sp<-rast(ncfile,subds = "sp") # Surface pressure (Pa)
u10<-rast(ncfile,subds = "u10") # U-wind at 10m (m/s)
v10<-rast(ncfile,subds = "v10") # V-wind at 10m (m/s)
tp<-rast(ncfile,subds = "tp") # Total precipitation (m)
msnlwrf<-rast(ncfile,subds = "msnlwrf") # Mean surface net long-wave radiation flux (W/m^2)
msdwlwrf<-rast(ncfile,subds = "msdwlwrf") # Mean surface downward long-wave radiation flux (W/m^2)
fdir<-rast(ncfile,subds = "fdir") # Total sky direct solar radiation at surface (W/m^2)
ssrd<-rast(ncfile,subds = "ssrd") # Surface short-wave (solar) radiation downwards (W/m^2)
lsm<-rast(ncfile,subds = "lsm") # Land sea mask
# Create coarse-resolution dtm to use as template for resampling
te<-terra::project(t2m[[1]],crs(dtm))
agf<-res(te)[1]/res(dtm)[1]
dtmc<-aggregate(dtm,fact=agf,fun=mean,na.rm=T)
# Apply coastal correction to temperature data
tmn<-.ehr(.hourtoday(as.array(t2m)-273.15,min))
mu<-(1-as.array(lsm))*dtr_cor_fac+1
tc<-.rast(((as.array(t2m)-273.15)-tmn)*mu+tmn,t2m)
# Calculate vapour pressure
ea<-.rast(.satvap(as.array(d2m)-273.15),t2m)
# Resample all variables to match dtmc
tc<-terra::project(tc,dtmc)
ea<-terra::project(ea,dtmc)
sp<-terra::project(sp,dtmc)
u10<-terra::project(u10,dtmc)
v10<-terra::project(v10,dtmc)
tp<-terra::project(tp,dtmc)
msnlwrf<-terra::project(msnlwrf,dtmc)
msdwlwrf<-terra::project(msdwlwrf,dtmc)
fdir<-terra::project(fdir,dtmc)
ssrd<-terra::project(ssrd,dtmc)
# Derive varies
temp<-as.array(tc) # Temperature (deg c)
relhum<-(as.array(ea)/.satvap(temp))*100 # Relative humidity (%)
pres<-as.array(sp)/1000 # Surface pressure (kPa)
swrad<-as.array(ssrd)/3600 # Downward shortwave radiation (W/m^2)
difrad<-swrad-as.array(fdir)/3600 # Downward diffuse radiation (W/m^2)
skyem<-as.array(msdwlwrf)/as.array(msdwlwrf-msnlwrf) # sky emissivity (0-1)
windspeed<-sqrt(as.array(u10)^2+as.array(v10)^2)*log(67.8*2-5.42)/log(67.8*10-5.42) # Wind speed (m/s)
winddir<-as.array((terra::atan2(u10,v10)*180/pi+180)%%360) # Wind direction (deg from N - from)
precarray<-.hourtoday(as.array(tp)*1000,sum)
# Save lists
climarray<-list(temp=temp,relhum=relhum,pres=pres,swrad=swrad,difrad=difrad,
skyem=skyem,windspeed=windspeed,winddir=winddir)
# Generate POSIXlt object of times
tme<-as.POSIXlt(time(t2m),tz="UTC")
# Output for returning
out<-list(tme=tme,climarray=climarray,precarray=precarray,dtmc=dtmc)
return(out)
}
#' Writes model output as ncdf4 file
#'
#' @description The function `writetonc` writes hourly model outputs as an ncdf4 file to a specified file
Expand Down
39 changes: 0 additions & 39 deletions man/nctoclimarray.Rd

This file was deleted.

0 comments on commit ac2c69e

Please sign in to comment.