0

I am trying to write an R script to do pollution routing in world rivers, and need some help on selecting matrix cell coordinates and applying these to other matrices of the same dimension.

My data: I have several matrices corresponding to hydrological parameters of world rivers on a half degree grid (360 rows, 720 columns). These matrices represent flow accumulation (how many cells flow into this cell), flow direction (which of the 8 surrounding cells does the load of certain cell flow to) and pollutant load.

My idea: compute pollutant load in each grid cell from the start to the end of a river. I can base this on flow accumulation (low to high). However, each river basin can have multiple cells with the same flow accumulation value.

The problem: I need to select all matrix cells of each value of flow accumulation (low to high), find their coordinates (row,column), and transfer the corresponding pollutant load to the correct adjacent cell using the flow direction matrix. I have tried various ways, but selecting the coordinates of the correct cells and applying these to another matrix I cannot get to work.

I will give an example of what I have tried, using two for loops on one single river basin. In this example, a flow direction value of 1 means that the pollutant load needs to be transferred to the adjacent cell to the right (row is the same, column +1):

 BasinFlowAccumulation <-FlowAccumulation[Basin]

 BasinFlowAccumulationMaximum <- max(BasinFlowAccumulation)

 BasinFlowDirection <-FlowDirection[Basin]

 BasinPollutant <-Pollutant[Basin]


 b<-0

for(i in 0:BasinFlowAccumulationMaximum){

cells.index<-which(BasinFlowAccumulation[]==b, arr.ind=TRUE)

for (j in 1:length(cells.index)){
    print(BasinFlowDirection[cells[j]])
    Row<-BasinPollutant[cells[j[1]]]
    Column<-BasinPollutant[cells[j[2]]]
    ifelse(BasinFlowDirection[cells.index[j]]==1, BasinPollutant[Row,(Column+1)]<-BasinPollutant[Row,(Column+1)]+Basinpollutant[Row,Column]

 }
b<-b+1

 }

Any advice would be greatly appreciated!

Lucie
  • 1
  • 1
  • 1
    Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Formulating your question correctly will increase your chances of getting answer. – CHP Mar 24 '14 at 14:01
  • Your `ifelse` line has got corrupted - there's no closing parenthesis, or third parameter that gets evaluated in the `false` conditions. Probably better to use the expand `if (...) { } else {}` here as well, for clarity. – Gavin Kelly Mar 24 '14 at 14:17
  • Start small: create, say, a 5x5 matrix with sample data and if necessary write non-looped commands to move your desired data. Then convert the (successful) commands to a loop over the matrix indices. – Carl Witthoft Mar 24 '14 at 14:45
  • Also, are you sure this is the correct algorithm anyway. Imagine the 1-dimensional case. A flow going in the same direction as the order in which the array indices are looped through will have a different result than a flow in the reverse direction - so the arbitrary sorting of cell.index will influence the outcome which is surely undesirable – Gavin Kelly Mar 24 '14 at 15:29

0 Answers0