[R-sig-Geo] Masking a raster changes its min and max values

O'Hanlon, Simon J simon.ohanlon at imperial.ac.uk
Thu Aug 16 15:47:46 CEST 2012


Hi Thiago,
mask() simply creates a new raster which sets any cells in the calls first argument (lai.cal) to NA if they are NA in the second argument (qc.cal), the mask raster. As far as I am aware it does not otherwise manipulate values in your original raster. 

I would conclude, that since your rasters have the same extent, resolution and coordinate system, that your mask  raster qc.cal contains NA cells over all cells in your query raster which are 0 and 7. I notice that in your min/max value in qc.cal is 1 not 0 (although this wouldn't necessarily matter, only the location of NA / not NA cells matters).

I do wonder however if you are implying that your data in the lai.cal raster are supposed to be integer values between 0 and 7?

You could just try to plot both rasters to see where the mask non-NA values are and where the 0 and 7 values are in your lai.cal raster. This may show you what is happening?

As it stands it doesn't seem like anything is 'wrong'  as such. Perhaps if this is not what you want, you can outline what output you would like to achieve.

HTH,

Simon

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago Veloso
Sent: 16 August 2012 14:02
To: R-SIG list
Subject: [R-sig-Geo] Masking a raster changes its min and max values

  Dear colleagues,

  To mask this image (note max and min values):

> lai.cal
class       : RasterLayer
dimensions  : 5568, 8289, 46153152  (nrow, ncol, ncell) resolution  : 0.00898, 0.00898  (x, y) extent      : -104.4326, -29.99736, -40.00064, 10  (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values      : /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081042_8103108.grd
layer name  : layer
min value   : 0
max value   : 7 

  I am using raster's mask function. Here is the raster object I am using to mask (which is composed only of 0's and NA's):

> qc.cal
class       : RasterLayer
dimensions  : 5568, 8289, 46153152  (nrow, ncol, ncell) resolution  : 0.00898, 0.00898  (x, y) extent      : -104.4326, -29.99736, -40.00064, 10  (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values      : /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_080805_76515.grd
layer name  : layer
min value   : 1
max value   : 1 


  However, please see the min and max values of the resulting raster:

> laiqc <- mask (lai.cal,qc.cal
> laiqc
class       : RasterLayer
dimensions  : 5568, 8289, 46153152  (nrow, ncol, ncell) resolution  : 0.00898, 0.00898  (x, y) extent      : -104.4326, -29.99736, -40.00064, 10  (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values      : /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081343_910828.grd
layer name  : layer
min value   : 0.1
max value   : 6.9 

  Does anyone know why this happens? How can I prevent this from happening?

  Latest R version running with updated packages. See below my session info:

> sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ncdf_1.6.6      raster_2.0-08   maptools_0.8-16 lattice_0.20-6  foreign_0.8-50  rgdal_0.7-12    sp_0.9-99      

loaded via a namespace (and not attached):
[1] grid_2.15.1  tools_2.15.1

  Thanks in advance,
  Thiago.


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list