Skip to content
Prev 18465 / 29559 Next

Adaptive Savitzky Golay Filter function

Nicolas,


using the meuse dataset you had a shift of 100km? or was it using your
'zone'? Is the problem in your 'zone' as I have no shift using the
'meuse'. 
Internally the specified extent is reprojected to LatLon using
rgdal:::spTransform() so maybe you have to check the CRS of your zone?
If you want to follow deeper in this argument you have to reveal more
about your 'zone'  
ie:
bbox(zone)
proj4string(zone)
zoneLatLon <- spTransform(zone,CRS("+proj=longlat +datum=WGS84
+ellps=WGS84 +towgs84=0,0,0"))

bbox(zoneLatLon)
proj4string(zoneLatLon)


# do these two shp files match visualized in a GIS? (with enabled 'on
the fly' reprojection!)
require(raster)
shapefile("z1",zone)
shapefile("z2",zoneLatLon)


#####
# Filtering
there are two helper function intended to organize the input data for
whittaker.raster() and smooth.spline.raster().
preStack and orgTime


the first ('preStack') has two rules:
1. to look for data in 'path' with a given pattern
2. to sort files by date and remove superfluous data (this info comes
from orgTime)


the secound ('orgTime') checks the available input data (from the first
preStack run), and generates a output structure based on your needs
(mainly these args: 'nDays','begin','end' and 'pillow') and the
available input data.


Example (in ?whittaker.raster):
path <- 'dir to your data'
# with the first run of preStack all files in 'path' with the ending
*_NDVI.tif$ are listened
vi <- preStack(path=path, pattern="*_NDVI.tif$")
vi


# with orgTime you say: retrieve and sort ascending by time, all
filenames (vi), that are needed to process the period begin to end,
adding (_if available_) a pillow of data on the begin and on the end you
your time serie to ensure a high quality filtering also on the temporal
borders. 
timeInfo <- orgTime(vi,nDays=16,begin="2005001",end="2005365",pillow=40)
timeInfo


# now just get the data in vi, selected and sorted by timeInfo     
vi <- preStack(files=vi,timeInfo=timeInfo)
vi
     

# now the simples run of whittaker.raster
# 'vi' is a vector of filenames, but you can also stack it:
# vi <- stack(vi)
whittaker.raster(vi=vi,timeInfo=timeInfo) # the result is written to a
file in the current dir as specified by the default argument in
whittaker.raster outDirPath="./"


# if you what 
use also the weighting info from VI_Quality and the precise day of the
year for the "X-Axis" [t], you have to organize also the following files
qa   <- preStack(path=path, pattern="*_VI_Quality.tif$")
time <- preStack(path=path, pattern="*_composite_day_of_the_year.tif$")


qa <- preStack(files=qa,timeInfo=timeInfo)

qa <- stack(qa)



time <- preStack(files=time,timeInfo=timeInfo)

time <- stack(time)




whittaker.raster(vi=vi, wt=qa, inT=time, timeInfo=timeInfo) # this
overwrites the result of the first filtering!
# in whittaker.raster and smooth.spline.raster there are functions that
are serializing the 'time' info and also extracting the bit-coded info
in qa, if you want to generate your own weighting rules based on qa you
need to run makeWeights() yourself and use it as input:


qa2 <- makeWeights(qa) # see ?makeWeights for the how to (important is:
threshold=XX, and decodeOnly=FALSE) see in the example what 'res' is
plotting
whittaker.raster(vi=vi, wt=qa2, inT=time, timeInfo=timeInfo) 


# NOTE you can also speedup the filtering using 'beginCluster()'
good luck, 
Matteo
Dear Matteo,

Thanks a lot for your help !
I installed MODIS 0.9-3.
First of all, I followed your example with meuse dataset.
The projection was kept but there is a shift biger than 100 km with the 
real position.
In a second time I tried your solution to avoid any resampling.
It's worked perfectly : no shift, no resampling !
Thank you !

Now, I have to understand how to get a long time serie smoothed and gap 
filled for each pixel.
It's tricky for me, because I don't understand clearly how to use 
whittaker.rasteRe: [R-sig-Geo] Adaptive Savitzky Golay Filter function
From:	 Matteo Mattiuzzi
To:	 Nicolas Bories; r-sig-geo at r-project.org
BC:	
Date:	 Tuesday - June 11, 2013 4:44 PM
Subject: 	Re: [R-sig-Geo] Adaptive Savitzky Golay Filter function



Dear Nicolas,

Fixed in MODIS 0.9-3. Thanks! (If you like to build it yourself you can
get the source from:
http://ivfl-arc.boku.ac.at/owncloud/public.php?service=shorty_relay&id=QR5FZSxe0g)


example with the meuse dataset:
###########################
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")

require(rgdal)
zone <- spTransform(meuse,CRS("+proj=sinu +lon_0=0 +x_0=0 +y_0=0
+a=6371007.181 +b=6371007.181 +units=m +no_defs"))

# make sure your extent has an assigned projection string, if not do:

# proj4string(zone) <- "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181
+b=6371007.181 +units=m +no_defs"

runGdal(product="MOD13Q1",begin="2002001",end="2002001", job="testJob",
extent=zone)

####################
Be aware, that if you use an 'extent' argument a certain re-sampling, in
form of a pixel shift, is nearly always performed, as a specified
'extent' may not matches the original MODIS grid! If you want absolutely
avoid any resampling you have to do something like that (processing the
entire TILE(s)):

tileH <- getTile(zone)$tileH
tileV <- getTile(zone)$tileV
runGdal(product="MOD13Q1",begin="2002001",end="2002001",
job="testJob",tileH=tileH,tileV=tileV)

and then you can use raster:::crop() to reduce the extents if you like!


Matteo



     Reply  Reply All  Forward   Move   Mark Unread    Delete   Print
View   
Mail Properties
From:	Nicolas Bories <nicolas.bories at orange.fr>	Tuesday - June
11, 2013 1:21 AM
To:	r-sig-geo at r-project.org
Subject:	Re: [R-sig-Geo] Adaptive Savitzky Golay Filter function
 Attachments:	
Part.001 (1 KB)	   View
Dear all,


The MODIS package seems to be very useful and very powerful !
Nonetheless I encounter some problems with the runGdal() function and 
extent argument.


I need to extract a part of "MOD13Q1" product between 2002 and 2005.
I also need to keep data in the same projection and the same resolution 
than hdf files, without resampling.
I tried 2 cases :


1) runGdal(product="MOD13Q1",begin="2002001",end="2006001", 
job="testJob",extent=zone)
zone is a SpatialPointsDataFrame with same projection than hdf files


This returns :
Error in ReplProj4string(obj, CRS(value)) :
  Geographical CRS given to non-conformant data:  -94112.4108437 
4925025.8738480


2) runGdal(product="MOD13Q1",begin="2002001",end="2002001", 
job="testJob",extent=zone,pixelSize="asIn",resamplingType="near",outProj="+proj=sinu

+lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")
zone is a SpatialPointsDataFrame  with CRS : "+proj=longlat
+datum=WGS84"


This produces TIFF files reprojected in LongLat


Can anyone please help me?


Many thanks in advance,


Nicolas.
On 07/06/2013 17:24, Matteo Mattiuzzi wrote:
do
to
root,
GDAL
job="testJob")
#
see?runMrt
detectBitInfo("MOD13Q1")
code
and
there