Exemple #1
0
int setdir(char *demfile, char *angfile, char *slopefile, char *pfile)
{
  int err,filetype;
  float mval;
  double bndbox[4],csize;

/* define directions */
  d1[1]=0; d1[2]= -1; d1[3]= -1; d1[4]= -1; d1[5]=0; d1[6]=1; d1[7]=1; d1[8]=1;
  d2[1]=1; d2[2]=1; d2[3]=0; d2[4]= -1; d2[5]= -1; d2[6]= -1; d2[7]=0; d2[8]=1;


  err=gridread(demfile,(void ***)&elev,RPFLTDTYPE,&nx,&ny,&dx,&dy,
               bndbox,&csize,&mval,&filetype);
  if(err != 0) goto ERROR2;

/*  allocate  dir and stack arrays  */
  dir = (short **) matalloc(nx, ny, RPSHRDTYPE);

  i1=0; i2=0; n1=nx; n2=ny;  /*  full grid  */
  err=setdfnoflood(mval);
  if(err != 0) goto ERROR1;
/*  err = gridwrite(pfile,(void **)dir,RPSHRDTYPE,nx,ny,dx,dy,
    bndbox,csize,-1,filetype);   */

/*  allocate memory for slope and angle  */
  slope = (float **) matalloc(nx, ny, RPFLTDTYPE);
  ang = (float **) matalloc(nx, ny, RPFLTDTYPE);
  setdf2();
  err = gridwrite(slopefile,(void **)slope,RPFLTDTYPE,nx,ny,dx,dy,
                  bndbox,csize,-1.,filetype);
  if (err != 0) goto ERROR3;
  err = gridwrite(angfile,(void **)ang,RPFLTDTYPE,nx,ny,dx,dy,
                  bndbox,csize,-2.,filetype);
  if (err != 0) goto ERROR3;

/*  Wrapping up  */
  err=0;
ERROR3:
  free(slope[0]);
  free(slope);
  free(ang[0]);
  free(ang);
ERROR1:
  free(dir[0]);
  free(dir);
ERROR2:
  free(elev[0]);
  free(elev);
  return(err);
}
Exemple #2
0
/*funzione che genera le nuove soluzioni
 ricecve il vettore dei geniori e quello cui salvare i figli*/
void offspring_generation(int **pieces,int npieces,population_t *pop,long *parents,solution_t *offspring,int row, int col){
    long i,j,tmp;//indice su pop e var temp per memorizzare il val estratto a caso
    long cnt;
    long gen[2];//vettore dei due indici dei genitori accoppiati da passare 
                //alla funzione di crossover
    /*Accoppiamento:
     seleziona 2 genitori a caso e li passa all funz di crossover per generare
     2 figli*/
    #pragma omp parallel default(none) shared(pieces,npieces,pop,parents,offspring,row,col) firstprivate(i,j,cnt,tmp,gen)
{
   #pragma omp for
    for(i=0;i<pop->gen_n;i+=2){
        j=i+1;
        /*ciclo di selezione genitori*/
       #pragma omp critical (print_tid)
      {
        for(cnt=0;cnt<2;cnt++){
                tmp=rand()%pop->gen_n;
                //se l'el. estratto e già stato accoppiato
                //prova con il successivo finchè non trova un el.da accoppiare.
     
                while(parents[tmp]>0)
                        tmp=((tmp+1)%pop->gen_n);
                gen[cnt]=tmp;
                parents[tmp]*=-1;
        }
      }
        offspring[i].matrice_pezzi=matalloc(row,col);
        offspring[j].matrice_pezzi=matalloc(row,col);
        crossover(&pop->soluzioni[parents[gen[0]]-1],&pop->soluzioni[parents[gen[1]]-1],&offspring[i],&offspring[j],pieces,npieces,row,col);
        offspring[i].fitness=fitness_solution_evaluation(pieces,&offspring[i],npieces,row,col);
        //printf("Valore fitness figlio %d: %d\n",i,offspring[i].fitness);
        offspring[j].fitness=fitness_solution_evaluation(pieces,&offspring[j],npieces,row,col);
        //printf("Valore fitness figlio %d : %d\n",i,offspring[i].fitness);
    }
    }

    return;
}
Exemple #3
0
int main(const int argc, const char *const argv[])
{
	const runconfig *const rc = formconfig(argc, argv, 64, 1024);
	const unsigned sz = rc->size;

	printf("\tmatrix size: %fMiB\n",
		(double)sz * sz * sizeof(eltype) / (double)(1 << 20));

	const workset ws = {
		.a = matalloc(sz, sz),
		.b = matalloc(sz, sz),
		.r = matalloc(sz, sz) };

	void *const a = ws.a;
	void *const b = ws.b;
	void *const r = ws.r;

	printf("\tallocated. a: %p; b: %p; r: %p\n", a, b, r);

	treeplugin tp = {
		.makeargument = makeidargument,
		.dropargument = dropidargument,
		.rc = rc,
		.extra = (void *)&ws };

	printf("randomization\n");
	tp.treeroutine = randroutine;
	treespawn(&tp);

	printf("multiplication\n");
	tp.treeroutine = multroutine;
	treespawn(&tp);

	free(a);
	free(b);

	printf("some values\n");
	const unsigned tr = tilerows;
	matdump(r, sz, sz, tr, tr, 127, 237, 8, 8);
	
	free(r);
	
	return 0;
}
Exemple #4
0
int gridread(char *file, void ***data, int *nx, int *ny, float *dx, float *dy, double bndbox[4], float *nodata)
{
FILE *fp;

    int i, j, hdrlines = 0;
    float value;
    char fline[MAXLN], keyword[21], utmetag, utmntag;
    float **farr;
    double utme,utmn;
	double csize;
 
    fp = fopen(file,"r");
    if(fp == NULL)
    {
        printf("\nERROR: Cannot open input file (%s).\n\n",file);
        return(1);
    }
    
    /* read ARC-Info header */
    while(1)
    {   
        readline(fp, fline);       
        if(!isalpha(*fline) || *fline == '-')
            break;       
        
        hdrlines++;

        sscanf(fline,"%s %f",keyword,&value);


	if(strcmp(keyword,"ncols") == 0 || strcmp(keyword,"NCOLS") == 0)
	    *nx = (int)value;
	else if(strcmp(keyword,"nrows") == 0 || strcmp(keyword,"NROWS") == 0)
	    *ny = (int)value;
	else if(strcmp(keyword,"xllcenter") == 0 || strcmp(keyword,"XLLCENTER") == 0)
	{
	    utmetag = 'c';
	    utme = value;
	}
	else if(strcmp(keyword,"xllcorner") == 0 || strcmp(keyword,"XLLCORNER") == 0)
	{
	    utmetag = 'e';
	    utme = value;
	}
	else if(strcmp(keyword,"yllcenter") == 0 || strcmp(keyword,"YLLCENTER") == 0)
	{
	    utmntag = 'c';
	    utmn = value;
	}
	else if(strcmp(keyword,"yllcorner") == 0 || strcmp(keyword,"YLLCORNER") == 0)
	{
	    utmntag = 'e';
	    utmn = value;
	}
	else if(strcmp(keyword,"cellsize") == 0 || strcmp(keyword,"CELLSIZE") == 0)
	{
	    *dx = *dy = value;
	    csize = (double) value;
	}
	else if(strcmp(keyword,"nodata_value") == 0 || strcmp(keyword,"NODATA_VALUE") == 0 ||
		strcmp(keyword,"NODATA_value") == 0)
	    *nodata = value;
    }
    
    /* adjust utme and utmn if necessary (we store center of reference cell) */
    if(utmetag == 'e') utme = utme + *dx/2;
    if(utmntag == 'e') utmn = utmn + *dy/2;
     bndbox[0] = utme - csize/2.;   
     bndbox[1] = utmn - csize/2.;
     bndbox[2] = bndbox[0] + csize * (*nx);
     bndbox[3] = bndbox[1] + csize * (*ny);
    /* position file pointer for ARC-Info file to beginning of image data */
    rewind(fp);
    for(i=0; i<hdrlines; i++) readline(fp, fline);
    

       farr = (float **) matalloc(*nx, *ny);

        /* read in the ARC-Info file */
        for(i=0; i< *ny; i++)
        {
            for(j=0; j< *nx; j++)
			{

                fscanf(fp,"%f",&farr[j][i]);
			}
        }
        *data = (void **) farr;   

    fclose(fp);
    return(0);
}
 int gridread(char *file, void ***data, int datatype, int *nx, int *ny,
              float *dx, float *dy, double bndbox[4], double *csize,
              float *nodata, int *filetype)
 {
 FILE *fp;
 int channel1,type,iesri;
 
     int i, j, hdrlines = 0;
     float value;
     char fline[MAXLN], keyword[21], utmetag, utmntag;
     float **farr;
     int **iarr;
     short **sarr;
     double adjbndbox[4],utme,utmn;
     CELLTYPE *rowbuf;
     float floatNull;
 
       
     if(iesri = GridIOSetup() >= 0)
         /*
         Open input cell layer with no evaluation        (5)
         */
         if ((channel1 = 
               CellLayerOpen(file,READONLY,ROWIO,
                             &type,csize)) >= 0 )
     {
 /*  The success of these means the file could be opened as an ESRI grid file  */
     printf("File could be opened as an ESRI grid file\n");
     *filetype=1;
 /*  File types are
     0=ASCII   
     1= ARCVIEW grid via the ESRI Application Programmers Interface  */ 
 /*  here is the ESRI grid file read stuff - following copyrow example */
         *dx = *dy = (float) *csize;
         if(type == CELLINT && datatype == RPFLTDTYPE)
         {
            printf("%s File contains integer data but was read as float\n",file);
         }
         if(type == CELLFLOAT && datatype != RPFLTDTYPE)
         {
            printf("%s File contains Float data but was read as Integer\n",file);
         } 
         /*
         Get the bounding box of the input cell layer            (7)
         */
         if (BndCellRead (file, bndbox) < 0)
         {
                 printf ("ERROR: Could not read bounding box of input grid\n");
                 CellLyrClose(channel1);
                 GridIOExit();
                 return(1);
         }
         /*  printf("%f %f %f %f %g\n",bndbox[0],bndbox[1],bndbox[2],bndbox[3],*nodata) */; 
         /*  Bounding box is xllcorner, yllcorner, xurcorner, yurcorner   */
       
         /*
         Set the Window to the output bounding box and cellsize  (9)
         */
         if (AccessWindowSet (bndbox, *csize, adjbndbox) < 0)
         {
                 printf ("ERROR: Could not set Window\n");
                 CellLyrClose(channel1);
                 GridIOExit();
                 return(2);
         }
         /*
         Get the number of rows and columns                      (10)
         in the window
         */
         *nx = WindowCols();
         *ny = WindowRows();
 
         /*  Allocate memory and set all type pointers  */
         *data = matalloc(*nx, *ny, datatype);
         farr = (float **) (*data);
         iarr = (int **) (*data);
         sarr = (short **) (*data);
         GetMissingFloat(&floatNull);
  
         *nodata = (datatype == RPFLTDTYPE) ? floatNull: -9999.; 
 
         /*
         Allocate row buffer                                     (11)
         */
         if ((rowbuf = (CELLTYPE *)
                         CAllocate1 ( *nx, sizeof(CELLTYPE)))
                         == NULL )
         {
                 printf ("ERROR: Could not allocate memory\n");
                 CellLyrClose(channel1);
                 GridIOExit();
                 return(3);
         }
         /*
         Now copy row into array                 (12)
         */
         for (i=0; i < *ny; i++)
         {
              GetWindowRow (channel1, i, rowbuf);
              if(type == CELLINT)
              {
                 register int *buf = (int *)rowbuf;
                 if(datatype == RPSHRDTYPE)
                 {
                    for(j=0; j < *nx; j++)
                    {
                       sarr[j][i]=(buf[j] == MISSINGINT) ? (short) *nodata : (short) buf[j];    
                    }
                  }
                  else if(datatype == RPINTDTYPE)
 	            {
                    for(j=0; j < *nx; j++)
                    {
                       iarr[j][i]=(buf[j] == MISSINGINT) ? (int) *nodata : buf[j]; 
                    }
                  }   
 	             else
 	            {
                    for(j=0; j < *nx; j++)
                    {
                       farr[j][i]=(buf[j] == MISSINGINT) ? *nodata: (float) buf[j];  
                    }
                 }
              }
              else
              {
                 register float *buf = (float *)rowbuf;
 								
 /*     This is all repeated to get right the typecasting of data from ESRI file into the format 
        we want   */
                 if(datatype == RPSHRDTYPE)
                 {
                    for(j=0; j < *nx; j++)
                    {
                       sarr[j][i]=(buf[j] == floatNull) ? (short) *nodata : (short) buf[j];    
                    }
                  }
                  else if(datatype == RPINTDTYPE)
 	            {
                    for(j=0; j < *nx; j++)
                    {
                       iarr[j][i]=(buf[j] == floatNull) ? (int) *nodata : (int) buf[j];
                    }
                  }   
 	             else
 	            {
                    for(j=0; j < *nx; j++)
                    {
 		      farr[j][i]= buf[j];
                    }
                 }
              }
        }
 
         /*
         Free row buffer                                         (13)
         */
         CFree1 ((char *)rowbuf);
         /*
         Close handles                                           (14)
         */
         CellLyrClose(channel1);  
         /*
         Done with the library                                   (15)
         */
         GridIOExit();
         return(0);
     }
 
 /*  Here assume file is ASCII  Close ESRI stuff. */
     CellLyrClose(channel1);
     GridIOExit();
 
     *filetype=0;  
     fp = fopen(file,"r");
     if(fp == NULL)
     {
         printf("\nERROR: Cannot open input file %s\n\n",file);
         return(1);
     }
     
     /* read ARC-Info header */
     while(1)
     {   
         readline(fp, fline);       
         if(!isalpha(*fline) || *fline == '-')
             break;       
         
         hdrlines++;
 
         sscanf(fline,"%s %f",keyword,&value);
         
 	if(strcmp(keyword,"ncols") == 0 || strcmp(keyword,"NCOLS") == 0)
 	    *nx = (int)value;
 	else if(strcmp(keyword,"nrows") == 0 || strcmp(keyword,"NROWS") == 0)
 	    *ny = (int)value;
 	else if(strcmp(keyword,"xllcenter") == 0 || strcmp(keyword,"XLLCENTER") == 0)
 	{
 	    utmetag = 'c';
 	    utme = value;
 	}
 	else if(strcmp(keyword,"xllcorner") == 0 || strcmp(keyword,"XLLCORNER") == 0)
 	{
 	    utmetag = 'e';
 	    utme = value;
 	}
 	else if(strcmp(keyword,"yllcenter") == 0 || strcmp(keyword,"YLLCENTER") == 0)
 	{
 	    utmntag = 'c';
 	    utmn = value;
 	}
 	else if(strcmp(keyword,"yllcorner") == 0 || strcmp(keyword,"YLLCORNER") == 0)
 	{
 	    utmntag = 'e';
 	    utmn = value;
 	}
 	else if(strcmp(keyword,"cellsize") == 0 || strcmp(keyword,"CELLSIZE") == 0)
 	{
 	    *dx = *dy = value;
 	    *csize = (double) value;
 	}
 	else if(strcmp(keyword,"nodata_value") == 0 || strcmp(keyword,"NODATA_VALUE") == 0 ||
 		strcmp(keyword,"NODATA_value") == 0)
 	    *nodata = value;
     }
     
     /* adjust utme and utmn if necessary (we store center of reference cell) */
     if(utmetag == 'e') utme = utme + *dx/2;
     if(utmntag == 'e') utmn = utmn + *dy/2;
     
     bndbox[0] = utme - *csize/2.;   
     bndbox[1] = utmn - *csize/2.;
     bndbox[2] = bndbox[0] + *csize * (*nx);
     bndbox[3] = bndbox[1] + *csize * (*ny);
     
     /* position file pointer for ARC-Info file to beginning of image data */
     rewind(fp);
     for(i=0; i<hdrlines; i++) readline(fp, fline);
     
       /* convert depending on datatype */
       if(datatype == RPSHRDTYPE)
       {   
          sarr = (short **) matalloc(*nx, *ny, datatype);
              
          /* read in the ARC-Info file */        
          for(i=0; i< *ny; i++)
          {
             for(j=0; j< *nx; j++)
                 fscanf(fp,"%hd",&sarr[j][i]);
          }
          *data = (void **) sarr;
        }
        else if(datatype == RPINTDTYPE)
        {
          iarr = (int **) matalloc(*nx, *ny, datatype);
         
          for(i=0; i< *ny; i++)
          {
             for(j=0; j< *nx; j++)
                 fscanf(fp,"%d",&iarr[j][i]);
          }
          *data = (void **) iarr;
         }
         else if(datatype == RPFLTDTYPE)
         {
          farr = (float **) matalloc(*nx, *ny, datatype);
 
         /* read in the ARC-Info file */
         for(i=0; i< *ny; i++)
         {
             for(j=0; j< *nx; j++)
 			{
 
                 fscanf(fp,"%f",&farr[j][i]);
 			}
         }
         *data = (void **) farr;   
       }
       else
       {
         printf("\nERROR: unknown datatype (%s).\n\n",datatype);
       }
       fclose(fp);
       return(0);
 }
Exemple #6
0
int flood(char *demfile, char *pointfile, char *newfile)
{
  int err;
/*  float mval;
  double bndbox[4],csize;    These are external now */   

/* define directions */
   d1[1]=0; d1[2]= -1; d1[3]= -1; d1[4]= -1; d1[5]=0; d1[6]=1; d1[7]=1; d1[8]=1;
   d2[1]=1; d2[2]=1; d2[3]=0; d2[4]= -1; d2[5]= -1; d2[6]= -1; d2[7]=0; d2[8]=1;

  err=gridread(demfile,(void ***)&elev,RPFLTDTYPE,&nx,&ny,&dx,&dy,bndbox,&csize,&mval,&filetype);
             
  if(err != 0)return(err);
/*  allocate  dir and stack arrays  */
  dir = (short **) matalloc(nx, ny, RPSHRDTYPE);
  apool = (short **) matalloc(nx, ny, RPSHRDTYPE);
  istack = (int) (nx * ny * 0.1);
  pstack=istack;
  dn = (short *)malloc(sizeof(short) * istack);
  is = (short *)malloc(sizeof(short) * istack);
  js = (short *)malloc(sizeof(short) * istack);
  ipool = (short *)malloc(sizeof(short) * pstack);
  jpool = (short *)malloc(sizeof(short) * pstack);
  if(dn == NULL || is == NULL || js == NULL || ipool == NULL || jpool == NULL)
  {
     printf("\nError:  Cannot allocate memory for stacks\n");
     return(11);
  }
/*  nmax=200;   Decided to elim iteration
  nnx=nx/nmax+1;
  nny=ny/nmax+1;
  while(nnx > 1 || nny > 1)
  {
    for(ix=0; ix<nnx; ix++)for(iy=0; iy<nny; iy++)
    {
      i1=ix*nmax;
      n1=i1+nmax;
      if(n1>nx)
      {
        n1=nx;
        i1=nx-nmax;
        if(i1<0)i1=0;
      }
      i2=iy*nmax;
      n2=i2+nmax;
      if(n2>ny)
      {
        n2=ny;
        i2=ny-nmax;
        if(i2<0)i2=0;
      }
      printf("Ranges: %d %d %d %d\n",i1,n1,i2,n2);
      setdf(mval);   /*  this call for range i1,n1,i2,n2  
    }
    nmax=(int) (nmax*3.5) ; 
    nnx=nx/nmax+1;
    nny=ny/nmax+1;
  }  /*  end of while  */
  i1=0; i2=0; n1=nx; n2=ny;  /*  full grid  */ 
  printf("Grid size: %d x %d\n",n1,n2);
  setdf(mval);
/*  free stacks  */
  free(dn); free(is); free(js); free(ipool); free(jpool); 
  free(apool[0]); free(apool);
/*    filetype=0;   0=ASCII   1= ARCVIEW proprietary  */
  printf("Writing output ...");
  err = gridwrite(newfile,(void **)elev,RPFLTDTYPE,nx,ny,dx,dy,
             bndbox,csize,mval,filetype);
  if(err != 0)return(err);
/*  err = gridwrite(pointfile,(void **)dir,RPSHRDTYPE,nx,ny,dx,dy,
             bndbox,csize,-1,filetype);  
  if(err != 0)return(err);    */ 
  return(0);  /*  ALL OK return from flood  */
}
Exemple #7
0
int setdfnoflood(float mval)
/*  This version is stripped of pit filling  */
{
  int i,j,k,ip, n, iter;
  float fact[9];
  short *s;  /*  variables for flat draining   */
  int **spos, *sloc;
  float *elev2;


/*  Initialize boundaries  */
  for(i=i1; i< n1; i++)
  {
    dir[i][0]= -1;
    dir[i][n2-1]= -1;
  }
  for(i=i2; i< n2; i++)
  {
    dir[0][i]= -1;
    dir[n1-1][i]= -1;
  }
/*  iup=0; */
/*  initialize internal pointers */
  for(i=i2+1; i< n2-1; i++) for(j=i1+1; j<n1-1; j++)
    {
      if(elev[j][i] <= mval) dir[j][i]= -1;
      else dir[j][i] = 0;
    }
/*  Direction factors  */
  for(k=1; k<= 8; k++)
    fact[k]= (float) (1./sqrt(d1[k]*dy*d1[k]*dy+d2[k]*d2[k]*dx*dx));
/*  Set positive slope directions   */
  n=0;
  for(i=i2+1; i< n2-1; i++)
    for(j=i1+1; j<n1-1; j++)
      if(elev[j][i] > mval)
      {
        set(i,j,fact,mval);
        if(dir[j][i] == 0) n++;
      }
  if(n > 0)
  {
/*  Now resolve flats following the Procedure of Garbrecht and Martz, Journal
   of Hydrology, 1997.  */

/*  Memory is utilized as follows
is, js, dn, s and elev2 are unidimensional arrays storing information for flats.
sloc is a indirect addressing array for accessing these - used during
recursive iteration
spos is a grid of pointers for accessing these to facilitate finding neighbors

The routine flatrout is recursive and at each recursion allocates a new sloc for
addressing these arrays and a new elev for keeping track of the elevations
for that recursion level.
  */
    iter=1;
    printf("Resolving %d Flats, Iteration: %d \n",n,iter);
    spos = (int **) matalloc(nx, ny, RPINTDTYPE);
    dn = (short *)malloc(sizeof(short) * n);
    is = (short *)malloc(sizeof(short) * n);
    js = (short *)malloc(sizeof(short) * n);
    s = (short *)malloc(sizeof(short) * n);
    sloc = (int *)malloc(sizeof(int) * n);
    elev2 = (float *)malloc(sizeof(float) *n);

    if(dn == NULL || is == NULL || js == NULL || s == NULL ||
       spos == NULL || elev2 == NULL || sloc == NULL)
    {
      printf("Unable to allocate at iteration %d\n",iter);
    }
/*  Put unresolved pixels on stack  */
    ip=0;
    for(i=i2; i< n2; i++)
      for(j=i1; j<n1; j++)
      {
        spos[j][i]=-1; /*  Initialize stack position  */
        if(dir[j][i] == 0)
        {
          is[ip]=i;
          js[ip]=j;
          dn[ip]=0;
          sloc[ip]=ip;
          /*   Initialize the stage 1 array for flat routing   */
          s[ip] = 1;
          spos[j][i]=ip; /*  pointer for back tracking  */
          ip++;
          if(ip > n) printf("PROBLEM - Stack logic\n");
        }
      }
    flatrout(n,sloc,s,spos,iter,elev2,elev2,fact,n);
/*  The direction 9 was used to flag pits.  Set these to 0  */
    for(i=i2; i< n2; i++)
      for(j=i1; j<n1; j++)
        if(dir[j][i] == 9) dir[j][i]=0;
    free(spos[0]); free(spos);
    free(elev2);
    free(dn);
    free(is);
    free(js);
    free(s);
    free(sloc);
    printf("Done iteration %d\nFlats resolved %d\n",iter,n);
  } /*  End if n > 0  */
  return(0);    /*  OK exit from setdir  */
}  /*  End setdfnoflood  */