-
Notifications
You must be signed in to change notification settings - Fork 23
Description
Hi @MagicForrest! I'm using the package to read a CF-compliant (as far as I know) netcdf file which has a time dimension of length 1. When I call getField() on this file, I get the following error from getField_NetCDF():
Error in if (mean.timestep.differences >= 28 & mean.timestep.differences <= :
missing value where TRUE/FALSE needed
This is because mean.timestep.differences is NA in this case (it is effectively mean(diff(c(0)))). A quick workaround that appears to work for me is to add an extra branch to the if statement on the line giving the error, which detects the presence of such a single-value time axis (e.g. if (length(all.time.intervals) == 1)). However, I'm not sure how comprehensive this solution would be (I haven't looked closely enough at this function to fully understand it and maybe this "solution" only fixes files with "days since" units).
A better longterm solution to this problem might be to use the CFtime package to handle the parsing of dates for us. This package lets you do something like this:
nc <- nc_open(input_file)
calendar <- ncatt_get(nc, "time", "calendar")$value
units <- ncatt_get(nc, "time", "units")$value
values <- ncvar_get(nc, "time")
dates <- as.Date(as_timestamp(CFtime(units, calendar, values)))That said, I haven't looked too closely at the current time-reading code, so maybe there's some extra logic in there that we need, or there are some other edge cases I haven't thought about.