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

Robert J. Hijmans r.hijmans at gmail.com
Thu May 30 17:52:48 CEST 2013


I suppose you are saying that the lowest values of your breaks (zero)
are converted to NA. That is because that value is considered to be
outside of the range. See the help file about how to change this (use
argument "include.lowest=TRUE"). You can, of course, also change the
lowest value of your breaks to a somewhat lower value. Robert

On Tue, May 28, 2013 at 8:32 PM, Thiago V. dos Santos
<thi_veloso at yahoo.com.br> wrote:
> Robert,
>
> Thanks, your tip worked. However, the NA's are allocated to the first class.
> Is there a way to keep the NA's as NA's?
>
> Adding na.rm=T in the cut command doesn't work.
>
> Here is the working code:
> --------------------------------------------
> library(raster)
>
> # 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')
>
> # List all files in directory with ".nc" in the name
> files <- list.files('.', pattern = 'nc', full.names=T)
>
> # Read all files and retrieve spooled data range
> s <- stack (files)
> min <- minValue(min(s)) # retrieve min values of the series
> max <- maxValue(max(s)) # ditto, for max
>
> # Define classes, breaks (class increments) and reclassify raster
> # Change this values if other number of classes is needed.
> classes <- 15
> brk = seq(min, max, by = (max-min)/classes)
> sc <- cut(s, breaks=brk)
> --------------------------------------------
>
> Thanks,
>
> --
> 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
> ________________________________
> From: Robert J. Hijmans <r.hijmans at gmail.com>
> To: Thiago V. dos Santos <thi_veloso at yahoo.com.br>
> Cc: R-SIG list <r-sig-geo at r-project.org>
> Sent: Monday, May 27, 2013 11:54 PM
> Subject: Re: [R-sig-Geo] Classify 106 netcdf's based on their spooled range
>
> 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