20#include <grass/N_pde.h>
21#include <grass/raster.h>
22#include <grass/glocale.h>
51 G_fatal_error(
"N_copy_array_2d: the arrays are not of equal size");
54 G_fatal_error(
"N_copy_array_2d: the arrays are not of equal size");
56 G_debug(3,
"N_copy_array_2d: copy source array to target array size %i",
63 if (source->
type == CELL_TYPE) {
64 if (Rast_is_c_null_value((
void *)&source->
cell_array[i]))
67 if (target->
type == CELL_TYPE) {
70 if (target->
type == FCELL_TYPE) {
72 Rast_set_f_null_value((
void *)&(target->
fcell_array[i]), 1);
76 if (target->
type == DCELL_TYPE) {
78 Rast_set_d_null_value((
void *)&(target->
dcell_array[i]), 1);
83 if (source->
type == FCELL_TYPE) {
84 if (Rast_is_f_null_value((
void *)&source->
fcell_array[i]))
87 if (target->
type == CELL_TYPE) {
89 Rast_set_c_null_value((
void *)&(target->
cell_array[i]), 1);
93 if (target->
type == FCELL_TYPE) {
96 if (target->
type == DCELL_TYPE) {
98 Rast_set_d_null_value((
void *)&(target->
dcell_array[i]), 1);
103 if (source->
type == DCELL_TYPE) {
104 if (Rast_is_d_null_value((
void *)&source->
dcell_array[i]))
107 if (target->
type == CELL_TYPE) {
109 Rast_set_c_null_value((
void *)&(target->
cell_array[i]), 1);
113 if (target->
type == FCELL_TYPE) {
115 Rast_set_f_null_value((
void *)&(target->
fcell_array[i]), 1);
119 if (target->
type == DCELL_TYPE) {
145 double norm = 0.0, tmp = 0.0;
146 double v1 = 0.0, v2 = 0.0;
148 if (a->cols_intern !=
b->cols_intern)
149 G_fatal_error(
"N_norm_array_2d: the arrays are not of equal size");
151 if (a->rows_intern !=
b->rows_intern)
152 G_fatal_error(
"N_norm_array_2d: the arrays are not of equal size");
154 G_debug(3,
"N_norm_array_2d: norm of a and b size %i",
155 a->cols_intern * a->rows_intern);
157 for (i = 0; i < a->cols_intern * a->rows_intern; i++) {
161 if (a->type == CELL_TYPE) {
162 if (!Rast_is_f_null_value((
void *)&(a->cell_array[i])))
163 v1 = (double)a->cell_array[i];
165 if (a->type == FCELL_TYPE) {
166 if (!Rast_is_f_null_value((
void *)&(a->fcell_array[i])))
167 v1 = (double)a->fcell_array[i];
169 if (a->type == DCELL_TYPE) {
170 if (!Rast_is_f_null_value((
void *)&(a->dcell_array[i])))
171 v1 = (double)a->dcell_array[i];
173 if (
b->type == CELL_TYPE) {
174 if (!Rast_is_f_null_value((
void *)&(
b->cell_array[i])))
175 v2 = (double)
b->cell_array[i];
177 if (
b->type == FCELL_TYPE) {
178 if (!Rast_is_f_null_value((
void *)&(
b->fcell_array[i])))
179 v2 = (double)
b->fcell_array[i];
181 if (
b->type == DCELL_TYPE) {
182 if (!Rast_is_f_null_value((
void *)&(
b->dcell_array[i])))
183 v2 = (double)
b->dcell_array[i];
192 norm += fabs(v2 - v1);
214 int *nonull,
int withoffset)
222 if (withoffset == 1) {
227 for (j = 0 - a->offset; j < a->rows + a->offset; j++) {
228 for (i = 0 - a->offset; i < a->cols + a->offset; i++) {
246 for (j = 0; j < a->rows; j++) {
247 for (i = 0; i < a->cols; i++) {
262 "N_calc_array_2d_stats: compute array stats, min %g, max %g, sum "
264 *
min, *
max, *sum, *nonull);
303 int i, j, setnull = 0;
304 double va = 0.0, vb = 0.0, vc = 0.0;
312 if (a->cols_intern !=
b->cols_intern)
313 G_fatal_error(
"N_math_array_2d: the arrays are not of equal size");
314 if (a->rows_intern !=
b->rows_intern)
315 G_fatal_error(
"N_math_array_2d: the arrays are not of equal size");
316 if (a->offset !=
b->offset)
317 G_fatal_error(
"N_math_array_2d: the arrays have different offsets");
319 G_debug(3,
"N_math_array_2d: mathematical calculations, size: %i",
320 a->cols_intern * a->rows_intern);
325 if (a->type == DCELL_TYPE ||
b->type == DCELL_TYPE) {
327 G_debug(3,
"N_math_array_2d: array of type DCELL_TYPE created");
329 else if (a->type == FCELL_TYPE ||
b->type == FCELL_TYPE) {
331 G_debug(3,
"N_math_array_2d: array of type FCELL_TYPE created");
335 G_debug(3,
"N_math_array_2d: array of type CELL_TYPE created");
340 if (a->cols_intern != c->cols_intern)
342 "N_math_array_2d: the arrays are not of equal size");
343 if (a->rows_intern != c->rows_intern)
345 "N_math_array_2d: the arrays are not of equal size");
346 if (a->offset != c->offset)
348 "N_math_array_2d: the arrays have different offsets");
352#pragma omp for private(va, vb, vc, setnull)
353 for (j = 0 - a->offset; j < a->rows + a->offset; j++) {
354 for (i = 0 - a->offset; i < a->cols + a->offset; i++) {
381 if (c->type == CELL_TYPE) {
387 if (c->type == FCELL_TYPE) {
393 if (c->type == DCELL_TYPE) {
420 int i = 0,
count = 0;
422 G_debug(3,
"N_convert_array_2d_null_to_zero: convert array of size %i",
423 a->cols_intern * a->rows_intern);
425 if (a->type == CELL_TYPE)
426 for (i = 0; i < a->cols_intern * a->rows_intern; i++) {
427 if (Rast_is_c_null_value((
void *)&(a->cell_array[i]))) {
428 a->cell_array[i] = 0;
433 if (a->type == FCELL_TYPE)
434 for (i = 0; i < a->cols_intern * a->rows_intern; i++) {
435 if (Rast_is_f_null_value((
void *)&(a->fcell_array[i]))) {
436 a->fcell_array[i] = 0.0;
441 if (a->type == DCELL_TYPE)
442 for (i = 0; i < a->cols_intern * a->rows_intern; i++) {
443 if (Rast_is_d_null_value((
void *)&(a->dcell_array[i]))) {
444 a->dcell_array[i] = 0.0;
449 if (a->type == CELL_TYPE)
451 "N_convert_array_2d_null_to_zero: %i values of type CELL_TYPE "
454 if (a->type == FCELL_TYPE)
456 "N_convert_array_2d_null_to_zero: %i values of type "
457 "FCELL_TYPE are converted",
459 if (a->type == DCELL_TYPE)
461 "N_convert_array_2d_null_to_zero: %i values of type "
462 "DCELL_TYPE are converted",
491 G_fatal_error(
"N_copy_array_3d: the arrays are not of equal size");
494 G_fatal_error(
"N_copy_array_3d: the arrays are not of equal size");
497 G_fatal_error(
"N_copy_array_3d: the arrays are not of equal size");
499 G_debug(3,
"N_copy_array_3d: copy source array to target array size %i",
506 if (source->
type == FCELL_TYPE) {
507 if (Rast3d_is_null_value_num((
void *)&(source->
fcell_array[i]),
511 if (target->
type == FCELL_TYPE) {
514 if (target->
type == DCELL_TYPE) {
516 Rast3d_set_null_value((
void *)&(target->
dcell_array[i]), 1,
522 if (source->
type == DCELL_TYPE) {
523 if (Rast3d_is_null_value_num((
void *)&(source->
dcell_array[i]),
527 if (target->
type == FCELL_TYPE) {
529 Rast3d_set_null_value((
void *)&(target->
fcell_array[i]), 1,
534 if (target->
type == DCELL_TYPE) {
559 double norm = 0.0, tmp = 0.0;
560 double v1 = 0.0, v2 = 0.0;
562 if (a->cols_intern !=
b->cols_intern)
563 G_fatal_error(
"N_norm_array_3d: the arrays are not of equal size");
565 if (a->rows_intern !=
b->rows_intern)
566 G_fatal_error(
"N_norm_array_3d: the arrays are not of equal size");
568 if (a->depths_intern !=
b->depths_intern)
569 G_fatal_error(
"N_norm_array_3d: the arrays are not of equal size");
571 G_debug(3,
"N_norm_array_3d: norm of a and b size %i",
572 a->cols_intern * a->rows_intern * a->depths_intern);
574 for (i = 0; i < a->cols_intern * a->rows_intern * a->depths_intern; i++) {
578 if (a->type == FCELL_TYPE) {
579 if (!Rast3d_is_null_value_num((
void *)&(a->fcell_array[i]),
581 v1 = (double)a->fcell_array[i];
583 if (a->type == DCELL_TYPE) {
584 if (!Rast3d_is_null_value_num((
void *)&(a->dcell_array[i]),
586 v1 = (double)a->dcell_array[i];
588 if (
b->type == FCELL_TYPE) {
589 if (!Rast3d_is_null_value_num((
void *)&(
b->fcell_array[i]),
591 v2 = (double)
b->fcell_array[i];
593 if (
b->type == DCELL_TYPE) {
594 if (!Rast3d_is_null_value_num((
void *)&(
b->dcell_array[i]),
596 v2 = (double)
b->dcell_array[i];
605 norm += fabs(v2 - v1);
628 int *nonull,
int withoffset)
636 if (withoffset == 1) {
643 for (k = 0 - a->offset; k < a->depths + a->offset; k++) {
644 for (j = 0 - a->offset; j < a->rows + a->offset; j++) {
645 for (i = 0 - a->offset; i < a->cols + a->offset; i++) {
664 for (k = 0; k < a->depths; k++) {
665 for (j = 0; j < a->rows; j++) {
666 for (i = 0; i < a->cols; i++) {
682 "N_calc_array_3d_stats: compute array stats, min %g, max %g, sum "
684 *
min, *
max, *sum, *nonull);
727 int i, j, k, setnull = 0;
728 double va = 0.0, vb = 0.0, vc = 0.0;
734 if (a->cols_intern !=
b->cols_intern)
735 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
736 if (a->rows_intern !=
b->rows_intern)
737 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
738 if (a->depths_intern !=
b->depths_intern)
739 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
740 if (a->offset !=
b->offset)
741 G_fatal_error(
"N_math_array_3d: the arrays have different offsets");
743 G_debug(3,
"N_math_array_3d: mathematical calculations, size: %i",
744 a->cols_intern * a->rows_intern * a->depths_intern);
749 if (a->type == DCELL_TYPE ||
b->type == DCELL_TYPE) {
752 G_debug(3,
"N_math_array_3d: array of type DCELL_TYPE created");
757 G_debug(3,
"N_math_array_3d: array of type FCELL_TYPE created");
762 if (a->cols_intern != c->cols_intern)
763 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
764 if (a->rows_intern != c->rows_intern)
765 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
766 if (a->depths_intern != c->depths_intern)
767 G_fatal_error(
"N_math_array_3d: the arrays are not of equal size");
768 if (a->offset != c->offset)
769 G_fatal_error(
"N_math_array_3d: the arrays have different offsets");
772 for (k = 0 - a->offset; k < a->depths + a->offset; k++) {
773 for (j = 0 - a->offset; j < a->rows + a->offset; j++) {
774 for (i = 0 - a->offset; i < a->cols + a->offset; i++) {
801 if (c->type == FCELL_TYPE) {
807 if (c->type == DCELL_TYPE) {
834 int i = 0,
count = 0;
836 G_debug(3,
"N_convert_array_3d_null_to_zero: convert array of size %i",
837 a->cols_intern * a->rows_intern * a->depths_intern);
839 if (a->type == FCELL_TYPE)
840 for (i = 0; i < a->cols_intern * a->rows_intern * a->depths_intern;
842 if (Rast3d_is_null_value_num((
void *)&(a->fcell_array[i]),
844 a->fcell_array[i] = 0.0;
849 if (a->type == DCELL_TYPE)
850 for (i = 0; i < a->cols_intern * a->rows_intern * a->depths_intern;
852 if (Rast3d_is_null_value_num((
void *)&(a->dcell_array[i]),
854 a->dcell_array[i] = 0.0;
859 if (a->type == FCELL_TYPE)
861 "N_convert_array_3d_null_to_zero: %i values of type FCELL_TYPE "
865 if (a->type == DCELL_TYPE)
867 "N_convert_array_3d_null_to_zero: %i values of type DCELL_TYPE "
int G_debug(int level, const char *msg,...)
Print debugging message.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void N_put_array_3d_value_null(N_array_3d *data, int col, int row, int depth)
This function writes a null value to the N_array_3d data at position col, row, depth.
N_array_3d * N_alloc_array_3d(int cols, int rows, int depths, int offset, int type)
Allocate memory for a N_array_3d data structure.
void N_put_array_2d_f_value(N_array_2d *data, int col, int row, FCELL value)
Writes a FCELL value to the N_array_2d struct at position col, row.
void N_put_array_3d_f_value(N_array_3d *data, int col, int row, int depth, float value)
This function writes a float value to the N_array_3d data at position col, row, depth.
DCELL N_get_array_2d_d_value(N_array_2d *data, int col, int row)
Returns the value of type DCELL at position col, row.
int N_is_array_3d_value_null(N_array_3d *data, int col, int row, int depth)
This function returns 1 if value of N_array_3d data at position col, row, depth is of type null,...
int N_is_array_2d_value_null(N_array_2d *data, int col, int row)
Returns 1 if the value of N_array_2d struct at position col, row is of type null, otherwise 0.
void N_put_array_3d_d_value(N_array_3d *data, int col, int row, int depth, double value)
Writes a double value to the N_array_3d struct at position col, row, depth.
N_array_2d * N_alloc_array_2d(int cols, int rows, int offset, int type)
Allocate memory for a N_array_2d data structure.
double N_get_array_3d_d_value(N_array_3d *data, int col, int row, int depth)
This function returns the value of type float at position col, row, depth.
void N_put_array_2d_value_null(N_array_2d *data, int col, int row)
Writes the null value to the N_array_2d struct at position col, row.
void N_put_array_2d_c_value(N_array_2d *data, int col, int row, CELL value)
Writes a CELL value to the N_array_2d struct at position col, row.
void N_put_array_2d_d_value(N_array_2d *data, int col, int row, DCELL value)
Writes a DCELL value to the N_array_2d struct at position col, row.
int N_convert_array_3d_null_to_zero(N_array_3d *a)
Convert all null values to zero values.
int N_convert_array_2d_null_to_zero(N_array_2d *a)
Convert all null values to zero values.
double N_norm_array_3d(N_array_3d *a, N_array_3d *b, int type)
Calculate the norm of the two input arrays.
void N_calc_array_3d_stats(N_array_3d *a, double *min, double *max, double *sum, int *nonull, int withoffset)
Calculate basic statistics of the N_array_3d struct.
void N_calc_array_2d_stats(N_array_2d *a, double *min, double *max, double *sum, int *nonull, int withoffset)
Calculate basic statistics of the N_array_2d struct.
void N_copy_array_3d(N_array_3d *source, N_array_3d *target)
Copy the source N_array_3d struct to the target N_array_3d struct.
void N_copy_array_2d(N_array_2d *source, N_array_2d *target)
Copy the source N_array_2d struct to the target N_array_2d struct.
double N_norm_array_2d(N_array_2d *a, N_array_2d *b, int type)
Calculate the norm of the two input arrays.
N_array_3d * N_math_array_3d(N_array_3d *a, N_array_3d *b, N_array_3d *result, int type)
Perform calculations with two input arrays, the result is written to a third array.
N_array_2d * N_math_array_2d(N_array_2d *a, N_array_2d *b, N_array_2d *result, int type)
Perform calculations with two input arrays, the result is written to a third array.