1

I have two netCDF files with different extents and resolutions. I would like to create rasters from both files that are the same extent and resolution. I want the resolution from one file, and the extent from the other.

Here is the code I'm using:

require(raster);

#Get information
iceMaxNineK <- raster("~/Desktop/TRACE-21k_Data/NineK.ICEFRAC.max.avg.nc")

saltNineK <- brick("~/Desktop/TRACE-21k_Data/NineK.SALT.nc", lvar = 4)

#Making everything nice and uniform and useable
#==============================================
#set up an initial "sampling raster"
e <- extent(0, 360, -90, 90) #xmin,xmax,ymin,ymax
e <- raster(e,nrows=1,ncols=1,crs=saltNineK@crs)
res(e) <- res(saltNineK)
values(e) <- 0

#Resample ice
iceMaxNineK <- resample(iceMaxNineK, e, method="ngb")
plot(iceMaxNineK)

#Resample salt
saltNineK <- resample(saltNineK, e, method="ngb")
plot(saltNineK)

Resampling iceMaxNineK works, but resampling saltNineK results in a map that is jammed up into one corner of the defined area of extent, as shown in the pictures below.

First, iceMaxNineK: iceMaxNineK plot

Second, saltNineK: saltNineK plot

Dimensions of iceMaxNineK before resampling:

class       : RasterLayer 
dimensions  : 48, 96, 4608  (nrow, ncol, ncell)
resolution  : 3.75, 3.708898  (x, y)
extent      : -1.875, 358.125, -89.01354, 89.01354  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : /Users/Hannah/Desktop/TRACE-21k_Data/NineK.ICEFRAC.max.avg.nc 
names       : Fraction.of.sfc.area.covered.by.sea.ice 
z-value     : -8.99945876078469 
zvar        : ICEFRAC 

Dimensions of iceMaxNineK after resampling:

class       : RasterLayer 
dimensions  : 180, 360, 64800  (nrow, ncol, ncell)
resolution  : 1, 1  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : Fraction.of.sfc.area.covered.by.sea.ice 
values      : 0, 0.9997393  (min, max)

Dimensions of saltNineK before resampling:

class       : RasterBrick 
dimensions  : 116, 100, 11600, 25  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 0.5, 100.5, 0.5, 116.5  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : /Users/Hannah/Desktop/TRACE-21k_Data/NineK.SALT.nc 
names       : X400, X1222.02453613281, X2108.88061523438, X3100.537109375, X4239.19677734375, X5577.873046875, X7187.42822265625, X9166.115234375, X11653.9140625, X14854.84765625, X19072.095703125, X24762.70703125, X32618.9296875, X43673.65625, X59384.83984375, ... 
centimeters : 400, 475128.78125 (min, max)
varname     : SALT 
level       : 1 

Dimensions of saltNineK after resampling:

class       : RasterBrick 
dimensions  : 180, 360, 64800, 25  (nrow, ncol, ncell, nlayers)
resolution  : 1, 1  (x, y)
extent      : 0, 360, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       :      X400, X1222.02453613281, X2108.88061523438, X3100.537109375, X4239.19677734375, X5577.873046875, X7187.42822265625, X9166.115234375, X11653.9140625, X14854.84765625, X19072.095703125, X24762.70703125, X32618.9296875, X43673.65625, X59384.83984375, ... 
min values  :  6.842899,          6.850603,          6.853004,        6.853779,          6.853567,       23.148109,         23.148115,       23.148115,      23.148119,       23.148121,        23.148121,       23.148121,      23.148121,    23.148121,       23.148121, ... 
max values  :  39.60786,          39.60786,          39.60783,        39.60777,          39.60769,        39.60766,          39.60765,        39.60757,       39.60755,        39.60742,         39.60739,        39.60732,       39.60730,     39.60730,        39.60730, ... 

Sample files can be accessed via the following link: https://www.dropbox.com/s/x8oqem317vmr7yq/DataForRResample.zip?dl=0

Thank you for your time.

6
  • why are you calling brick on the saltNinek instead of raster?
    – Stedy
    Oct 28, 2014 at 23:19
  • I'm calling brick because the netCDF that is the source for saltNineK is a 25 layer stack, and I will need all of those layers for the next step in data processing. Each layer is a depth slice of the ocean--so surface to 40m, 40m to 120m, etc. I will be creating a single grid by creating a mosaic of the bottom-most measurements from each grid cell.
    – Hannah O.
    Oct 29, 2014 at 15:00
  • I am having a really hard time finding a similar dataset to try to replicate this on. Can you either post the data or a link to a similar public netcdf?
    – Stedy
    Oct 30, 2014 at 18:54
  • There is not a similar public netCDF. I'm trying to find a kosher way to share the problem files, but it seems to be frowned on to link to external data...
    – Hannah O.
    Nov 11, 2014 at 20:26
  • I would say put up a link, most of the frowning upon is for poorly worded vague questions with a link to some data. Yours is well-defined and pretty clear.
    – Stedy
    Nov 12, 2014 at 6:14

1 Answer 1

0

I have now solved this question. The problem was the input file, which was at a T31_gx3v5 resolution (http://www.cgd.ucar.edu/ccr/TraCE/; Yeager et al., 2006; Otto-Bliesner et al., 2006). R doesn't pick up on this. The layers needed to be regridded to 1X1 degree using the ncl language before importing them into R. For more information on regridding in ncl, follow this link: https://www.ncl.ucar.edu/Document/Functions/Pop_remap/PopLatLon.shtml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.