Home > Uncategorized > MoshTemp201

MoshTemp201

Time to get Organized!

In MoshTemp201 we are going to spend a bunch of time organizing our resources. While not the final form the  directory structure you see is the one we will be working with for a while. The code from now on will be dropped in Zip files. Or individual files that you can drop into the structure that we set up.

Moving forward we need to organize our code and data to keep from getting overwhelmed. When  unzip moshtemp 201 you should get a file system that looks something like the structure above. Lets start, with a list of what you get and what it does.

v2.mean.Z and v2.mean. I’ve included the compressed file of GHCN v2.mean and the unzipped version.

land_water_masks_xdeg.zip and land_water_masks_xdeg (Folder). These files will be generated AFTER you run moshtemp201 successfully. We download and unzip the file for you. Inside that folder when your done you’ll find the land mask file. Its a 1 degree file of land coverage percentages.

InstallPackages.R. If you want to know what packages we will use here the file shows you. Get started early and install these packages. You can run the file, or do it using your package manager. Let me know if there are any missing dependencies. I do need to work on this aspect of the system build and haven’t dug into managing packages.

install.packages(“raster”)

install.packages(“sp”)

install.packages(“maps”)

install.packages(“zoo”)

install.packages(“R.utils”)

install.packages(“uncompress”)


Moshtemp10X(Folder) All the prior code for you pleasure.

V2mean.Rdata and V2Anomalies.Rdata. These two files will be created when you moshtemp201. It takes a LONG time, but we only have to do it once. Sorry about that. From here on out we just use the V2Anomalies data. They form the basis of everything we do. These are temperature Normals, in the style of CRU. The only reason for regenerating this file is to recalculate your window (1900-2009) or your anomaly period. The file contains everything you need. I may update this, by making the object more self documenting, that is by encoding the camCriteria and file dates into a larger object. Later on that, need to test it.

Global.R This file is key to everything we write from here on out. It executes many scripts located in the functions folder. All the functions we write are going to be “organized” loosely into a different scripts. For now some of the scripts are just empty dummy files. The routines will be added as we go along. By the time we are done all the scripts will have more code. Plus if you want to add something ( Like graphics) you can see where it goes. I’ve tried to modularize things so that graphics are independent of the programs you will write. The scripts we write will all take on the same form: get the data; process it; output it; graph it. I like to do things that way, because I may come back to a study I did and decide to graph other things without running the whole thing over.  The various scripts are named as below:

source(file.path(“Functions”,”FileNames.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”Constants.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”Url.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”InvFunctions.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”GeoFunctions.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”DataFunctions.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”Graphics.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”OutputFunctions.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”RObjects.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”AnalysisFunctions.R”,fsep=.Platform$file.sep))

source(file.path(“Functions”,”Download.R”,fsep=.Platform$file.sep))

The next bit is the Functions folder. which contains a bunch of scripts.

The scripts include, constants, filenames,graphics,datafunctions,download,inventory functions, analysis functions, scripts for Robjects or Rdata files and urls. As I said some of these files, like Url.R are fully developed. Url for example contains the urls of most of the data I have worked with on this project. There will be more. For Moshtemp201, there are a few key ones so we will walk through those first, leaving the others for your self enjoyment, confusion or whatever.

download.R is straightforward and requires little explanation

downloadMask<-function(){

download.file(url_Land_Mask,Land_Mask_Zip)

unzip(Land_Mask_Zip,files=Mask_File,exdir=getwd())

}

downloadV2Mean<-function(){

download.file(url_GhcnV2mean_Zipped,GhcnV2_Zipped)

# add code for uncompress

}

downloadGhcnInv<-function(){

}

In Moshtemp201, we will just download the land mask and unzip it. And you see what is coming as we have other files to download. Also, I still need to report a problem on uncompress to the maintainer.

Next RObjects

V2MEAN.RDATA<-“V2Mean.Rdata”

V2ANOMALIES.RDATA<-“V2Anomalies.Rdata”

Again straightforward. As we go along there will be a few more .Rdata files that we save, so I collect all the names here. I hate having quoted strings in my code. Typos R Us.

Next Output functions. I will collect all data output in this place. Grunt code. In the final version there are also routines for outputing all .Rdata files as ascii, for data exchange with non R systems. You’ll find some grunt code that starts to lay the foundation for that, but here is the workhorse function:

saveManyOBJ<-function(objects,directories,filenames){

o<-length(objects)

f<-length(filenames)

d<-length(directories)

if(!((o==f)&(o==d))) stop(“number of objects must match file names and directores”)

for(calls in 1:length(objects)){

saveOBJ(objects[[calls]],

directories[[calls]],

filenames[[calls]])

}

}

Just a function that takes three lists: a list of objects, a list of directories and filenames. When we get to the end, if we need to write out 5 .Rdata files, then we just call this function and it takes care of the rest. It calls the saveOBJ function which checks your file extension and writes the object. You’ll also find a function that takes a “xyxx.Rdata” file name and switches it to “xyxx.dat” for ascii output. So if you save V2mean.Rdata as.ascii() then the system will switch the names and call the right file writing method.

That’s It.

Now, for Moshtemp201

source(“Global.R”)

downloadMask()

V2mean<-averageDuplicates(windowYears(readV2Mean(),yearRange))

V2mean<-V2mean[in.Base(V2mean,camCriteria),]

V2Anomalies<-as.anomaly(as.Zoo(V2mean),camCriteria)

##########

#now write the Rdata objects

myobjects<-list(V2mean,V2Anomalies)

mydirectories<-list(getwd(),getwd())

mynames<-list(V2MEAN.RDATA,V2ANOMALIES.RDATA)

saveManyOBJ(myobjects,mydirectories,mynames)

This will take a long time to run as we push up to R’s memory limit at least on the MAC. But thankfully we only have to do it once. we download the land mask. It is unzipped for us. Then we run the functions that we learned about in Moshtemp101-105. Finally, we test out our output function by writing the .Rdata files to our working directory. Should work. Lemme know if you have troubles.
Finally test this at the console:
> Land<-getPercentLand()
> Land
class       : RasterLayer
filename    :
nrow        : 60
ncol        : 120
ncell       : 7200
min value   : 0
max value   : 1
projection  : +proj=longlat +datum=WGS84
xmin        : -180
xmax        : 180
ymin        : -90
ymax        : 90
xres        : 3
yres        : 3
If you see that then you have successfully downloaded the land mask. Try this:

> cellFromXY(Land,c(0,0))
[1] 3661
> cellValues(Land,cellFromXY(Land,c(100,27)))
1
> Area<-area(Land)
> cellValues(Area,cellFromXY(Area,c(100,27)))
100661.6
The first command gets the ‘cell’ number of the longtitude/latitude pair 0,0 The world is a big long vector of cells based on the size of the grid I’ve selected in the constants.R file.
The second command gets the actual value of the FRACTION of land in the specified lon/lat.
The third command and fourth command get you a New raster of Area (sqkm) and then reports the actual area for a cell. Slick. Steve hates trig, so we will NOT mess around with any code that calculates weights of cells based on sin and cos and earth radius and degrees to radian or any of that. The package will assign stations to grid cells so NO hand written binning problems, boundary issues, issues at the pole. None of that nonsense to clutter up our code. Raster to the rescue. Cleaner code, always good.
Code at the drop in a Zip. Download. Unzip. Make it your working directory! or suffer the pain of file not found.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: