Import text files sea surface temperature in ASCII format in R

I uploaded a few .txt.gz files for Hadley Sea Surface Temperature Monitoring . The data was unpacked, resulting in several .txt files in ASCII format .
I have the following files (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 use temperature data to create a numerical vector of sea surface temperature every day since 1950, in order to ultimately plot a time series.

That will look something like this.

enter image description here

[ps this is for reference only ...]

Thanks in advance!

+4
3

[: Linux]

R NetCDF (http://www.metoffice.gov.uk/hadobs/hadisst/data/HadISST_sst.nc.gz). "" , :

library(raster)
library(xts)
library(caTools)    

:

startYear <- 1950   # start of the period
endYear <- 2011     # end of the period
subp <- '1951-01-01/1980-12-01'   # period for the climatology calculation

:

sst <- brick('HadISST_sst.nc')
Date <- substr(names(sst),2,11) 
Date <- gsub('\\.', '\\-', Date)
Date <- as.Date(Date)
dstart <- paste(startYear,'01','01',sep='-'); dstart <- grep(dstart, Date)
dend <- paste(endYear,'12','01',sep='-'); dend <- grep(dend, Date)
sst <- subset(sst, dstart:dend)
Date <- Date[dstart:dend]

(lat = 35, lon = 120):

tserie <- as.vector(extract(sst, cbind(116, -35)))
tserie <- xts(tserie, order.by=Date)

:

clim <- as.numeric()
for(ii in 1:12){
  clim[ii] <- mean(tserie[subp][(.indexmon(tserie[subp])+1) == ii])
}
clim <- xts(rep(clim, length(tserie)/12), order.by=Date)

:

tserie <- tserie - clim

:

par(las=1)
plot(tserie, t='n', main='HadISST')
lines(tserie, col='grey')
lines(xts(runmean(tserie, 12), order.by=Date), col='red', lwd=2)
legend('bottomleft', c('Monthly anomaly','12-month moving avg'), lty=c(1,1), lwd=c(1,2), col=c('grey','red'))
+3

NetCDF - , , , ascii . , , , .

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
}

, 100% (-100) (-32768) NA.

(1Mb) :

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

+3

You get an error because if you look at the structure of dates, they always go from the 16th to the 16th. If you replace:

dstart <- paste(startYear,'01','01',sep='-'); dstart <- grep(dstart, Date)
dend <- paste(endYear,'12','01',sep='-'); dend <- grep(dend, Date)

for

dstart <- paste(startYear,'01','16',sep='-'); dstart <- grep(dstart, Date)
dend <- paste(endYear,'12','16',sep='-'); dend <- grep(dend, Date)

He will work.

+1
source

Source: https://habr.com/ru/post/1543826/


All Articles