Skip to content

Commit 0dc1e50

Browse files
committed
Boston data and test/cod-cov added
1 parent 844894b commit 0dc1e50

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

R/calculate_column.R

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#' @import terra ncdf4
2323
#' @importFrom utils menu
2424
#'
25+
#' @examples
26+
#' file <- paste0(system.file("extdata",package="eva3dm"),"/wrf_column_o3_Boston.nc")
27+
#' O3_column <- calculate_column(file,'o3', verbose = TRUE)
28+
#'
2529
#' @export
2630
#'
2731

@@ -67,13 +71,13 @@ calculate_column <- function(file = file.choose(),
6771
for(i in 1:(dim(Z)[3]-1)){
6872
dz[,,i] = Z[,,i+1] - Z[,,i]
6973
}
70-
}else if(length(dim(Z)) == 4){ # this is a version for 4d (XYZT)
71-
dz <- array(0,c(dim(Z)[1],dim(Z)[2],dim(Z)[3]-1,dim(Z)[4]))
72-
for(i in 1:(dim(Z)[3]-1)){
73-
dz[,,i,] = Z[,,i+1,] - Z[,,i,]
74+
}else if(length(dim(Z)) == 4){ # this is a version for 4d (XYZT) # nocov
75+
dz <- array(0,c(dim(Z)[1],dim(Z)[2],dim(Z)[3]-1,dim(Z)[4])) # nocov
76+
for(i in 1:(dim(Z)[3]-1)){ # nocov
77+
dz[,,i,] = Z[,,i+1,] - Z[,,i,] # nocov
7478
}
7579
}else{
76-
stop('dimention not suported')
80+
stop('dimention not suported') # nocov
7781
}
7882
return(dz)
7983
}
@@ -86,10 +90,10 @@ calculate_column <- function(file = file.choose(),
8690
}
8791

8892
f4 <- function(x){
89-
x <- as.array(x)
90-
x <- aperm(x, c(2,1,3,4))
91-
x <- x[rev(seq_len(dim(x)[1])),,,]
92-
return(x)
93+
x <- as.array(x) # nocov
94+
x <- aperm(x, c(2,1,3,4)) # nocov
95+
x <- x[rev(seq_len(dim(x)[1])),,,] # nocov
96+
return(x) # nocov
9397
}
9498

9599
avo = 6.02E+23 # Avogadro s number
@@ -112,17 +116,17 @@ calculate_column <- function(file = file.choose(),
112116
flip_h = flip_v,
113117
verbose = verbose, ... )
114118
VAR <- rast_to_netcdf(r)
115-
}else if(length(dim(Temp)) == 4){
116-
sds <- wrf_sds(file = file,
117-
name = name,
118-
flip_v = flip_v,
119-
flip_h = flip_v,
120-
verbose = verbose, ... )
121-
VAR <- f4(sds)
122-
}else if(length(dim(Temp)) <= 2){
123-
stop('insuficient meteorological input') # less data than needed / include more data
119+
}else if(length(dim(Temp)) == 4){ # nocov
120+
sds <- wrf_sds(file = file, # nocov
121+
name = name, # nocov
122+
flip_v = flip_v, # nocov
123+
flip_h = flip_v, # nocov
124+
verbose = verbose, ... ) # nocov
125+
VAR <- f4(sds) # nocov
126+
}else if(length(dim(Temp)) <= 2){ # nocov
127+
stop('insuficient meteorological input') # less data than needed / include more data # nocov
124128
}else{
125-
stop('meteorological input not suported') # dimension not supported / reduce data
129+
stop('meteorological input not suported') # dimension not supported / reduce data # nocov
126130
}
127131

128132
P <- P1 + P2 # total pressure [pa]
@@ -138,26 +142,26 @@ calculate_column <- function(file = file.choose(),
138142
names(r) <- paste0(name,'_column')
139143
time(r) <- times_r[1]
140144
}
141-
if(length(dim(VAR)) == 4){ # XLAT XLONG LEVEL TIME
142-
times_r <- time(sds[,1])
143-
make_rast <- function(i = 1, x = sds){
144-
r <- sds[[i,]]
145-
r[] <- c(f3(VAR[,,,i],1))
146-
r <- sum(r, na.rm = TRUE)
147-
names(r) <- paste0(name,'_column')
148-
return(r)
149-
}
150-
r_list <- lapply(X = 1:length(times_r),FUN = make_rast)
151-
r <- terra::rast(r_list)
152-
time(r) <- times_r
145+
if(length(dim(VAR)) == 4){ # XLAT XLONG LEVEL TIME # nocov
146+
times_r <- time(sds[,1]) # nocov
147+
make_rast <- function(i = 1, x = sds){ # nocov
148+
r <- sds[[i,]] # nocov
149+
r[] <- c(f3(VAR[,,,i],1)) # nocov
150+
r <- sum(r, na.rm = TRUE) # nocov
151+
names(r) <- paste0(name,'_column') # nocov
152+
return(r) # nocov
153+
} # nocov
154+
r_list <- lapply(X = 1:length(times_r),FUN = make_rast) # nocov
155+
r <- terra::rast(r_list) # nocov
156+
time(r) <- times_r # nocov
153157
}
154158

155159
if(DU){
156160
du = 2.687E+16 # Conversion constant: molecules cm-2 to DU
157161
r <- r / du
158162
units(r) <- 'DU'
159163
}else{
160-
units(r) <- 'molecules cm-2'
164+
units(r) <- 'molecules cm-2' # nocov
161165
}
162166
return(r)
163167
}

R/wrf_sds.R

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@
1919
#' @import terra ncdf4
2020
#' @importFrom utils menu
2121
#'
22+
#' @examples
23+
#' file <- paste0(system.file("extdata",package="eva3dm"),"/wrf_4d_o3_Boston.nc")
24+
#' O34d <- wrf_sds(file,'o3',verbose = TRUE)
25+
#'
26+
#' # selecting one time, keeping multiple layers
27+
#' O34d[1,]
28+
#'
29+
#' # selecting one layer, keeping multiple times
30+
#' O34d[,1]
31+
#'
2232
#' @export
2333
#'
2434

@@ -88,11 +98,11 @@ wrf_sds <- function(file = file.choose(),
8898

8999
# Reverse column order to get UL in UL
90100
if(length(dim(inNCLon)) == 3){ # for special case of lat/lon has more dimensions
91-
x <- as.vector(inNCLon[, ncol(inNCLon):1,]) # nocov
92-
y <- as.vector(inNCLat[, ncol(inNCLat):1,]) # nocov
101+
x <- as.vector(inNCLon[, ncol(inNCLon):1,])
102+
y <- as.vector(inNCLat[, ncol(inNCLat):1,])
93103
}else{
94-
x <- as.vector(inNCLon[,ncol(inNCLon):1])
95-
y <- as.vector(inNCLat[,ncol(inNCLat):1])
104+
x <- as.vector(inNCLon[,ncol(inNCLon):1]) # nocov
105+
y <- as.vector(inNCLat[,ncol(inNCLat):1]) # nocov
96106
}
97107
rm(inNCLon,inNCLat)
98108

@@ -225,7 +235,7 @@ wrf_sds <- function(file = file.choose(),
225235
names(r) <- paste0(name,'_',1:length(TIME),'h')
226236

227237
mem_order <- ncdf4::ncatt_get(nc = wrf,varid = name, attname = 'MemoryOrder')$value
228-
if(mem_order %in% c('XY','XY ')) r <- terra::flip(r,direction ='horizontal')
238+
if(mem_order %in% c('XY','XY ')) r <- terra::flip(r,direction ='horizontal') # nocov
229239

230240
nc_close(wrf)
231241

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ The following workflow is recommended:
5656

5757
**4. Visualization**: try some of the visualization functions from this package or other packages.
5858

59-
The function `template()` can help to setup folders and scripts from steps 1-4 for different models, observations datasets and evaluation types.
59+
**The function `template()` can help to setup folders and scripts from steps 1-4 for different models, observations datasets and evaluation types.**
6060

6161
This package includes:
6262

inst/extdata/TEMPO_crop_Boston.nc

80.8 KB
Binary file not shown.

inst/extdata/wrf_4d_o3_Boston.nc

27.7 KB
Binary file not shown.
62.4 KB
Binary file not shown.

man/calculate_column.Rd

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/template.Rd

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/wrf_sds.Rd

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-rast.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ test_that("rast based functions are ok", {
3535

3636
plot_diff(r,r2)
3737

38+
O34d <- wrf_sds(paste0(system.file("extdata",package="eva3dm"),"/wrf_4d_o3_Boston.nc"),'o3')
39+
O3_column <- calculate_column(paste0(system.file("extdata",package="eva3dm"),"/wrf_column_o3_Boston.nc"),'o3')
40+
3841
expect_equal(dim(r_ncdf), c(149,99,1))
3942
})

0 commit comments

Comments
 (0)