Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SortGeoData doesn't work with BranchData #73

Closed
hakanpersson opened this issue Sep 13, 2017 · 4 comments
Closed

SortGeoData doesn't work with BranchData #73

hakanpersson opened this issue Sep 13, 2017 · 4 comments

Comments

@hakanpersson
Copy link

It seems lite SortGeoData gives wrong order when the sorting includes BranchData (for S-HYPE2016).

@dgustafsson
Copy link
Collaborator

Hi Håkan,
yes it is still some issue with SortGeoData (see also previous thread #37 ).

I get around this issue by sorting the different branches separately and then join the results manually.

Still, you might run into problems if you also have irrigation source basins outside the HARO you want to sort (I ran into this issue on the Danube).
/David

@dgustafsson
Copy link
Collaborator

Hi Håkan and Rene,
I made a function that might help to sort geodata with branchdata. Itäs a bit clumsy but it works at least for ArcticHYPE:

  # some error, lets make an additional function to improve the sorting of geodata
  sortGeodataAssistant <- function(gd = NULL,bd = NULL,outNum=-9999){
    library(HYPEtools)
    
    if(is.null(gd)){
      print("ERROR: no valid geodata given as input gd")
      return(-1)
    }
    
    # initialize output geodata
    gdout = gd
    for(i in 1:ncol(gd)){
      gdout[,i]=NA
    }

    # find all outlet subbasins
    iOut = which(gd$MAINDOWN==outNum)
    subOut = gd$SUBID[iOut]
    
    # loop over outlets and sort these geodatas
    iStart=1
    for(j in 1:length(subOut)){
      iHaro = which(gd$HAROID==gd$HAROID[iOut[j]])
      iUp   = match(AllUpstreamSubids(subid = subOut[j],gd = gd),gd$SUBID)
      if(length(which(is.na(match(iHaro,iUp))))>0){
        print("some error")
        print(j)
        break
      }
      gdAdd = gd[iUp,]
      if(length(iHaro)>1){
        gdAdd = SortGeoData(gd = gdAdd)
      }
      iEnd=iStart + nrow(gdAdd)-1
      gdout[iStart:iEnd,] = gdAdd
      iStart=iEnd+1
    }
    
    # check and adjust for interaction of branches
    i=1
    j=1
    while(i<nrow(bd) & j<1000){
      iSource = which(gdout$SUBID==bd$SOURCEID[i])
      iBranch = which(gdout$SUBID==bd$BRANCHID[i])
      iMain   = which(gdout$SUBID==gdout$MAINDOWN[iSource])
      haroMain = gdout$HAROID[iSource]
      haroBranch = gdout$HAROID[iBranch]
      if(iSource>iBranch){
        print(c(i,j,iSource,iBranch,iMain,haroMain,haroBranch,as.matrix(bd[i,2:4])))
        #if the source is below the branch in geodata, we move the branch and all of its downstream basins to the end
        iBranchDown = match(AllDownstreamSubids(subid = bd$BRANCHID[i],gd = gdout),gdout$SUBID)
        iBranchDown = sort(iBranchDown,decreasing = F)
        iNotBranch  = which(is.na(match(gdout$SUBID,gdout$SUBID[iBranchDown])))
        gd.branch = gdout[iBranchDown,]
        gd.notbranch = gdout[iNotBranch,]
        gdout = rbind(gd.notbranch,gd.branch)
        i=1
      }else{
        i=i+1
      }
      j=j+1
    }
    return(gdout)
  }

@dgustafsson
Copy link
Collaborator

the function does not work properly if there are subbasins with the wrong HAROID in the geodata (then "some error" is vaguely printed on the screen). I wrote a function to identify such cases:

checkHaroidConsistency <-function(gd = NULL,bd = NULL,outNum=-9999){
# find all outlet subbasins
iOut = which(gd$MAINDOWN==outNum)
subOut = gd$SUBID[iOut]

# loop over outlets and sort these geodatas
iStart=1
for(j in 1:length(subOut)){
  iHaro = which(gd$HAROID==gd$HAROID[iOut[j]])
  iUp   = match(AllUpstreamSubids(subid = subOut[j],gd = gd),gd$SUBID)
  if(length(which(is.na(match(iHaro,iUp))))>0){
    print("some error")
    print(cbind(gd$SUBID[iHaro],gd$HAROID[iHaro],gd$MAINDOWN[iHaro],gd$HAROID[match(gd$MAINDOWN[iHaro],gd$SUBID)],gd$MAINDOWN[match(gd$MAINDOWN[iHaro],gd$SUBID)]))
  }
}
return(0)    

}

rcapell added a commit that referenced this issue Aug 19, 2022
@rcapell
Copy link
Owner

rcapell commented Aug 19, 2022

I finally tried and found the bug. Turned out to be a logical error in the outlet SUBID identification. So I did not use @dgustafsson's code for now. I tested the solution with ArcticHYPE and SHYPE, both worked after GeoData shuffling and subsequent sorting with SortGeoData().

@rcapell rcapell closed this as completed Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants