Changeset 12229


Ignore:
Timestamp:
05/10/12 10:41:54 (13 years ago)
Author:
Mathieu Morlighem
Message:

Added 2 options: boxlength and searchrange

Location:
issm/trunk-jpl/src/c
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified issm/trunk-jpl/src/c/Container/Observations.cpp

    r12228 r12229  
    3636
    3737        /*Intermediaries*/
    38         int          i,maxdepth;
     38        int          i,maxdepth,level,counter;
    3939        int          xi,yi;
    4040        double       xmin,xmax,ymin,ymax;
     
    6363        }
    6464
    65 
    6665        /*Initialize Quadtree*/
    67         printf("Generating quadtree with a maximum box size %g (depth=%i)...",minlength,maxdepth);
     66        printf("Generating quadtree with a maximum box size %g (depth=%i)... ",minlength,maxdepth);
    6867        this->quadtree = new Quadtree(xmin,xmax,ymin,ymax,maxdepth);
    6968
    7069        /*Add observations one by one*/
    71         int level;
     70        counter = 0;
    7271        for(i=0;i<n;i++){
    7372                this->quadtree->IntergerCoordinates(&xi,&yi,x[i],y[i]);
    7473                this->quadtree->QuadtreeDepth2(&level,xi,yi);
    75                 if((int)level >= maxdepth){
     74                if((int)level <= maxdepth){
     75                        observation = new Observation(x[i],y[i],xi,yi,counter++,observations_list[i]);
     76                        this->quadtree->Add(observation);
     77                        this->AddObject(observation);
    7678                }
    7779                else{
    78                         observation = new Observation(x[i],y[i],xi,yi,observations_list[i]);
    79                         this->quadtree->Add(observation);
    80                         this->AddObject(observation);
     80                        //We need to average with the current observations (not done yet)
    8181                }
    8282        }
     
    9393/*Methods*/
    9494/*FUNCTION Observations::ObservationList{{{*/
    95 void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp){
     95void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp,double range){
    9696
    9797        /*Output and Intermediaries*/
    98         int          nobs,i;
    99         double      *x           = NULL;
    100         double      *y           = NULL;
    101         double      *obs         = NULL;
    102         Observation *observation = NULL;
     98        int          nobs,i,index;
     99        double      *x            = NULL;
     100        double      *y            = NULL;
     101        double      *obs          = NULL;
     102        Observation *observation  = NULL;
     103        int         *indices      = NULL;
    103104
    104         /*Get number of observations*/
    105         nobs = this->Size();
     105        /*Treat range*/
     106        if(range==0){
     107                range=this->quadtree->root->length;
     108        }
    106109
    107         /*Allocate vectors*/
    108         x   = (double*)xmalloc(nobs*sizeof(double));
    109         y   = (double*)xmalloc(nobs*sizeof(double));
    110         obs = (double*)xmalloc(nobs*sizeof(double));
     110        /*Find all observations that are in range*/
     111        this->quadtree->RangeSearch(&indices,&nobs,x_interp,y_interp,range);
    111112
    112         /*Loop over all observations and fill in x, y and obs*/
    113         for (i=0;i<nobs;i++){
    114                 observation=(Observation*)this->GetObjectByOffset(i);
    115                 observation->WriteXYObs(&x[i],&y[i],&obs[i]);
     113        if(nobs==0){
     114                /*No observation found, double range*/
     115                printf("No observation found within range, doubling range\n");
     116                xfree((void**)&indices);
     117                this->ObservationList(&x,&y,&obs,&nobs,x_interp,y_interp,range*2);
     118        }
     119        else{
     120                if(nobs>1000) printf("Taking more than 1000 observations\n");
     121                /*Allocate vectors*/
     122                x   = (double*)xmalloc(nobs*sizeof(double));
     123                y   = (double*)xmalloc(nobs*sizeof(double));
     124                obs = (double*)xmalloc(nobs*sizeof(double));
     125
     126                /*Loop over all observations and fill in x, y and obs*/
     127                for (i=0;i<nobs;i++){
     128                        observation=(Observation*)this->GetObjectByOffset(indices[i]);
     129                        observation->WriteXYObs(&x[i],&y[i],&obs[i]);
     130                }
    116131        }
    117132
    118133        /*Assign output pointer*/
     134        xfree((void**)&indices);
    119135        *px=x;
    120136        *py=y;
  • TabularUnified issm/trunk-jpl/src/c/Container/Observations.h

    r12226 r12229  
    2323
    2424                /*Methods*/
    25                 void ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp);
     25                void ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp,double range);
    2626                void QuadtreeColoring(double* A,double *x,double *y,int n);
    2727
  • TabularUnified issm/trunk-jpl/src/c/modules/Krigingx/Krigingx.cpp

    r12226 r12229  
    2323        /*Intermediaries*/
    2424        int           i,j,n_obs;
     25        double        range;
    2526        double        numerator,denominator,ratio;
    2627        double       *x            = NULL;
     
    3334        double       *gamma0       = NULL;
    3435        double       *ones         = NULL;
     36        char         *output       = NULL;
    3537        Variogram    *variogram    = NULL;
    3638        Observations *observations = NULL;
     
    3840        /*Get Variogram from Options*/
    3941        ProcessVariogram(&variogram,options);
     42        options->Get(&range,"searchrange",0.);
    4043
    4144        /*Process observation dataset*/
     
    4447        /*Allocation output*/
    4548        predictions =(double*)xmalloc(n_interp*sizeof(double));
     49        for(i=0;i<n_interp;i++) predictions[i]=0;
    4650
    47         for(i=0;i<n_interp;i++) predictions[i]=0;
    48         observations->QuadtreeColoring(predictions,x_interp,y_interp,n_interp);
    49         *ppredictions=predictions;
    50         return 1;
     51        /*Get output*/
     52        options->Get(&output,"output","quadtree");
     53
     54        if(strcmp(output,"quadtree")==0){
     55                observations->QuadtreeColoring(predictions,x_interp,y_interp,n_interp);
     56                *ppredictions=predictions;
     57                return 1;
     58        }
    5159
    5260        /*Loop over all interpolations*/
     
    5664
    5765                /*Get list of observations for current point*/
    58                 observations->ObservationList(&x,&y,&obs,&n_obs,x_interp[idx],y_interp[idx]);
     66                observations->ObservationList(&x,&y,&obs,&n_obs,x_interp[idx],y_interp[idx],range);
    5967
    6068                /*Allocate intermediary matrix and vectors*/
     
    105113        delete variogram;
    106114        delete observations;
     115        xfree((void**)&output);
    107116        *ppredictions=predictions;
    108117        return 1;
  • TabularUnified issm/trunk-jpl/src/c/objects/Bamg/BamgQuadtree.h

    r12218 r12229  
    11/*Original code from Frederic Hecht <hecht@ann.jussieu.fr> (BAMG v1.01, BamgQuadtree.h)*/
    2 #ifndef _QUADTREE_H
    3 #define _QUADTREE_H
     2#ifndef _BAMGQUADTREE_H
     3#define _BAMGQUADTREE_H
    44
    55#include "./include.h"
  • TabularUnified issm/trunk-jpl/src/c/objects/Kriging/Observation.cpp

    r12213 r12229  
    1212}
    1313/*}}}*/
    14 /*FUNCTION Observation::Observation(double x,double y,int xi,int yi,double value){{{1*/
    15 Observation::Observation(double x_in,double y_in,int xi_in,int yi_in,double value_in){
     14/*FUNCTION Observation::Observation(double x,double y,int xi,int yi,int index,double value){{{1*/
     15Observation::Observation(double x_in,double y_in,int xi_in,int yi_in,int index_in,double value_in){
    1616
    1717        this->x     = x_in;
     
    1919        this->xi    = xi_in;
    2020        this->yi    = yi_in;
     21        this->index = index_in;
    2122        this->value = value_in;
    2223
     
    3637
    3738        printf("Observation\n");
     39        printf("   index : %i\n",this->index);
    3840        printf("   x     : %g\n",this->x);
    3941        printf("   y     : %g\n",this->y);
  • TabularUnified issm/trunk-jpl/src/c/objects/Kriging/Observation.h

    r12207 r12229  
    1313                double x,y;
    1414                int    xi,yi;
     15                int    index;
    1516                double value;
    1617
    1718                /*Observation constructors, destructors*/
    1819                Observation();
    19                 Observation(double x_in,double y_in,int xi_in,int yi_in,double value_in);
     20                Observation(double x_in,double y_in,int xi_in,int yi_in,int index_in,double value_in);
    2021                ~Observation();
    2122
     
    3132                void WriteXYObs(double* px,double* py,double* pobs);
    3233};
    33 #endif  /* _EXPONENTIALVARIOGRAM_H */
     34#endif  /* _OBSERVATION_*/
  • TabularUnified issm/trunk-jpl/src/c/objects/Kriging/Quadtree.cpp

    r12226 r12229  
    393393        *A=level;
    394394}/*}}}*/
     395/*FUNCTION Quadtree::RangeSearch{{{1*/
     396void Quadtree::RangeSearch(int **pindices,int *pnobs,double x,double y,double range){
     397
     398        /*Intermediaries*/
     399        int  nobs;
     400        int *indices = NULL;
     401
     402        /*Allocate indices (maximum by default*/
     403        indices = (int*)xmalloc(this->NbObs*sizeof(int));
     404        nobs = 0;
     405
     406        this->root->RangeSearch(indices,&nobs,x,y,range);
     407
     408        /*Clean-up and return*/
     409        *pnobs=nobs;
     410        *pindices=indices;
     411
     412}/*}}}*/
    395413
    396414/*QuadtreeBox methos*/
     
    405423
    406424}/*}}}*/
     425/*FUNCTION QuadtreeBox::IsWithinRange{{{1*/
     426int Quadtree::QuadtreeBox::IsWithinRange(double x,double y,double range){
     427
     428        /*Return 0 if the 2 boxes do not overlap*/
     429        if(this->xcenter+this->length/2 < x-range) return 0;
     430        if(this->xcenter-this->length/2 > x+range) return 0;
     431        if(this->ycenter+this->length/2 < y-range) return 0;
     432        if(this->ycenter-this->length/2 > y+range) return 0;
     433
     434        /*Return 2 if the this box is included in the range*/
     435        if(
     436                                this->xcenter+this->length/2 <= x+range &&
     437                                this->ycenter+this->length/2 <= y+range &&
     438                                this->xcenter-this->length/2 >= x-range &&
     439                                this->ycenter-this->length/2 >= y-range
     440          ) return 2;
     441
     442        /*This is a simple overlap*/
     443        return 1;
     444
     445}/*}}}*/
     446/*FUNCTION QuadtreeBox::RangeSearch{{{1*/
     447void Quadtree::QuadtreeBox::RangeSearch(int* indices,int *pnobs,double x,double y,double range){
     448
     449        /*Intermediaries*/
     450        int i,nobs;
     451
     452        /*Recover current number of observations*/
     453        nobs = *pnobs;
     454
     455        switch(this->IsWithinRange(x,y,range)){
     456                case 0:
     457                        /*If this box is not within range, return*/
     458                        break;
     459                case 2:
     460                        /*This box is included in range*/
     461                        this->WriteObservations(indices,&nobs);
     462                        break;
     463                case 1:
     464                        /*This box is partly included*/
     465                        if(this->nbitems>0){
     466                                /*If this box has only observations, add indices that are within range*/
     467                                for(i=0;i<this->nbitems;i++){
     468                                        if(fabs(this->obs[i]->x-x) <= range && fabs(this->obs[i]->y-y) <= range){
     469                                                indices[nobs++]=this->obs[i]->index;
     470                                        }
     471                                }
     472                        }
     473                        else{
     474                                /*This box points toward boxes*/
     475                                if(this->box[0]) this->box[0]->RangeSearch(indices,&nobs,x,y,range);
     476                                if(this->box[1]) this->box[1]->RangeSearch(indices,&nobs,x,y,range);
     477                                if(this->box[2]) this->box[2]->RangeSearch(indices,&nobs,x,y,range);
     478                                if(this->box[3]) this->box[3]->RangeSearch(indices,&nobs,x,y,range);
     479                        }
     480                        break;
     481                default:
     482                        _error_("Case %i not supported",this->IsWithinRange(x,y,range));
     483        }
     484
     485        /*Assign output pointers: */
     486        *pnobs=nobs;
     487}/*}}}*/
     488/*FUNCTION QuadtreeBox::WriteObservations{{{1*/
     489void Quadtree::QuadtreeBox::WriteObservations(int* indices,int *pnobs){
     490
     491        /*Intermediaries*/
     492        int i,nobs;
     493
     494        /*Recover current number of observations*/
     495        nobs = *pnobs;
     496
     497        if(this->nbitems>0){
     498                /*If this box has only observations, add all indices*/
     499                for(i=0;i<this->nbitems;i++){
     500                        indices[nobs++]=this->obs[i]->index;
     501                }
     502        }
     503        else{
     504                /*This box points toward boxes, */
     505                if(this->box[0]) this->box[0]->WriteObservations(indices,&nobs);
     506                if(this->box[1]) this->box[1]->WriteObservations(indices,&nobs);
     507                if(this->box[2]) this->box[2]->WriteObservations(indices,&nobs);
     508                if(this->box[3]) this->box[3]->WriteObservations(indices,&nobs);
     509        }
     510
     511        /*Assign output pointers: */
     512        *pnobs=nobs;
     513}/*}}}*/
  • TabularUnified issm/trunk-jpl/src/c/objects/Kriging/Quadtree.h

    r12226 r12229  
    11
    2 #ifndef _QUADTREEK_H
    3 #define _QUADTREEK_H
     2#ifndef _QUADTREE_H
     3#define _QUADTREE_H
    44
    55class Observation;
     
    3232                                int     ObjectEnum(){_error_("not implemented yet"); };
    3333                                Object *copy()      {_error_("not implemented yet"); };
     34
     35                                /*Methods*/
     36                                int IsWithinRange(double x,double y,double range);
     37                                void RangeSearch(int* indices,int *pnobs,double x,double y,double range);
     38                                void WriteObservations(int* indices,int *pnobs);
     39
    3440                };
    3541
     
    3945        public:
    4046                int          MaxDepth;          // maximum number of subdivision
    41                 double       coordconversion;   // Coefficient to convert coordinates to integer
    4247                QuadtreeBox *root;              // main box
    4348                long         NbQuadtreeBox;     // total number of boxes
     
    5560                void         QuadtreeDepth(int *A,int xi,int yi);
    5661                void         QuadtreeDepth2(int *A,int xi,int yi);
     62                void         RangeSearch(int **pindices,int *pnobs,double x,double y,double range);
    5763};
    58 #endif //_QUADTREEK_H
     64#endif //_QUADTREE_H
Note: See TracChangeset for help on using the changeset viewer.