        DESCRIPTION OF INTERPOLATION LIBRARY

  written by Irina Kosinovsky 1993
  updated by Mitasova Nov. 96
  NEEDS UPDATE FOR spatially variable smoothing and other changes 
  done by Helena in 1997

Note by Irina in 1993: Below is the collection of functions 
needed for interpolation programs. It should be 
divided into several libraries to provide better 
functionality.


DATA STRUCTURES:
----------------  

struct interp_params 
{
   double zmult;       /* multiplier for z-values */
   FILE *fdinp;         /* input stream */
   int kmin;           /* min number of points per segment for interpolation */
   int kmax;           /* max number of points per segment */ 
   char *maskmap;       /* name of mask */
   int nsizr,nsizc;     /* number of rows and columns */
   double *az,*adx,*ady,*adxx,*adyy,*adxy;  /* array for interpolated values */
   double fi;                               /* tension */
   int    KMAX2;                            /* max num. of points for interp.*/
   int    scik1,scik2,scik3;                /* multipliers for interp. values*/
   double        rsm;          /* smoothing, for rsm<0 use variable smooth from sites */
   char   *elev,*slope,*aspect,*pcurv,*tcurv,*mcurv; /* output files */
   double dmin;                             /* min distance between points */
   double x_orig, y_orig;                   /* origin */
   int    deriv;                            /* 1 if compute partial derivs */
   FILE   *Tmp_fd_z,*Tmp_fd_dx,*Tmp_fd_dy,  /* temp files for writing interp.*/
          *Tmp_fd_xx,*Tmp_fd_yy,*Tmp_fd_xy; /* values */
   FILE   *fddevi;     /* pointer to deviations file */

   int    (*grid_calc) ();                  /*calculates grid for given segm*/
   int    (*matrix_create) ();              /*creates matrix for a given segm*/
   int    (*check_points) ();               /*checks interp. func. at points */
   int    (*secpar) ();                     /* calculates aspect,slope,curv. */
   double (*interp) ();                     /* radial basis function */
   int    (*interpder) ();                  /* derivatives of radial basis function */
   int    (*wr_temp) ();                    /* writes temp files */

};


FUNCTIONS:
----------

void 
IL_init_params_2d(params,inp,zm,k1,k2,msk,rows,cols,ar1,ar2,ar3,ar4,ar5,ar6,
                 tension,k3,sc1,sc2,sc3,sm,f1,f2,f3,f4,f5,f6,dm,x_or,y_or,
                 der,t1,t2,t3,t4,t5,t6,dev)

   struct interp_params *params;
   FILE *inp;         /* input stream */
   double zm;       /* multiplier for z-values */
   int k1;           /* min number of points per segment for interpolation */
   int k2;           /* max number of points per segment */ 
   char *msk;       /* name of mask */
   int rows,cols;     /* number of rows and columns */
   double *ar1,*ar2,*ar3,*ar4,*ar5,*ar6;  /* arrays for interpolated values */
   double tension;                              /* tension */
   int    k3;                            /* max num. of points for interp.*/
   int    sc1,sc2,sc3;                /* multipliers for interp. values*/
   double        sm;               /* smoothing, if sm<0 take it from sites file input */
   char   *f1,*f2,*f3,*f4,*f5,*f6;          /*output files */
   double dm;                             /* min distance between points */
   double x_or, y_or;                   /* origin */
   int    der;                            /* 1 if compute partial derivs */
   FILE   *t1,*t2,*t3,*t4,*t5,*t6;  /* temp files for writing interp. values */
   FILE   *dev;     /* pointer to deviations file */

Initializes parameters called by the library 


void 
IL_init_func_2d(params,grid_f,matr_f,point_f,secp_f,interp_f,
                                                   interpder_f,temp_f)

   struct interp_params *params;
   int    (*grid_f) ();             /*calculates grid for given segm*/
   int    (*matr_f) ();             /*creates matrix for a given segm*/
   int    (*point_f) ();            /*checks interp. func. at points */
   int    (*secp_f) ();             /* calculates aspect,slope,curv. */
   double (*interp_f) ();           /* radial basis function*/
   int    (*interpder_f) ();        /* derivatives of radial basis fucntion */
   int    (*temp_f) ();             /* writes temp files */

Initializes functions called by the library 




double 
IL_crst (r,fi)
   double r;  /* distance squared*/
   double fi; /* tension */

Radial basis function - regularized spline with tension (d=2)


int 
IL_crstg (r, fi, gd1, gd2)
    double r;  /* distance squared*/
    double fi; /* tension */
    double         *gd1;
    double         *gd2;

Derivatives of radial basis function - regularized spline with tension(d=2)


int 
IL_input_data_2d(params,info,xmin,xmax,ymin,ymax,zmin,zmax,MAXPOINTS,n_points)
   struct interp_params *params;
   struct tree_info *info;  /* quadtree info */
   double *xmin,*xmax,*ymin,*ymax,*zmin,*zmax;  
   int maxpoints;      /* max number of points per segment for interpolation */
   int *n_points;      /* number of points used for interpolation */

Inserts input data inside the region into a quad tree.
Also translates data.
Returns number of segments in the quad tree.




int IL_vector_input_data_2d(params,Map,dmax,cats,iselev,info,xmin,
                     xmax,ymin,ymax,zmin,zmax,n_points)

   struct interp_params *params;
   struct Map_info *Map;    /* input vector file */
   double dmax;             /* max distance between points */
   struct Categories *cats; /* Cats file */
   int    iselev;           /* do zeroes represent elevation? */
   struct tree_info *info;  /* quadtree info */
   double *xmin,*xmax,*ymin,*ymax,*zmin,*zmax;
   int *n_points;      /* number of points used for interpolation */

Inserts input data inside the region into a quad tree.
Also translates data.
Returns number of segments in the quad tree.







struct BM *
IL_create_bitmask(params)
  struct interp_params *params;

Creates a bitmap mask from maskmap raster file and/or current MASK if
present and returns a pointer to the bitmask. If no mask is in force returns
NULL.



int 
IL_grid_calc_2d(params,data,bitmask,zmin,zmax,zminac,zmaxac,gmin,gmax,
                              c1min,c1max,c2min,c2max,ertot,b,offset1)
    struct interp_params *params;
    struct quaddata *data;                   /* given segment */
    struct BM *bitmask;                      /* bitmask */
    double zmin,zmax;                        /* min and max input z-values */
    double *zminac,*zmaxac,                  /* min and max interp. z-values */
           *gmin,*gmax,                      /* min and max inperp. slope val.*/
           *c1min,*c1max,*c2min,*c2max;      /* min and max interp. curv. val.*/
    double *ertot;                           /* rms deviation of the interpolated surface */
    double *b;                               /* solutions of linear equations */
    int    offset1;                          /* offset for temp file writing */

Calculates grid for the given segment represented by data (contains n_rows,
n_cols, ew_res,ns_res, and all points inside + overlap) using solutions of
system of lin. equations and interpolating functions interp() and interpder().
Also calls secpar() to compute slope, aspect and curvatures if required.




int 
IL_matrix_create(params,points,n_points,matrix,indx)
    struct interp_params *params;
    struct triple *points;           /* points for interpolation */
    int           n_points;          /* number of points */
    double       **matrix;           /* matrix */
    int           *indx;

Creates system of linear equations represented by matrix using given points
and interpolating function interp()




 
int 
IL_check_at_points_2d (params,n_points,points,b,ertot,zmin)
    struct interp_params *params;
    int           n_points;    /* number of points */ 
    struct triple *points;     /* points for interpolation */
    double        *b;          /* solution of linear equations */
    double        *ertot;      /* rms deviation of the interpolated surface */
    double        zmin;        /* min z-value */

Checks if interpolating function interp() evaluates correct z-values at given
points. If smoothing is used calculate the maximum and rms deviation caused by smoothing.






int 
IL_secpar_loop_2d(params,ngstc,nszc,k,bitmask,gmin,gmax,c1min,c1max,c2min,
                                                         c2max,cond1,cond2)
    struct interp_params *params;
    int    ngstc;       /* starting column */
    int    nszc;        /* ending column */
    int    k;           /* current row */
    struct BM *bitmask;
    double *gmin,*gmax,*c1min,*c1max,*c2min,*c2max; /* min,max interp. values */
    int    cond1,cond2; /*determine if particular values need to be computed*/

Computes slope, aspect and curvatures (depending on cond1, cond2) for derivative
arrays adx,...,adxy between columns ngstc and nszc.



int 
IL_write_temp_2d(params,ngstc,nszc,offset2)
    struct interp_params *params;
    int    ngstc,nszc,offset2;  /* begin. and end. column, offset */

Writes az,adx,...,adxy into appropriate place (depending on ngstc, nszc and
offset) in corresponding temp file */




int 
IL_interp_segments_2d (params,info,tree,bitmask,zmin,zmax,zminac,zmaxac,
               gmin,gmax,c1min,c1max,c2min,c2max,ertot,totsegm,offset1,dnorm)
    struct interp_params *params;
    struct tree_info *info;    /* info for the quad tree */
    struct multtree *tree;    /* current leaf of the quad tree */
    struct BM *bitmask;                      /* bitmask */
    double zmin,zmax;                        /* min and max input z-values */
    double *zminac,*zmaxac,                  /* min and max interp. z-values */
           *gmin,*gmax,                      /* min and max inperp. slope val.*/
           *c1min,*c1max,*c2min,*c2max;      /* min and max interp. curv. val.*/
    double *ertot;                           /* rms deviation of the interpolated surface*/
    int    totsegm;                          /* total number of segments */
    int    offset1;                          /* offset for temp file writing */
    double dnorm;                            /* normalization factor */

Recursively processes each segment in a tree by
  a) finding points from neighbouring segments so that the total number of
     points is between KMIN and KMAX2 by calling tree function MT_get_region().
  b) creating and solving the system of linear equations using these points
     and interp() by calling matrix_create() and G_ludcmp().
  c) checking the interpolated values at given points by calling
     check_points().
  d) computing grid for this segment using points and interp() by calling
     grid_calc().
    
int 
IL_interp_segments_new_2d (params,info,tree,bitmask,
         zmin,zmax,zminac,zmaxac,gmin,gmax,c1min,c1max,
         c2min,c2max,ertot,totsegm,offset1,dnorm)

    struct interp_params *params;
    struct tree_info *info;    /* info for the quad tree */
    struct multtree *tree;    /* current leaf of the quad tree */
    struct BM *bitmask;                      /* bitmask */
    double zmin,zmax;                        /* min and max input z-values */
    double *zminac,*zmaxac,                  /* min and max interp. z-values */
           *gmin,*gmax,                      /* min and max inperp. slope val.*/
           *c1min,*c1max,*c2min,*c2max;      /* min and max interp. curv. val.*/
    double *ertot;                           /* rms deviation of the interpolated surface*/
    int    totsegm;                          /* total number of segments */
    int    offset1;                          /* offset for temp file writing */
    double dnorm;                            /* normalization factor */

The difference between this function and IL_interp_segments_2d() is making
sure that additional points are taken from all directions, i.e. it finds
equal number of points from neigbouring segments in each of 8 neigbourhoods.

Recursively processes each segment in a tree by
  a) finding points from neighbouring segments so that the total number of
     points is between KMIN and KMAX2 by calling tree function MT_get_region().
  b) creating and solving the system of linear equations using these points
     and interp() by calling matrix_create() and G_ludcmp().
  c) checking the interpolated function values at points by calling
     check_points().
  d) computing grid for this segment using points and interp() by calling
     grid_calc().


    
int 
IL_output_2d (params,cellhd,zmin,zmax,zminac,zmaxac,c1min,c1max,c2min,c2max,
                                               gmin,gmax,ertot,dnorm)
    struct interp_params *params;
    struct Cell_head *cellhd;   /* current region */
    double zmin,zmax,           /* min,max input z-values */
           zminac,zmaxac,c1min,c1max,  /* min,max interpolated values */
           c2min,c2max,gmin,gmax;
    double *ertot;                           /* rms deviation of the interpolated surface*/
    double dnorm;                            /* normalization factor */

Creates output files as well as history files  and color tables for them.



