TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
b_readgrid.c
Go to the documentation of this file.
1 
25 #include <sys/stat.h>
26 #include "turtle.h"
27 #include "t_utilities.h"
28 #include "get_filenames.h"
29 #include "rw_maps.h"
30 #include "linear_span.h"
31 #include "geometry.h"
32 #include "geometry_utilities.h"
33 #include "read_command_line.h"
35 #include "geometry_io.h"
36 #include "geometry_attribute.h"
37 #include "geometry_freememory.h"
38 
39 
40 #include "g_raster2plvector.h"
41 #include "bigcells2.h"
42 #include "geometry2.h"
43 #include "keywords_file_b.h"
44 #include "b_solver.h"
45 #include "b_volumes.h"
46 #include "b_utilities.h"
47 #include "b_readgrid.h"
48 
49 #define NULL_ELEVATION -9999
50 #define INTEGER_NULL -99
51 #define POLYGON_SUFFIX "_polygons.txt"
52 #define LINE_SUFFIX "_lines.txt"
53 #define CONNECTION_SUFFIX "_connections.txt"
54 #define no_PRINT 0
55 
56 LINEVECTOR *read_linevector (char *filename, short print) {
73  FILE *fd;
74  DOUBLEMATRIX *ldata;
75  LINEVECTOR *lines;
76  POINT *P1,*P2;
77  long index,j,ja;
78 
79  int ix=1;
80  int iy=ix+1;
81  int iline_index=iy+1;
82  int ilength2d=iline_index+1;
83  int iP1_x=ilength2d+1;
84  int iP1_y=iP1_x+1;
85  int iP2_x=iP1_y+1;
86  int iP2_y=iP2_x+1;
87  int indata=iP2_y; /* number of data */
88 
89  fd=t_fopen(filename,"r");
90  index=(long)read_index(fd,no_PRINT);
91  ldata=read_doublematrix(fd,"a",no_PRINT);
92  if (ldata->nch!=indata) printf("Warning in read_linevector: inconstancy on number of columns (data) : %ld %d \n",ldata->nch,indata);
93  //if (ldata->nrh!=index) printf("Error in read_linevector: inconstancy on number of lines : %ld %ld",ldata->nrh,index);
94  lines=new_linevector(ldata->nrh);
95  for (j=lines->nl;j<=lines->nh;j++) {
96  ja=(long)ldata->element[j][iline_index];
97  if (j!=ja) printf("Error in read_linevector (line %ld of %ld): inconstancy on line index : %ld %ld \n",j,lines->nh,j,ja);
98  P1=new_point(j-1,ldata->element[j][iP1_x],ldata->element[j][iP1_y],NULL_ELEVATION);
99  P2=new_point(j,ldata->element[j][iP2_x],ldata->element[j][iP2_y],NULL_ELEVATION);
100  lines->element[j]=new_line_from_points(j,P1,P2);
101  free_point(P1);
102  free_point(P2);
103 
104  }
105 
106  free_doublematrix(ldata);
107  if (print==1) printf("Function read_linevector (number of lines %ld) was successfully executed!! \n",lines->nh);
108 
109  return lines;
110 
111 }
112 
113 POLYGON *read_polygon(FILE *fd,short print) {
123  int ix=1; /* x coordinate of the centroid */
124  int iy=ix+1; /* y coordinate of the centroid */
125  int ipolygon_index=iy+1; /* index of the polygon */
126  int iarea2d=ipolygon_index+1; /* area of the polygon */
127  int n_data=iarea2d;
128  long i;
129  DOUBLEVECTOR *v_data;
130  POLYGON *po;
131 
132  v_data=read_doublearray(fd,print);
133 
134  if (v_data->nh<=n_data) printf ("Error in read_polygon there no sufficient data !!\n");
135 
136  po=(POLYGON *)malloc(sizeof(POLYGON));
137  if (!po) t_error("Polygon in read_polygon struct was not allocated");
138 
139  po->area2D=v_data->element[iarea2d];
140  po->index=v_data->element[ipolygon_index];
141 
142  po->centroid=new_point(po->index,v_data->element[ix],v_data->element[iy],NULL_ELEVATION);
143 
144  po->edge_indices=new_longvector(v_data->nh-n_data);
145  for(i=po->edge_indices->nl;i<=po->edge_indices->nh;i++) {
146  po->edge_indices->element[i]=v_data->element[i+n_data];
147  }
148 
149  free_doublevector(v_data);
150 
151  return po;
152 }
153 
154 POLYGONVECTOR *read_polygonvector(char *filename,short print) {
165  POLYGONVECTOR *polygons;
166  FILE *fd;
167  long j,n_po;
168  fd=t_fopen(filename,"r");
169 
170  n_po=(long)read_index(fd,no_PRINT);
171  polygons=new_polygonvector(n_po);
172 
173  for (j=polygons->nl;j<=polygons->nh;j++) {
174  polygons->element[j]=read_polygon(fd,print);
175  if (polygons->element[j]->index!=j) printf ("Error in read_polygonvector (polygon %ld) inconstancy: %ld %ld \n",j,j,polygons->element[j]->index);
176  }
177 
178  t_fclose(fd);
179  if (print==1) printf("Function read_polygonvector (number of polygons %ld) was successfully executed!!",polygons->nh);
180 
181  return polygons;
182 
183 }
184 
197  DOUBLEVECTOR *v_data;
198  long j;
199 
200  v_data=read_doublearray(fd,no_PRINT);
201  int s=(v_data->nh-1)%2;
202  if (s!=0) printf("Error in read_connections (index %ld) odd number of elements in the vector after the first one which is the polygon index",v_data->nh);
203 
204  long l=(v_data->nh-1)/2;
205 
206 
208  if (!pca) printf("Error: polygon_connection_attributes was not allocated at %ld polygon",(long)v_data->element[1]);
209 
210  pca->connections=new_longvector(l);
212 
213  for (j=pca->connections->nl;j<=pca->connections->nh;j++) {
214  pca->connections->element[j]=(long)(v_data->element[j*2]);
215  pca->d_connections->element[j]=v_data->element[j*2+1];
216 
217  }
218 
219  free_doublevector(v_data);
220 
221  return pca;
222 
223 
224 }
225 
226 
239  FILE *fd;
240  long j,n_po;
241 
242  fd=t_fopen(filename,"r");
243  n_po=(long)read_index(fd,no_PRINT);
244  pca=new_connection_attributes(n_po);
245 
246  for (j=pca->nl;j<=pca->nh;j++) {
247  pca->element[j]=read_connections(fd,print);
248 
249  // if (pca->element[j]->index!=j) printf ("Error in read_connection_attributes (polygon %ld) inconstancy: %ld %ld \n",j,j,pca->element[j]->index);
250  }
251 
252  t_fclose(fd);
253  if (print==1) printf("Function read_connection_attributes (number of polygons %ld) was successfully executed!!",pca->nh);
254 
255  return pca;
256 
257 }
258 
259 
260 GRID *read_grid(char *keyname,short print) {
266  GRID *grid;
267  long j,c;
268 
269  grid=(GRID *)malloc(sizeof(GRID));
270  if (!grid) t_error("Grid in read_grid was not allocated");
271 
275 
276  grid->lines=read_linevector(grid->file_resume_lines,print);
279 
280  /* check the boudaries */
281 
282  if (grid->polygons->nh!=grid->links->nh) printf ("Error in read_grid inconstancy betenn numbers of polygons %ld and of connection attributes %ld \n",grid->lines->nh,grid->links->nh);
283 
284 
285  //j=link->nl;
286  //while ((cf==0) || (df==0)) {
287  for (j=grid->links->nl;j<=grid->links->nh;j++) {
288  if (grid->links->element[j]->connections->nh!=grid->polygons->element[j]->edge_indices->nh) printf ("Error in read_grid inconstancy between numbers of edge %ld and of connections %ld at the polygon %ld \n",grid->polygons->element[j]->edge_indices->nh,grid->links->element[j]->connections->nh,j);
289 
290  for (c=grid->links->element[j]->connections->nl;c<=grid->links->element[j]->connections->nh;c++) {
291  if (grid->links->element[j]->connections->element[c]<0) grid->boundary_indicator=(long)grid->links->element[j]->connections->element[c];
292  }
293  }
294  //grid->boundary_indicator=-10;
295  if (print==1) printf("\n Function read_grid was successfully executed (number of polygons: %ld - number of lines: %ld - boundary indactor : %ld) \n",grid->polygons->nh,grid->lines->nh,grid->boundary_indicator);
296 
297 
298  //}
299 
300 
301 
302 // char *file_resume_lines_fine=join_strings(resume_filenames,"_lines_fine.txt");
303 // char *file_resume_polygons_fine=join_strings(resume_filenames,"_polygons_fine.txt");
304 // char *file_resume_connections_fine=join_strings(resume_filenames,"_connections_fine.txt");
305 // char *file_resume_c_polygon=join_strings(resume_filenames,"_c_polygon.txt");
306 // char *file_resume_c_line=join_strings(resume_filenames,"_c_line.txt");
307 
308  return grid;
309 }
310 
311 SQUARE_GRID *read_square_grid(char *keyname,DOUBLEMATRIX *DTM,long (*index_pixel_from_a_bin)(long r, long c,LONGVECTOR *s_index),DOUBLEVECTOR *V,int (*check_novalues)(double x, DOUBLEVECTOR *V),short print) {
330  SQUARE_GRID *sq;
331  long count,r,c;
332 
333 // sq=(SQUARE_GRID *)malloc(sizeof(SQUARE_GRID));
334 // if (!sq) t_error("Square Grid sq was not allocated");
335 
336 // sq->indices_pixel=m_indices_from_mask(DTM,0,0,INTEGER_NULL,0,index_pixel_from_a_bin,V,(*check_novalues));
337 // sq->indices_vertex=m_indices_from_mask(DTM,-1,-1,INTEGER_NULL,0,(*index_pixel_from_a_bin),V,(*check_novalues));
338 // sq->indices_horizontal_lines=m_indices_from_mask(DTM,-1,0,INTEGER_NULL,0,(*index_pixel_from_a_bin),V,(*check_novalues));
339 
340  sq=(SQUARE_GRID *)malloc(sizeof(SQUARE_GRID));
341  if (!sq) t_error("Square Grid sq was not allocated");
342  sq->indices_pixel=m_indices_from_mask(DTM,0,0,INTEGER_NULL,0,index_pixel_from_a_bin,V,(*check_novalues));
343  sq->indices_vertex=m_indices_from_mask(DTM,-1,-1,INTEGER_NULL,0,(*index_pixel_from_a_bin),V,(*check_novalues));
345  count=0;
346  for(r=sq->indices_horizontal_lines->nrl;r<=sq->indices_horizontal_lines->nrh;r++){
347  for (c=sq->indices_horizontal_lines->ncl;c<=sq->indices_horizontal_lines->nch;c++){
348  if (sq->indices_horizontal_lines->element[r][c]!=INTEGER_NULL) count++;
349  }
350  }
351  sq->nhorizontal_lines=count;
352  sq->novalue=INTEGER_NULL;
353  sq->indices_vertical_lines=m_indices_from_mask(DTM,0,-1,INTEGER_NULL,count,(*index_pixel_from_a_bin),V,(*check_novalues));
354 
355 
356  printf("Warning: index matrix of SQUARE_GRID (already created) %s was not checked!!\n",keyname);
357  sq->novalue=sq->indices_pixel->element[1][1];
358  sq->grid=read_grid(keyname,print);
359  if (print==1) printf("Function read_square_grid (%s) was correctly executed!!! \n",keyname);
360  return sq;
361 
362 }
363 
364 LONGMATRIX_VECTOR *read_fine_indices(char *filename,short print) {
372  LONGMATRIX_VECTOR *lm;
373  FILE *fd;
374  long j,n_po;
375  fd=t_fopen(filename,"r");
376 
377  n_po=(long)read_index(fd,no_PRINT);
378  lm=new_longmatrix_vector(n_po);
379 
380  for (j=lm->nl;j<=lm->nh;j++) {
381  if (print==1) printf("Function read_fine_indices element %ld of %ld is being defined \n",j,lm->nh);
382  lm->element[j]=read_longmatrix(fd,"a",no_PRINT);
383  if (print==1) printf("Function read_fine_indices element %ld of %ld is read [%ld,%ld] f.e. %ld \n",j,lm->nh,lm->element[j]->nrh,lm->element[j]->nrh,lm->element[j]->element[1][1]);
384  if (!lm->element[j]) t_error("lm->element[j] in read_fine_indices was not defined");
385 
386  // if (polygons->element[j]->index!=j) printf ("Error in read_polygonvector (polygon %ld) inconstancy: %ld %ld \n",j,j,polygons->element[j]->index);
387  }
388 
389  t_fclose(fd);
390 
391  if (print==1) printf("Function read_fine_indices was successfully executed! \n");
392 
393  return lm;
394 }
395 
396 
397 LONGBIN *read_line_indices(char *filename,short print) {
398  /*
399  *
400  * \author Emanuele Cordano
401  * date May 2009
402  *
403  * \param (char*) - name of filename
404  *\param (short) -
405  *
406  */
407  LONGVECTOR **lv;
408  LONGBIN *lb;
409  LONGVECTOR *vi;
410  FILE *fd;
411  long j,n_l,c;
412  fd=t_fopen(filename,"r");
413 
414  n_l=(long)read_index(fd,print);
415 // printf("n_l=%ld",n_l);
416 
417  lv=(LONGVECTOR **)malloc((size_t)(n_l*sizeof(LONGVECTOR *)));
418 // stop_execution();
419  for (j=0;j<=n_l-1;j++) {
420  lv[j]=read_longarray(fd,print);
421  }
422  vi=new_longvector(n_l);
423  for (j=vi->nl;j<=vi->nh;j++) {
424  vi->element[j]=lv[j-1]->nh;
425  }
426  lb=new_longbin(vi);
427  for (j=lb->index->nl;j<=lb->index->nh;j++) {
428  for(c=1;c<=lb->index->element[j];c++) {
429  lb->element[j][c]=lv[j-1]->element[c];
430  }
431  }
432 
433  free_longvector(vi);
434  for (j=0;j<=n_l-1;j++) {
435  free_longvector(lv[j]);
436  }
437  free(lv);
438  t_fclose(fd);
439 
440  if (print==1) printf("Function read_line_indices was successfully executed! \n");
441 
442 
443 
444 
445  return lb;
446 
447 
448 }
449 
450 
451 
452 DOUBLESQUARE_GRID *read_doublesquare_grid (DOUBLERASTER_MAP *draster, char *keyname,long (*index_pixel_from_a_bin_coarse)(long r, long c,LONGVECTOR *s_index),long (*index_pixel_from_a_bin_fine)(long r, long c,LONGVECTOR *s_index),short print){
453  /*
454  *
455  * \author Emanuele Cordano
456  * \date May 2009
457  *
458  *\param - DOUBLERASTER_MAP *draster
459  *\param - (char *) root neme of textfiles containing the struct information.
460  *\param long (*index_pixel_from_a_bin_coarse)(long r, long c,LONGVECTOR *s_index) - equation of the filling curve for the pixel of a coarse grid
461  *\param long (*index_pixel_from_a_bin_fine)(long r, long c,LONGVECTOR *s_index),long d_coarse,long d_fine,short print) - equation of the filling curve for the pixel of a fine grid
462 
463  *\param (short) print
464  *
465  */
466 
468 
469  char *keyname_coarse=join_strings(keyname,"__coarse");
470  char *keyname_fine=join_strings(keyname,"__fine");
471 
472  char *filename_c_polygon=join_strings(keyname,"_c_polygon.txt");
473  char *filename_c_line=join_strings(keyname,"_c_line.txt");
474 
475  dsq=(DOUBLESQUARE_GRID *)malloc(sizeof(DOUBLESQUARE_GRID));
476  if (!dsq) t_error("Double Square Grid dsq in read_doublesquare_grid was not allocated");
477 
478  dsq->big=read_square_grid(keyname_coarse,draster->coarse->layer[draster->coarse->reference_index_map],index_pixel_from_a_bin_coarse,draster->coarse->UV->V,draster->coarse->check_novalues,print);
479  dsq->fine=read_square_grid(keyname_fine,draster->fine->layer[draster->fine->reference_index_map],index_pixel_from_a_bin_fine,draster->fine->UV->V,draster->fine->check_novalues,print);
480 
481  dsq->file_resume_c_line=copy_string(filename_c_line);
482  dsq->file_resume_c_polygon=copy_string(filename_c_polygon);
483 
486 
487  if (dsq->small_content_line->index->nh!=dsq->big->grid->lines->nh) {
488  printf("Error in read_doublesquare_grid inconstancy with number of (big) lines %ld and %ld \n",dsq->small_content_line->index->nh,dsq->big->grid->lines->nh);
489  stop_execution();
490  }
491  if (dsq->small_content_polygon->nh!=dsq->big->grid->polygons->nh) {
492  printf("Error in read_doublesquare_grid inconstancy with number of (big) lines %ld and %ld \n",dsq->small_content_polygon->nh,dsq->big->grid->polygons->nh);
493  stop_execution();
494  }
495 
496  if (print==1) printf("Function read_doublesquare_grid was successfully executed! \n ");
497 
498  return dsq;
499 }
500 
501 
502