-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Semantics of crop(SpatRaster, extent) on SpatRaster attributes #1302
Comments
Thanks. The units getting lost was a bug and that is fixed now. |
Why are var/long-names not conserved if multiple layers and sources? library(terra)
#> terra 1.7.46
theFiles = c("/Volumes/Samples/InputData/cache/A.P1D.20230821T053000Z.aust.chl_gsm.nc",
"/Volumes/Samples/InputData/cache/A.P1D.20230822T053000Z.aust.chl_gsm.nc",
"/Volumes/Samples/InputData/cache/A.P1D.20230823T053000Z.aust.chl_gsm.nc"
)
theRast = rast(theFiles)
theRast
#> class : SpatRaster
#> dimensions : 7001, 10001, 3 (nrow, ncol, nlyr)
#> resolution : 0.01, 0.01 (x, y)
#> extent : 79.995, 180.005, -60.005, 10.005 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> sources : A.P1D.20230821T053000Z.aust.chl_gsm.nc
#> A.P1D.20230822T053000Z.aust.chl_gsm.nc
#> A.P1D.20230823T053000Z.aust.chl_gsm.nc
#> varnames : chl_gsm (Chlorophyll Concentration, GSM model)
#> chl_gsm (Chlorophyll Concentration, GSM model)
#> chl_gsm (Chlorophyll Concentration, GSM model)
#> names : chl_gsm, chl_gsm, chl_gsm
#> unit : mg/m^3, mg/m^3, mg/m^3
#> time (days) : 2023-08-21 to 2023-08-23
theUnits = units(theRast)
longNames <- longnames(theRast)
vn <-varnames(theRast)
theRast<-crop(theRast,ext(c(140,150,-50,-10)))
theRast
#> class : SpatRaster
#> dimensions : 4000, 1000, 3 (nrow, ncol, nlyr)
#> resolution : 0.01, 0.01 (x, y)
#> extent : 140.005, 150.005, -49.995, -9.995 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source(s) : memory
#> names : chl_gsm, chl_gsm, chl_gsm
#> min values : 0.001007809, 0.001007874, 0.001009486
#> max values : 98.747230530, 95.338264465, 98.991081238
#> time (days) : 2023-08-21 to 2023-08-23
varnames(theRast) <- vn
#> Error: [varnames<-,SpatRaster] cannot set these names
longnames(theRast) <- longNames
#> Error: [longnames<-,SpatRaster] cannot set these names
units(theRast) <- theUnits
theRast
#> class : SpatRaster
#> dimensions : 4000, 1000, 3 (nrow, ncol, nlyr)
#> resolution : 0.01, 0.01 (x, y)
#> extent : 140.005, 150.005, -49.995, -9.995 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84
#> source(s) : memory
#> names : chl_gsm, chl_gsm, chl_gsm
#> min values : 0.001007809, 0.001007874, 0.001009486
#> max values : 98.747230530, 95.338264465, 98.991081238
#> unit : mg/m^3, mg/m^3, mg/m^3
#> time (days) : 2023-08-21 to 2023-08-23 Created on 2023-10-06 with reprex v2.0.2 |
var/long-names are properties of "data sources". These may exist in the input, but since the output is always a single data source, they cannot always be maintained in the output. "terra" now keeps them if they are all the same, as in your example. In other cases you would need to process variable by variable (data source); perhaps using SpatRasterDatasets. I have also expanded the docs to clarify this a bit. |
I am trying to work out the behavior of SpatRaster attributes when created from a netcdf file.
The example is below (the .nc file) that is a grid of readings off satellite
Before the crop I have all the data including the units.
After the crop - the attributes units, varname are dropped and can only be reconstructed using copy and assign functions.
Somewhat surprisingly when I create the raster in a different way, and crop, attribute units is preserved.
See the second example.
crop() clearly preserves some attributes, but not all and does not appear to do so consistently.
I am sure I am missing something here - as only a few weeks starting with Terra
I want to carry as many attributes forward as possible as part of developing a data pipeline to do multiple data source analysis on geo-graphic regions.
Many thanks
Created on 2023-10-05 with reprex v2.0.2
Created on 2023-10-05 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: