Well, after a deep reading of raster manual I found setMinMax, which is exactly what I was looking for.

Congratulations for the brilliant package and documentation as well.
 
Best,
--
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


________________________________

To: Robert J. Hijmans <r.hijmans@gmail.com> 
Cc: R-SIG list <r-sig-geo@r-project.org> 
Sent: Friday, May 31, 2013 11:00 AM
Subject: Re: [R-sig-Geo] Classify 106 netcdf's based on their spooled range


Robert,

Thank you, it worked. 

Just a quick follow-up: is there a way to retrieve the extreme values of a layer of a RasterStack object that is not yet in the memory?

I receive error trying to do that. My s object below is a stack with the 106 netcdf files.

> files <- list.files('.', pattern = 'nc', full.names=T)

> s <- stack (files)


> s
class       : RasterStack 
dimensions  : 360, 720, 259200, 106  (nrow, ncol, ncell, nlayers)
resolution  : 0.5, 0.5  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
names       : transition.1, transition.2, transition.3, transition.4, transition.5, transition.6, transition.7, transition.8, transition.9, transition.10, transition.11, transition.12, transition.13, transition.14, transition.15, ... 
min values  :           NA,           NA,           NA,           NA,           NA,           NA,           NA,           NA,           NA,            NA,            NA,            NA,            NA,            NA,            NA, ... 
max values  :           NA,           NA,           NA,           NA,           NA,           NA,           NA,           NA,           NA,            NA,            NA,            NA,            NA,            NA,            NA, ... 

> inMemory(s)
[1] FALSE

> maxValue(s[[3]])

[1] NA
Warning message:
In .local(x, ...) : max value not known, use setMinMax

> minValue(s[[3]])
[1] NA
Warning message:
In .local(x, ...) : min value not known, use setMinMax

It works if I send the object to the memory:

> s <- s*1

> inMemory(s)
[1] TRUE

> maxValue(s[[3]])
[1] 0.0045886

> minValue(s[[3]])
[1] 0


However, I admit that throwing 106 (or more, if it's the case) files in the memory is not a good programming practice. 

In case they are useful, here are the files that can be used to reproduce the problem:

# 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)


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@gmail.com>

Cc: R-SIG list <r-sig-geo@r-project.org> 
Sent: Thursday, May 30, 2013 10:52 AM
Subject: Re: [R-sig-Geo] Classify 106 netcdf's based on their spooled range


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

> 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@gmail.com>

> Cc: R-SIG list <r-sig-geo@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

>> 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@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
    [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
	[[alternative HTML version deleted]]

