2
votes

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)
1
What does nlayers(temp) say?Roman Luštrik
nlayers is 184 in this case. Per file the number of layers is either 184 or 181, two files make one year.Elisah
I think you'll need to provide two or more RasterBricks to actually merge them. Your code as it is, just merges one object, which may not do what you think it does.Roman Luštrik
Thanks for the response Roman. How would you do that in a loop though?Elisah

1 Answers

1
votes

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) 

}