if (FALSE) {
# Download the file. (This may break if the server changes.)
file <- tempfile(fileext = ".nc")
url <- paste0(
"https://cioosatlantic.ca/erddap/files/",
"bio_maritimes_region_ecosystem_survey_ctd/",
"Maritimes%20Region%20Ecosystem%20Survey%20Summer/",
"2023/CTD_CAR2023011_001_496780_DN.ODF.nc"
)
download.file(url, file)
# Example 1: read without translating names
d <- read.netcdf(file)
summary(d)
# Example 2: as Example 1, but translate (some) names
d <- read.netcdf(file, renamer = bodcNames2oceNames)
summary(d)
# Example 3: as Example 2, but handle some flags that were
# noticed in this particular file. See Details for more
# notes on variable names. Note that the following code
# only deals with the first instance of a variable, e.g.
# temperature, and not temperature2 or temperature3.
# (This is of little consequence, since all 3 of the temperatures
# are identical.)
d <- read.netcdf(file, renamer = bodcNames2oceNames)
# Looking within the NetCDF file indicates that the built-in
# scheme for DFO files is appropriate here.
d <- initializeFlagScheme(d, name = "DFO")
# Move some data elements to the `metadata@flags` list,
# so they can be used for flag-handling operations. Some
# guesses had to be made on the name mapping (see Details).
flags <- list(
QALTB_01 = "heightAboveBottom",
QCPHLPR01 = "cholorophyll-a",
QCNDC_01 = "conductivity",
QDOXY_01 = "oxygen",
QOXYV_01 = "oxygenVoltage",
QPOTM_01 = "theta",
QPRES_01 = "pressure",
QPSAL_01 = "salinity",
QPSAR_01 = "downwellingIrradiance",
QSIGP_01 = "sigmaTheta",
QTEMP_01 = "temperature"
)
for (i in seq_along(flags)) {
varName <- flags[[i]]
flagName <- names(flags)[i]
# cat("fileName=", varName, ", flagName=", flagName, "\n", sep="")
d@metadata$flags[[varName]] <- d[[flagName]] # move
d@data[[flagName]] <- NULL # delete original
}
# For this group of files, it appears that sensor metadata are
# stored with particular names, e.g. "TemperatureSensor". The
# following moves these from the data slot to the metadata slot.
dataNames <- names(d@data)
for (sensorName in dataNames[grep("Sensor$", dataNames)]) {
d@metadata[[sensorName]] <- d@data[[sensorName]]
d@data[[sensorName]] <- NULL
}
summary(d)
# Display information about the temperator sensor
cat("Temperature Sensor\n")
if (require("jsonlite")) {
str(jsonlite::fromJSON(d[["TemperatureSensor"]]))
}
# Finally, remove the downloaded file, according to CRAN
# policy regarding downloads in documentation examples.
file.remove(file)
}
Run the code above in your browser using DataLab