Skip to content

Instantly share code, notes, and snippets.

@lauratboyer
Last active December 15, 2015 01:59
Show Gist options
  • Save lauratboyer/5184134 to your computer and use it in GitHub Desktop.
Save lauratboyer/5184134 to your computer and use it in GitHub Desktop.
Code to extract GODAS oceanography data from database, given that connection "channel" is defined.
#Function to extract GODAS oceanography data
#from SPC databases (adapted from Simon Hoyle)
#Gets the value from the nearest lon/lat grid cell
#lat resolution is 1/3 of a degree, lon is 1 degree
#Started: <2013-03-14 15:29:57 Laura>
#Time-stamp: <2013-03-15 14:37:55 Laura>
getGODAS <- function(tb,variable,year,month,rlat,rlon) {
if(any(c(length(rlat),length(rlon))!=2)) {
stop("Provide range for lat/lon coordinates as so: c(minlat,maxlat)") }
if(!exists("channel")) stop("Define object channel (connection to database)")
variable <- paste(unique(variable),collapse=", ")
year <- paste(unique(year),collapse=", ")
month <- paste(unique(month),collapse=", ")
query <- sprintf("select yy, mm, lat, lon, %s from %s where yy in (%s)
and mm in (%s)
and lon between %s and %s and lat between %s and %s",
variable,tb,year,month,
rlon[1]-1,rlon[2]+1,rlat[1]-1,rlat[2]+1)
res <- sqlQuery(channel, query)
}
# [Not used anymore]
# These functions assign coordinates to the appropriate
# GODAS cell lat/lon (works for lat if centered on 0.5)
prep.lat <- function(x) {
latv <- round(seq(-45.5,45.5,by=(1/3)),2)
latv[cut(x,latv,labels=FALSE)] }
prep.lon <- function(x) {
lonv <- round(seq(100.5,280.5,by=1),2)
lonv[cut(x,lonv,labels=FALSE)] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment