dankelley / ocencdf

Interface from oce to netcdf
https://dankelley.github.io/ocencdf/
2 stars 1 forks source link

adv2ncdf() seems to alter R notion of NA #14

Closed dankelley closed 1 year ago

dankelley commented 1 year ago

I see a problem (illustrated below) and will look into it. Notice the last few lines: R does not recognize the NA values in the adv object until I reload it. It seems that adv2ncdf() is altering the object.

library(ocencdf)
#> Loading required package: oce
#> Loading required package: gsw
data(adv, package="oce")
dput(adv@data$timeBurst[1])
#> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC")
adv2ncdf(adv)
dput(adv@data$timeBurst[1])
#> structure(99999, class = c("POSIXct", "POSIXt"), tzone = "UTC")
data(adv, package="oce")
dput(adv@data$timeBurst[1])
#> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC")

Created on 2023-06-29 with reprex v2.0.2

dankelley commented 1 year ago

The following sheds some light. (NOTE: I am not going to attempt to explain things a lot in this issue. I'm using comments as notes to myself.)

library(oce)
#> Loading required package: gsw
library(ocencdf)
data(adv, package="oce")
data(ctd, package="oce")
dput(adv@data$timeBurst[1])
#> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC")
ctd2ncdf(ctd)
#> Converting temperature from IPTS-68 scale to ITS-90 scale.
dput(adv@data$timeBurst[1])
#> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC")

Created on 2023-06-29 with reprex v2.0.2

dankelley commented 1 year ago

It's not the mere existence of ncdf4 calls that causes the problem, as this stripped-down example shows.

``` r library(ocencdf) #> Loading required package: oce #> Loading required package: gsw test <- function(x) { five <- ncdf4::ncdim_def(name="FIVE", units="", vals=1:5) vars <- list() vars[["cents"]] <- ncdf4::ncvar_def("cents", units="penny", dim=five, missval=99999.0) nc <- ncdf4::nc_create("test.nc", vars) ncdf4::ncvar_put(nc, "cents", head(x@data$heading, 5)) ncdf4::nc_close(nc) } data(adv, package="oce") dput(adv@data$timeBurst[1]) #> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC") test(adv) dput(adv@data$timeBurst[1]) #> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC") data(adv, package="oce") dput(adv@data$timeBurst[1]) #> structure(NA_real_, class = c("POSIXct", "POSIXt"), tzone = "UTC") ``` Created on 2023-06-29 with [reprex v2.0.2](https://reprex.tidyverse.org)
dankelley commented 1 year ago

Hm, I tried a test in which I just didn't specify missvalwhen I call ncvar_def(), and now the test code works.

Frankly, I don't see any need to even set a particular missval -- why not let netcdf decide on that -- and so I'll just use the missing_value in the varTable when reading the data. Data matching that will get set to NA.

dankelley commented 1 year ago

Fixed in develop branch

commit fdcd0c5a47543b3bab2b0704ad5535ca99b86615 Author: dankelley kelley.dan@gmail.com Date: Thu Jun 29 11:51:34 2023 -0300

fix problem with missing-values (issue 14)