Automated downloading of global fire maps from Nasa using R to write commands to the shell

fires2006.gif

Many actions that could be done using shell scripts can be achieved within R. The R syntax can be used to form a command that is passed to the shell using either shell() ,in Windows, or system() in Linux. For iterated commands this can often be simpler than working out an alternative as a shell script.

With minor changes the same script will usually work either in Windows or Linux.

This example downloads a set of images from Nasa. The tricky part is to format the URL correctly to be consistent with the naming convention used for the image files.

Windows users often don’t have wget installed although it a default part of part of any Linux install. It is a small, powerful program and is is free and simple to use (http://www.gnu.org/software/wget/). Assuming wget has been placed in a directory “c:/wget” the following lines in R will download jpeg images from the nasa site at thirty day intervals.


for (i in seq(1,370,by=30)){
URL<-paste("c:/wget/wget http://rapidfire.sci.gsfc.nasa.gov/firemaps/firemap.2006"
,
sprintf("%03.0f", i) ,
"-2006",
sprintf("%03.0f", i+9),
".8192x4096.jpg",
sep="")

shell(URL)
}

########################################

#For Linux.

for (i in seq(1,370,by=10)){
URL<-paste("wget http://rapidfire.sci.gsfc.nasa.gov/firemaps/firemap.2006"
,
sprintf("%03.0f", i) ,
"-2006",
sprintf("%03.0f", i+9),
".8192x4096.jpg",
sep="")

system(URL)
}

The Nasa site has files of varying resolutions which each show the accumulated fires at ten day intervals. A nice trick in Linux is to download all the files for a year and paste them together using image magic to form an animated gif as shown at the top of this page. Larger files obviously look better.

for (i in seq(1,370,by=10)){
URL<-paste("wget http://rapidfire.sci.gsfc.nasa.gov/firemaps/firemap.2006"
,
sprintf("%03.0f", i) ,
"-2006",
sprintf("%03.0f", i+9),
".2048x1024.jpg",
sep="")

system(URL)
}

system ("convert -geometry 200 -delay 200 -loop 0 *.jpg fires2006.gif")

firemap.jpg


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