~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.resamp.rst/main.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
#include <stdlib.h>
36
36
#include <unistd.h>
37
37
 
38
 
#define MAIN
39
 
 
40
38
#include <grass/dbmi.h>
41
39
#include <grass/gis.h>
 
40
#include <grass/raster.h>
42
41
#include <grass/linkm.h>
43
42
#include <grass/bitmap.h>
44
43
#include "surf.h"
46
45
#include <grass/glocale.h>
47
46
#include <grass/gmath.h>
48
47
 
49
 
#include "local_proto.h"
50
 
 
51
 
 
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;
57
 
int nsizr, nsizc;
58
 
int KMAX2 /* , KMIN, KMAX */ ;
59
 
 
60
 
double /* datgr */ *az, *adx, *ady, *adxx, *adyy, *adxy;
61
 
double /* error */ ertot, ertre, zminac, zmaxac, zmult;
62
 
 
63
 
int total = 0;
64
 
int NPOINT = 0;
65
 
int OUTRANGE = 0;
66
 
int NPT = 0;
67
 
int deriv, overlap, cursegm, dtens;
68
 
double fi;
69
 
 
70
 
double DETERM;
71
 
int NERROR, cond1, cond2;
72
 
char fncdsm[32];
73
 
char filnam[10];
74
 
char msg[1024];
75
 
double fstar2, tfsta2, xmin, xmax, ymin, ymax, zmin, zmax, gmin, gmax, c1min,
76
 
    c1max, c2min, c2max;
77
 
double dnorm;
78
 
double smc;
79
 
double theta, scalex;
80
 
 
81
 
FCELL *zero_array_cell;
82
 
struct interp_params params;
83
 
 
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;
 
50
 
 
51
static double inp_x_orig, inp_y_orig;
 
52
static double dmin, ertre, deltx, delty;
 
53
static int nsizr, nsizc;
 
54
 
 
55
static double /* datgr */ *az, *adx, *ady, *adxx, *adyy, *adxy;
 
56
static double /* error */ ertot, ertre, zminac, zmaxac, zmult;
 
57
 
 
58
static int NPOINT;
 
59
static int deriv, overlap, cursegm, dtens;
 
60
static double fi;
 
61
 
 
62
static int cond1, cond2;
 
63
static double fstar2, tfsta2, xmin, xmax, ymin, ymax, zmin, zmax, gmin, gmax,
 
64
    c1min, c1max, c2min, c2max;
 
65
static double dnorm;
 
66
static double smc;
 
67
static double theta, scalex;
 
68
 
 
69
static FCELL *zero_array_cell;
 
70
static struct interp_params params;
 
71
 
 
72
static FILE *fd4;                       /* unused? */
 
73
static int fdinp, fdsmooth = -1;
87
74
 
88
75
/*
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
100
87
 */
101
88
 
102
 
char *input;
103
 
char *smooth = NULL;
104
 
char *mapset;
105
 
char *elev = NULL;
106
 
char *slope = NULL;
107
 
char *aspect = NULL;
108
 
char *pcurv = NULL;
109
 
char *tcurv = NULL;
110
 
char *mcurv = NULL;
111
 
char *maskmap = NULL;
112
 
char *redinp = NULL;
113
 
int sdisk, disk;
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;
126
 
 
127
 
struct BM *bitmask;
128
 
struct Cell_head winhd;
129
 
struct Cell_head inphd;
130
 
struct Cell_head outhd;
131
 
struct Cell_head smhd;
 
89
static char *input;
 
90
static char *smooth;
 
91
static char *elev;
 
92
static char *slope;
 
93
static char *aspect;
 
94
static char *pcurv;
 
95
static char *tcurv;
 
96
static char *mcurv;
 
97
static char *maskmap;
 
98
 
 
99
static off_t sdisk, disk;
 
100
 
 
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;
 
107
 
 
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;
 
115
 
 
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;
 
121
 
 
122
static void create_temp_files(void);
 
123
static void clean(void);
132
124
 
133
125
int main(int argc, char *argv[])
134
126
{
135
 
    int m1, ret_val;
 
127
    int m1;
136
128
    struct FPRange range;
137
129
    DCELL cellmin, cellmax;
138
130
    FCELL *cellrow, fcellmin;
153
145
    G_gisinit(argv[0]);
154
146
 
155
147
    module = G_define_module();
156
 
    module->keywords = _("raster, resample");
 
148
    G_add_keyword(_("raster"));
 
149
    G_add_keyword(_("resample"));
157
150
    module->description =
158
151
        _("Reinterpolates and optionally computes topographic analysis from "
159
152
          "input raster map to a new raster map (possibly with "
174
167
    parm.res_ns->required = YES;
175
168
    parm.res_ns->description = _("Desired north-south resolution");
176
169
 
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");
184
175
 
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");
192
181
 
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");
200
187
 
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");
208
193
 
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");
216
199
 
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");
224
205
 
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");
232
211
 
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");
240
217
 
241
218
    parm.overlap = G_define_option();
247
224
    parm.overlap->guisection = _("Settings");
248
225
 
249
226
    parm.zmult = G_define_option();
250
 
    parm.zmult->key = "zmult";
 
227
    parm.zmult->key = "zscale";
251
228
    parm.zmult->type = TYPE_DOUBLE;
252
229
    parm.zmult->answer = ZMULT;
253
230
    parm.zmult->required = NO;
266
243
    parm.theta->key = "theta";
267
244
    parm.theta->type = TYPE_DOUBLE;
268
245
    parm.theta->required = NO;
269
 
    parm.theta->description = _("Anisotropy angle (in degrees)");
 
246
    parm.theta->description = _("Anisotropy angle (in degrees counterclockwise from East)");
270
247
    parm.theta->guisection = _("Anisotropy");
271
248
 
272
249
    parm.scalex = G_define_option();
284
261
    flag.deriv->key = 'd';
285
262
    flag.deriv->description =
286
263
        _("Output partial derivatives instead of topographic parameters");
287
 
    flag.deriv->guisection = _("Output_options");
 
264
    flag.deriv->guisection = _("Output");
288
265
 
289
266
    if (G_parser(argc, argv))
290
267
        exit(EXIT_FAILURE);
291
268
 
292
 
    if (G_get_set_window(&winhd) == -1)
293
 
        G_fatal_error(_("Retrieving and setting region failed"));
 
269
    G_get_set_window(&winhd);
294
270
 
295
271
    inp_ew_res = winhd.ew_res;
296
272
    inp_ns_res = winhd.ns_res;
318
294
    ertre = 0.1;
319
295
 
320
296
    if (!G_scan_resolution(parm.res_ew->answer, &ew_res, winhd.proj))
321
 
        G_fatal_error(_("Cannot read ew_res value"));
 
297
        G_fatal_error(_("Unable to read ew_res value"));
322
298
 
323
299
    if (!G_scan_resolution(parm.res_ns->answer, &ns_res, winhd.proj))
324
 
        G_fatal_error(_("Cannot read ns_res value"));
 
300
        G_fatal_error(_("Unable to read ns_res value"));
325
301
 
326
302
    if (sscanf(parm.fi->answer, "%lf", &fi) != 1)
327
303
        G_fatal_error(_("Invalid value for tension"));
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);
363
339
 
364
340
    az = G_alloc_vector(nsizc + 1);
365
 
    if (!az)
366
 
        G_fatal_error(_("Not enough memory for az"));
367
341
 
368
342
    if (cond1) {
369
343
        adx = G_alloc_vector(nsizc + 1);
370
 
        if (!adx)
371
 
            G_fatal_error(_("Not enough memory for adx"));
372
 
 
373
344
        ady = G_alloc_vector(nsizc + 1);
374
 
        if (!ady)
375
 
            G_fatal_error(_("Not enough memory for ady"));
376
 
 
377
345
        if (cond2) {
378
346
            adxx = G_alloc_vector(nsizc + 1);
379
 
            if (!adxx)
380
 
                G_fatal_error(_("Not enough memory for adxx"));
381
 
 
382
347
            adyy = G_alloc_vector(nsizc + 1);
383
 
            if (!adyy)
384
 
                G_fatal_error(_("Not enough memory for adyy"));
385
 
 
386
348
            adxy = G_alloc_vector(nsizc + 1);
387
 
            if (!adxy)
388
 
                G_fatal_error(_("Not enough memory for adxy"));
389
349
        }
390
350
    }
391
 
    mapset = NULL;
 
351
 
392
352
    if (smooth != NULL) {
393
353
 
394
 
        mapset = G_find_file("cell", smooth, "");
395
 
 
396
 
        if (mapset == NULL)
397
 
            G_fatal_error(_("Raster map <%s> not found"), smooth);
398
 
 
399
 
        G_debug(1, "mapset for smooth map is [%s]", mapset);
400
 
 
401
 
        if ((fdsmooth = G_open_cell_old(smooth, mapset)) < 0)
402
 
            G_fatal_error(_("Unable to open raster map <%s>"), smooth);
403
 
 
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);
406
355
 
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);
409
358
 
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);
412
361
 
413
362
        fcellmin = (float)cellmin;
414
363
 
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"));
417
366
    }
418
367
 
419
 
    mapset = NULL;
420
 
    mapset = G_find_file("cell", input, "");
421
 
 
422
 
    if (mapset == NULL)
423
 
        G_fatal_error(_("Raster map <%s> not found"), input);
424
 
 
425
 
    G_debug(1, "mapset for input map is [%s]", mapset);
426
 
 
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);
429
369
 
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!"));
432
372
 
433
 
    if ((fdinp = G_open_cell_old(input, mapset)) < 0)
434
 
        G_fatal_error(_("Unable to open raster map <%s>"), input);
435
 
 
436
 
 
437
373
    sdisk = 0;
438
374
    if (elev != NULL)
439
375
        sdisk += disk;
449
385
        sdisk += disk;
450
386
 
451
387
    G_message(_("Processing all selected output files will require"));
452
 
    G_message(_("%d bytes of disk space for temp files."), sdisk);
 
388
    if (sdisk > 1024) {
 
389
        if (sdisk > 1024 * 1024) {
 
390
            if (sdisk > 1024 * 1024 * 1024) {
 
391
                G_message(_("%.2f GB of disk space for temp files."), sdisk / (1024. * 1024. * 1024.));
 
392
            }
 
393
            else
 
394
                G_message(_("%.2f MB of disk space for temp files."), sdisk / (1024. * 1024.));
 
395
        }
 
396
        else
 
397
            G_message(_("%.2f KB of disk space for temp files."), sdisk / 1024.);
 
398
    }
 
399
    else
 
400
        G_message(_n("%d byte of disk space for temp files.", 
 
401
        "%d bytes of disk space for temp files.", (int)sdisk), (int)sdisk);
453
402
 
454
403
 
455
404
    fstar2 = fi * fi / 4.;
466
415
        smc = 0.01;
467
416
 
468
417
 
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);
471
420
    }
472
421
    else {
473
 
        cellrow = G_allocate_f_raster_buf();
 
422
        fdinp = Rast_open_old(input, "");
 
423
 
 
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);
476
 
            if (ret_val < 0)
477
 
                G_fatal_error(_("Cannot get row %d (error = %d)"), m1,
478
 
                              ret_val);
479
 
 
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);
481
428
        }
482
 
        G_get_fp_range_min_max(&range, &cellmin, &cellmax);
 
429
        Rast_get_fp_range_min_max(&range, &cellmin, &cellmax);
 
430
 
 
431
        Rast_close(fdinp);
483
432
    }
484
433
 
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."));
488
437
 
489
438
    zmin = (double)cellmin *zmult;
513
462
                    IL_secpar_loop_2d, IL_crst, IL_crstg, IL_write_temp_2d);
514
463
 
515
464
    G_message(_("Temporarily changing the region to desired resolution ..."));
516
 
    if (G_set_window(&outhd) < 0)
517
 
        G_fatal_error("Cannot set region to output region!");
 
465
    Rast_set_window(&outhd);
518
466
 
519
467
    bitmask = IL_create_bitmask(&params);
520
468
    /* change region to initial region */
521
469
    G_message(_("Changing back to the original region ..."));
522
 
    if (G_set_window(&winhd) < 0)
523
 
        G_fatal_error(_("Cannot set region to back to the initial region !!!"));
 
470
    Rast_set_window(&winhd);
 
471
 
 
472
    fdinp = Rast_open_old(input, "");
 
473
    if (smooth != NULL)
 
474
        fdsmooth = Rast_open_old(smooth, "");
524
475
 
525
476
    ertot = 0.;
526
477
    cursegm = 0;
538
489
 
539
490
    G_message(_("dnorm in mainc after grid before out1= %f"), dnorm);
540
491
 
541
 
    if (NPOINT < 0)
542
 
        clean_fatal_error("split_and_interpolate() failed");
 
492
    if (NPOINT < 0) {
 
493
        clean();
 
494
        G_fatal_error(_("split_and_interpolate() failed"));
 
495
    }
543
496
 
544
497
    if (fd4 != NULL)
545
498
        fprintf(fd4, "max. error found = %f \n", ertot);
557
510
 
558
511
    if (IL_resample_output_2d(&params, zmin, zmax, zminac, zmaxac, c1min,
559
512
                              c1max, c2min, c2max, gmin, gmax, ertot, input,
560
 
                              &dnorm, &outhd, &winhd, smooth, NPOINT) < 0)
561
 
        clean_fatal_error
562
 
            ("Can not write raster maps -- try increasing cell size");
 
513
                              &dnorm, &outhd, &winhd, smooth, NPOINT) < 0) {
 
514
        clean();
 
515
        G_fatal_error(_("Unable to write raster maps -- try increasing cell size"));
 
516
    }
563
517
 
564
518
    G_free(zero_array_cell);
565
 
    if (elev != NULL)
566
 
        fclose(Tmp_fd_z);
567
 
    if (slope != NULL)
568
 
        fclose(Tmp_fd_dx);
569
 
    if (aspect != NULL)
570
 
        fclose(Tmp_fd_dy);
571
 
    if (pcurv != NULL)
572
 
        fclose(Tmp_fd_xx);
573
 
    if (tcurv != NULL)
574
 
        fclose(Tmp_fd_yy);
575
 
    if (mcurv != NULL)
576
 
        fclose(Tmp_fd_xy);
577
 
 
578
 
    if (elev != NULL)
579
 
        unlink(Tmp_file_z);
580
 
    if (slope != NULL)
581
 
        unlink(Tmp_file_dx);
582
 
    if (aspect != NULL)
583
 
        unlink(Tmp_file_dy);
584
 
    if (pcurv != NULL)
585
 
        unlink(Tmp_file_xx);
586
 
    if (tcurv != NULL)
587
 
        unlink(Tmp_file_yy);
588
 
    if (mcurv != NULL)
589
 
        unlink(Tmp_file_xy);
 
519
    clean();
590
520
    if (fd4)
591
521
        fclose(fd4);
592
 
    G_close_cell(fdinp);
 
522
    Rast_close(fdinp);
593
523
    if (smooth != NULL)
594
 
        G_close_cell(fdsmooth);
 
524
        Rast_close(fdsmooth);
595
525
 
596
 
    G_done_msg("");
 
526
    G_done_msg(" ");
597
527
    exit(EXIT_SUCCESS);
598
528
}
599
529
 
600
 
 
601
 
void create_temp_files(void)
 
530
static FILE *create_temp_file(const char *name, char **tmpname)
602
531
{
 
532
    FILE *fp;
 
533
    char *tmp;
603
534
    int i;
604
535
 
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"));
608
 
 
609
 
    for (i = 0; i < nsizc; i++) {
610
 
        zero_array_cell[i] = (FCELL) 0;
611
 
    }
612
 
 
613
 
    if (elev != NULL) {
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>"),
617
 
                          Tmp_file_z);
618
 
 
619
 
        for (i = 0; i < nsizr; i++) {
620
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_z)))
621
 
                clean_fatal_error
622
 
                    (_("Not enough disk space -- cannot write files"));
623
 
        }
624
 
    }
625
 
    if (slope != NULL) {
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>"),
629
 
                    Tmp_file_dx);
630
 
            clean_fatal_error(msg);
631
 
        }
632
 
        for (i = 0; i < nsizr; i++) {
633
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_dx)))
634
 
                clean_fatal_error
635
 
                    (_("Not enough disk space -- cannot write files"));
636
 
        }
637
 
    }
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>"),
642
 
                    Tmp_file_dy);
643
 
            clean_fatal_error(msg);
644
 
        }
645
 
        for (i = 0; i < nsizr; i++) {
646
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_dy)))
647
 
                clean_fatal_error
648
 
                    (_("Not enough disk space -- cannot write files"));
649
 
        }
650
 
    }
651
 
 
652
 
    if (pcurv != NULL) {
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>"),
656
 
                    Tmp_file_xx);
657
 
            clean_fatal_error(msg);
658
 
        }
659
 
        for (i = 0; i < nsizr; i++) {
660
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_xx)))
661
 
                clean_fatal_error
662
 
                    (_("Not enough disk space -- cannot write files"));
663
 
        }
664
 
    }
665
 
    if (tcurv != NULL) {
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>"),
669
 
                    Tmp_file_yy);
670
 
            clean_fatal_error(msg);
671
 
        }
672
 
        for (i = 0; i < nsizr; i++) {
673
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_yy)))
674
 
                clean_fatal_error
675
 
                    (_("Not enough disk space -- cannot write files"));
676
 
        }
677
 
    }
678
 
    if (mcurv != NULL) {
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>"),
682
 
                    Tmp_file_xy);
683
 
            clean_fatal_error(msg);
684
 
        }
685
 
        for (i = 0; i < nsizr; i++) {
686
 
            if (!(fwrite(zero_array_cell, sizeof(FCELL), nsizc, Tmp_fd_xy)))
687
 
                clean_fatal_error
688
 
                    (_("Not enough disk space -- cannot write files"));
689
 
        }
690
 
    }
691
 
 
692
 
    return;
693
 
}
694
 
 
695
 
void clean_fatal_error(char *str)
696
 
{
697
 
    if (Tmp_fd_z) {
698
 
        fclose(Tmp_fd_z);
699
 
        unlink(Tmp_file_z);
700
 
    }
701
 
    if (Tmp_fd_dx) {
702
 
        fclose(Tmp_fd_dx);
703
 
        unlink(Tmp_file_dx);
704
 
    }
705
 
    if (Tmp_fd_dy) {
706
 
        fclose(Tmp_fd_dy);
707
 
        unlink(Tmp_file_dy);
708
 
    }
709
 
    if (Tmp_fd_xx) {
710
 
        fclose(Tmp_fd_xx);
711
 
        unlink(Tmp_file_xx);
712
 
    }
713
 
    if (Tmp_fd_yy) {
714
 
        fclose(Tmp_fd_yy);
715
 
        unlink(Tmp_file_yy);
716
 
    }
717
 
    if (Tmp_fd_xy) {
718
 
        fclose(Tmp_fd_xy);
719
 
        unlink(Tmp_file_xy);
720
 
    }
721
 
    G_fatal_error(str);
 
536
    if (!name)
 
537
        return NULL;
 
538
 
 
539
    *tmpname = tmp = G_tempfile();
 
540
    fp = fopen(tmp, "w+");
 
541
    if (!fp)
 
542
        G_fatal_error(_("Unable to open temporary file <%s>"), *tmpname);
 
543
 
 
544
    for (i = 0; i < nsizr; i++) {
 
545
        if (fwrite(zero_array_cell, sizeof(FCELL), nsizc, fp) != nsizc) {
 
546
            clean();
 
547
            G_fatal_error(_("Error writing temporary file <%s>"), *tmpname);
 
548
        }
 
549
    }
 
550
 
 
551
    return fp;
 
552
}
 
553
 
 
554
static void create_temp_files(void)
 
555
{
 
556
    zero_array_cell = (FCELL *) G_calloc(nsizc, sizeof(FCELL));
 
557
 
 
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);
 
564
}
 
565
 
 
566
static void clean(void)
 
567
{
 
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);
 
574
 
 
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);
722
581
}