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 Vermeulen, PhD candidate at Wageningen University, the Netherlands
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.