[R-sig-Geo] Classify 106 netcdf's based on their spooled range

Robert J. Hijmans r.hijmans at gmail.com
Tue May 28 06:54:25 CEST 2013


Thiago, you could of course do

mx <- max( cellStats(r, stat='max', na.rm=TRUE) )

but this is faster:

mx <- maxValue(max(r))

for writing to ncdf you can use writeRaster (or a filename argument in
cut, as long as the extension is ".nc".

Robert


On Mon, May 27, 2013 at 9:09 PM, Thiago V. dos Santos
<thi_veloso at yahoo.com.br> wrote:
> Dear all,
>
> I'm working on land use transition data that comes as netcdf. I would like to reclassify each one of these files in 15 classes, based on the min and max values of the entire series.
>
> Although I know this specific dataset ranges from 0 to 0.0680, I'd rather obtain it dinamically, via R. It seems to be a low value, but it makes sense since the unit is fraction of grid, which is relatively coarse (0.5 degree).
>
> So, the criteria to reclassify the files would be (max-min)/15. For example, the class 1 would range from 0 to 0.0045, class 2 from 0.0046 to 0.0090 and so on.
>
>
> However, I'm not sure on the better approach. I wrote a code that stacks all the 106 files and attempts to extract the extreme values, but cellStats returns me a value per layer, instead of a single value.
>
> Here is the fully reproducible code I have so far:
>
> -----------------------------------------
> require(raster) # raster operations
> require(ncdf4)  # write updated netcdf files
>
> # Download, unzip, list and stack files
> # Warning: 9Mb download and 106 files unzipped
> download.file('http://dl.dropboxusercontent.com/u/27700634/transition.zip', 'transition.zip')
> unzip('transition.zip')
> files <- list.files('.', pattern = 'nc', full.names=T)
> r <- stack (files)
>
> # retrieve min and max values of the series
> # there are NA's, so na.rm=T is required
> min <- cellStats(r, stat='min', na.rm=T)
> max <- cellStats(r, stat='max', na.rm=T)
>
> # Define classes, breaks and reclassify raster
> classes <- 15
> brk = c(min, max, by = classes)
> brk = seq(min, max, by = classes)
> rc <- cut(r, breaks=brk)
>
> # Then, write each layer as a netcdf using ncdf4 - in progress
> -----------------------------------------
>
>
> Does anyone have another one/a better ideia to do this?
>
> Thanks in advance,
>
> Greetings,
> --
> Thiago V. dos Santos
> PhD student
> Land and Atmospheric Science
> University of Minnesota
> http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm
> Phone: (612) 323 9898
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> 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