Skip to content

Commit 478a884

Browse files
1. CRS added to group. 2. netCDF updating data error fixed.
1 parent 8fc8c79 commit 478a884

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

R/makeNetCDF_file.R

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
#' updated with new data to \code{updateFrom}. The default is two days ago as YYYY-MM-DD.
3030
#' @param vars is a vector of variables names to build or update. The available variables are: daily precipitation,
3131
#' daily minimum temperature, daily maximum temperature, daily 3pm vapour pressure grids and daily solar radiation.
32-
#' Any or all of the defaults are available. The default is \code{c('precip', 'tmin', 'tmax', 'vprp', 'solarrad')} and
33-
#' provided by \code{rownames(get.variableSource())}.
32+
#' Any or all of the defaults are available. If \code{vars=''} and the netCDF does not exist, then the default is
33+
#' \code{c('precip', 'tmin', 'tmax', 'vprp', 'solarrad')} and provided by \code{rownames(get.variableSource())}.
34+
#' However, if \code{vars=''} and the netCDF file does exist, then default is to use the variable names in the file.
3435
#' @param workingFolder is the file path (as string) in which to download the AWAP grid files. The default is \code{getwd()}.
3536
#' @param keepFiles is a logical scalar to keep the downloaded AWAP grid files. The default is \code{FALSE}.
3637
#' @param compressionLevel is the netCDF compression level between 1 (low) and 9 (high), and \code{NA} for no compression.
@@ -76,7 +77,7 @@ makeNetCDF_file <- function(
7677
ncdfFilename=file.path(getwd(),'AWAP.nc'),
7778
updateFrom = as.Date("1900-01-01","%Y-%m-%d"),
7879
updateTo = as.Date(Sys.Date()-2,"%Y-%m-%d"),
79-
vars = rownames(get.variableSource()),
80+
vars = '',
8081
workingFolder=getwd(),
8182
keepFiles=FALSE,
8283
compressionLevel = 5,
@@ -94,6 +95,17 @@ makeNetCDF_file <- function(
9495
if (!is.character(ncdfFilename))
9596
stop('ncdfFilename is invalid. It must be a character string for the file name.')
9697

98+
if (!is.character(vars))
99+
stop('vars must be a character vector of variables names.')
100+
101+
# If vars is empty, then set the defaults.
102+
if (length(vars)==1 && vars=='') {
103+
if (file.exists(ncdfFilename))
104+
vars = rownames(AWAPer::file.summary(ncdfFilename))
105+
else
106+
vars = rownames(get.variableSource())
107+
}
108+
97109
# Check the input variables
98110
vars.all = get.variableSource()
99111
vars.all.names = rownames(vars.all)
@@ -175,6 +187,7 @@ makeNetCDF_file <- function(
175187
SWLong = rep(NA,nvars), SWLat = rep(NA,nvars),
176188
DPixel = rep(NA,nvars), nodata= rep(NA,nvars),
177189
has.geom = rep(F,nvars),
190+
ellipsoid.crs = rep(NA,nvars),
178191
row.names = vars)
179192
message('... Testing downloading of each variable.')
180193
for (ivar in vars.2modify) {
@@ -190,6 +203,9 @@ makeNetCDF_file <- function(
190203
gridgeo[ivar,]$DPixel <- headerData$DPixel
191204
gridgeo[ivar,]$nodata <- headerData$nodata
192205
gridgeo[ivar,]$has.geom = TRUE;
206+
207+
# Add ellipsoid CRS from get.variableSource()
208+
gridgeo[ivar,]$ellipsoid.crs = vars.all[ivar,]$ellipsoid.crs
193209
}
194210

195211
# Identify the unique grid dimensions and assign grid
@@ -251,7 +267,10 @@ makeNetCDF_file <- function(
251267

252268
timeVec = 0:(length(timepoints)-1)
253269

254-
grid.dims[[i]] = list(long = longVec, lat = latVec, t = timeVec)
270+
grid.dims[[i]] = list(long = longVec,
271+
lat = latVec,
272+
t = timeVec,
273+
crs = gridgeo.unique[i,]$ellipsoid.crs)
255274
}
256275

257276

@@ -291,12 +310,12 @@ makeNetCDF_file <- function(
291310
# Add new group
292311
grp = RNetCDF::grp.def.nc(ncout, names(grid.dims)[i])
293312

294-
# Add CRS attribute to group
313+
# Add ellipoid CRS to the grid
295314
RNetCDF::att.put.nc(grp,
296315
variable = 'NC_GLOBAL',
297316
name = "CRS",
298317
type = "NC_CHAR",
299-
value = '+proj=longlat +ellps=GRS80')
318+
value = grid.dims[[i]]$crs)
300319

301320
# Add time dimension to group. Variable for dim also added
302321
# to allow attributes.
@@ -464,7 +483,8 @@ makeNetCDF_file <- function(
464483
names(ind) = vars.2update
465484
for (ivar in vars.2update) {
466485
if (updateFrom >= vars.summary[ivar,]$from &&
467-
updateTo <= vars.summary[ivar,]$to)
486+
updateTo <= vars.summary[ivar,]$to &&
487+
!(any(ivar %in% vars)))
468488
ind[ivar] = F
469489
}
470490
vars.2update = vars.2update[ind]

0 commit comments

Comments
 (0)