/* Saves map file from 2d array. NULL must be 0. Also meanwhile calculates area and volume. */ void save_map(FCELL ** out, int out_fd, int rows, int cols, int flag, FCELL * min_depth, FCELL * max_depth, double *area, double *volume) { int row, col; double cellsize = -1; G_debug(1, "Saving new map"); if (G_begin_cell_area_calculations() == 0 || G_begin_cell_area_calculations() == 1) { /* All cells have constant size... */ cellsize = G_area_of_cell_at_row(0); } G_debug(1, "Cell area: %f", cellsize); for (row = 0; row < rows; row++) { if (cellsize == -1) /* Get LatLon current rows cell size */ cellsize = G_area_of_cell_at_row(row); for (col = 0; col < cols; col++) { if (flag == 1) /* Create negative map */ out[row][col] = 0 - out[row][col]; if (out[row][col] == 0) { G_set_f_null_value(&out[row][col], 1); } if (out[row][col] > 0 || out[row][col] < 0) { G_debug(5, "volume %f += cellsize %f * value %f [%d,%d]", *volume, cellsize, out[row][col], row, col); *area += cellsize; *volume += cellsize * out[row][col]; } /* Get min/max depth. Can be usefull ;) */ if (out[row][col] > *max_depth) *max_depth = out[row][col]; if (out[row][col] < *min_depth) *min_depth = out[row][col]; } if (G_put_f_raster_row(out_fd, out[row]) == -1) G_fatal_error(_("Failed writing output raster map row %d"), row); G_percent(row + 1, rows, 5); } }
int cell_stats(int fd[], int with_percents, int with_counts, int with_areas, int do_sort, int with_labels, char *fmt) { CELL **cell; int i; int row; double unit_area; int planimetric = 0; int compute_areas; double G_area_of_cell_at_row(); /* allocate i/o buffers for each raster map */ cell = (CELL **) G_calloc(nfiles, sizeof(CELL *)); for (i = 0; i < nfiles; i++) cell[i] = Rast_allocate_c_buf(); /* if we want area totals, set this up. * distinguish projections which are planimetric (all cells same size) * from those which are not (e.g., lat-long) */ unit_area = 0.0; if (with_areas) { switch (G_begin_cell_area_calculations()) { case 0: /* areas don't make sense, but ignore this for now */ case 1: planimetric = 1; unit_area = G_area_of_cell_at_row(0); break; default: planimetric = 0; break; } } compute_areas = with_areas && !planimetric; /* here we go */ initialize_cell_stats(nfiles); for (row = 0; row < nrows; row++) { if (compute_areas) unit_area = G_area_of_cell_at_row(row); G_percent(row, nrows, 2); for (i = 0; i < nfiles; i++) { Rast_get_c_row(fd[i], cell[i], row); /* include max FP value in nsteps'th bin */ if(is_fp[i]) fix_max_fp_val(cell[i], ncols); /* we can't compute hash on null values, so we change all nulls to max+1, set NULL_CELL to max+1, and later compare with NULL_CELL to chack for nulls */ reset_null_vals(cell[i], ncols); } update_cell_stats(cell, ncols, unit_area); } G_percent(row, nrows, 2); sort_cell_stats(do_sort); print_cell_stats(fmt, with_percents, with_counts, with_areas, with_labels, fs); return 0; }
/*! * \brief Initiate a pde geometry data structure with a 2d region * * If the projection is not planimetric, a double array will be created based on the * number of rows of the provided region storing all computed areas for each row * * \param region sruct Cell_head * * \param geodata N_geom_data * - if a NULL pointer is given, a new structure will be allocatet and returned * * \return N_geom_data * * */ N_geom_data *N_init_geom_data_2d(struct Cell_head * region, N_geom_data * geodata) { N_geom_data *geom = geodata; struct Cell_head backup; double meters; short ll = 0; int i; /*create an openmp lock to assure that only one thread at a time will access this function */ #pragma omp critical { G_debug(2, "N_init_geom_data_2d: initializing the geometry structure"); /*make a backup from this region */ G_get_set_window(&backup); /*this function is not thread safe */ /*set the current region */ Rast_set_window(region); /*this function is not thread safe */ if (geom == NULL) geom = N_alloc_geom_data(); meters = G_database_units_to_meters_factor(); /*this function is not thread safe */ /*set the dim to 2d if it was not initiated with 3, that's a bit ugly :( */ if (geom->dim != 3) geom->dim = 2; geom->planimetric = 1; geom->rows = region->rows; geom->cols = region->cols; geom->dx = region->ew_res * meters; geom->dy = region->ns_res * meters; geom->Az = geom->dy * geom->dx; /*square meters in planimetric proj */ /*depths and dz are initialized with a 3d region */ /*Begin the area calculation */ ll = G_begin_cell_area_calculations(); /*this function is not thread safe */ /*if the projection is not planimetric, calc the area for each row */ if (ll == 2) { G_debug(2, "N_init_geom_data_2d: calculating the areas for non parametric projection"); geom->planimetric = 0; if (geom->area != NULL) G_free(geom->area); else geom->area = G_calloc(geom->rows, sizeof(double)); /*fill the area vector */ for (i = 0; i < geom->rows; i++) { geom->area[i] = G_area_of_cell_at_row(i); /*square meters */ } } /*restore the old region */ Rast_set_window(&backup); /*this function is not thread safe */ } return geom; }