GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
N_geom.c
Go to the documentation of this file.
1 
2 /*****************************************************************************
3 *
4 * MODULE: Grass PDE Numerical Library
5 * AUTHOR(S): Soeren Gebbert, Berlin (GER) Dec 2006
6 * soerengebbert <at> gmx <dot> de
7 *
8 * PURPOSE: part of the gpde library
9 * allocation, destroing and initializing the geometric struct
10 *
11 * COPYRIGHT: (C) 2000 by the GRASS Development Team
12 *
13 * This program is free software under the GNU General Public
14 * License (>=v2). Read the file COPYING that comes with GRASS
15 * for details.
16 *
17 *****************************************************************************/
18 
19 
20 #include "grass/N_pde.h"
21 
22 /* *************************************************************** *
23  * *********** Konstruktor *************************************** *
24  * *************************************************************** */
31 {
32  N_geom_data *geom = (N_geom_data *) G_calloc(1, sizeof(N_geom_data));
33 
34  geom->area = NULL;
35  geom->planimetric = 1;
36  geom->dim = 0;
37 
38  return geom;
39 }
40 
41 /* *************************************************************** *
42  * *********** Destruktor **************************************** *
43  * *************************************************************** */
51 {
52  if (geom->area != NULL)
53  G_free(geom->area);
54 
55  G_free(geom);
56  return;
57 }
58 
59 /* *************************************************************** *
60  * *************************************************************** *
61  * *************************************************************** */
73 N_geom_data *N_init_geom_data_3d(G3D_Region * region3d, N_geom_data * geodata)
74 {
75  N_geom_data *geom = geodata;
76 
77  struct Cell_head region2d;
78 
79 #pragma omp critical
80  {
81 
82  G_debug(2,
83  "N_init_geom_data_3d: initializing the geometry structure");
84 
85  if (geom == NULL)
86  geom = N_alloc_geom_data();
87 
88  geom->dz = region3d->tb_res * G_database_units_to_meters_factor(); /*this function is not thread safe */
89  geom->depths = region3d->depths;
90  geom->dim = 3;
91 
92  /*convert the 3d into a 2d region and begin the area calculation */
93  G_get_set_window(&region2d); /*this function is not thread safe */
94  G3d_regionToCellHead(region3d, &region2d);
95  }
96 
97  return N_init_geom_data_2d(&region2d, geom);
98 }
99 
100 
101 /* *************************************************************** *
102  * *************************************************************** *
103  * *************************************************************** */
115 N_geom_data *N_init_geom_data_2d(struct Cell_head * region,
116  N_geom_data * geodata)
117 {
118  N_geom_data *geom = geodata;
119 
120  struct Cell_head backup;
121 
122  double meters;
123 
124  short ll = 0;
125 
126  int i;
127 
128 
129  /*create an openmp lock to assure that only one thread at a time will access this function */
130 #pragma omp critical
131  {
132  G_debug(2,
133  "N_init_geom_data_2d: initializing the geometry structure");
134 
135  /*make a backup from this region */
136  G_get_set_window(&backup); /*this function is not thread safe */
137  /*set the current region */
138  G_set_window(region); /*this function is not thread safe */
139 
140  if (geom == NULL)
141  geom = N_alloc_geom_data();
142 
143  meters = G_database_units_to_meters_factor(); /*this function is not thread safe */
144 
145  /*set the dim to 2d if it was not initiated with 3, thats a bit ugly :( */
146  if (geom->dim != 3)
147  geom->dim = 2;
148 
149  geom->planimetric = 1;
150  geom->rows = region->rows;
151  geom->cols = region->cols;
152  geom->dx = region->ew_res * meters;
153  geom->dy = region->ns_res * meters;
154  geom->Az = geom->dy * geom->dx; /*square meters in planimetric proj */
155  /*depths and dz are initialized with a 3d region */
156 
157  /*Begin the area calculation */
158  ll = G_begin_cell_area_calculations(); /*this function is not thread safe */
159 
160  /*if the projection is not planimetric, calc the area for each row */
161  if (ll == 2) {
162  G_debug(2,
163  "N_init_geom_data_2d: calculating the areas for non parametric projection");
164  geom->planimetric = 0;
165 
166  if (geom->area != NULL) {
167  G_free(geom->area);
168  geom->area = G_calloc(geom->rows, sizeof(double));
169  } else {
170  geom->area = G_calloc(geom->rows, sizeof(double));
171  }
172 
173  /*fill the area vector */
174  for (i = 0; i < geom->rows; i++) {
175  geom->area[i] = G_area_of_cell_at_row(i); /*square meters */
176  }
177  }
178 
179  /*restore the old region */
180  G_set_window(&backup); /*this function is not thread safe */
181  }
182 
183  return geom;
184 }
185 
186 /* *************************************************************** *
187  * *************************************************************** *
188  * *************************************************************** */
200 {
201  if (geom->planimetric) {
202  G_debug(6, "N_get_geom_data_area_of_cell: %g", geom->Az);
203  return geom->Az;
204  }
205  else {
206  G_debug(6, "N_get_geom_data_area_of_cell: %g", geom->area[row]);
207  return geom->area[row];
208  }
209 
210  return 0.0;
211 }
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
int G_get_set_window(struct Cell_head *window)
Get the current working window.
Definition: set_window.c:30
int dim
Definition: N_pde.h:131
int G_set_window(struct Cell_head *window)
Establishes &#39;window&#39; as the current working window.
Definition: set_window.c:49
void G3d_regionToCellHead(G3D_Region *region3d, struct Cell_head *region2d)
Returns in region2d the 2d portion of region3d.
Definition: g3dregion.c:46
double G_area_of_cell_at_row(int row)
Cell area in specified row.
Definition: gis/area.c:91
int depths
Definition: N_pde.h:139
double dy
Definition: N_pde.h:134
N_geom_data * N_init_geom_data_2d(struct Cell_head *region, N_geom_data *geodata)
Initiate a pde geometry data structure with a 2d region.
Definition: N_geom.c:115
Geometric information about the structured grid.
Definition: N_pde.h:127
double dz
Definition: N_pde.h:135
int rows
Definition: N_pde.h:140
int G_begin_cell_area_calculations(void)
Begin cell area calculations.
Definition: gis/area.c:50
double Az
Definition: N_pde.h:137
int cols
Definition: N_pde.h:141
double N_get_geom_data_area_of_cell(N_geom_data *geom, int row)
Get the areay size in square meter of one cell (x*y) at row.
Definition: N_geom.c:199
int planimetric
Definition: N_pde.h:129
N_geom_data * N_alloc_geom_data(void)
Allocate the pde geometry data structure and return a pointer to the new allocated structure...
Definition: N_geom.c:30
return NULL
Definition: dbfopen.c:1394
void N_free_geom_data(N_geom_data *geom)
Release memory of a pde geometry data structure.
Definition: N_geom.c:50
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
double * area
Definition: N_pde.h:130
double dx
Definition: N_pde.h:133
double G_database_units_to_meters_factor(void)
conversion to meters
Definition: proj3.c:80
N_geom_data * N_init_geom_data_3d(G3D_Region *region3d, N_geom_data *geodata)
Initiate a pde geometry data structure with a 3d region.
Definition: N_geom.c:73