GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
n_gradient.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * MODULE: Grass PDE Numerical Library
4  * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
5  * soerengebbert <at> gmx <dot> de
6  *
7  * PURPOSE: gradient management functions
8  * part of the gpde library
9  *
10  * COPYRIGHT: (C) 2000 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 
18 #include <grass/N_pde.h>
19 
20 /*!
21  * \brief Allocate a N_gradient_2d structure
22  *
23  * \return N_gradient_2d *
24  *
25  * */
27 {
28  N_gradient_2d *grad;
29 
30  grad = (N_gradient_2d *)G_calloc(1, sizeof(N_gradient_2d));
31 
32  return grad;
33 }
34 
35 /*!
36  * \brief Free's a N_gradient_2d structure
37  *
38  * \return void
39  *
40  * */
42 {
43  G_free(grad);
44  grad = NULL;
45 
46  return;
47 }
48 
49 /*!
50  * \brief allocate and initialize a N_gradient_2d structure
51  *
52  * \param NC double - the gradient between northern and center cell
53  * \param SC double - the gradient between southern and center cell
54  * \param WC double - the gradient between western and center cell
55  * \param EC double - the gradient between eastern and center cell
56  * \return N_gradient_2d *
57  *
58  * */
59 N_gradient_2d *N_create_gradient_2d(double NC, double SC, double WC, double EC)
60 {
61  N_gradient_2d *grad;
62 
63  G_debug(5, "N_create_gradient_2d: create N_gradient_2d");
64 
65  grad = N_alloc_gradient_2d();
66 
67  grad->NC = NC;
68  grad->SC = SC;
69  grad->WC = WC;
70  grad->EC = EC;
71 
72  return grad;
73 }
74 
75 /*!
76  * \brief copy a N_gradient_2d structure
77  *
78  * \param source - the source N_gradient_2d struct
79  * \param target - the target N_gradient_2d struct
80  * \return int - 1 success, 0 failure while copying
81  *
82  * */
84 {
85  G_debug(5, "N_copy_gradient_2d: copy N_gradient_2d");
86 
87  if (!source || !target)
88  return 0;
89 
90  target->NC = source->NC;
91  target->SC = source->SC;
92  target->WC = source->WC;
93  target->EC = source->EC;
94 
95  return 1;
96 }
97 
98 /*!
99  * \brief Return a N_gradient_2d structure calculated from the input gradient
100  * field at position [row][col]
101  *
102  * This function returns the gradient of a cell at position [row][col] from the
103  * input gradient field. Returned is a new structure of type N_gradient_2d.
104  *
105  * \param field N_gradient_field_2d * - A two dimensional gradient field
106  * \param gradient N_gradient_2d * - the gradient structure which should be
107  * filled with data, if a NULL pointer is given, a new structure will be created
108  * \param col int
109  * \param row int
110  * \return N_gradient_2d * - the new or filled gradient structure
111  *
112  *
113  * */
115  N_gradient_2d *gradient, int col, int row)
116 {
117  double NC = 0, SC = 0, WC = 0, EC = 0;
118  N_gradient_2d *grad = gradient;
119 
120  NC = N_get_array_2d_d_value(field->y_array, col, row);
121  SC = N_get_array_2d_d_value(field->y_array, col, row + 1);
122  WC = N_get_array_2d_d_value(field->x_array, col, row);
123  EC = N_get_array_2d_d_value(field->x_array, col + 1, row);
124 
125  G_debug(
126  5, "N_get_gradient_2d: calculate N_gradient_2d NC %g SC %g WC %g EC %g",
127  NC, SC, WC, EC);
128 
129  /*if gradient is a NULL pointer, create a new one */
130  if (!grad) {
131  grad = N_create_gradient_2d(NC, SC, WC, EC);
132  }
133  else {
134  grad->NC = NC;
135  grad->SC = SC;
136  grad->WC = WC;
137  grad->EC = EC;
138  }
139 
140  return grad;
141 }
142 
143 /*!
144  * \brief Allocate a N_gradient_3d structure
145  *
146  * \return N_gradient_3d *
147  *
148  * */
150 {
151  N_gradient_3d *grad;
152 
153  grad = (N_gradient_3d *)G_calloc(1, sizeof(N_gradient_3d));
154 
155  return grad;
156 }
157 
158 /*!
159  * \brief Free's a N_gradient_3d structure
160  *
161  * \return void
162  *
163  * */
165 {
166  G_free(grad);
167  grad = NULL;
168 
169  return;
170 }
171 
172 /*!
173  * \brief allocate and initialize a N_gradient_3d structure
174  *
175  * \param NC double - the gradient between northern and center cell
176  * \param SC double - the gradient between southern and center cell
177  * \param WC double - the gradient between western and center cell
178  * \param EC double - the gradient between eastern and center cell
179  * \param TC double - the gradient between top and center cell
180  * \param BC double - the gradient between bottom and center cell
181  * \return N_gradient_3d *
182  *
183  * */
184 N_gradient_3d *N_create_gradient_3d(double NC, double SC, double WC, double EC,
185  double TC, double BC)
186 {
187  N_gradient_3d *grad;
188 
189  G_debug(5, "N_create_gradient_3d: create N_gradient_3d");
190 
191  grad = N_alloc_gradient_3d();
192 
193  grad->NC = NC;
194  grad->SC = SC;
195  grad->WC = WC;
196  grad->EC = EC;
197  grad->TC = TC;
198  grad->BC = BC;
199 
200  return grad;
201 }
202 
203 /*!
204  * \brief copy a N_gradient_3d structure
205  *
206  * \param source - the source N_gradient_3d struct
207  * \param target - the target N_gradient_3d struct
208  * \return int - 1 success, 0 failure while copying
209  *
210  * */
212 {
213  G_debug(5, "N_copy_gradient_3d: copy N_gradient_3d");
214 
215  if (!source || !target)
216  return 0;
217 
218  target->NC = source->NC;
219  target->SC = source->SC;
220  target->WC = source->WC;
221  target->EC = source->EC;
222  target->TC = source->TC;
223  target->BC = source->BC;
224 
225  return 1;
226 }
227 
228 /*!
229  * \brief Return a N_gradient_3d structure calculated from the input gradient
230  * field at position [depth][row][col]
231  *
232  * This function returns the gradient of a 3d cell at position
233  * [depth][row][col] from the input gradient field. Returned is a new structure
234  * of type N_gradient_3d.
235  *
236  * \param field N_gradient_field_3d * - A three dimensional gradient field
237  * \param gradient N_gradient_3d * - an existing gradient structure or a NULL
238  * pointer, if a NULL pointer is providet a new structure will be returned
239  * \param col int
240  * \param row int
241  * \param depth int
242  * \return N_gradient_3d *
243  *
244  *
245  * */
247  N_gradient_3d *gradient, int col, int row,
248  int depth)
249 {
250  double NC, SC, WC, EC, TC, BC;
251  N_gradient_3d *grad = gradient;
252 
253  NC = N_get_array_3d_d_value(field->y_array, col, row, depth);
254  SC = N_get_array_3d_d_value(field->y_array, col, row + 1, depth);
255  WC = N_get_array_3d_d_value(field->x_array, col, row, depth);
256  EC = N_get_array_3d_d_value(field->x_array, col + 1, row, depth);
257  BC = N_get_array_3d_d_value(field->z_array, col, row, depth);
258  TC = N_get_array_3d_d_value(field->z_array, col, row, depth + 1);
259 
260  G_debug(6,
261  "N_get_gradient_3d: calculate N_gradient_3d NC %g SC %g WC %g EC "
262  "%g TC %g BC %g",
263  NC, SC, WC, EC, TC, BC);
264 
265  /*if gradient is a NULL pointer, create a new one */
266  if (!grad) {
267  grad = N_create_gradient_3d(NC, SC, WC, EC, TC, BC);
268  }
269  else {
270  grad->NC = NC;
271  grad->SC = SC;
272  grad->WC = WC;
273  grad->EC = EC;
274  grad->BC = BC;
275  grad->TC = TC;
276  }
277 
278  return grad;
279 }
280 
281 /*!
282  * \brief Allocate a N_gradient_neighbours_x structure
283  *
284  * This structure contains all neighbour gradients in x direction of one cell
285  *
286  * \return N_gradient_neighbours_x *
287  *
288  * */
290 {
292 
293  grad =
295 
296  return grad;
297 }
298 
299 /*!
300  * \brief Free's a N_gradient_neighbours_x structure
301  *
302  * \return void
303  *
304  * */
306 {
307  G_free(grad);
308  grad = NULL;
309 
310  return;
311 }
312 
313 /*!
314  * \brief Allocate and initialize a N_gradient_neighbours_x structure
315  *
316  * \param NWN double - the gradient between north-west and northern cell
317  * \param NEN double - the gradient between north-east and northern cell
318  * \param WC double - the gradient between western and center cell
319  * \param EC double - the gradient between eastern and center cell
320  * \param SWS double - the gradient between south-west and southern cell
321  * \param SES double - the gradient between south-east and southern cell
322  * \return N_gradient_neighbours_x *
323 
324  *
325  * */
327  double WC, double EC,
328  double SWS, double SES)
329 {
331 
332  G_debug(6,
333  "N_create_gradient_neighbours_x: create N_gradient_neighbours_x");
334 
336 
337  grad->NWN = NWN;
338  grad->NEN = NEN;
339  grad->WC = WC;
340  grad->EC = EC;
341  grad->SWS = SWS;
342  grad->SES = SES;
343 
344  return grad;
345 }
346 
347 /*!
348  * \brief copy a N_gradient_neighbours_x structure
349  *
350  * \param source - the source N_gradient_neighbours_x struct
351  * \param target - the target N_gradient_neighbours_x struct
352  * \return int - 1 success, 0 failure while copying
353  *
354  * */
356  N_gradient_neighbours_x *target)
357 {
358  G_debug(6, "N_copy_gradient_neighbours_x: copy N_gradient_neighbours_x");
359 
360  if (!source || !target)
361  return 0;
362 
363  target->NWN = source->NWN;
364  target->NEN = source->NEN;
365  target->WC = source->WC;
366  target->EC = source->EC;
367  target->SWS = source->SWS;
368  target->SES = source->SES;
369 
370  return 1;
371 }
372 
373 /*!
374  * \brief Allocate a N_gradient_neighbours_y structure
375  *
376  * This structure contains all neighbour gradients in y direction of one cell
377  *
378  * \return N_gradient_neighbours_y *
379  *
380  * */
382 {
384 
385  grad =
387 
388  return grad;
389 }
390 
391 /*!
392  * \brief Free's a N_gradient_neighbours_y structure
393  *
394  * \return void
395  *
396  * */
398 {
399  G_free(grad);
400  grad = NULL;
401 
402  return;
403 }
404 
405 /*!
406  * \brief Allocate and initialize a N_gradient_neighbours_y structure
407  *
408  * \param NWW double - the gradient between north-west and western cell
409  * \param NEE double - the gradient between north-east and eastern cell
410  * \param NC double - the gradient between northern and center cell
411  * \param SC double - the gradient between southern and center cell
412  * \param SWW double - the gradient between south-west and western cell
413  * \param SEE double - the gradient between south-east and eastern cell
414  * \return N_gradient_neighbours_y *
415 
416  *
417  * */
419  double NC, double SC,
420  double SWW, double SEE)
421 {
423 
424  G_debug(6,
425  "N_create_gradient_neighbours_y: create N_gradient_neighbours_y");
426 
428 
429  grad->NWW = NWW;
430  grad->NEE = NEE;
431  grad->NC = NC;
432  grad->SC = SC;
433  grad->SWW = SWW;
434  grad->SEE = SEE;
435 
436  return grad;
437 }
438 
439 /*!
440  * \brief copy a N_gradient_neighbours_y structure
441  *
442  * \param source - the source N_gradient_neighbours_y struct
443  * \param target - the target N_gradient_neighbours_y struct
444  * \return int - 1 success, 0 failure while copying
445  *
446  * */
448  N_gradient_neighbours_y *target)
449 {
450  G_debug(6, "N_copy_gradient_neighbours_y: copy N_gradient_neighbours_y");
451 
452  if (!source || !target)
453  return 0;
454 
455  target->NWW = source->NWW;
456  target->NEE = source->NEE;
457  target->NC = source->NC;
458  target->SC = source->SC;
459  target->SWW = source->SWW;
460  target->SEE = source->SEE;
461 
462  return 1;
463 }
464 
465 /*!
466  * \brief Allocate a N_gradient_neighbours_z structure
467  *
468  * This structure contains all neighbour gradients in z direction of one cell
469  *
470  * \return N_gradient_neighbours_z *
471  *
472  * */
474 {
476 
477  grad =
479 
480  return grad;
481 }
482 
483 /*!
484  * \brief Free's a N_gradient_neighbours_z structure
485  *
486  * \return void
487  *
488  * */
490 {
491  G_free(grad);
492  grad = NULL;
493 
494  return;
495 }
496 
497 /*!
498  * \brief Allocate and initialize a N_gradient_neighbours_z structure
499  *
500  * \param NWZ double - the gradient between upper and lower north-western cells
501  * \param NZ double - the gradient between upper and lower northern cells
502  * \param NEZ double - the gradient between upper and lower north-eastern cells
503  * \param WZ double - the gradient between upper and lower western cells
504  * \param CZ double - the gradient between upper and lower center cells
505  * \param EZ double - the gradient between upper and lower eastern cells
506  * \param SWZ double - the gradient between upper and lower south-western cells
507  * \param SZ double - the gradient between upper and lower southern cells
508  * \param SEZ double - the gradient between upper and lower south-eastern cells
509  * \return N_gradient_neighbours_z *
510 
511  *
512  * */
514  double NEZ, double WZ,
515  double CZ, double EZ,
516  double SWZ, double SZ,
517  double SEZ)
518 {
520 
521  G_debug(6,
522  "N_create_gradient_neighbours_z: create N_gradient_neighbours_z");
523 
525 
526  grad->NWZ = NWZ;
527  grad->NZ = NZ;
528  grad->NEZ = NEZ;
529  grad->WZ = WZ;
530  grad->CZ = CZ;
531  grad->EZ = EZ;
532  grad->SWZ = SWZ;
533  grad->SZ = SZ;
534  grad->SEZ = SEZ;
535 
536  return grad;
537 }
538 
539 /*!
540  * \brief copy a N_gradient_neighbours_z structure
541  *
542  * \param source - the source N_gradient_neighbours_z struct
543  * \param target - the target N_gradient_neighbours_z struct
544  * \return int - 1 success, 0 failure while copying
545  *
546  * */
548  N_gradient_neighbours_z *target)
549 {
550  G_debug(6, "N_copy_gradient_neighbours_z: copy N_gradient_neighbours_z");
551 
552  if (!source || !target)
553  return 0;
554 
555  target->NWZ = source->NWZ;
556  target->NZ = source->NZ;
557  target->NEZ = source->NEZ;
558  target->WZ = source->WZ;
559  target->CZ = source->CZ;
560  target->EZ = source->EZ;
561  target->SWZ = source->SWZ;
562  target->SZ = source->SZ;
563  target->SEZ = source->SEZ;
564 
565  return 1;
566 }
567 
568 /*!
569  * \brief Allocate a N_gradient_neighbours_2d structure
570  *
571  * This structure contains all neighbour gradients in all directions of one cell
572  * in a 2d raster layer
573  *
574  * \return N_gradient_neighbours_2d *
575  *
576  * */
578 {
580 
582  1, sizeof(N_gradient_neighbours_2d));
583 
586 
587  return grad;
588 }
589 
590 /*!
591  * \brief Free's a N_gradient_neighbours_2d structure
592  *
593  * \return void
594  *
595  * */
597 {
598 
601 
602  G_free(grad);
603  grad = NULL;
604 
605  return;
606 }
607 
608 /*!
609  * \brief Allocate and initialize a N_gradient_neighbours_2d structure
610  *
611  * The parameter N_gradient_neighbours x and y are copied into the new allocated
612  * structure and can be deleted after the initializing
613  *
614  * \return N_gradient_neighbours_2d * -- if failure NULL is returned
615  *
616  * */
620 {
622  int fail = 0;
623 
624  G_debug(5,
625  "N_create_gradient_neighbours_2d: create N_gradient_neighbours_2d");
626 
628 
629  if (!N_copy_gradient_neighbours_x(x, grad->x))
630  fail++;
631  if (!N_copy_gradient_neighbours_y(y, grad->y))
632  fail++;
633 
634  if (fail > 0) {
636  grad = NULL;
637  }
638 
639  return grad;
640 }
641 
642 /*!
643  * \brief copy a N_gradient_neighbours_2d structure
644  *
645  * \param source - the source N_gradient_neighbours_2d struct
646  * \param target - the target N_gradient_neighbours_2d struct
647  * \return int - 1 success, 0 failure while copying
648  *
649  * */
651  N_gradient_neighbours_2d *target)
652 {
653  int fail = 0;
654 
655  G_debug(5, "N_copy_gradient_neighbours_2d: copy N_gradient_neighbours_2d");
656 
657  if (!source || !target)
658  return 0;
659 
660  if (!(N_copy_gradient_neighbours_x(source->x, target->x)))
661  fail++;
662  if (!(N_copy_gradient_neighbours_y(source->y, target->y)))
663  fail++;
664 
665  if (fail > 0) {
666  return 0;
667  }
668 
669  return 1;
670 }
671 
672 /*!
673  * \brief Return a N_gradient_neighbours_2d structure calculated from the input
674  * gradient field at position [row][col]
675  *
676  * This function returns the gradient neighbours in x and y dierection
677  * of a cell at position [row][col] from the input gradient field.
678  * Returned is a pointer to a structure of type N_gradient_neighbours_2d.
679  *
680  * \param field N_gradient_field_2d * - A two dimensional gradient field
681  * \param gradient N_gradient_neighbours_2d * - the gradient structure which
682  * should be filled with data, if a NULL pointer is given, a new structure will
683  * be created \param col int \param row int \return N_gradient_neighbours_2d * -
684  * the new or filled gradient structure
685  *
686  *
687  * */
690  N_gradient_neighbours_2d *gradient, int col,
691  int row)
692 {
693  double NWN, NEN, WC, EC, SWS, SES;
694  double NWW, NEE, NC, SC, SWW, SEE;
696  N_gradient_neighbours_x *grad_x = NULL;
697  N_gradient_neighbours_y *grad_y = NULL;
698 
699  NWN = N_get_array_2d_d_value(field->x_array, col, row - 1);
700  NEN = N_get_array_2d_d_value(field->x_array, col + 1, row - 1);
701  WC = N_get_array_2d_d_value(field->x_array, col, row);
702  EC = N_get_array_2d_d_value(field->x_array, col + 1, row);
703  SWS = N_get_array_2d_d_value(field->x_array, col, row + 1);
704  SES = N_get_array_2d_d_value(field->x_array, col + 1, row + 1);
705 
706  NWW = N_get_array_2d_d_value(field->y_array, col - 1, row);
707  NEE = N_get_array_2d_d_value(field->y_array, col + 1, row);
708  NC = N_get_array_2d_d_value(field->y_array, col, row);
709  SC = N_get_array_2d_d_value(field->y_array, col, row + 1);
710  SWW = N_get_array_2d_d_value(field->y_array, col - 1, row + 1);
711  SEE = N_get_array_2d_d_value(field->y_array, col + 1, row + 1);
712 
713  grad_x = N_create_gradient_neighbours_x(NWN, NEN, WC, EC, SWS, SES);
714  grad_y = N_create_gradient_neighbours_y(NWW, NEE, NC, SC, SWW, SEE);
715 
716  G_debug(5,
717  "N_get_gradient_neighbours_2d: calculate N_gradient_neighbours_x "
718  "NWN %g NEN %g WC %g EC %g SWS %g SES %g",
719  NWN, NEN, WC, EC, SWS, SES);
720 
721  G_debug(5,
722  "N_get_gradient_neighbours_2d: calculate N_gradient_neighbours_y "
723  "NWW %g NEE %g NC %g SC %g SWW %g SEE %g",
724  NWW, NEE, NC, SC, SWW, SEE);
725 
726  /*if gradient is a NULL pointer, create a new one */
727  if (!gradient) {
728  grad = N_create_gradient_neighbours_2d(grad_x, grad_y);
729  gradient = grad;
730  }
731  else {
732  grad = N_create_gradient_neighbours_2d(grad_x, grad_y);
733  N_copy_gradient_neighbours_2d(grad, gradient);
735  }
736 
739 
740  return gradient;
741 }
742 
743 /*!
744  * \brief Allocate a N_gradient_neighbours_3d structure
745  *
746  * This structure contains all neighbour gradients in all directions of one cell
747  * in a 3d raster layer
748  *
749  * \return N_gradient_neighbours_3d *
750  *
751  * */
753 {
755 
757  1, sizeof(N_gradient_neighbours_3d));
758 
767 
768  return grad;
769 }
770 
771 /*!
772  * \brief Free's a N_gradient_neighbours_3d structure
773  *
774  * \return void
775  *
776  * */
778 {
779 
788 
789  G_free(grad);
790  grad = NULL;
791 
792  return;
793 }
794 
795 /*!
796  * \brief Allocate and initialize a N_gradient_neighbours_3d structure
797  *
798  * The parameter N_gradient_neighbours x(tcb) and y(tcb) and z(tb) are copied
799  into the new allocated structure
800  * and can be deleted after the initializing
801  *
802  * \return N_gradient_neighbours_3d * -- if failure NULL is returned
803 
804  *
805  * */
811 {
813  int fail = 0;
814 
815  G_debug(5,
816  "N_create_gradient_neighbours_3d: create N_gradient_neighbours_3d");
817 
819 
820  if (!(N_copy_gradient_neighbours_x(xt, grad->xt)))
821  fail++;
822  if (!(N_copy_gradient_neighbours_x(xc, grad->xc)))
823  fail++;
824  if (!(N_copy_gradient_neighbours_x(xb, grad->xb)))
825  fail++;
826  if (!(N_copy_gradient_neighbours_y(yt, grad->yt)))
827  fail++;
828  if (!(N_copy_gradient_neighbours_y(yc, grad->yc)))
829  fail++;
830  if (!(N_copy_gradient_neighbours_y(yb, grad->yb)))
831  fail++;
832  if (!(N_copy_gradient_neighbours_z(zt, grad->zt)))
833  fail++;
834  if (!(N_copy_gradient_neighbours_z(zb, grad->zb)))
835  fail++;
836 
837  if (fail > 0) {
838  return NULL;
839  }
840 
841  return grad;
842 }
843 
844 /*!
845  * \brief copy a N_gradient_neighbours_3d structure
846  *
847  * \param source - the source N_gradient_neighbours_3d struct
848  * \param target - the target N_gradient_neighbours_3d struct
849  * \return int - 1 success, 0 failure while copying
850  *
851  * */
853  N_gradient_neighbours_3d *target)
854 {
855  int fail = 0;
856 
857  G_debug(5, "N_copy_gradient_neighbours_3d: copy N_gradient_neighbours_3d");
858 
859  if (!source || !target)
860  return 0;
861 
862  if (!(N_copy_gradient_neighbours_x(source->xt, target->xt)))
863  fail++;
864  if (!(N_copy_gradient_neighbours_x(source->xc, target->xc)))
865  fail++;
866  if (!(N_copy_gradient_neighbours_x(source->xb, target->xb)))
867  fail++;
868  if (!(N_copy_gradient_neighbours_y(source->yt, target->yt)))
869  fail++;
870  if (!(N_copy_gradient_neighbours_y(source->yc, target->yc)))
871  fail++;
872  if (!(N_copy_gradient_neighbours_y(source->yb, target->yb)))
873  fail++;
874  if (!(N_copy_gradient_neighbours_z(source->zt, target->zt)))
875  fail++;
876  if (!(N_copy_gradient_neighbours_z(source->zb, target->zb)))
877  fail++;
878 
879  if (fail > 0) {
880  return 0;
881  }
882 
883  return 1;
884 }
885 
886 /*!
887  * \brief Allocate a N_gradient_field_2d
888  *
889  * The field arrays are of type DCELL.
890  *
891  * \param rows - number of rows of the 2d array from which the gradient should
892  * be calculated \param cols - number of cols of the 2d array from which the
893  * gradient should be calculated \return N_gradient_field_2d *
894  *
895  * */
897 {
898  N_gradient_field_2d *field;
899 
900  G_debug(5,
901  "N_alloc_gradient_field_2d: allocate a N_gradient_field_2d struct");
902 
903  field = (N_gradient_field_2d *)G_calloc(1, sizeof(N_gradient_field_2d));
904 
905  field->x_array = N_alloc_array_2d(cols, rows, 1, DCELL_TYPE);
906  field->y_array = N_alloc_array_2d(cols, rows, 1, DCELL_TYPE);
907 
908  field->cols = cols;
909  field->rows = rows;
910 
911  return field;
912 }
913 
914 /*!
915  * \brief Free's a N_gradient_neighbours_2d structure
916  *
917  * \return void
918  *
919  * */
921 {
922 
923  N_free_array_2d(field->x_array);
924  N_free_array_2d(field->y_array);
925 
926  G_free(field);
927 
928  field = NULL;
929 
930  return;
931 }
932 
933 /*!
934  * \brief Copy N_gradient_field_2d structure from source to target
935  *
936  * \param source - the source N_gradient_field_2d struct
937  * \param target - the target N_gradient_field_2d struct
938  * \return int - 1 success, 0 failure while copying
939  *
940  * */
942  N_gradient_field_2d *target)
943 {
944  G_debug(3, "N_copy_gradient_field_2d: copy N_gradient_field_2d");
945 
946  if (!source || !target)
947  return 0;
948 
949  N_copy_array_2d(source->x_array, target->x_array);
950  N_copy_array_2d(source->y_array, target->y_array);
951 
952  return 1;
953 }
954 
955 /*! \brief Print gradient field information to stdout
956  *
957  * \param field N_gradient_2d_field *
958  * \return void
959  *
960  * */
962 {
963  fprintf(stdout, "N_gradient_field_2d \n");
964  fprintf(stdout, "Cols %i\n", field->cols);
965  fprintf(stdout, "Rows: %i\n", field->rows);
966  fprintf(stdout, "X array pointer: %p\n", (void *)field->x_array);
967  fprintf(stdout, "Y array pointer: %p\n", (void *)field->y_array);
968  fprintf(stdout, "Min %g\n", field->min);
969  fprintf(stdout, "Max %g\n", field->max);
970  fprintf(stdout, "Sum %g\n", field->sum);
971  fprintf(stdout, "Mean %g\n", field->mean);
972  fprintf(stdout, "Nonull %i\n", field->nonull);
973  fprintf(stdout, "X array info \n");
975  fprintf(stdout, "Y array info \n");
977 
978  return;
979 }
980 
981 /*!
982  * \brief Allocate a N_gradient_field_3d
983  *
984  * The field arrays are always of type DCELL_TYPE.
985  *
986  * \param cols - number of cols of the 3d array from which the gradient should
987  * be calculated \param rows - number of rows of the 3d array from which the
988  * gradient should be calculated \param depths - number of depths of the 3d
989  * array from which the gradient should be calculated \return
990  * N_gradient_field_3d *
991  *
992  * */
993 N_gradient_field_3d *N_alloc_gradient_field_3d(int cols, int rows, int depths)
994 {
995  N_gradient_field_3d *field;
996 
997  G_debug(5,
998  "N_alloc_gradient_field_3d: allocate a N_gradient_field_3d struct");
999 
1000  field = (N_gradient_field_3d *)G_calloc(1, sizeof(N_gradient_field_3d));
1001 
1002  field->x_array = N_alloc_array_3d(cols, rows, depths, 1, DCELL_TYPE);
1003  field->y_array = N_alloc_array_3d(cols, rows, depths, 1, DCELL_TYPE);
1004  field->z_array = N_alloc_array_3d(cols, rows, depths, 1, DCELL_TYPE);
1005 
1006  field->cols = cols;
1007  field->rows = rows;
1008  field->depths = depths;
1009 
1010  return field;
1011 }
1012 
1013 /*!
1014  * \brief Free's a N_gradient_neighbours_3d structure
1015  *
1016  * \return void
1017  *
1018  * */
1020 {
1021 
1022  N_free_array_3d(field->x_array);
1023  N_free_array_3d(field->y_array);
1024  N_free_array_3d(field->z_array);
1025 
1026  G_free(field);
1027 
1028  field = NULL;
1029 
1030  return;
1031 }
1032 
1033 /*!
1034  * \brief Copy N_gradient_field_3d structure from source to target
1035  *
1036  * \param source - the source N_gradient_field_3d struct
1037  * \param target - the target N_gradient_field_3d struct
1038  * \return int - 1 success, 0 failure while copying
1039  *
1040  * */
1042  N_gradient_field_3d *target)
1043 {
1044  G_debug(3, "N_copy_gradient_field_3d: copy N_gradient_field_3d");
1045 
1046  if (!source || !target)
1047  return 0;
1048 
1049  N_copy_array_3d(source->x_array, target->x_array);
1050  N_copy_array_3d(source->y_array, target->y_array);
1051  N_copy_array_3d(source->z_array, target->z_array);
1052 
1053  return 1;
1054 }
1055 
1056 /*! \brief Print gradient field information to stdout
1057  *
1058  * \param field N_gradient_3d_field *
1059  * \return void
1060  *
1061  * */
1063 {
1064 
1065  fprintf(stdout, "N_gradient_field_3d \n");
1066  fprintf(stdout, "Cols %i\n", field->cols);
1067  fprintf(stdout, "Rows: %i\n", field->rows);
1068  fprintf(stdout, "Depths %i\n", field->depths);
1069  fprintf(stdout, "X array pointer: %p\n", (void *)field->x_array);
1070  fprintf(stdout, "Y array pointer: %p\n", (void *)field->y_array);
1071  fprintf(stdout, "Z array pointer: %p\n", (void *)field->z_array);
1072  fprintf(stdout, "Min %g\n", field->min);
1073  fprintf(stdout, "Max %g\n", field->max);
1074  fprintf(stdout, "Sum %g\n", field->sum);
1075  fprintf(stdout, "Mean %g\n", field->mean);
1076  fprintf(stdout, "Nonull %i\n", field->nonull);
1077  fprintf(stdout, "X array info \n");
1079  fprintf(stdout, "Y array info \n");
1081  fprintf(stdout, "Z array info \n");
1083 
1084  return;
1085 }
#define NULL
Definition: ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_calloc(m, n)
Definition: defs/gis.h:95
int G_debug(int, const char *,...) __attribute__((format(printf
void N_print_array_3d_info(N_array_3d *data)
Write the info of the array to stdout.
Definition: n_arrays.c:1170
void N_free_array_3d(N_array_3d *data)
Release the memory of a N_array_3d.
Definition: n_arrays.c:774
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.
Definition: n_arrays.c:380
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.
Definition: n_arrays.c:719
void N_print_array_2d_info(N_array_2d *data)
This function writes the data info of the array data to stdout.
Definition: n_arrays.c:603
N_array_2d * N_alloc_array_2d(int cols, int rows, int offset, int type)
Allocate memory for a N_array_2d data structure.
Definition: n_arrays.c:75
void N_free_array_2d(N_array_2d *data)
Release the memory of a N_array_2d structure.
Definition: n_arrays.c:132
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.
Definition: n_arrays.c:979
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.
Definition: n_arrays_calc.c:43
int N_copy_gradient_neighbours_2d(N_gradient_neighbours_2d *source, N_gradient_neighbours_2d *target)
copy a N_gradient_neighbours_2d structure
Definition: n_gradient.c:650
N_gradient_neighbours_y * N_alloc_gradient_neighbours_y(void)
Allocate a N_gradient_neighbours_y structure.
Definition: n_gradient.c:381
N_gradient_neighbours_3d * N_alloc_gradient_neighbours_3d(void)
Allocate a N_gradient_neighbours_3d structure.
Definition: n_gradient.c:752
int N_copy_gradient_3d(N_gradient_3d *source, N_gradient_3d *target)
copy a N_gradient_3d structure
Definition: n_gradient.c:211
N_gradient_2d * N_alloc_gradient_2d(void)
Allocate a N_gradient_2d structure.
Definition: n_gradient.c:26
N_gradient_3d * N_get_gradient_3d(N_gradient_field_3d *field, N_gradient_3d *gradient, int col, int row, int depth)
Return a N_gradient_3d structure calculated from the input gradient field at position [depth][row][co...
Definition: n_gradient.c:246
N_gradient_2d * N_create_gradient_2d(double NC, double SC, double WC, double EC)
allocate and initialize a N_gradient_2d structure
Definition: n_gradient.c:59
N_gradient_neighbours_2d * N_alloc_gradient_neighbours_2d(void)
Allocate a N_gradient_neighbours_2d structure.
Definition: n_gradient.c:577
void N_free_gradient_field_3d(N_gradient_field_3d *field)
Free's a N_gradient_neighbours_3d structure.
Definition: n_gradient.c:1019
N_gradient_neighbours_2d * N_create_gradient_neighbours_2d(N_gradient_neighbours_x *x, N_gradient_neighbours_y *y)
Allocate and initialize a N_gradient_neighbours_2d structure.
Definition: n_gradient.c:618
int N_copy_gradient_field_3d(N_gradient_field_3d *source, N_gradient_field_3d *target)
Copy N_gradient_field_3d structure from source to target.
Definition: n_gradient.c:1041
int N_copy_gradient_neighbours_x(N_gradient_neighbours_x *source, N_gradient_neighbours_x *target)
copy a N_gradient_neighbours_x structure
Definition: n_gradient.c:355
void N_free_gradient_neighbours_2d(N_gradient_neighbours_2d *grad)
Free's a N_gradient_neighbours_2d structure.
Definition: n_gradient.c:596
void N_print_gradient_field_2d_info(N_gradient_field_2d *field)
Print gradient field information to stdout.
Definition: n_gradient.c:961
N_gradient_neighbours_x * N_alloc_gradient_neighbours_x(void)
Allocate a N_gradient_neighbours_x structure.
Definition: n_gradient.c:289
N_gradient_neighbours_y * N_create_gradient_neighbours_y(double NWW, double NEE, double NC, double SC, double SWW, double SEE)
Allocate and initialize a N_gradient_neighbours_y structure.
Definition: n_gradient.c:418
void N_print_gradient_field_3d_info(N_gradient_field_3d *field)
Print gradient field information to stdout.
Definition: n_gradient.c:1062
void N_free_gradient_neighbours_z(N_gradient_neighbours_z *grad)
Free's a N_gradient_neighbours_z structure.
Definition: n_gradient.c:489
int N_copy_gradient_neighbours_z(N_gradient_neighbours_z *source, N_gradient_neighbours_z *target)
copy a N_gradient_neighbours_z structure
Definition: n_gradient.c:547
int N_copy_gradient_neighbours_3d(N_gradient_neighbours_3d *source, N_gradient_neighbours_3d *target)
copy a N_gradient_neighbours_3d structure
Definition: n_gradient.c:852
N_gradient_field_2d * N_alloc_gradient_field_2d(int cols, int rows)
Allocate a N_gradient_field_2d.
Definition: n_gradient.c:896
N_gradient_neighbours_2d * N_get_gradient_neighbours_2d(N_gradient_field_2d *field, N_gradient_neighbours_2d *gradient, int col, int row)
Return a N_gradient_neighbours_2d structure calculated from the input gradient field at position [row...
Definition: n_gradient.c:689
N_gradient_3d * N_create_gradient_3d(double NC, double SC, double WC, double EC, double TC, double BC)
allocate and initialize a N_gradient_3d structure
Definition: n_gradient.c:184
N_gradient_3d * N_alloc_gradient_3d(void)
Allocate a N_gradient_3d structure.
Definition: n_gradient.c:149
N_gradient_2d * N_get_gradient_2d(N_gradient_field_2d *field, N_gradient_2d *gradient, int col, int row)
Return a N_gradient_2d structure calculated from the input gradient field at position [row][col].
Definition: n_gradient.c:114
void N_free_gradient_2d(N_gradient_2d *grad)
Free's a N_gradient_2d structure.
Definition: n_gradient.c:41
int N_copy_gradient_neighbours_y(N_gradient_neighbours_y *source, N_gradient_neighbours_y *target)
copy a N_gradient_neighbours_y structure
Definition: n_gradient.c:447
N_gradient_neighbours_z * N_alloc_gradient_neighbours_z(void)
Allocate a N_gradient_neighbours_z structure.
Definition: n_gradient.c:473
int N_copy_gradient_2d(N_gradient_2d *source, N_gradient_2d *target)
copy a N_gradient_2d structure
Definition: n_gradient.c:83
void N_free_gradient_3d(N_gradient_3d *grad)
Free's a N_gradient_3d structure.
Definition: n_gradient.c:164
void N_free_gradient_neighbours_y(N_gradient_neighbours_y *grad)
Free's a N_gradient_neighbours_y structure.
Definition: n_gradient.c:397
N_gradient_neighbours_x * N_create_gradient_neighbours_x(double NWN, double NEN, double WC, double EC, double SWS, double SES)
Allocate and initialize a N_gradient_neighbours_x structure.
Definition: n_gradient.c:326
N_gradient_field_3d * N_alloc_gradient_field_3d(int cols, int rows, int depths)
Allocate a N_gradient_field_3d.
Definition: n_gradient.c:993
void N_free_gradient_field_2d(N_gradient_field_2d *field)
Free's a N_gradient_neighbours_2d structure.
Definition: n_gradient.c:920
N_gradient_neighbours_3d * N_create_gradient_neighbours_3d(N_gradient_neighbours_x *xt, N_gradient_neighbours_x *xc, N_gradient_neighbours_x *xb, N_gradient_neighbours_y *yt, N_gradient_neighbours_y *yc, N_gradient_neighbours_y *yb, N_gradient_neighbours_z *zt, N_gradient_neighbours_z *zb)
Allocate and initialize a N_gradient_neighbours_3d structure.
Definition: n_gradient.c:806
void N_free_gradient_neighbours_x(N_gradient_neighbours_x *grad)
Free's a N_gradient_neighbours_x structure.
Definition: n_gradient.c:305
N_gradient_neighbours_z * N_create_gradient_neighbours_z(double NWZ, double NZ, double NEZ, double WZ, double CZ, double EZ, double SWZ, double SZ, double SEZ)
Allocate and initialize a N_gradient_neighbours_z structure.
Definition: n_gradient.c:513
int N_copy_gradient_field_2d(N_gradient_field_2d *source, N_gradient_field_2d *target)
Copy N_gradient_field_2d structure from source to target.
Definition: n_gradient.c:941
void N_free_gradient_neighbours_3d(N_gradient_neighbours_3d *grad)
Free's a N_gradient_neighbours_3d structure.
Definition: n_gradient.c:777
#define DCELL_TYPE
Definition: raster.h:13
Gradient between the cells in X and Y direction.
Definition: N_pde.h:457
double EC
Definition: N_pde.h:459
double SC
Definition: N_pde.h:459
double WC
Definition: N_pde.h:459
double NC
Definition: N_pde.h:459
Gradient between the cells in X, Y and Z direction.
Definition: N_pde.h:464
double WC
Definition: N_pde.h:466
double NC
Definition: N_pde.h:466
double EC
Definition: N_pde.h:466
double SC
Definition: N_pde.h:466
double BC
Definition: N_pde.h:466
double TC
Definition: N_pde.h:466
N_array_2d * y_array
Definition: N_pde.h:562
N_array_2d * x_array
Definition: N_pde.h:561
N_array_3d * y_array
Definition: N_pde.h:573
N_array_3d * x_array
Definition: N_pde.h:572
N_array_3d * z_array
Definition: N_pde.h:574
Gradient between the cell neighbours in X and Y direction.
Definition: N_pde.h:535
N_gradient_neighbours_y * y
Definition: N_pde.h:538
N_gradient_neighbours_x * x
Definition: N_pde.h:537
Gradient between the cell neighbours in X, Y and Z direction.
Definition: N_pde.h:543
N_gradient_neighbours_y * yb
Definition: N_pde.h:551
N_gradient_neighbours_z * zb
Definition: N_pde.h:554
N_gradient_neighbours_y * yt
Definition: N_pde.h:549
N_gradient_neighbours_z * zt
Definition: N_pde.h:553
N_gradient_neighbours_y * yc
Definition: N_pde.h:550
N_gradient_neighbours_x * xb
Definition: N_pde.h:547
N_gradient_neighbours_x * xt
Definition: N_pde.h:545
N_gradient_neighbours_x * xc
Definition: N_pde.h:546
Gradient between the cell neighbours in X direction.
Definition: N_pde.h:514
Gradient between the cell neighbours in Y direction.
Definition: N_pde.h:521
Gradient between the cell neighbours in Z direction.
Definition: N_pde.h:528
#define x