[R-sig-Geo] RSAGA .asc morphology loop question

Alexander Brenning brenning at uwaterloo.ca
Mon Mar 2 02:41:13 CET 2009


Hi,

this...

 > for (i in length(asc.files[i])) {

...only works if the variable i existed previously, and it seems that 
its value happened to be 1 in your case. It then counts from 
length(asc.files[i]) to length(asc.files[i]); asc.files[i] is a vector 
of length one (because i is 1), so the for loop goes from i=1 to i=1.

This is what you want to do:

 > for (i in 1:length(asc.files)) {

Cheers
  Alex


Tim Sippel wrote:
> Hi Alex-
> Thanks for the response, which helped me get a step closer to what I want.
> Now that I have removed the extensions from the files as you suggest, I am
> able to process one file, but am not able to process an entire directory of
> files.  Upon running a loop over all files in the directory, it only
> processes the first file and returns the calculated values (slope in my
> case).  However, there are 62 files in my directory and I'm at a loss for
> why I can't process all of them in my loop.  Sample code follows:
> 
> library(RSAGA)
> wd.in<-"C:\\my.dir"
> setwd(wd.in)
> env<-rsaga.env(path="C:\\programs\\saga_vc", cmd="saga_cmd.exe")
> asc.files<-dir(wd.in, pattern='.asc$')
> asc.files<-substr(basename(asc.files), 1, nchar(basename(asc.files)) - 4)
> 
>> asc.files
>  [1] "tmp1_220E-15N2005-03-02"   "tmp10_220E-15N2005-05-13"
> "tmp100_220E-15N2007-05-13" "tmp101_220E-15N2007-05-21"
> "tmp102_220E-15N2007-05-29"
> for (i in length(asc.files[i])) {
>   rsaga.esri.wrapper(fun=rsaga.slope, in.dem=asc.files[i],
> method="maxslope", in.esri=T, out.esri=T, clean.up=T,
>   out.slope=c(paste("SLOPE",asc.files[i],sep="")), env=env, esri.workspace=
> wd.in, format="ascii", georef="corner", prec=3)
>   }
> 
> This loop works, but only processes one file and stops.  My directory now
> includes the following files:
> 
>  [1] "SLOPEtmp1_220E-15N2005-03-02" "tmp1_220E-15N2005-03-02"
> "tmp10_220E-15N2005-05-13"     "tmp100_220E-15N2007-05-13"
> 
> Thanks again for suggestions on how to process the whole directory.
> 
> Tim Sippel
> 
> 
> 
> On Mon, Mar 2, 2009 at 4:43 AM, Alexander Brenning <brenning at uwaterloo.ca>wrote:
> 
>> Hi Tim,
>>
>> the rsaga.esri.wrapper currently expects the in.* and out.* arguments (with
>> the ASCII/float ESRI grids) to have NO file extension - the file extension
>> to be used for all ESRI grids can be given by the esri.extension argument;
>> it currently defaults to ".asc" when format="ascii" and to ".flt" when
>> format="binary". (Sorry that the help file is not explicit enough regarding
>> the file extensions; will add this to the next update.)
>>
>> Two more remarks on your code:
>>
>>>   read.ascii.grid(i)
>> This simply reads the grid but does nothing else - redundant.
>>
>>> prec=1
>> Slope is calculated in radians (see ?rsaga.slope). So with a 0.1 radian
>> increment (the smallest increment that can be represented with prec=1
>> digits) you get 0.1 * 180 / pi = 5.7 degree increments on the degree scale -
>> you probably want to haver a finer discretization. I would recommend prec=3
>> in the case of slope (and probably prec=6 or 7 for curvature attributes).
>>
>> I hope this helps.
>>
>> Cheers
>>  Alex
>>
>>
>> Tim Sippel wrote:
>>
>>>  I'm using RSAGA (version 0.9-4) to calculate rate of change (slope) on a
>>> series of .asc files.  I'm getting en error message saying it can't open
>>> the
>>> grid. My code follows:
>>>
>>> library(RSAGA)
>>> # data directory containing my .asc files to calculate slope on
>>> wd.in<-"C:\\my.data.dir"
>>> setwd(wd.in)
>>> env<-rsaga.env(path="C:\\programs\\saga_vc", cmd="saga_cmd.exe")
>>> asc.list<-dir(wd.in, pattern='.asc$')
>>> for (i in asc.list) {
>>>  read.ascii.grid(i)
>>>  rsaga.esri.wrapper(fun=rsaga.slope, in.dem=i, method="maxslope",
>>> in.esri=T, out.esri=T, clean.up=T,
>>>  out.slope=c(paste("SLOPE",as.character(i),sep="")), env=env,
>>> esri.workspace=wd.in, format="ascii", georef="corner", prec=1)
>>>  }
>>>
>>> Here are some of the data files in my directory from my asc.list object
>>> above:
>>>
>>> asc.list
>>>  [1] "tmp1_220E-15N2005-03-02.asc"   "tmp10_220E-15N2005-05-13.asc"
>>> "tmp100_220E-15N2007-05-13.asc" "tmp101_220E-15N2007-05-21.asc"
>>>  [5] "tmp102_220E-15N2007-05-29.asc" "tmp103_220E-15N2007-06-06.asc"
>>> "tmp104_220E-15N2007-06-14.asc" "tmp105_220E-15N2007-06-22.asc"
>>>
>>> The error I'm getting is below:
>>>
>>> SAGA CMD 2.0.3
>>> library path:   C:\programs\saga_vc/modules
>>> library name:   ta_morphometry
>>> module name :   Local Morphometry
>>> author      :   (c) 2001 by O.Conrad
>>> Load grid: tmp1_220E-15N2005-03-02.asc...
>>> failed
>>> error: Grid file could not be opened.
>>> error: input file [tmp1_220E-15N2005-03-02.asc]
>>> error: executing module [Local Morphometry]
>>>
>>> Not sure what I've done wrong in calling in the grid files and would
>>> appreciate advice.
>>>
>>> Best regards,
>>>
>>> Tim Sippel
>>> University of Auckland
>>>
>>>        [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>>
>>>
>>>
>> --
>> Alexander Brenning
>> brenning at uwaterloo.ca - T +1-519-888-4567 ext 35783
>> Department of Geography and Environmental Management
>> University of Waterloo
>> 200 University Ave. W - Waterloo, ON - Canada N2L 3G1
>> http://www.fes.uwaterloo.ca/geography/faculty/brenning/
>>
> 

-- 
Alexander Brenning
brenning at uwaterloo.ca - T +1-519-888-4567 ext 35783
Department of Geography and Environmental Management
University of Waterloo
200 University Ave. W - Waterloo, ON - Canada N2L 3G1
http://www.fes.uwaterloo.ca/geography/faculty/brenning/



More information about the R-sig-Geo mailing list