read.msc {climate.plot} | R Documentation |
Reads a Meteorological Service of Canada (MSC) digital archive files (HLY and DLY formats) into a data.frame.
read.msc(file) read.msc(file, flags = FALSE, add.elem, format, verbose = TRUE)
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 |
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 parameters). The multiple stations are deciphered through the id
column, and the multiple parameters 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.
Returns a data.frame
object with the following minimum fields:
id |
factor used to distinguish multiple stations within a single data frame |
year |
integer year |
jday |
integer Julian day; 1–365 or 1–366 |
date |
Date , useful for plotting a continuous time-series |
datetime |
POSIXct , includes date and time info, only included if file is in HLY archive format |
element |
numeric ... |
flag |
factor ... (included if flags=TRUE ) |
max_t |
maximum temperature |
min_t |
minimum temperature |
mean_t |
mean temperature |
rain |
rain |
snow |
snow |
precip |
total precipitation |
snow_d |
snow depth |
... |
other elements can be added using the add.elem parameter |
Units are in common metric units: mm for precipitation-related measurements, cm for snow depth, and degrees C for temperature. The flag
columns are a single character factor
, described in the MSC Archive documentation.
This function is not optimized, since it uses many for
loops. It would be nice to have this function operate faster, however this is not priority for me right now. If any one is interested in optimizing this function (by writing the second half in C or FORTRAN), please contact me, and I will supply you with example archive files.
M.W. Toews
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
).
Documentation for the MSC digital archive formats are found at http://climate.weatheroffice.ec.gc.ca/prods_servs/documentation_index_e.html
ISO file images of the CD-ROMs with DLY data can be obtained from http://www.climate.weatheroffice.ec.gc.ca/prods_servs/cdcd_iso_e.html
mscstn, \code{mksub}, \code{mkfact}, \code{A1128551.DLY}
file <- system.file("data/A1128551.DLY",package="climate.plot") 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 <- mkfact(mlydat$date,"mon") # means for temperature data mlydat$max_t <- as.numeric(tapply(dat$max_t, dat$yearmonth, mean,na.rm=TRUE)) mlydat$min_t <- as.numeric(tapply(dat$min_t, dat$yearmonth, mean,na.rm=TRUE)) mlydat$mean_t <- as.numeric(tapply(dat$mean_t, 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 ## Not run: # 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,] plot.seas.param(Summerland,param="solar", col="yellow", width=5, ylab="Daily total global solar radiation (W/(m^2*day)") ## End(Not run)