Skip to content

Commit

Permalink
Addition of dates parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalhorton committed Jun 27, 2017
1 parent 18ba9a2 commit 881eedf
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export(crpsVectorWeightedByCriteria)
export(optimalNbAnalog)
export(optimalNbAnalogOfAnalogs)
export(parseAllNcOutputs)
export(parseDatesNcOutputs)
export(parseScoresNcOutputs)
55 changes: 53 additions & 2 deletions R/parseOutputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,61 @@ parseAllNcOutputs <- function(directory, station.id, period, level = 1) {
return(AM)
}

#' Parse NetCDF files resulting from AtmoSwing optimizer.
#'
#' Extract results (for both analogues and the target situations: dates) from
#' the NetCDF files resulting from AtmoSwing optimizer.
#'
#' @param directory Directory containing the outputs from AtmoSwing (containing
#' the "calibration" or "validation" directories).
#' @param station.id ID of the station time series.
#' @param period Either "calibration" or "validation".
#' @param level Analogy level.
#'
#' @return Results of the analogue method.
#'
#' @examples
#' \dontrun{
#' data <- atmoswing::parseDatesNcOutputs('optimizer-outputs/1/results', 1, 'validation', 2)
#' }
#'
#' @export
#'
parseDatesNcOutputs <- function(directory, station.id, period, level = 1) {

assertthat::assert_that((period=='calibration' || period=='validation'),
msg = 'period must be "calibration" or "validation"')
assertthat::assert_that(assertthat::is.dir(directory),
msg = paste(directory, 'is not a directory (wd:',
getwd(), ')'))

# Look for the files
path.dates <- paste(directory, '/', period, '/AnalogsDates_id_',
station.id, '_step_', level-1, '.nc', sep='')
assertthat::assert_that(file.exists(path.dates),
msg = paste(path.dates, 'not found'))

# Open all files
AD.nc = ncdf4::nc_open(path.dates)

# Extract data
AM <- list(
analog.dates.MJD = t(ncdf4::ncvar_get(AD.nc, 'analog_dates')),
target.dates.MJD = ncdf4::ncvar_get(AD.nc, 'target_dates'),
target.dates.UTC = as.Date(astroFns::dmjd2ut(
ncdf4::ncvar_get(AD.nc, 'target_dates'), tz= 'UTC' ), format='%Y.%m.%d')
)

# Close all files
ncdf4::nc_close(AD.nc)

return(AM)
}

#' Parse NetCDF score files resulting from AtmoSwing optimizer.
#'
#' Extract results (for the target situations: dates, predictand values,
#' prediction score) from the NetCDF files resulting from AtmoSwing optimizer.
#' Extract results (for the target situations: predictand values, prediction
#' score) from the NetCDF files resulting from AtmoSwing optimizer.
#'
#' @param directory Directory containing the outputs from AtmoSwing (containing
#' the "calibration" or "validation" directories).
Expand Down
31 changes: 31 additions & 0 deletions man/parseDatesNcOutputs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/parseScoresNcOutputs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/testthat/testParsingOutputs.R
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,29 @@ test_that("NetCDF outputs are correctly parsed only for the scores", {
expect_equal(A$predict.score[7], 0.9594, tolerance = .0001)

})

test_that("NetCDF outputs are correctly parsed only for the dates", {
A <- atmoswing::parseDatesNcOutputs(file.path('test_files', 'optim', '1', 'results'),
1, 'calibration')

expect_equal(A$analog.dates.MJD[1,1], 53777.0)
expect_equal(A$analog.dates.MJD[19,1], 48960.0)
expect_equal(A$analog.dates.MJD[23,5], 47875.0)

expect_equal(A$target.dates.MJD[6], 54837.0)
expect_equal(A$target.dates.UTC[6], as.Date('2009-01-06'))

})

test_that("NetCDF outputs are correctly parsed only for the dates on the 2nd level", {
A <- atmoswing::parseDatesNcOutputs(file.path('test_files', 'optim', '2', 'results'),
1, 'validation', 2)

expect_equal(A$analog.dates.MJD[1,1], 51216.0)
expect_equal(A$analog.dates.MJD[19,1], 50821.0)
expect_equal(A$analog.dates.MJD[23,5], 49703.0)

expect_equal(A$target.dates.MJD[6], 55202.0)
expect_equal(A$target.dates.UTC[6], as.Date('2010-01-06'))

})

0 comments on commit 881eedf

Please sign in to comment.