pihmRasterLIBS/aread8.cpp

Go to the documentation of this file.
00001 /* Program to compute area contributing to each pixel from pointers using  */
00002 /* recursive algorithm  */
00003 
00004 /* Created by David G Tarboton  */
00005 /*  File: aread8.c 
00006         
00007 */
00008 #include <iostream>
00009 #include "lsm.h"
00010 int **arr;    /*  Global array with area results  */
00011 void darea(int, int);
00012 
00013 int aread8(char *pfile,char *afile, double x, double y, int doall)
00014  {                
00015    int i,j,err,row,col;    
00016 /* define directions */
00017    d1[1]=0; d1[2]= -1; d1[3]= -1; d1[4]= -1; d1[5]=0; d1[6]=1; d1[7]=1; d1[8]=1;
00018    d2[1]=1; d2[2]=1; d2[3]=0; d2[4]= -1; d2[5]= -1; d2[6]= -1; d2[7]=0; d2[8]=1;
00019      
00020 /*  read pointers */
00021    err=gridread(pfile,(void ***)&dir,RPSHRDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&ndv,&filetype);
00022    if(err != 0)goto ERROR1;
00023 /*  Allocate area memory  */
00024    arr =   (int **)  matalloc(nx,ny,RPINTDTYPE);
00025 /* initialize area array to 0, -1 on boundary */
00026    for(i=0; i<ny; i++)
00027    {
00028       for(j=0; j<nx; j++)
00029         {
00030         if(i!=0 && i!=ny-1 && j!=0 && j!=nx-1 && dir[j][i]!= -1)
00031            arr[j][i]=0;
00032         else arr[j][i]= -1;
00033         }
00034    }
00035     if(doall == 0)   /*  Only compute area's for designated location  */
00036    {
00037                 col= (int)floor((x-bndbox[0])/csize);
00038                 row= (int)floor((bndbox[3]-y)/csize);
00039                 if(row <0 || row > ny || col < 0 || col > nx)
00040                 {
00041                         printf("Given coords out of area - whole area will be calculated\n");
00042                         row=0; col=0;
00043                 }
00044         }
00045         else
00046         {
00047                 row=0; col=0;
00048         }
00049         if(row > 0 && col > 0)
00050    {
00051 /* call drainage area subroutine for pixel to zero on  */
00052       darea(row,col);
00053     }
00054     else
00055     {
00056  
00057 /* call drainage area subroutine for each area */
00058 /* work from middle outwards to avoid deep recursions  */
00059         //cout<<"Just a flag\n";
00060         printf("Just a flag\n");
00061         for(i=ny/2; i<ny-1; i++)                    
00062         {  for(j=nx/2; j<nx-1; j++)
00063              darea(i,j);
00064            for(j=nx/2-1; j>=1; j--)
00065              darea(i,j);
00066         }  
00067          for(i=ny/2-1; i>=1; i--)                    
00068          {  for(j=nx/2; j<nx-1; j++)
00069                  darea(i,j);
00070             for(j=nx/2-1; j>=1; j--)
00071                  darea(i,j);
00072         } 
00073    } 
00074 /* write out areas */
00075    err=gridwrite(afile, (void **)arr,RPINTDTYPE,nx,ny,dx,dy,
00076        bndbox,csize, -1., filetype);
00077    free(arr[0]);
00078    free(arr);
00079 ERROR1:
00080    free(dir[0]);
00081    free(dir);
00082    return(err);
00083  }     
00084 
00085 /* function to compute area recursively */
00086 void darea(int i, int j)
00087   {                                        
00088     int in,jn,k,con=0;
00089       /* con is a flag that signifies possible contaminatin of area
00090          due to edge effects  */
00091     if(arr[j][i]==0)
00092     {
00093       if(i!=0 && i!=ny-1 && j!=0 && j!=nx-1 && dir[j][i]!= -1)
00094                  /* not on boundary  */
00095       {
00096         arr[j][i]=1;                               
00097         for(k=1; k<=8; k++)
00098         {  in=i+d1[k];
00099            jn=j+d2[k];
00100 /* test if neighbor drains towards cell excluding boundaryies */
00101            if(dir[jn][in]>0 && (dir[jn][in]-k==4||dir[jn][in]-k==-4))
00102              {
00103                 darea(in,jn);
00104                 if(arr[jn][in] < 0)con = -1;
00105                 else arr[j][i]=arr[j][i]+arr[jn][in];
00106              }
00107            if(dir[jn][in] < 0)con = -1;
00108         }
00109         //Here: GOPAL commented next line
00110         //if(con == -1)arr[j][i]= -1;
00111       }
00112     }
00113   }

Generated on Sun Aug 5 17:34:00 2007 for PIHMgis by  doxygen 1.5.2