Related
Simply trying to use a vector (.shp) to mask a SpatRaster using terra::mask; get the following error
>Error: \[mask\] cannot create dataset
LCC84 <- rast("C:/Users_forest_VLCE2_1984.tif")
vec <- vect("C:/Users/Land_Management_Units.shp")
vec_proj <- project(vec, LCC84)
LCC84_masked <- terra::mask(LCC84, vec_proj)
Error: [mask] cannot create dataset
vec
#class : SpatVector
#geometry : polygons
#dimensions : 1, 8 (geometries, attributes)
#extent : -117.3165, -115.1691, 50.70613, 52.27127 (xmin, xmax, ymin, ymax)
#coord. ref. : lon/lat NAD83 (EPSG:4269)
LCC84
#class : SpatRaster
#dimensions : 128340, 193936, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -2660911, 3157169, -851351.9, 2998848 (xmin, xmax, ymin, ymax)
#coord. ref.: Lambert_Conformal_Conic_2SP
#source : CA_forest_VLCE2_1984.tif
#name : CA_forest_VLCE2_1984
crs(LCC84, proj=TRUE)
[1] "+proj=lcc +lat_0=49 +lon_0=-95 +lat_1=49 +lat_2=77 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs"
You can use the following code
library(terra)
library(sf)
#Read the data
LCC84 <- rast("C:/Users_forest_VLCE2_1984.tif")
vec <- st_read("C:/Users/Land_Management_Units.shp")
#Convert the crs of shapefile
vec_proj <- sf::st_transform(vec, crs(LCC84))
#Masking the raster using the shapefile
LCC84_masked <- terra::mask(LCC84, vec_proj)
It works for me with the data you provided
library(terra)
#terra 1.6.51
v <- vect("Extent_BNP_Extact.shp")
r <- rast("CA_forest_VLCE2_1984.tif")
pv <- project(v, r)
z <- crop(r, pv, mask=T)
r
#class : SpatRaster
#dimensions : 128340, 193936, 1 (nrow, ncol, nlyr)
#resolution : 30, 30 (x, y)
#extent : -2660911, 3157169, -851351.9, 2998848 (xmin, xmax, ymin, ymax)
#coord. ref. : Lambert_Conformal_Conic_2SP
#source : CA_forest_VLCE2_1984.tif
#name : CA_forest_VLCE2_1984
v
# class : SpatVector
# geometry : polygons
# dimensions : 1, 2 (geometries, attributes)
# extent : 474065.5, 635666.6, 5613645, 5798288 (xmin, xmax, ymin, ymax)
# source : Extent_BNP_Extact.shp
# coord. ref. : NAD83 / UTM zone 11N (EPSG:26911)
# names : Shape_Leng Shape_Area
# type : <num> <num>
# values : 6.744e+05 2.828e+10
plot(z)
First cropping seems logical here because the entire dataset has ~25 billion cells. I also tried
mask(r, pv)
It took a while to run, but it worked. If it does not for you, my guess would be that you may not have sufficient disk-space in your temp folder. See terraOptions() for the location of the temp folder.
Also, you do the equivalent of
pv <- project(v, "EPSG:4269")
But that makes no sense, as your raster data do not have the EPSG:4269 coordinate reference system (lon/lat NAD83).
I am trying to calculate the mean of a rasterbrick over specific timesteps in R.
Subsetting the dataset shows a lay-out that appears to be correct (to me), however R does not allow any computations to follow up.
> r_sub <- b[[1699:1862]]
> r_sub
class : RasterStack
dimensions : 127, 147, 18669, 164 (nrow, ncol, ncell, nlayers)
resolution : 5, 5 (x, y)
extent : 603299.4, 604034.4, 6615598, 6616233 (xmin, xmax, ymin, ymax)
crs : NA
names : X2019.02.09.19, X2019.02.09.20, X2019.02.09.21, X2019.02.09.22, X2019.02.09.23, X2019.02.09.24, X2019.02.10.1, X2019.02.10.2, X2019.02.10.3, X2019.02.10.4, X2019.02.10.5, X2019.02.10.6, X2019.02.10.7, X2019.02.10.8, X2019.02.10.9, ...
> r_mean <- calc(r_sub, mean)
Error in Rsx_nc4_get_vara_double: NetCDF: Index exceeds dimension bound
Var: SWIT Ndims: 3 Start: 1698,0,0 Count: 1,127,147
Error in ncvar_get_inner(ncid2use, varid2use, nc$var[[li]]$missval, addOffset, :
C function R_nc4_get_vara_double returned error
How to solve this specific error?
I am guessing that this is related to a problem with your file, not with the code.
I did this and it worked well
library(raster)
b <- brick("filename.nc")
r_sub <- b[[1699:1862]]
r_sub
#class : RasterStack
#dimensions : 160, 320, 51200, 164 (nrow, ncol, ncell, nlayers)
#resolution : 1.125, 1.121277 (x, y)
#extent : -0.5625, 359.4375, -89.70216, 89.70216 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
mean(r_sub)
#class : RasterLayer
#dimensions : 160, 320, 51200 (nrow, ncol, ncell)
#resolution : 1.125, 1.121277 (x, y)
#extent : -0.5625, 359.4375, -89.70216, 89.70216 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
#source : memory
#names : layer
#values : 3.033032e-07, 0.0002482847 (min, max)
Also, you might want to look into using stackApply
I am using GLASS albedo data stored here for pre-2000 (AVHRR) data and here for post-2000 data (MODIS). My end goal is to create a raster stack of each month that contains white sky albedo data from 1982-2015. The problem I have run into is that the MODIS and AVHRR data are in different spatial reference systems and I can't seem to reproject them to be in the same system.
I convert from hdf to tif using R like this:
fileavhrr <- ".../GLASS02B05.V04.A1990161.2018062.hdf"
filemodis<-".../GLASS02B06.V04.A2013169.2017128.hdf"
gdal_translate(get_subdatasets(filemodis)[10], dst_dataset =
".../modis.tif")
gdal_translate(get_subdatasets(fileavhrr)[8], projwin = c(-180,90,180,50), dst_dataset = ".../avhrr.tif") #ideally I'd only like data north of 50 degrees
avhrr<- raster(".../avhrr.tif")
#class : RasterLayer
#dimensions : 800, 7200, 5760000 (nrow, ncol, ncell)
#resolution : 0.05, 0.05 (x, y)
#extent : -180, 180, 50, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +ellps=clrk66 +no_defs
#values : -32768, 32767 (min, max)
modis<- raster(".../modis.tif")
#class : RasterLayer
#dimensions : 3600, 7200, 25920000 (nrow, ncol, ncell)
#resolution : 154.4376, 308.8751 (x, y)
#extent : -20015109, -18903159, 8895604, 10007555 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181
+b=6371007.181 +units=m +no_defs
#values : -32768, 32767 (min, max)
Here are things I have tried:
1.) Use the MODIS Reprojection Tool. For whatever reason, this tool seems to think the subdatasets of the MODIS .hdf files are only one tile (the upper left most tile, tile 0,0) and not the global dataset. My understanding is that the MODIS data are global (not in tiles?), so I do not know why the MRT is doing this.
2.) Use the raster package in R.
projectedMODIS <- projectRaster(modis,avhrr,method="bilinear")
This returns a raster with values that are all NA:
class : RasterLayer
dimensions : 800, 7200, 5760000 (nrow,> ncol, ncell)
resolution : 0.05, 0.05 (x, y)
extent : -180, 180,> 50, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=clrk66 +no_defs
values : NA, NA (min, max)
3.) Use the gdalUtils package in R:
gdalwarp(srcfile=get_subdatasets(filemodis)[10], dstfile= ".../gdalMODIS_avhrr.tif", s_srs = crs(modis), t_srs =crs(avhrr) )
This returns a raster with essentially no spatial extent.
gdalMODISavhrr<-raster(".../gdalMODIS_avhrr.tif")
#class : RasterLayer
#dimensions : 357, 12850, 4587450 (nrow, ncol, ncell)
#resolution : 0.02801551, 0.02801573 (x, y)
#extent : -180, 179.9993, 79.99838, 90 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +ellps=clrk66 +no_defs
#values : -32768, 32767 (min, max)
Any ideas on why reprojecting this MODIS data is so difficult?
I have not tried this, but from looking at the package gdalUtils, the function gdalwarp() might do what you need?
I have two geotiff raster files, one has all the metadata information and in the other the metadata information was lost. I know that all the metadata information was exactly the same so I want to copy it from one file to the other. I tried to use raster, because I made all the processing in R.
This is the file with metadata
af1_patch <-raster(a_files[6])
af1_patch
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 231.656, 231.656 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
data source : Forest_patches_AF_1.tif
names : Forest_patches_AF_1
values : 0, 255 (min, max)
And this is the file without the metadata
af1_area <-raster(a_files[1])
af1_area
class : RasterLayer
dimensions : 38400, 38400, 1474560000 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 0, 38400, 0, 38400 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : africa_AF_1.tif
names : africa_AF_1
values : 0, 255 (min, max)
I tried to copy the metadata using:
res(af1_area) <- res(af1_patch)
crs(af1_area) <- crs(af1_patch)
extent(af1_area) <- extent(af1_patch)
but it doesn't work, dimensions and resolution are incorrect and the data values are lost:
af1_area
class : RasterLayer
dimensions : 166, 166, 27556 (nrow, ncol, ncell)
resolution : 53588, 53588 (x, y)
extent : -2223901, 6671703, -4447802, 4447802 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
hist(af1_area)
Error in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...) :
cannot make a histogram; need data on disk or in memory
Thanks!
I think that this is occuring because you are changing the resolution before changeing the extent, because this, the extent is bound by the resolution that you have assigned to it. I was able to reproduce your problem and solve it by changing the order of the process. Hope it works for you!
library(raster)
x <- raster(matrix(1:10))
proj4string(x) <- CRS("+proj=longlat")
extent(x) <- extent(-10,10,-10,10)
y <- raster(matrix(1:10))
z <- raster(matrix(1:10))
#Current Process
res(y) <- res(x)
crs(y) <- crs(x)
extent(y) <- extent(x)
#Working Process
extent(z) <- extent(x)
res(z) <- res(x)
crs(z) <- crs(x)
Output:
> x
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
> y
class : RasterLayer
dimensions : 1, 1, 1 (nrow, ncol, ncell)
resolution : 20, 20 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
> z
class : RasterLayer
dimensions : 10, 1, 10 (nrow, ncol, ncell)
resolution : 20, 2 (x, y)
extent : -10, 10, -10, 10 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +ellps=WGS84
data source : in memory
names : layer
values : 1, 10 (min, max)
I would just reassign the values which will preserve the metadata, and re-write the file:
af1_patch <-raster(a_files[6])
af1_area <-raster(a_files[1])
af1_patch[] <- af1_area[]
#writeRaster(af1_patch, a_files[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:
Second, saltNineK:
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.
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.