2

My question builds further upon an earlier post How to extract data from a RasterBrick? . I have multiple netCDF files with temperature data at lat, long and depth for different time periods (6 months each). I also have a data frame with lat, long and time for with I would like to extract sea surface temp data out of the netcdf files. Before I extract this data I would like to convert and merge all the netcdf files to one RaterBrick (which is easier to use). When I use a loop to 'brick' and 'merge' the files it creates a RasterLayer object with only one layer in stead of the Raster Brick that I was expecting. I have search the internet but so far I can't find the proper solution for this problem.

I'm quit new to using loops so I'm sorry in advance if I'm asking a question with a really easy answer but I could really use some advice.

Here is what I have done so far:

Create a file list of all the netcdf files.

#Open all files, creats a list of 12 files
    files= list.files('copernicus/daily_temp/',pattern='*.nc', full.names=TRUE)

Try to loop over the file list to create bricks. Merge these multi layer bricks together to form one large RasterBrick with sea surface (level=1) temperature data per day.

# Loop over files to creat a RasterBrick of temp at SST
for(i in 1:length(files)) {
  temp <- brick(files[i], varname="votemper",level=1)
  temp.brick<-merge(temp)}

# Look what one brick is build out of
> temp
class       : RasterBrick 
dimensions  : 375, 297, 111375, 184  (nrow, ncol, ncell, nlayers)
resolution  : 0.11111, 0.06667  (x, y)
extent      : -19.94444, 13.05523, 40.03333, 65.03459  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : C:\Users\PFA\Dropbox\HOM\copernicus\daily_temp\daily_temp_2003_2.nc 
names       : X2003.07.01, X2003.07.02, X2003.07.03, X2003.07.04, X2003.07.05, X2003.07.06, X2003.07.07, X2003.07.08, X2003.07.09, X2003.07.10, X2003.07.11, X2003.07.12, X2003.07.13, X2003.07.14, X2003.07.15, ... 
Date        : 2003-07-01, 2003-12-31 (min, max)
varname     : votemper 
level       : 1 

   # Look at what the merged bricks are build out of 
class       : RasterLayer 
dimensions  : 375, 297, 111375  (nrow, ncol, ncell)
resolution  : 0.11111, 0.06667  (x, y)
extent      : -19.94444, 13.05523, 40.03333, 65.03459  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 280.677, 297.669  (min, max)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Elisah
  • 115
  • 1
  • 1
  • 6

1 Answers1

1

You could try creating the first brick by hand and then loop through the rest of the files, merging them. You may need to switch to work with files on hard drive if this doesn't fit into your RAM. See last ?merge example to see how to do that.

tmp <- brick(files[1], ...) # create first brick
for (i in 2:length(files)) {
  newbrick <- brick(files[i], ...) # this will be the second file in first iteration
  # on first iteration, it will merge file 1 and 2
  # on second interation, merged file (1,2) and file 3
  # on third iteration, merged file (1,2,3) and file 4
  # ...
  mergedbrick <- merge(tmp, newbrick) 

}

Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
  • That works very well. Thanks! There is a small downside to it though. It also changes the layer names making it almost impossible to link the date/time layers to the dataframe date's that I would like to get the temperature data for. ('names : Sea.Water//perature.1, Sea.Water//perature.2, Sea.Water//perature.3, ....') Is there any way keep the original layer names? – Elisah Oct 18 '16 at 15:08
  • simplest way would be to create anarray of original names while looping (e.g., a "growing" arrray where at each cycle you add the names of the last file). then use this to replace the "wrong" ones at the end of the cycle. – lbusett Oct 25 '16 at 17:59