wsim_merge.R 2.34 KB
Newer Older
Dan Baston's avatar
Dan Baston committed
1
#!/usr/bin/env Rscript
Dan Baston's avatar
Dan Baston committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15

# Copyright (c) 2018 ISciences, LLC.
# All rights reserved.
#
# WSIM is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. You may
# obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
wsim.io::logging_init('wsim_merge')
Dan Baston's avatar
Dan Baston committed
17 18 19 20

'
Merge raster datasets into a single netCDF

21
Usage: wsim_merge (--input=<file>)... (--output=<file>) [--attr=<attr>]...
Dan Baston's avatar
Dan Baston committed
22 23 24 25 26 27 28 29 30 31 32 33
'->usage

main <- function(raw_args) {
  args <- wsim.io::parse_args(usage, raw_args)

  inputs <- wsim.io::expand_inputs(args$input)

  attrs <- lapply(args$attr, wsim.io::parse_attr)

  combined <- list(
    attrs= list(),
    extent= NULL,
34
    ids= NULL,
Dan Baston's avatar
Dan Baston committed
35 36 37 38
    data= list()
  )

  for (input in inputs) {
39
    wsim.io::info('Processing', input)
40 41 42
    
    if (length(combined$data) == 0) {
      v <- wsim.io::read_vars(input)
Dan Baston's avatar
Dan Baston committed
43
      combined$extent <- v$extent
44
      combined$ids <- v$ids
Dan Baston's avatar
Dan Baston committed
45
    } else {
46 47
      v <- wsim.io::read_vars(input,
                              expect.extent=combined$extent,
48
                              expect.ids=combined$ids,
49
                              expect.dims=dim(combined$data[[1]]))
Dan Baston's avatar
Dan Baston committed
50 51 52 53 54 55 56 57
    }

    for (var in names(v$data)) {
      if (var %in% names(combined$data)) {
        wsim.io::die_with_message("Multiple definitions of variable ", var)
      }

      combined$data[[var]] <- v$data[[var]]
58 59 60
      
      # Attempt to assign any attributes that were specified with --attr
      # but did not have an assigned value
61
      for (i in seq_along(attrs)) {
62 63 64 65
        if (attrs[[i]]$var == var && is.null(attrs[[i]]$val)) {
          attrs[[i]]$val <- attr(v$data[[var]], attrs[[i]]$key)
        }
      }
Dan Baston's avatar
Dan Baston committed
66 67 68
    }
  }

69
  wsim.io::info('Writing to', args$output)
Dan Baston's avatar
Dan Baston committed
70 71 72
  wsim.io::write_vars_to_cdf(combined$data,
                             args$output,
                             extent=combined$extent,
73
                             ids=combined$ids,
Dan Baston's avatar
Dan Baston committed
74 75 76
                             attrs=attrs)
}

77
tryCatch(main(commandArgs(trailingOnly = TRUE)), error=wsim.io::die_with_message)