/*****************************************************************************/ /*** ***/ /*** process() ***/ /*** Reads in a raster file row by row for processing. ***/ /*** Jo Wood, Project ASSIST, V1.0 7th February 1993 ***/ /*** ***/ /*****************************************************************************/ #include #include #include #include "param.h" #include "nrutil.h" void process(void) { /*--------------------------------------------------------------------------*/ /* INITIALISE */ /*--------------------------------------------------------------------------*/ DCELL *row_in, /* Buffer large enough to hold `wsize' */ *row_out=NULL, /* raster rows. When GRASS reads in a */ /* raster row, each element is of type */ /* DCELL */ *window_ptr, /* Stores local terrain window. */ centre; /* Elevation of central cell in window. */ CELL *featrow_out=NULL; /* store features in CELL */ struct Cell_head region; /* Structure to hold region information */ int nrows, /* Will store the current number of */ ncols, /* rows and columns in the raster. */ row,col, /* Counts through each row and column */ /* of the input raster. */ wind_row, /* Counts through each row and column */ wind_col, /* of the local neighbourhood window. */ *index_ptr; /* Row permutation vector for LU decomp.*/ double **normal_ptr, /* Cross-products matrix. */ *obs_ptr, /* Observed vector. */ temp; /* Unused */ double *weight_ptr; /* Weighting matrix for observed values.*/ /*--------------------------------------------------------------------------*/ /* GET RASTER AND WINDOW DETAILS */ /*--------------------------------------------------------------------------*/ G_get_window(®ion); /* Fill out the region structure (the */ /* geographical limits etc.) */ nrows = G_window_rows(); /* Find out the number of rows and */ ncols = G_window_cols(); /* columns of the raster. */ if ((region.ew_res/region.ns_res >= 1.01) || /* If EW and NS resolns are */ (region.ns_res/region.ew_res >= 1.01)) /* >1% different, warn user. */ { G_warning(_("E-W and N-S grid resolutions are different. Taking average.")); resoln = (region.ns_res + region.ew_res)/2; } else resoln = region.ns_res; /*--------------------------------------------------------------------------*/ /* RESERVE MEMORY TO HOLD Z VALUES AND MATRICES */ /*--------------------------------------------------------------------------*/ row_in = (DCELL *) G_malloc(ncols*sizeof(DCELL)*wsize); /* Reserve `wsize' rows of memory. */ if(mparam != FEATURE) row_out = G_allocate_raster_buf(DCELL_TYPE); /* Initialise output row buffer. */ else featrow_out = G_allocate_raster_buf(CELL_TYPE); /* Initialise output row buffer. */ window_ptr = (DCELL *) G_malloc(SQR(wsize) * sizeof(DCELL)); /* Reserve enough memory for local wind.*/ weight_ptr = (double *) G_malloc(SQR(wsize) * sizeof(double)); /* Reserve enough memory weights matrix.*/ normal_ptr = dmatrix(0,5,0,5); /* Allocate memory for 6*6 matrix */ index_ptr = ivector(0,5); /* and for 1D vector holding indices */ obs_ptr = dvector(0,5); /* and for 1D vector holding observed z */ /* ---------------------------------------------------------------- */ /* - CALCULATE LEAST SQUARES COEFFICIENTS - */ /* ---------------------------------------------------------------- */ /*--- Calculate weighting matrix. ---*/ find_weight(weight_ptr); /* Initial coefficients need only be found once since they are constant for any given window size. The only element that changes is the observed vector (RHS of normal equations). */ /*--- Find normal equations in matrix form. ---*/ find_normal(normal_ptr,weight_ptr); /*--- Apply LU decomposition to normal equations. ---*/ if (constrained){ G_ludcmp(normal_ptr,5,index_ptr,&temp); /* To constrain the quadtratic through the central cell, ignore the calculations involving the coefficient f. Since these are all in the last row and column of the matrix, simply redimension. */ /* disp_matrix(normal_ptr,obs_ptr,obs_ptr,5); */ } else{ G_ludcmp(normal_ptr,6,index_ptr,&temp); /* disp_matrix(normal_ptr,obs_ptr,obs_ptr,6); */ } /*--------------------------------------------------------------------------*/ /* PROCESS INPUT RASTER AND WRITE OUT RASTER LINE BY LINE */ /*--------------------------------------------------------------------------*/ if(mparam != FEATURE) for (wind_row=0; wind_row