Learn R Programming

seas (version 0.3-3)

read.msc: Read a MSC archive file into a data.frame

Description

Reads a Meteorological Service of Canada (MSC) digital archive files (HLY and DLY formats) into a data.frame.

Usage

read.msc(file)

read.msc(file, flags = FALSE, add.elem, format, verbose = TRUE)

Arguments

file
file name (with path, if not in getwd); it can also be a connection, such as bzfile
flags
logical return the flags with the data.frame
add.elem
either a data.frame or a list with additional elements not found in this function
format
force this function to read a format (not recommended, since this is automatically done)
verbose
logical verbose output, such as number of stations, elements, records and years in the archive file

encoding

latin1

synopsis

read.msc(file, flags = FALSE, add.elem, format, verbose = TRUE)

source

Climate data can be requested from MSC, or can be obtained directly from the Canadian Daily Climate Data (CDCD) CD-ROMs, which are available for a free download (procedure described in A1128551.DLY).

Details

This function currently reads in HLY (hourly) and DLY (daily) archive formats. This is automatically detected. The other formats, FIF (fifteen-minute) and MLY (monthly), are not currently supported. The input file can include multiple stations and multiple elements (measured variables). The multiple stations are deciphered through the id column, and the multiple variables appear as columns to the output data frame. This function currently only reads a limited number of elements, however additional elements can be used by editing two lines in the R source for this function.

References

http://climate.weatheroffice.ec.gc.ca/prods_servs/documentation_index_e.html documentation for the MSC digital archive formats http://www.climate.weatheroffice.ec.gc.ca/prods_servs/cdcd_iso_e.html download ISO file images of the CD-ROMs with DLY data

See Also

mscstn, mksub, mkseas, A1128551.DLY

Examples

Run this code
file <- system.file("data/A1128551.DLY",package="seas")
print(file)
dat <- read.msc(file)
print(head(dat))

plot.seas.temp(dat)
plot.year(dat)

# Show how to convert from daily to monthly data
dat$yearmonth <- factor(paste(format(dat$date,"%Y-%m"),15,sep="-"))
mlydat <- data.frame(date=as.Date(levels(dat$yearmonth)))
mlydat$year <- factor(format(mlydat$date,"%Y"))
mlydat$month <- mkseas(mlydat,"mon")

# means for temperature data
mlydat$t_max <- as.numeric(tapply(dat$t_max, dat$yearmonth,
                                  mean,na.rm=TRUE))
mlydat$t_min <- as.numeric(tapply(dat$t_min, dat$yearmonth,
                                  mean,na.rm=TRUE))
mlydat$t_mean <- as.numeric(tapply(dat$t_mean, dat$yearmonth,
                                   mean,na.rm=TRUE))
# sums for precipitation-related data

mlydat$rain <- as.numeric(tapply(dat$rain, dat$yearmonth,
                                 sum,na.rm=TRUE))
mlydat$snow <- as.numeric(tapply(dat$snow, dat$yearmonth,
                                 sum,na.rm=TRUE))
mlydat$precip <- as.numeric(tapply(dat$precip, dat$yearmonth,
                                   sum,na.rm=TRUE))
print(head(mlydat),12)

# Show how to convert from a HLY file into daily summaries
# this is currently shown somewhere at http://www.sfu.ca/~mwtoews/
hlydat <- read.msc(bzfile("HLY11_L1127800.bz2"), flags=TRUE)
hlydat$date <- factor(hlydat$date)

# sum the solar radiation for each day to find the 'total daily'
sumdat <- tapply(hlydat$solar, hlydat$date, sum, na.rm=TRUE)
dlydat <- data.frame(date=as.Date(names(sumdat)),
                     solar=as.numeric(sumdat))

# sum the number of hours without measurements
sumdat <- tapply(hlydat$solar, hlydat$date,
                 function(v)(24 - sum(!is.na(v))))
dlydat$na <- as.integer(sumdat)

# quality control to remove days with less than 4 hours missing
Summerland <- dlydat[dlydat$na < 4,]

attr(Summerland$solar,"units") <- "W/(m^2*day"
attr(Summerland$solar,"long.name") <- "Daily total global solar radiation"
plot.seas.var(Summerland,var="solar", col="yellow", width=5)

Run the code above in your browser using DataLab