Skip to content

Commit

Permalink
all read.*() functions now check for empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
dankelley committed Aug 21, 2019
1 parent 9bda34f commit 80e3e7f
Show file tree
Hide file tree
Showing 31 changed files with 87 additions and 8 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ export(abbreviateTimeLabels,
read.adp,
read.adp.ad2cp,
read.adp.rdi,
read.adp.nortek,
read.adp.sontek,
read.adp.sontek.serial,
read.adv,
Expand Down
12 changes: 10 additions & 2 deletions R/adp.nortek.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ read.adp.ad2cp <- function(file, from=1, to=0, by=1, tz=getOption("oceTz"),
monitor=FALSE, despike=FALSE, processingLog,
debug=getOption("oceDebug"), ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!missing(orientation))
warning("ignoring 'orientation' (see documentation)")
if (!missing(distance))
Expand Down Expand Up @@ -600,8 +602,6 @@ read.adp.ad2cp <- function(file, from=1, to=0, by=1, tz=getOption("oceTz"),
if (to == 0)
to <- 1e9 # this should be enough to read any file
if (is.character(file)) {
if (0 == file.info(file)$size)
stop("empty file")
filename <- fullFilename(file)
file <- file(file, "rb")
on.exit(close(file))
Expand Down Expand Up @@ -2184,6 +2184,8 @@ read.aquadopp <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
monitor=FALSE, despike=FALSE, processingLog,
debug=getOption("oceDebug"), ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
return(read.adp.nortek(file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
type="aquadopp", orientation=orientation, distance=distance,
Expand Down Expand Up @@ -2237,6 +2239,8 @@ read.aquadoppHR <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
monitor=FALSE, despike=FALSE, processingLog,
debug=getOption("oceDebug"), ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
return(read.adp.nortek(file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
type="aquadoppHR",
Expand Down Expand Up @@ -2291,6 +2295,8 @@ read.aquadoppProfiler <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
monitor=FALSE, despike=FALSE, processingLog,
debug=getOption("oceDebug"), ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
return(read.adp.nortek(file, from=from, to=to, by=by, tz=tz,
longitude=longitude, latitude=latitude,
type="aquadoppProfiler",
Expand Down Expand Up @@ -2338,6 +2344,8 @@ read.adp.nortek <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
debug=getOption("oceDebug"),
...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
##degToRad <- atan2(1, 1) / 45
profileStart <- NULL # prevents scope warning in rstudio; defined later anyway
bisectAdpNortek <- function(buf, t.find, add=0, debug=0) {
Expand Down
4 changes: 4 additions & 0 deletions R/adp.sontek.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ read.adp.sontek <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
debug=getOption("oceDebug"),
...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
missing.to <- missing(to)
## In this function, comments in [] refer to logical page number of ADPManual_v710.pd; add 14 for file page number
profileStart <- NULL # prevent scope warning from rstudio; defined later anyway
Expand Down Expand Up @@ -439,6 +441,8 @@ read.adp.sontek.serial <- function(file, from=1, to, by=1, tz=getOption("oceTz")
monitor=FALSE, processingLog,
debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
## Data format is described in
## SonTek/YSI
## ADPManual_v710.pdf
Expand Down
2 changes: 2 additions & 0 deletions R/adv.R
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ read.adv <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
start=NULL, deltat=NA,
debug=getOption("oceDebug"), monitor=FALSE, processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
type <- match.arg(type)
## FIXME: all these read.adv variants should have the same argument list
if (type == "nortek")
Expand Down
2 changes: 2 additions & 0 deletions R/adv.nortek.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ read.adv.nortek <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
haveAnalog1=FALSE, haveAnalog2=FALSE,
debug=getOption("oceDebug"), monitor=FALSE, processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
## vvd=vector velocity data [p35 SIG], containing the data: pressure, vel, amp, corr (plus sensemble counter etc)
## vsd=velocity system data [p36 SIG], containing times, temperatures, angles, etc
## NOTE: we interpolate from vsd to vvd, to get the final data$time, etc.
Expand Down
6 changes: 6 additions & 0 deletions R/adv.sontek.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ read.adv.sontek.serial <- function(file, from=1, to, by=1, tz=getOption("oceTz")
start=NULL, deltat=NULL,
debug=getOption("oceDebug"), monitor=FALSE, processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, paste("read.adv.sontek.serial(file[1]=\"", file[1],
"\", from=", format(from),
if (!missing(to)) sprintf(", to=%s, ", format(to)),
Expand Down Expand Up @@ -154,6 +156,8 @@ read.adv.sontek.adr <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
longitude=NA, latitude=NA,
debug=getOption("oceDebug"), monitor=FALSE, processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
bisectAdvSontekAdr <- function(burstTime, tFind, add=0, debug=0) {
oceDebug(debug, "bisectAdvSontekAdr(tFind=", format(tFind), ", add=", add, "\n")
len <- length(burstTime)
Expand Down Expand Up @@ -639,6 +643,8 @@ read.adv.sontek.text <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
debug=getOption("oceDebug"), monitor=FALSE,
processingLog=NULL)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
## FIXME: It would be better to deal with the binary file, but the format is unclear to me;
## FIXME: two files are available to me, and they differ considerably, neither matching the
## FIXME: SonTek documentation.
Expand Down
2 changes: 2 additions & 0 deletions R/amsr.R
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ download.amsr <- function(year, month, day, destdir=".", server="http://data.rem
#' @md
read.amsr <- function(file, debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.amsr(file=\"", file, "\",",
#if (length(band) > 1) paste("band=c(\"", paste(band, collapse="\",\""), "\")", sep="") else
", debug=", debug, ") {\n", sep="", unindent=1)
Expand Down
2 changes: 2 additions & 0 deletions R/argo.R
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,8 @@ argoDecodeFlags <- function(f) # local function
#' @md
read.argo <- function(file, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!requireNamespace("ncdf4", quietly=TRUE))
stop('must install.packages("ncdf4") to read argo data')
if (missing(processingLog)) processingLog <- paste(deparse(match.call()), sep="", collapse="")
Expand Down
2 changes: 2 additions & 0 deletions R/bremen.R
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ findInHeaderBremen <- function(key, lines)
#' @md
read.bremen <- function(file)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (is.character(file)) {
filename <- fullFilename(file)
file <- file(file, "r")
Expand Down
2 changes: 2 additions & 0 deletions R/cm.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ read.cm <- function(file, from=1, to, by=1, tz=getOption("oceTz"),
longitude=NA, latitude=NA,
debug=getOption("oceDebug"), monitor=FALSE, processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.cm(file=\"", file,
"\", from=", format(from),
", to=", if (missing(to)) "(missing)" else format(to), ", by=", by, "type=", type, ", ...) {\n", sep="", unindent=1)
Expand Down
6 changes: 6 additions & 0 deletions R/coastline.R
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,8 @@ read.coastline <- function(file,
type=c("R", "S", "mapgen", "shapefile", "openstreetmap"),
debug=getOption("oceDebug"), monitor=FALSE, processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
type <- match.arg(type)
oceDebug(debug, "read.coastline(file=\"", file, "\", type=\"", type, "\", ...) {\n", sep="", unindent=1)
file <- fullFilename(file)
Expand Down Expand Up @@ -1086,6 +1088,8 @@ read.coastline <- function(file,
read.coastline.shapefile <- function(file, lonlim=c(-180, 180), latlim=c(-90, 90),
debug=getOption("oceDebug"), monitor=FALSE, processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.shapefile(file=\"", file, "\", ...) {\n", sep="", unindent=1)
shapeTypeList <- c("nullshape", # 0
"point", # 1
Expand Down Expand Up @@ -1295,6 +1299,8 @@ read.coastline.shapefile <- function(file, lonlim=c(-180, 180), latlim=c(-90, 90
read.coastline.openstreetmap <- function(file, lonlim=c(-180, 180), latlim=c(-90, 90),
debug=getOption("oceDebug"), monitor=FALSE, processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.coastline.openstreetmap(file=\"", file, "\", ...) {\n", sep="", unindent=1)
## FIXME: ignoring lonlim and latlim
if (is.character(file)) {
Expand Down
2 changes: 2 additions & 0 deletions R/ctd.R
Original file line number Diff line number Diff line change
Expand Up @@ -4064,6 +4064,8 @@ plotScan <- function(x, which=1, xtype="scan", flipy=FALSE,
read.ctd <- function(file, type=NULL, columns=NULL, station=NULL, missingValue, deploymentType="unknown",
monitor=FALSE, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
## Special case: ruskin files are handled by read.rsk()
if (is.character(file) && length(grep(".rsk$", file))) {
return(read.rsk(file=file, debug=debug))
Expand Down
2 changes: 2 additions & 0 deletions R/ctd.itp.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
read.ctd.itp <- function(file, columns=NULL, station=NULL, missingValue, deploymentType="unknown",
monitor=FALSE, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.ctd.itp() {\n", unindent=1)
if (is.character(file)) {
filename <- fullFilename(file)
Expand Down
2 changes: 2 additions & 0 deletions R/ctd.sbe.R
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,8 @@ cnvName2oceName <- function(h, columns=NULL, debug=getOption("oceDebug"))
read.ctd.sbe <- function(file, columns=NULL, station=NULL, missingValue, deploymentType="unknown",
monitor=FALSE, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (length(grep("\\*", file, ignore.case=TRUE))) {
oceDebug(debug, "read.ctd.sbe(file=\"", file, "\") { # will read a series of files\n", unindent=1)
files <- list.files(pattern=file)
Expand Down
4 changes: 4 additions & 0 deletions R/ctd.woce.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ woceUnit2oceUnit <- function(woceUnit)
read.ctd.woce <- function(file, columns=NULL, station=NULL, missingValue, deploymentType="unknown",
monitor=FALSE, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (length(grep("\\*", file))) {
oceDebug(debug, "read.ctd.woce(file=\"", file, "\") { # will read a series of files\n", unindent=1)
files <- list.files(pattern=file)
Expand Down Expand Up @@ -668,6 +670,8 @@ read.ctd.woce <- function(file, columns=NULL, station=NULL, missingValue, deploy
read.ctd.woce.other <- function(file, columns=NULL, station=NULL, missingValue, deploymentType="unknown",
monitor=FALSE, debug=getOption("oceDebug"), processingLog, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
##EXPOCODE 06MT18/1 WHP-ID A1E DATE 090591
##STNNBR 558 CASTNO 1 NO.RECORDS= 83
##INSTRUMENT NO. NB3 SAMPLING RATE 31.25 HZ
Expand Down
2 changes: 2 additions & 0 deletions R/echosounder.R
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,8 @@ read.echosounder <- function(file, channel=1, soundSpeed,
tz=getOption("oceTz"), debug=getOption("oceDebug"),
processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.echosounder(file=\"", file, "\", tz=\"", tz, "\", debug=", debug, ") {\n", sep="", unindent=1)
##ofile <- file
filename <- NULL
Expand Down
12 changes: 8 additions & 4 deletions R/g1sst.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ setMethod(f="[[<-",
#' consulting reference 2 (clicking and unclicking the radio button to
#' show just the data) that the field mostly derives from simulation.
#'
#' @param filename name of a netcdf file containing G1SST data.
#' @param file character value containing the name of a netcdf file containing G1SST data.
#'
#' @return A [g1sst-class] object.
#'
Expand Down Expand Up @@ -132,12 +132,16 @@ setMethod(f="[[<-",
#' @family things related to satellite data
#'
#' @md
read.g1sst <- function(filename)
read.g1sst <- function(file)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!requireNamespace("ncdf4", quietly=TRUE))
stop('must install.packages("ncdf4") to read g1sst data')
f <- ncdf4::nc_open(filename)
res <- new("g1sst", filename=filename)
if (!is.character(file))
stop("file must be a character string")
f <- ncdf4::nc_open(file)
res <- new("g1sst", file=file)
## Change the 1-col ncdf4 output to a vector
res@metadata$longitude <- as.vector(ncdf4::ncvar_get(f, "longitude"))
res@metadata$latitude <- as.vector(ncdf4::ncvar_get(f, "latitude"))
Expand Down
2 changes: 2 additions & 0 deletions R/gps.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ as.gps <- function(longitude, latitude, filename="")
#' @md
read.gps <- function(file, type=NULL, debug=getOption("oceDebug"), processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.gps(...) {\n", sep="", unindent=1)
filename <- NULL
if (is.character(file)) {
Expand Down
2 changes: 2 additions & 0 deletions R/index.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
#' @md
read.index <- function(file, format, missingValue, tz=getOption("oceTz"), debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (is.character(file)) {
##filename <- fullFilename(file)
file <- file(file, "r")
Expand Down
2 changes: 2 additions & 0 deletions R/landsat.R
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,8 @@ read.landsatmeta <- function(file, debug=getOption("oceDebug"))
#' @md
read.landsat <- function(file, band="all", emissivity=0.984, decimate, debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.landsat(file=\"", file, "\",",
if (length(band) > 1) paste("band=c(\"", paste(band, collapse="\",\""), "\")", sep="") else
paste("band=\"", band, "\"", sep=""),
Expand Down
2 changes: 2 additions & 0 deletions R/lisst.R
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ as.lisst <- function(data, filename="", year=0, tz="UTC", longitude=NA, latitude
#' @md
read.lisst <- function(file, year=0, tz="UTC", longitude=NA, latitude=NA)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
filename <- NULL
if (is.character(file)) {
filename <- fullFilename(file)
Expand Down
2 changes: 2 additions & 0 deletions R/lobo.R
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ setMethod(f="plot",
#' @family things related to lobo data
read.lobo <- function(file, cols=7, processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
## header <- scan(file, what=character(), sep="\t", nlines=1, quiet=TRUE)
## d <- scan(file, what=character(), sep="\t", skip=1, quiet=TRUE)
filename <- ""
Expand Down
2 changes: 2 additions & 0 deletions R/met.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ metNames2oceNames <- function(names, scheme)
#' @md
read.met <- function(file, type=NULL, skip, tz=getOption("oceTz"), debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.met() {\n", unindent=1)
if (is.character(file)) {
filename <- fullFilename(file)
Expand Down
2 changes: 2 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ argShow <- function(x, nshow=4, last=FALSE, sep="=")
#'}
read.woa <- function(file, name, positive=FALSE)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!is.character(file))
stop("'file' must be a character string")
con <- ncdf4::nc_open(file)
Expand Down
2 changes: 2 additions & 0 deletions R/oce.R
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,8 @@ oceMagic <- function(file, debug=getOption("oceDebug"))
#' plotTS(x) # just the TS
read.oce <- function(file, ...)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
type <- oceMagic(file)
dots <- list(...)
debug <- if ("debug" %in% names(dots)) dots$debug else 0
Expand Down
2 changes: 2 additions & 0 deletions R/odf.R
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,8 @@ ODFListFromHeader <- function(header)
#' @md
read.odf <- function(file, columns=NULL, header="list", debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.odf(\"", file, "\", ...) {\n", unindent=1, sep="")
if (!is.null(header)) {
if (!is.character(header))
Expand Down
2 changes: 2 additions & 0 deletions R/rsk.R
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ setMethod(f="plot",
read.rsk <- function(file, from=1, to, by=1, type, tz=getOption("oceTz", default="UTC"),
patm=FALSE, processingLog, debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
debug <- max(0, min(debug, 2))
oceDebug(debug, "read.rsk(file=\"", file, "\", from=", format(from),
", to=", if (missing(to))"(not given)" else format(to),
Expand Down
2 changes: 2 additions & 0 deletions R/sealevel.R
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ setMethod(f="plot",
#' @md
read.sealevel <- function(file, tz=getOption("oceTz"), processingLog, debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
if (!is.character(file))
stop("'file' must be a character string")
fileOrig <- file
Expand Down
2 changes: 2 additions & 0 deletions R/section.R
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,8 @@ read.section <- function(file, directory, sectionId="", flags,
missingValue=-999,
debug=getOption("oceDebug"), processingLog)
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.section(file=\"", file, "\", ...) {\n", unindent=1)
if (!missing(directory)) {
if (!missing(file))
Expand Down
2 changes: 2 additions & 0 deletions R/topo.R
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,8 @@ setMethod(f="plot",
#' @md
read.topo <- function(file, debug=getOption("oceDebug"))
{
if (!missing(file) && is.character(file) && 0 == file.info(file)$size)
stop("empty file")
oceDebug(debug, "read.topo(file=\"", file, "\") {\n", sep="", unindent=1)
## handle GEBCO netcdf files or an ascii format
dataNamesOriginal <- list()
Expand Down
Loading

0 comments on commit 80e3e7f

Please sign in to comment.