Changeset 12419


Ignore:
Timestamp:
06/15/12 11:41:06 (13 years ago)
Author:
Mathieu Morlighem
Message:

Moved Kriging to method of Observations

Location:
issm/trunk-jpl/src/c/Container
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Container/Elements.h

    r12365 r12419  
    1515class DataSet;
    1616class Inputs;
    17 
    1817
    1918class Elements: public DataSet{
  • issm/trunk-jpl/src/c/Container/Observations.cpp

    r12377 r12419  
    2020#include "../shared/shared.h"
    2121#include "../include/include.h"
     22#include "../modules/modules.h"
    2223#include "../EnumDefinitions/EnumDefinitions.h"
    2324#include "../io/io.h"
     
    115116
    116117/*Methods*/
    117 /*FUNCTION Observations::ObservationList{{{*/
     118/*FUNCTION Observations::ClosestObservation{{{*/
     119void Observations::ClosestObservation(double *px,double *py,double *pobs,double x_interp,double y_interp,double radius){
     120
     121        /*Output and Intermediaries*/
     122        bool         stop;
     123        int          nobs,i,index;
     124        double       h2,hmin2,radius2;
     125        int         *indices      = NULL;
     126        Observation *observation  = NULL;
     127
     128        /*If radius is not provided or is 0, return all observations*/
     129        if(radius==0) radius=this->quadtree->root->length;
     130
     131        /*Compute radius square*/
     132        radius2 = radius*radius;
     133
     134        /*Find all observations that are in radius*/
     135        this->quadtree->RangeSearch(&indices,&nobs,x_interp,y_interp,radius);
     136        for (i=0;i<nobs;i++){
     137                observation=(Observation*)this->GetObjectByOffset(indices[i]);
     138                h2 = (observation->x-x_interp)*(observation->x-x_interp) + (observation->y-y_interp)*(observation->y-y_interp);
     139
     140                if(i==0){
     141                        hmin2 = h2;
     142                        index = i;
     143                }
     144                else{
     145                        if(h2<hmin2){
     146                                hmin2 = h2;
     147                                index = i;
     148                        }
     149                }
     150        } 
     151
     152        /*Assign output pointer*/
     153        if(!nobs){
     154                *px=UNDEF;
     155                *py=UNDEF;
     156                *pobs=UNDEF;
     157        }
     158        else{
     159                observation=(Observation*)this->GetObjectByOffset(indices[index]);
     160                *px=observation->x;
     161                *py=observation->y;
     162                *pobs=observation->value;
     163        }
     164        xfree((void**)&indices);
     165
     166}/*}}}*/
     167/*FUNCTION Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp,double radius,int maxdata){{{*/
    118168void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp,double radius,int maxdata){
    119169
     
    130180        Observation *observation  = NULL;
    131181
     182        /*If radius is not provided or is 0, return all observations*/
     183        if(radius==0) radius=this->quadtree->root->length;
     184
    132185        /*Compute radius square*/
    133         if(radius==0) radius=this->quadtree->root->length;
    134186        radius2 = radius*radius;
    135187
     
    196248        *pnobs=nobs;
    197249}/*}}}*/
     250/*FUNCTION Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs){{{*/
     251void Observations::ObservationList(double **px,double **py,double **pobs,int* pnobs){
     252
     253        /*Output and Intermediaries*/
     254        int          nobs;
     255        double      *x            = NULL;
     256        double      *y            = NULL;
     257        double      *obs          = NULL;
     258        Observation *observation  = NULL;
     259
     260        nobs = this->Size();
     261
     262        if(nobs){
     263                x   = (double*)xmalloc(nobs*sizeof(double));
     264                y   = (double*)xmalloc(nobs*sizeof(double));
     265                obs = (double*)xmalloc(nobs*sizeof(double));
     266                for(int i=0;i<this->Size();i++){
     267                        observation=(Observation*)this->GetObjectByOffset(i);
     268                        observation->WriteXYObs(&x[i],&y[i],&obs[i]);
     269                }
     270        }
     271
     272        /*Assign output pointer*/
     273        *px=x;
     274        *py=y;
     275        *pobs=obs;
     276        *pnobs=nobs;
     277}/*}}}*/
     278/*FUNCTION Observations::InterpolationKriging{{{*/
     279void Observations::InterpolationKriging(double *pprediction,double *perror,double x_interp,double y_interp,double radius,int mindata,int maxdata,Variogram* variogram){
     280
     281        /*Intermediaries*/
     282        int           i,j,n_obs;
     283        double        prediction,error;
     284        double        numerator,denominator,ratio;
     285        double       *x            = NULL;
     286        double       *y            = NULL;
     287        double       *obs          = NULL;
     288        double       *Gamma        = NULL;
     289        double       *GinvG0       = NULL;
     290        double       *Ginv1        = NULL;
     291        double       *GinvZ        = NULL;
     292        double       *gamma0       = NULL;
     293        double       *ones         = NULL;
     294
     295        /*Some checks*/
     296        _assert_(mindata>0 && maxdata>0);
     297        _assert_(pprediction && perror);
     298
     299        /*If radius is not provided or is 0, return all observations*/
     300        if(radius==0) radius=this->quadtree->root->length;
     301
     302        /*Get list of observations for current point*/
     303        this->ObservationList(&x,&y,&obs,&n_obs,x_interp,y_interp,radius,maxdata);
     304
     305        /*If we have less observations than mindata, return UNDEF*/
     306        if(n_obs<mindata){
     307                *pprediction = -999.0;
     308                *perror      = -999.0;
     309                return;
     310        }
     311
     312        /*Allocate intermediary matrix and vectors*/
     313        Gamma  = (double*)xmalloc(n_obs*n_obs*sizeof(double));
     314        gamma0 = (double*)xmalloc(n_obs*sizeof(double));
     315        ones   = (double*)xmalloc(n_obs*sizeof(double));
     316
     317        /*First: Create semivariogram matrix for observations*/
     318        for(i=0;i<n_obs;i++){
     319                for(j=0;j<=i;j++){
     320                        //Gamma[i*n_obs+j] = variogram->SemiVariogram(x[i]-x[j],y[i]-y[j]);
     321                        Gamma[i*n_obs+j] = variogram->Covariance(x[i]-x[j],y[i]-y[j]);
     322                        Gamma[j*n_obs+i] = Gamma[i*n_obs+j];
     323                }
     324        }
     325        for(i=0;i<n_obs;i++) ones[i]=1;
     326
     327        /*Get semivariogram vector associated to this location*/
     328        //for(i=0;i<n_obs;i++) gamma0[i] = variogram->SemiVariogram(x[i]-x_interp,y[i]-y_interp);
     329        for(i=0;i<n_obs;i++) gamma0[i] = variogram->Covariance(x[i]-x_interp,y[i]-y_interp);
     330
     331        /*Solve the three linear systems*/
     332#if _HAVE_GSL_
     333        SolverxGsl(&GinvG0,Gamma,gamma0,n_obs); // Gamma^-1 gamma0
     334        SolverxGsl(&Ginv1, Gamma,ones,n_obs);   // Gamma^-1 ones
     335        SolverxGsl(&GinvZ, Gamma,obs,n_obs);    // Gamma^-1 Z
     336#else
     337        _error_("GSL is required");
     338#endif
     339
     340        /*Prepare predictor*/
     341        numerator=-1.; denominator=0.;
     342        for(i=0;i<n_obs;i++) numerator  +=GinvG0[i];
     343        for(i=0;i<n_obs;i++) denominator+=Ginv1[i];
     344        ratio=numerator/denominator;
     345
     346        prediction = 0.;
     347        error      = - numerator*numerator/denominator;
     348        for(i=0;i<n_obs;i++) prediction += (gamma0[i]-ratio)*GinvZ[i];
     349        for(i=0;i<n_obs;i++) error += gamma0[i]*GinvG0[i];
     350
     351        /*clean-up*/
     352        *pprediction = prediction;
     353        *perror = error;
     354        xfree((void**)&x);
     355        xfree((void**)&y);
     356        xfree((void**)&obs);
     357        xfree((void**)&Gamma);
     358        xfree((void**)&gamma0);
     359        xfree((void**)&ones);
     360        xfree((void**)&GinvG0);
     361        xfree((void**)&Ginv1);
     362        xfree((void**)&GinvZ);
     363
     364}/*}}}*/
    198365/*FUNCTION Observations::QuadtreeColoring{{{*/
    199366void Observations::QuadtreeColoring(double* A,double *x,double *y,int n){
  • issm/trunk-jpl/src/c/Container/Observations.h

    r12292 r12419  
    88class Obsevration;
    99class Quadtree;
     10class Variogram;
    1011class Options;
    1112
     
    2324
    2425                /*Methods*/
     26                void ClosestObservation(double *px,double *py,double *pobs,double x_interp,double y_interp,double radius);
     27                void InterpolationKriging(double *pprediction,double *perror,double x_interp,double y_interp,double radius,int mindata,int maxdata,Variogram* variogram);
     28                void ObservationList(double **px,double **py,double **pobs,int* pnobs);
    2529                void ObservationList(double **px,double **py,double **pobs,int* pnobs,double x_interp,double y_interp,double radius,int maxdata);
    2630                void QuadtreeColoring(double* A,double *x,double *y,int n);
Note: See TracChangeset for help on using the changeset viewer.