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