db2dat: Get daily or monthly data from a database and build input...

View source: R/clihomog.R

db2datR Documentation

Get daily or monthly data from a database and build input files *.dat and *.est

Description

This function facilitates the creation of the input files needed by this package by retrieving the data from a database through an RODBC connection.

Usage

db2dat(varcli, anyi, anyf, minny=5, daily=TRUE, ch, dformat='%Y-%m-%d',
vtable, vcode, vdate, vval, stable, scode, sname, sx, sy, sz)

Arguments

varcli

Short name of the studied climatic variable, as it will appear in all data file names.

anyi

Initial year of the data to be included in the file.

anyf

Final year of the data to be included in the file.

minny

Minimum number of years with data for a series to be included in the file.

daily

Logical flag indicating whether the data are daily (the default) or monthly (set daily=FALSE in this case).

ch

Already open ODBC connection to the climatic database.

dformat

Date format in the database.

vtable

Name of the table containing our climatic variable.

vcode

Name of the variable containing station codes in the vtable table.

vdate

Name of the variable containing dates in the vtable table.

vval

Name of the climatic variable in the vtable table.

stable

Name of the table containing station information (metadata).

scode

Name of the variable containing station codes in the table stable.

sname

Name of the variable containing station names in the stable table.

sx

Name of the variable containing longitudes (degrees with decimals!) in the stable table.

sy

Name of the variable containing latitudes (degrees with decimals!) in the stable table.

sz

Name of the variable containing elevations (meters) in the stable table.

Details

This function creates the two input files needed by the homogenization functions of this package, ‘VRB_YEAR-YEAR.dat’ (holding the data) and ‘VRB_YEAR-YEAR.est’ (holding station coordinates, codes and names).

The table in the accessed database must contain either daily or monthly data (set daily=FALSE in this case). Otherwise the number of data per series will not be match the expected value and the function will fail.

Moreover, every data item must be in a different record in the database, as in this example table of monthly data (different variables for the same time step are O.K.):

Station Date T.max T.min Rel.Hum Precip Wind.speed
S032 1991-01-01 12.1 -2.1 59 128.2 5.4
S032 1991-02-01 13.2 -2.5 62 78.4 6.2
...

But if the table in the database arranges all monthly values of one year (or all daily values of one month) in a single record, then this function cannot be applied. In this cases, try to use the database functionalities to output series into CSV files and apply other conversion functions as csv2climatol.

See Also

homogen, csv2climatol

Examples

## Not run: 
## First we must access our climatic database through RODBC, wich requires to
## have this package installed. System programs that allow ODBC connections to
## databases must also be installed and properly configured.

## For this example we will assume that our database is named "climate" and we
## access it with user "USER" and password "PASS".  Then we open the connection
## with:
library(RODBC)
ch <- odbcConnect("climate",uid="USER",pwd="PASS")

## Now we want to use this function to gather all monthly relative humidity
## averages for the period 1961-2015, requiring a minimum of 10 years of data
## (not necessarily consecutive). We must use the corresponding names of tables
## and headers existing the the database, and putting the parameters in the
## required order we avoid the need to name them:
db2dat('HRel',1961,2015,10,FALSE,ch,'%Y-%m-%d','monthly_relhum',
'Station','Date','Value','stations','Station','Name','Longitude',
'Latitude','Elevation')

odbcClose(ch) #close the connection if you do not need it anymore

## Our data would now be ready to be homogenized with the homogen function:
homogen('HRel',1961,2015,vmin=0,vmax=100)

## End(Not run)

climatol documentation built on June 22, 2024, 11:07 a.m.

Related to db2dat in climatol...