@@ -31,8 +31,8 @@ nc_get_dim_min_max <- function(nc, dimension, time_as_date = TRUE) {
31
31
if (units $ hasatt && units $ value == " julian_day" ) {
32
32
time_vals <- as.character(as.Date(vals , origin = structure(- 2440588 , class = " Date" )))
33
33
} else {
34
- pcict_time <- ncdf4.helpers :: nc.get.time.series (nc , time.dim.name = dimension )
35
- posixct_time <- PCICt :: as.POSIXct.PCICt (pcict_time )
34
+ pcict_time <- get_nc_time_series (nc , time.dim.name = dimension )
35
+ posixct_time <- convert_pcict_to_posixct (pcict_time )
36
36
time_vals <- as.character(as.Date(posixct_time ))
37
37
}
38
38
})
@@ -103,8 +103,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
103
103
warning(" Cannot subset data when some dimension axes cannot be identified." )
104
104
start <- NA
105
105
count <- NA
106
- }
107
- else {
106
+ } else {
108
107
start <- c()
109
108
count <- c()
110
109
for (dim in c(" X" , " Y" , " Z" , " T" , " S" )) {
@@ -120,28 +119,24 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
120
119
if (units $ hasatt && units $ value == " julian_day" ) {
121
120
# RDotNet interprets Date class as numeric so character needed to preserve date
122
121
time_vals <- as.Date(curr_dim_values , origin = structure(- 2440588 , class = " Date" ))
123
- }
124
- else {
125
- pcict_time <- ncdf4.helpers :: nc.get.time.series(nc , time.dim.name = dim_var )
126
- posixct_time <- PCICt :: as.POSIXct.PCICt(pcict_time )
122
+ } else {
123
+ pcict_time <- get_nc_time_series(nc , time.dim.name = dim_var )
124
+ posixct_time <- convert_pcict_to_posixct(pcict_time )
127
125
time_vals <- as.Date(posixct_time )
128
126
}
129
127
ind <- which(time_vals > = boundary [[dim_var ]][[1 ]] & time_vals < = boundary [[dim_var ]][[2 ]])
130
128
})
131
- }
132
- else ind <- which(curr_dim_values > = boundary [[dim_var ]][1 ] & curr_dim_values < = boundary [[dim_var ]][2 ])
129
+ } else ind <- which(curr_dim_values > = boundary [[dim_var ]][1 ] & curr_dim_values < = boundary [[dim_var ]][2 ])
133
130
# TODO This is temporary solution for when there is only one value for a dimension and there are rounding difference
134
131
if (length(ind ) == 0 && length(curr_dim_values ) == 1 && round(curr_dim_values , 3 ) == round(boundary [[dim_var ]][1 ], 3 ) && round(curr_dim_values , 3 ) == round(boundary [[dim_var ]][2 ], 3 )) ind <- 1
135
132
if (length(ind ) == 0 ) {
136
133
stop(" No values within the range specified for " , dim_var , " ." )
137
- }
138
- else {
134
+ } else {
139
135
start <- c(start , min(ind ))
140
136
count <- c(count , length(ind ))
141
137
dim_values [[dim_var ]] <- dim_values [[dim_var ]][ind ]
142
138
}
143
- }
144
- else {
139
+ } else {
145
140
start <- c(start , 1 )
146
141
count <- c(count , length(curr_dim_values ))
147
142
}
@@ -152,8 +147,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
152
147
count <- rep(- 1 , length(dim_axes ))
153
148
}
154
149
}
155
- }
156
- else {
150
+ } else {
157
151
start <- rep(1 , length(dim_axes ))
158
152
count <- rep(- 1 , length(dim_axes ))
159
153
}
@@ -192,8 +186,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
192
186
count_list [[i ]] <- curr_count
193
187
dim_values_list [[i ]] <- curr_dim_values
194
188
}
195
- }
196
- else {
189
+ } else {
197
190
start_list [[1 ]] <- start
198
191
count_list [[1 ]] <- count
199
192
dim_values_list [[1 ]] <- dim_values
@@ -214,8 +207,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
214
207
curr_dim_names <- get_nc_dim_names(nc , var )
215
208
if (! setequal(curr_dim_names , dim_names )) {
216
209
warning(" The dimensions of" , var , " do not match the other variables." , var , " will be dropped." )
217
- }
218
- else {
210
+ } else {
219
211
included_vars <- c(included_vars , var )
220
212
curr_var_data [[var ]] <- as.vector(get_ncvar_values(nc , var , start = start_list [[i ]], count = count_list [[i ]]))
221
213
}
@@ -234,11 +226,10 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
234
226
units <- get_nc_attribute(nc , time_var , " units" )
235
227
if (units $ hasatt && units $ value == " julian_day" ) {
236
228
time_df [[" date" ]] <- as.Date(raw_time , origin = structure(- 2440588 , class = " Date" ))
237
- }
238
- else {
239
- pcict_time <- ncdf4.helpers :: nc.get.time.series(nc , time.dim.name = time_var )
229
+ } else {
230
+ pcict_time <- get_nc_time_series(nc , time.dim.name = time_var )
240
231
pcict_time <- pcict_time [time_ind ]
241
- posixct_time <- PCICt :: as.POSIXct.PCICt (pcict_time )
232
+ posixct_time <- convert_pcict_to_posixct (pcict_time )
242
233
time_df [[" date" ]] <- as.Date(posixct_time )
243
234
time_df [[" datetime" ]] <- posixct_time
244
235
}
@@ -251,9 +242,7 @@ nc_as_data_frame <- function(nc, vars, keep_raw_time = TRUE, include_metadata =
251
242
}
252
243
var_data_list [[i ]] <- curr_var_data
253
244
}
254
- if (length(var_data_list ) > 1 ) {
255
- var_data <- dplyr :: bind_rows(var_data_list )
256
- }
245
+ if (length(var_data_list ) > 1 ) var_data <- dplyr :: bind_rows(var_data_list )
257
246
else if (length(var_data_list ) == 1 ) var_data <- var_data_list [[1 ]]
258
247
else var_data_list <- data.frame ()
259
248
@@ -380,3 +369,13 @@ close_nc_file <- function(nc) {
380
369
process_nc_file <- function (nc , vars , keep_raw_time , include_metadata , boundary , lon_points , lat_points , id_points , show_requested_points , great_circle_dist ) {
381
370
nc_as_data_frame(nc , vars , keep_raw_time , include_metadata , boundary , lon_points , lat_points , id_points , show_requested_points , great_circle_dist )
382
371
}
372
+
373
+ # Wrapper for getting time series
374
+ get_nc_time_series <- function (nc , time.dim.name ) {
375
+ ncdf4.helpers :: nc.get.time.series(nc , time.dim.name )
376
+ }
377
+
378
+ # Wrapper for converting PCICt time to POSIXct
379
+ convert_pcict_to_posixct <- function (pcict_time ) {
380
+ PCICt :: as.POSIXct.PCICt(pcict_time )
381
+ }
0 commit comments