Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Sea Surface Temperature text files in ASCII format into R

I have downloaded multiple .txt.gz files for Hadley Sea Surface Temperature observations. The data have been unzipped, resulting in mutiple .txt files in ASCII format.
I have the following files (the R script is the one I'm working on):

list.files()
 [1] "Get_SST_Data.R"                "HadISST1_SST_1931-1960.txt"    "HadISST1_SST_1931-1960.txt.gz"
 [4] "HadISST1_SST_1961-1990.txt"    "HadISST1_SST_1961-1990.txt.gz" "HadISST1_SST_1991-2003.txt"   
 [7] "HadISST1_SST_2004.txt"         "HadISST1_SST_2005.txt"         "HadISST1_SST_2006.txt"        
[10] "HadISST1_SST_2007.txt"         "HadISST1_SST_2008.txt"         "HadISST1_SST_2009.txt"        
[13] "HadISST1_SST_2010.txt"         "HadISST1_SST_2011.txt"         "HadISST1_SST_2012.txt"        
[16] "HadISST1_SST_2013.txt"    

I would like to be able to utilize the temperature data to make a numeric vector for the Sea Surface Temperature for everyday since 1950, to eventually make a time series plot.

Which will look something like this

enter image description here

[p.s. this is just for reference...]

Thanks in advance!

like image 798
emboylen Avatar asked Jun 09 '14 06:06

emboylen


1 Answers

NetCDF is definitely a better way to go since the format of the ascii data is pretty horrible. That said, here's a function that reads in the data you have downloaded.

read.things <- function(f) {
  # f is the file path of your ascii data
  require(raster)
  d <- readLines(f)
  d <- split(d, rep(1:12, each=181))
  d <- lapply(d, function(x) read.fwf(textConnection(x), rep(6, 360), 
                                      skip=1, stringsAsFactors=FALSE,
                                      na.strings=c(-1000, -32768)))
  d <- lapply(d, function(x) sapply(x, as.numeric))
  out <- stack(lapply(d, raster))
  names(out) <- month.abb
  extent(out) <- c(-180, 180, -90, 90)
  out/100
}

Note that I've set 100% ice cells (-100) and land cells (-32768) as NA.

Below, we download one of the files (1Mb) as an example:

download.file(
  'http://www.metoffice.gov.uk/hadobs/hadisst/data/HadISST1_SST_2004.txt.gz',
  destfile= {f <- tempfile()})

s <- read.things(f)

s

# class       : RasterBrick 
# dimensions  : 180, 360, 64800, 12  (nrow, ncol, ncell, nlayers)
# resolution  : 1, 1  (x, y)
# extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
# coord. ref. : NA 
# data source : in memory
# names       :   Jan,   Feb,   Mar,   Apr,   May,   Jun,   Jul,   Aug,   Sep,   Oct,   Nov,   Dec 
# min values  :   -10,   -10,   -10,   -10,   -10,   -10,   -10,   -10,   -10,   -10,   -10,   -10 
# max values  : 30.34, 30.58, 30.43, 30.50, 30.83, 31.39, 32.71, 33.40, 32.61, 31.52, 30.60, 30.51 

library(rasterVis)
levelplot(s, at=seq(min(s[], na.rm=T), max(s[], na.rm=T), len=100),
          col.regions=colorRampPalette(c('#2c7bb6', '#abd9e9', '#ffffbf', 
                                         '#fdae61', '#d7191c')))

enter image description here

like image 54
jbaums Avatar answered Oct 20 '22 01:10

jbaums