46
45
#include <grass/glocale.h>
47
46
#include <grass/gmath.h>
49
#include "local_proto.h"
52
double /* pargr */ ns_res, ew_res, inp_ew_res, inp_ns_res;
53
int inp_rows, inp_cols;
54
double x_orig, y_orig;
55
double inp_x_orig, inp_y_orig;
56
double dmin, ertre, deltx, delty;
58
int KMAX2 /* , KMIN, KMAX */ ;
60
double /* datgr */ *az, *adx, *ady, *adxx, *adyy, *adxy;
61
double /* error */ ertot, ertre, zminac, zmaxac, zmult;
67
int deriv, overlap, cursegm, dtens;
71
int NERROR, cond1, cond2;
75
double fstar2, tfsta2, xmin, xmax, ymin, ymax, zmin, zmax, gmin, gmax, c1min,
81
FCELL *zero_array_cell;
82
struct interp_params params;
84
FILE *fdredinp, *fdzout, *fddxout, *fddyout, *fdxxout, *fdyyout, *fxyout;
85
FILE *fd4; /* unused? */
86
int fdinp, fdsmooth = -1;
48
static double /* pargr */ ns_res, ew_res, inp_ew_res, inp_ns_res;
49
static int inp_rows, inp_cols;
51
static double inp_x_orig, inp_y_orig;
52
static double dmin, ertre, deltx, delty;
53
static int nsizr, nsizc;
55
static double /* datgr */ *az, *adx, *ady, *adxx, *adyy, *adxy;
56
static double /* error */ ertot, ertre, zminac, zmaxac, zmult;
59
static int deriv, overlap, cursegm, dtens;
62
static int cond1, cond2;
63
static double fstar2, tfsta2, xmin, xmax, ymin, ymax, zmin, zmax, gmin, gmax,
64
c1min, c1max, c2min, c2max;
67
static double theta, scalex;
69
static FCELL *zero_array_cell;
70
static struct interp_params params;
72
static FILE *fd4; /* unused? */
73
static int fdinp, fdsmooth = -1;
89
76
* x,y,z - input data npoint - number of input data fi - tension parameter
99
86
* interpolation of z-values to given point x,y
111
char *maskmap = NULL;
114
FILE *Tmp_fd_z = NULL;
115
char *Tmp_file_z = NULL;
116
FILE *Tmp_fd_dx = NULL;
117
char *Tmp_file_dx = NULL;
118
FILE *Tmp_fd_dy = NULL;
119
char *Tmp_file_dy = NULL;
120
FILE *Tmp_fd_xx = NULL;
121
char *Tmp_file_xx = NULL;
122
FILE *Tmp_fd_yy = NULL;
123
char *Tmp_file_yy = NULL;
124
FILE *Tmp_fd_xy = NULL;
125
char *Tmp_file_xy = NULL;
128
struct Cell_head winhd;
129
struct Cell_head inphd;
130
struct Cell_head outhd;
131
struct Cell_head smhd;
99
static off_t sdisk, disk;
101
static char *Tmp_file_z;
102
static char *Tmp_file_dx;
103
static char *Tmp_file_dy;
104
static char *Tmp_file_xx;
105
static char *Tmp_file_yy;
106
static char *Tmp_file_xy;
108
static FILE *Tmp_fd_z;
109
static FILE *Tmp_fd_dx;
110
static FILE *Tmp_fd_dy;
111
static FILE *Tmp_fd_xx;
112
static FILE *Tmp_fd_yy;
113
static FILE *Tmp_fd_xy;
114
static FILE *Tmp_fd_z;
116
static struct BM *bitmask;
117
static struct Cell_head winhd;
118
static struct Cell_head inphd;
119
static struct Cell_head outhd;
120
static struct Cell_head smhd;
122
static void create_temp_files(void);
123
static void clean(void);
133
125
int main(int argc, char *argv[])
136
128
struct FPRange range;
137
129
DCELL cellmin, cellmax;
138
130
FCELL *cellrow, fcellmin;
174
167
parm.res_ns->required = YES;
175
168
parm.res_ns->description = _("Desired north-south resolution");
177
parm.elev = G_define_option();
178
parm.elev->key = "elev";
179
parm.elev->type = TYPE_STRING;
170
parm.elev = G_define_standard_option(G_OPT_R_ELEV);
180
171
parm.elev->required = NO;
181
172
parm.elev->gisprompt = "new,cell,raster";
182
parm.elev->description = _("Output z-file (elevation) map");
183
parm.elev->guisection = _("Output_options");
173
parm.elev->description = _("Name for output elevation raster map");
174
parm.elev->guisection = _("Output");
185
parm.slope = G_define_option();
176
parm.slope = G_define_standard_option(G_OPT_R_OUTPUT);
186
177
parm.slope->key = "slope";
187
parm.slope->type = TYPE_STRING;
188
178
parm.slope->required = NO;
189
parm.slope->gisprompt = "new,cell,raster";
190
parm.slope->description = _("Output slope map (or fx)");
191
parm.slope->guisection = _("Output_options");
179
parm.slope->description = _("Name for output slope map (or fx)");
180
parm.slope->guisection = _("Output");
193
parm.aspect = G_define_option();
182
parm.aspect = G_define_standard_option(G_OPT_R_OUTPUT);
194
183
parm.aspect->key = "aspect";
195
parm.aspect->type = TYPE_STRING;
196
184
parm.aspect->required = NO;
197
parm.aspect->gisprompt = "new,cell,raster";
198
parm.aspect->description = _("Output aspect map (or fy)");
199
parm.aspect->guisection = _("Output_options");
185
parm.aspect->description = _("Name for output aspect map (or fy)");
186
parm.aspect->guisection = _("Output");
201
parm.pcurv = G_define_option();
202
parm.pcurv->key = "pcurv";
203
parm.pcurv->type = TYPE_STRING;
188
parm.pcurv = G_define_standard_option(G_OPT_R_OUTPUT);
189
parm.pcurv->key = "pcurvature";
204
190
parm.pcurv->required = NO;
205
parm.pcurv->gisprompt = "new,cell,raster";
206
parm.pcurv->description = _("Output profile curvature map (or fxx)");
207
parm.pcurv->guisection = _("Output_options");
191
parm.pcurv->description = _("Name for output profile curvature map (or fxx)");
192
parm.pcurv->guisection = _("Output");
209
parm.tcurv = G_define_option();
210
parm.tcurv->key = "tcurv";
211
parm.tcurv->type = TYPE_STRING;
194
parm.tcurv = G_define_standard_option(G_OPT_R_OUTPUT);
195
parm.tcurv->key = "tcurvature";
212
196
parm.tcurv->required = NO;
213
parm.tcurv->gisprompt = "new,cell,raster";
214
parm.tcurv->description = _("Output tangential curvature map (or fyy)");
215
parm.tcurv->guisection = _("Output_options");
197
parm.tcurv->description = _("Name for output tangential curvature map (or fyy)");
198
parm.tcurv->guisection = _("Output");
217
parm.mcurv = G_define_option();
218
parm.mcurv->key = "mcurv";
219
parm.mcurv->type = TYPE_STRING;
200
parm.mcurv = G_define_standard_option(G_OPT_R_OUTPUT);
201
parm.mcurv->key = "mcurvature";
220
202
parm.mcurv->required = NO;
221
parm.mcurv->gisprompt = "new,cell,raster";
222
parm.mcurv->description = _("Output mean curvature map (or fxy)");
223
parm.mcurv->guisection = _("Output_options");
203
parm.mcurv->description = _("Name for output mean curvature map (or fxy)");
204
parm.mcurv->guisection = _("Output");
225
parm.smooth = G_define_option();
206
parm.smooth = G_define_standard_option(G_OPT_R_INPUT);
226
207
parm.smooth->key = "smooth";
227
parm.smooth->type = TYPE_STRING;
228
208
parm.smooth->required = NO;
229
parm.smooth->gisprompt = "old,cell,raster";
230
parm.smooth->description = _("Name of raster map containing smoothing");
209
parm.smooth->description = _("Name of input raster map containing smoothing");
231
210
parm.smooth->guisection = _("Settings");
233
parm.maskmap = G_define_option();
212
parm.maskmap = G_define_standard_option(G_OPT_R_INPUT);
234
213
parm.maskmap->key = "maskmap";
235
parm.maskmap->type = TYPE_STRING;
236
214
parm.maskmap->required = NO;
237
parm.maskmap->gisprompt = "old,cell,raster";
238
parm.maskmap->description = _("Name of raster map to be used as mask");
215
parm.maskmap->description = _("Name of input raster map to be used as mask");
239
216
parm.maskmap->guisection = _("Settings");
241
218
parm.overlap = G_define_option();
359
335
ns_res = outhd.ns_res;
360
336
nsizc = outhd.cols;
361
337
nsizr = outhd.rows;
362
disk = nsizc * nsizr * sizeof(int);
338
disk = (off_t)nsizc * nsizr * sizeof(int);
364
340
az = G_alloc_vector(nsizc + 1);
366
G_fatal_error(_("Not enough memory for az"));
369
343
adx = G_alloc_vector(nsizc + 1);
371
G_fatal_error(_("Not enough memory for adx"));
373
344
ady = G_alloc_vector(nsizc + 1);
375
G_fatal_error(_("Not enough memory for ady"));
378
346
adxx = G_alloc_vector(nsizc + 1);
380
G_fatal_error(_("Not enough memory for adxx"));
382
347
adyy = G_alloc_vector(nsizc + 1);
384
G_fatal_error(_("Not enough memory for adyy"));
386
348
adxy = G_alloc_vector(nsizc + 1);
388
G_fatal_error(_("Not enough memory for adxy"));
392
352
if (smooth != NULL) {
394
mapset = G_find_file("cell", smooth, "");
397
G_fatal_error(_("Raster map <%s> not found"), smooth);
399
G_debug(1, "mapset for smooth map is [%s]", mapset);
401
if ((fdsmooth = G_open_cell_old(smooth, mapset)) < 0)
402
G_fatal_error(_("Unable to open raster map <%s>"), smooth);
404
if (G_get_cellhd(smooth, mapset, &smhd) < 0)
405
G_fatal_error(_("[%s]: Cannot read map header"), smooth);
354
Rast_get_cellhd(smooth, "", &smhd);
407
356
if ((winhd.ew_res != smhd.ew_res) || (winhd.ns_res != smhd.ns_res))
408
G_fatal_error(_("[%s]: Map is the wrong resolution"), smooth);
357
G_fatal_error(_("Map <%s> is the wrong resolution"), smooth);
410
if (G_read_fp_range(smooth, mapset, &range) >= 0)
411
G_get_fp_range_min_max(&range, &cellmin, &cellmax);
359
if (Rast_read_fp_range(smooth, "", &range) >= 0)
360
Rast_get_fp_range_min_max(&range, &cellmin, &cellmax);
413
362
fcellmin = (float)cellmin;
415
if (G_is_f_null_value(&fcellmin) || fcellmin < 0.0)
364
if (Rast_is_f_null_value(&fcellmin) || fcellmin < 0.0)
416
365
G_fatal_error(_("Smoothing values can not be negative or NULL"));
420
mapset = G_find_file("cell", input, "");
423
G_fatal_error(_("Raster map <%s> not found"), input);
425
G_debug(1, "mapset for input map is [%s]", mapset);
427
if (G_get_cellhd(input, mapset, &inphd) < 0)
428
G_fatal_error(_("[%s]: Cannot read map header"), input);
368
Rast_get_cellhd(input, "", &inphd);
430
370
if ((winhd.ew_res != inphd.ew_res) || (winhd.ns_res != inphd.ns_res))
431
371
G_fatal_error(_("Input map resolution differs from current region resolution!"));
433
if ((fdinp = G_open_cell_old(input, mapset)) < 0)
434
G_fatal_error(_("Unable to open raster map <%s>"), input);
438
374
if (elev != NULL)
469
if (G_read_fp_range(input, mapset, &range) >= 0) {
470
G_get_fp_range_min_max(&range, &cellmin, &cellmax);
418
if (Rast_read_fp_range(input, "", &range) >= 0) {
419
Rast_get_fp_range_min_max(&range, &cellmin, &cellmax);
473
cellrow = G_allocate_f_raster_buf();
422
fdinp = Rast_open_old(input, "");
424
cellrow = Rast_allocate_f_buf();
474
425
for (m1 = 0; m1 < inp_rows; m1++) {
475
ret_val = G_get_f_raster_row(fdinp, cellrow, m1);
477
G_fatal_error(_("Cannot get row %d (error = %d)"), m1,
480
G_row_update_fp_range(cellrow, m1, &range, FCELL_TYPE);
426
Rast_get_f_row(fdinp, cellrow, m1);
427
Rast_row_update_fp_range(cellrow, m1, &range, FCELL_TYPE);
482
G_get_fp_range_min_max(&range, &cellmin, &cellmax);
429
Rast_get_fp_range_min_max(&range, &cellmin, &cellmax);
485
434
fcellmin = (float)cellmin;
486
if (G_is_f_null_value(&fcellmin))
435
if (Rast_is_f_null_value(&fcellmin))
487
436
G_fatal_error(_("Maximum value of a raster map is NULL."));
489
438
zmin = (double)cellmin *zmult;
558
511
if (IL_resample_output_2d(¶ms, zmin, zmax, zminac, zmaxac, c1min,
559
512
c1max, c2min, c2max, gmin, gmax, ertot, input,
560
&dnorm, &outhd, &winhd, smooth, NPOINT) < 0)
562
("Can not write raster maps -- try increasing cell size");
513
&dnorm, &outhd, &winhd, smooth, NPOINT) < 0) {
515
G_fatal_error(_("Unable to write raster maps -- try increasing cell size"));
564
518
G_free(zero_array_cell);
593
523
if (smooth != NULL)
594
G_close_cell(fdsmooth);
524
Rast_close(fdsmooth);
597
527
exit(EXIT_SUCCESS);
601
void create_temp_files(void)
530
static FILE *create_temp_file(const char *name, char **tmpname)
605
zero_array_cell = (FCELL *) G_malloc(sizeof(FCELL) * nsizc);
606
if (!zero_array_cell)
607
G_fatal_error(_("Not enough memory for zero_array_cell"));
609
for (i = 0; i < nsizc; i++) {
610
zero_array_cell[i] = (FCELL) 0;
614
Tmp_file_z = G_tempfile();
615
if (NULL == (Tmp_fd_z = fopen(Tmp_file_z, "w+")))
616
G_fatal_error(_("Unable to open temporary file <%s>"),
619
for (i = 0; i < nsizr; i++) {
620
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_z)))
622
(_("Not enough disk space -- cannot write files"));
626
Tmp_file_dx = G_tempfile();
627
if (NULL == (Tmp_fd_dx = fopen(Tmp_file_dx, "w+"))) {
628
sprintf(msg, _("Unable to open temporary file <%s>"),
630
clean_fatal_error(msg);
632
for (i = 0; i < nsizr; i++) {
633
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_dx)))
635
(_("Not enough disk space -- cannot write files"));
638
if (aspect != NULL) {
639
Tmp_file_dy = G_tempfile();
640
if (NULL == (Tmp_fd_dy = fopen(Tmp_file_dy, "w+"))) {
641
sprintf(msg, _("Unable to open temporary file <%s>"),
643
clean_fatal_error(msg);
645
for (i = 0; i < nsizr; i++) {
646
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_dy)))
648
(_("Not enough disk space -- cannot write files"));
653
Tmp_file_xx = G_tempfile();
654
if (NULL == (Tmp_fd_xx = fopen(Tmp_file_xx, "w+"))) {
655
sprintf(msg, _("Unable to open temporary file <%s>"),
657
clean_fatal_error(msg);
659
for (i = 0; i < nsizr; i++) {
660
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_xx)))
662
(_("Not enough disk space -- cannot write files"));
666
Tmp_file_yy = G_tempfile();
667
if (NULL == (Tmp_fd_yy = fopen(Tmp_file_yy, "w+"))) {
668
sprintf(msg, _("Unable to open temporary file <%s>"),
670
clean_fatal_error(msg);
672
for (i = 0; i < nsizr; i++) {
673
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_yy)))
675
(_("Not enough disk space -- cannot write files"));
679
Tmp_file_xy = G_tempfile();
680
if (NULL == (Tmp_fd_xy = fopen(Tmp_file_xy, "w+"))) {
681
sprintf(msg, _("Unable to open temporary file <%s>"),
683
clean_fatal_error(msg);
685
for (i = 0; i < nsizr; i++) {
686
if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_xy)))
688
(_("Not enough disk space -- cannot write files"));
695
void clean_fatal_error(char *str)
539
*tmpname = tmp = G_tempfile();
540
fp = fopen(tmp, "w+");
542
G_fatal_error(_("Unable to open temporary file <%s>"), *tmpname);
544
for (i = 0; i < nsizr; i++) {
545
if (fwrite(zero_array_cell, sizeof(FCELL), nsizc, fp) != nsizc) {
547
G_fatal_error(_("Error writing temporary file <%s>"), *tmpname);
554
static void create_temp_files(void)
556
zero_array_cell = (FCELL *) G_calloc(nsizc, sizeof(FCELL));
558
Tmp_fd_z = create_temp_file(elev, &Tmp_file_z );
559
Tmp_fd_dx = create_temp_file(slope, &Tmp_file_dx);
560
Tmp_fd_dy = create_temp_file(aspect, &Tmp_file_dy);
561
Tmp_fd_xx = create_temp_file(pcurv, &Tmp_file_xx);
562
Tmp_fd_yy = create_temp_file(tcurv, &Tmp_file_yy);
563
Tmp_fd_xy = create_temp_file(mcurv, &Tmp_file_xy);
566
static void clean(void)
568
if (Tmp_fd_z) fclose(Tmp_fd_z);
569
if (Tmp_fd_dx) fclose(Tmp_fd_dx);
570
if (Tmp_fd_dy) fclose(Tmp_fd_dy);
571
if (Tmp_fd_xx) fclose(Tmp_fd_xx);
572
if (Tmp_fd_yy) fclose(Tmp_fd_yy);
573
if (Tmp_fd_xy) fclose(Tmp_fd_xy);
575
if (Tmp_file_z) unlink(Tmp_file_z);
576
if (Tmp_file_dx) unlink(Tmp_file_dx);
577
if (Tmp_file_dy) unlink(Tmp_file_dy);
578
if (Tmp_file_xx) unlink(Tmp_file_xx);
579
if (Tmp_file_yy) unlink(Tmp_file_yy);
580
if (Tmp_file_xy) unlink(Tmp_file_xy);