Greetings -
Some inquisitive human beings asked that I post responses to this thread back
to the list. A asterisks-delimited quadruplex of such follows. ;)
Smiles
=B Brian
*****************
One approach - which I found to be pleasantly succinct, straightforward and
easy to implement - is a little ditty in R that goes like this: (Thanks to Ben
Taft and Dr. Peter Gould for providing variations on this approach!)
Say your dataset is named "climate" with variables of Station, Year, Date, and
DDay (degree day).
## first create a unique station-year identifier (ID)
climate$ID = paste(climate$Station, climate$Year,sep="_")
## sort dataset by station-year identifier (ID) and date
climate = climate[order(climate$ID,climate$Date),]
## take the cumulative sum for each unique station year
climate$CumDD = unsplit(lapply(split(climate$DDay,
climate$ID),cumsum),climate$ID)
## that's it!
*****************
There are apparently ways to do this in the National Center for Atmospheric
Research Command Language, NCL (www.ncl.ucar.edu), which I didn't really look
into in much detail because it would mean learning a new language with a fairly
steep learning curve. However, NCL appears to be a very powerful language-tool
for spatial data. (Thanks to Ruben van Hooidonk for opening this avenue and
for generously providing some example script.)
*****************
And this from Adam Sibley (thank you, Adam!):
"What you might want is a matrix where rows = stations, columns = julian days
from start of record to end (see the julian function in R). Lets consider an
individual station: for each day, calculate the gdd and add it to the previous
column in the matrix:
for(i in 1:dim(gdd_matrix)[1]){
for(j in 2:dim(gdd_matrix)[2]){
x = todays gdd
gdd_matrix[i,j] = x + gdd_matrix[i,j-1]
}
}
To get the accumulation to start over at the start of each year, you could
include an if statement that tests if your column corresponds to the first day
of a year; if it is the first day of the year, then gdd_matrix[i,j] = x, else
gdd_matrix[i,j] = x + gdd_matrix[i,j-1]. I'm a little pressed for time so I
can't really code it all out, but if you look into the function as.Date to
convert your column index to a date, substr to chop off the year from the date
string and the function identical to compare the month/date information you are
left with the string '01-01', you should be able to accomplish what you need
without much trouble."
*****************
There were also some suggestions of using nested do loops in SAS. (Thank you,
Mystery Contributors!)
-----Original Message-----
From: Chalfant, Brian
Sent: Wednesday, February 16, 2011 10:15 AM
To: [email protected]
Subject: Accumulating degree days with oodles of data.
Hello Ecolog-gers!
Anyone have ideas on how to calculate accumulated degree days for a larger
dataset?
The data I'm working with consists of daily maximum and minimum air temperature
readings from a few score of sites across Pennsylvania over the last 20 years.
What I want to do is to calculate accumulated degree days for each year at each
site. I'm going with calendar year for simplicity's sake. I know how to
calculate degree days, it's the accumulation part I'm stumbling on. And I know
how to sum degree days for each year and each station - that's easy. I also
know how to calculate time-lagged running averages, but that's not really what
I'm aiming for here. I want the degrees days to accumulate each successive
(Julian) day at each station for each year, and to start over accumulating for
each new station and/or year combination. That is to say, for 1 May 2008 at
the Chalk Hill site, I want to sum all the degree days from 1 January 2008 to
30 April 2008 at that site. The only way I'm presently aware of to do this is
"by hand" in Excel, and that's not really practical given the size of the
dataset I'm working with and temporal allocation considerations imposed by
mortality. Does any one know of a script (R? SAS?) to do this? I can envision
some, but they get unwieldy pretty quick. Or is a way this can be done with a
database query?
Any thoughts would be much appreciated!
Smiles
=] Brian
Brian A. Chalfant | -ologist
Pennsylvania Department of Environmental Protection
Rachel Carson State Office Building
400 Market Street | Harrisburg, PA 17101
Phone: 717.787.9639 | Fax: 717.772.3249
www.depweb.state.pa.us<http://www.depweb.state.pa.us/>