TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
import_ascii.c
Go to the documentation of this file.
1 
2 /* STATEMENT:
3 
4 ASCII-GEOtop LIBRARIES
5 
6 Copyright, 2008 Stefano Endrizzi, Riccardo Rigon
7 
8  LICENSE:
9 
10  This file is part of ASCII-GEOtop LIBRARIES
11  ASCII-GEOtop LIBRARIES is a free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program. If not, see <http://www.gnu.org/licenses/>.*/
23 
24 #include "turtle.h"
25 #include "t_utilities.h" /* line addded by Emanuele Cordano on 1 september 2009 */
26 #include "import_ascii.h"
27 #include "extensions.h"
28 
29 //-----------------------------------------------------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------------------------------------------------
31 //-----------------------------------------------------------------------------------------------------------------------
32 
33 double *read_grassascii(double *header, double novalue, char *name, long max_figures)
34 
35 {
36  FILE *f;
37  long cont,i,j,ch[max_figures],r,c,nr,nc;
38  short sgn, end=0;
39  double *dtm;
40  char *grass=join_strings(name,ascii_grass);
41 
42  f=fopen(grass,"r");
43  if(f==NULL){
44  printf("\nFile %s doesn't exist",grass);
45  t_error("Fatal error");
46  }
47 
48  //read header
49  for(i=0;i<=5;i++){
50  header[i]=0.0;
51  cont=0;
52  sgn=0;
53  do{
54  ch[0]=fgetc(f);
55  error_message(1,ch[0],-1,10,0,name);
56  cont+=1;
57  if(cont==1){
58  if( (i==0 && ch[0]!=110) || (i==1 && ch[0]!=115) || (i==2 && ch[0]!=101) || (i==3 && ch[0]!=119) || (i==4 && ch[0]!=114) || (i==5 && ch[0]!=99) ){
59  printf("\n Warning: check if the file %s is in grass ascii format, the header is not ok \n",grass);
61  }
62  }
63  }while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
64  cont=0;
65  if(ch[0]==45){
66  sgn=1; //sign minus
67  ch[0]=fgetc(f);
68  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
69  printf("Error: - not followed by a number for r=%ld c=%ld, file %s",r,c,grass);
70  t_error("Fatal error");
71  }
72  }
73  if(ch[0]>=48 && ch[0]<=57){
74  do{
75  cont+=1;
76  if(cont>=max_figures){
77  printf("Increase max_figures_in_a_number in file %s",grass);
78  t_error("Fatal error");
79  }
80  ch[cont]=fgetc(f);
81  }while(ch[cont]>=48 && ch[cont]<=57);
82  for(j=0;j<=cont-1;j++){
83  header[i]+=(ch[j]-48)*pow(10,cont-j-1);
84  }
85  ch[0]=ch[cont];
86  }
87  if(ch[0]==46){
88  cont=0;
89  do{
90  ch[0]=fgetc(f);
91  if(ch[0]>=48 && ch[0]<=57){
92  cont+=1;
93  header[i]+=(ch[0]-48)*pow(10,-cont);
94  }
95  }while(ch[0]>=48 && ch[0]<=57);
96  }
97  error_message(1,ch[0],-1,0,0,name);
98  if(ch[0]!=10){
99  do{
100  ch[0]=fgetc(f);
101  }while(ch[0]!=10);
102  }
103  if(sgn==1) header[i]*=-1;
104  }
105  if(header[1]>=header[0]){
106  printf("In file %s south larger than or equal to north",grass);
107  t_error("Fatal error");
108  }
109  if(header[3]>=header[2]){
110  printf("In file %s west larger than or equal to east",grass);
111  t_error("Fatal error");
112  }
113  if(header[4]<=0 || header[5]<=0){
114  printf("In file %s nrows or ncols negative or null",grass);
115  t_error("Fatal error");
116  }
117 
118  //read matrix
119  nr=(long)header[4];
120  nc=(long)header[5];
121  dtm = (double *) malloc(nr*nc*sizeof(double));
122  for(r=1;r<=nr;r++){
123  for(c=1;c<=nc;c++){
124  dtm[(r-1)*nc+c-1]=0.0;
125  }
126  }
127  for(r=1;r<=nr;r++){
128  c=0;
129  do{
130  c+=1;
131  sgn=0;
132  do{
133  ch[0]=fgetc(f);
134  if(ch[0]==10){ //end of line
135  if(c==nc+1 || r>nr){
136  r++;
137  c=1;
138  }else if(c!=1){
139  printf("Number of cols less than declared in row %ld in file %s",r,grass);
140  t_error("Fatal error");
141  }
142  }
143  if(ch[0]==-1){ //end of file
144  if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
145  end=1;
146  c=nc;
147  r=nr;
148  }else{
149  printf("Number of rows less than declared in file %s",grass);
150  t_error("Fatal error");
151  }
152  }
153  if(ch[0]==58){
154  printf("Header cannot consist of more than 6 lines, error in file %s",grass);
155  t_error("Fatal error");
156  }
157  }while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
158  //while ch is different from "*" "." "+" "-" or a number
159  if(end==0){
160  if(ch[0]==45) sgn=1; //sign minus
161  if(ch[0]==43 || ch[0]==45){
162  ch[0]=fgetc(f);
163  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
164  printf("Error: +- not followed by a number for r=%ld c=%ld, file %s",r,c,grass);
165  t_error("Fatal error");
166  }
167  }
168  if(ch[0]>=48 && ch[0]<=57){
169  cont=0;
170  do{
171  cont+=1;
172  ch[cont]=fgetc(f);
173  }while(ch[cont]>=48 && ch[cont]<=57);
174  //while ch[cont] is a number
175  for(j=0;j<=cont-1;j++){
176  dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
177  }
178  ch[0]=ch[cont];
179  }
180  if(ch[0]==46){
181  cont=0;
182  do{
183  ch[0]=fgetc(f);
184  if(ch[0]>=48 && ch[0]<=57){
185  cont+=1;
186  dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
187  }
188  }while(ch[0]>=48 && ch[0]<=57);
189  }else if(ch[0]==42){
190  dtm[(r-1)*nc+c-1]=novalue;
191  ch[0]=fgetc(f);
192  }else if(ch[0]!=32 && ch[0]!=10){
193  ch[0]=fgetc(f);
194  }
195  if(sgn==1) dtm[(r-1)*nc+c-1]*=(-1);
196  }
197  }while(ch[0]!=10 && ch[0]!=-1);
198 
199  if(c<nc && r<=nr){
200  printf("Number of cols less than declared in row %ld in file %s",r,grass);
201  t_error("Fatal error");
202  }
203  if(c>nc && r<=nr){
204  printf("Number of cols higher than declared in row %ld in file %s",r,grass);
205  t_error("Fatal error");
206  }
207  if(ch[0]==-1 && r<nr){
208  printf("Number of rows less than declared in file %s",grass);
209  t_error("Fatal error");
210  }
211  }
212 
213  fclose(f);
214  free(grass);
215  return(dtm);
216 
217 }
218 
219 //-----------------------------------------------------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------------------------------------------------
221 //-----------------------------------------------------------------------------------------------------------------------
222 
223 double *read_esriascii(double *header, double novalue, char *name, long max_figures)
224 
225 {
226  FILE *f;
227  long cont,i,j,ch[max_figures],r,c,nr,nc;
228  short sgn, end=0;
229  double *dtm;
230 
231  f=fopen(join_strings(name,ascii_esri),"r");
232  if(f==NULL){
233  printf("\nFile %s doesn't exist",join_strings(name,ascii_esri));
234  t_error("Fatal error");
235  }
236 
237  //read header
238  for(i=0;i<=4;i++){
239  header[i]=0.0;
240  cont=0;
241  sgn=0;
242  do{
243  ch[0]=fgetc(f);
244  error_message(2,ch[0],-1,10,0,name);
245  if(ch[0]==32 && cont==0){
246  }else{
247  cont+=1;
248  }
249  if(cont==1){
250  if( (i==0 && ch[0]!=110) || (i==1 && ch[0]!=110) || (i==2 && ch[0]!=120) || (i==3 && ch[0]!=121) || (i==4 && ch[0]!=99) ){
251  printf("\n Warning: check if the file %s is in esri ascii format, the header is not ok \n",join_strings(name,ascii_esri));
252  stop_execution(); //commented for rastermap generated by R raster (CRAN package) EC 11 2011
253  }
254  }
255  if( cont==5 && i==2 && ch[0]==101 ){
256  printf("\n Warning: only xllcorner and yllcorner in the header of %s are allowed, if it is xllcenter and yllcenter the map cannot be correctly read \n",join_strings(name,ascii_esri));
257  stop_execution();
258  }
259  }while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
260  cont=0;
261  if(ch[0]==45){
262  sgn=1; //sign minus
263  ch[0]=fgetc(f);
264  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
265  printf("Error: +- not followed by a number for r=%ld c=%ld, file %s",r,c,join_strings(name,ascii_esri));
266  t_error("Fatal error");
267  }
268  }
269  if(ch[0]>=48 && ch[0]<=57){
270  do{
271  cont+=1;
272  if(cont>=max_figures){
273  printf("Increase max_figures_in_a_number in file %s",join_strings(name,ascii_esri));
274  t_error("Fatal error");
275  }
276  ch[cont]=fgetc(f);
277  }while(ch[cont]>=48 && ch[cont]<=57);
278  for(j=0;j<=cont-1;j++){
279  header[i]+=(ch[j]-48)*pow(10,cont-j-1);
280  }
281  ch[0]=ch[cont];
282  }
283  if(ch[0]==46){
284  cont=0;
285  do{
286  ch[0]=fgetc(f);
287  if(ch[0]>=48 && ch[0]<=57){
288  cont+=1;
289  header[i]+=(ch[0]-48)*pow(10,-cont);
290  }
291  }while(ch[0]>=48 && ch[0]<=57);
292  }
293  error_message(2,ch[0],-1,0,0,name);
294  if(ch[0]!=10){
295  do{
296  ch[0]=fgetc(f);
297  }while(ch[0]!=10);
298  }
299  if(sgn==1) header[i]*=-1;
300  }
301  if(header[0]<=0 || header[1]<=0){
302  printf("In file %s nrows or ncols negative or null %lf %lf",join_strings(name,ascii_esri),header[0], header[1]);
303  t_error("Fatal error");
304  }
305 
306  //read matrix
307  nr=(long)header[1];
308  nc=(long)header[0];
309  dtm = (double *) malloc(nr*nc*sizeof(double));
310  for(r=1;r<=nr;r++){
311  for(c=1;c<=nc;c++){
312  dtm[(r-1)*nc+c-1]=0.0;
313  }
314  }
315 
316  //check if a novalue line is present
317  do{
318  ch[0]=fgetc(f);
319  error_message(2,ch[0],-1,10,0,name);
320  }while(ch[0]==32);
321  if(ch[0]==78){ //character "N", NODATA_value
322  header[5]=0.0;
323  cont=0;
324  sgn=0;
325  do{
326  ch[0]=fgetc(f);
327  error_message(2,ch[0],-1,10,0,name);
328  }while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
329  if(ch[0]==45){
330  sgn=1; //sign minus
331  ch[0]=fgetc(f);
332  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
333  printf("Error: +- not followed by a number for r=%ld c=%ld, file %s",r,c,join_strings(name,ascii_esri));
334  t_error("Fatal error");
335  }
336  }
337  if(ch[0]>=48 && ch[0]<=57){
338  do{
339  cont+=1;
340  if(cont>=max_figures){
341  printf("Increase max_figures_in_a_number in file %s",join_strings(name,ascii_esri));
342  t_error("Fatal error");
343  }
344  ch[cont]=fgetc(f);
345  }while(ch[cont]>=48 && ch[cont]<=57);
346  for(j=0;j<=cont-1;j++){
347  header[5]+=(ch[j]-48)*pow(10,cont-j-1);
348  }
349  ch[0]=ch[cont];
350  }
351  if(ch[0]==46){
352  cont=0;
353  do{
354  ch[0]=fgetc(f);
355  if(ch[0]>=48 && ch[0]<=57){
356  cont+=1;
357  header[5]+=(ch[0]-48)*pow(10,-cont);
358  }
359  }while(ch[0]>=48 && ch[0]<=57);
360  }
361  error_message(2,ch[0],-1,0,0,name);
362  if(ch[0]!=10){
363  do{
364  ch[0]=fgetc(f);
365  }while(ch[0]!=10);
366  }
367  if(sgn==1) header[i]*=-1;
368 
369  for(r=1;r<=nr;r++){
370  c=0;
371  do{
372  c+=1;
373  sgn=0;
374  do{
375  ch[0]=fgetc(f);
376  if(ch[0]==10){ //end of line
377  if(c==nc+1 || r>nr){
378  r++;
379  c=1;
380  }else if(c!=1){
381  printf("Number of cols less than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
382  t_error("Fatal error");
383  }
384  }
385  if(ch[0]==-1){ //end of file
386  if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
387  end=1;
388  c=nc;
389  r=nr;
390  }else{
391  printf("Number of rows less than declared in file %s",join_strings(name,ascii_esri));
392  t_error("Fatal error");
393  }
394  }
395  }while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
396  //while ch is different from "*" "." "+" "-" or a number
397  if(end==0){
398  if(ch[0]==45) sgn=1; //sign minus
399  if(ch[0]==43 || ch[0]==45){
400  ch[0]=fgetc(f);
401  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
402  printf("Error: +- not followed by a number for r=%ld c=%ld, file %s",r,c,join_strings(name,ascii_esri));
403  t_error("Fatal error");
404  }
405  }
406  if(ch[0]>=48 && ch[0]<=57){
407  cont=0;
408  do{
409  cont+=1;
410  ch[cont]=fgetc(f);
411  }while(ch[cont]>=48 && ch[cont]<=57);
412  //while ch[cont] is a number
413  for(j=0;j<=cont-1;j++){
414  dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
415  }
416  ch[0]=ch[cont];
417  }
418  if(ch[0]==46){
419  cont=0;
420  do{
421  ch[0]=fgetc(f);
422  if(ch[0]>=48 && ch[0]<=57){
423  cont+=1;
424  dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
425  }
426  }while(ch[0]>=48 && ch[0]<=57);
427  }else if(ch[0]==42){
428  dtm[(r-1)*nc+c-1]=novalue;
429  ch[0]=fgetc(f);
430  }else if(ch[0]!=32 && ch[0]!=10){
431  ch[0]=fgetc(f);
432  }
433  if(sgn==1) dtm[(r-1)*nc+c-1]*=(-1);
434  if(dtm[(r-1)*nc+c-1]==header[5]) dtm[(r-1)*nc+c-1]=novalue;
435  }
436  }while(ch[0]!=10 && ch[0]!=-1);
437 
438  if(c<nc && r<=nr){
439  printf("Number of cols less than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
440  t_error("Fatal error");
441  }
442  if(c>nc && r<=nr){
443  printf("Number of cols higher than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
444  t_error("Fatal error");
445  }
446  if(ch[0]==-1 && r<nr){
447  printf("Number of rows less than declared in file %s",join_strings(name,ascii_esri));
448  t_error("Fatal error");
449  }
450  }
451 
452  }else{
453 
454  for(r=1;r<=nr;r++){
455  c=0;
456  do{
457  c+=1;
458  do{
459  if(c==1 && r==1){
460  sgn=0;
461  }else{
462  ch[0]=fgetc(f);
463  sgn=0;
464  }
465  if(ch[0]==10){ //end of line
466  if(c==nc+1 || r>nr){
467  r++;
468  c=1;
469  }else if(c!=1){
470  printf("Number of cols less than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
471  t_error("Fatal error");
472  }
473  }
474  if(ch[0]==-1){ //end of file
475  if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
476  end=1;
477  c=nc;
478  r=nr;
479  }else{
480  printf("Number of rows less than declared in file %s",join_strings(name,ascii_esri));
481  t_error("Fatal error");
482  }
483  }
484  }while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
485  //while ch is different from "*" "." "+" "-" or a number
486  if(end==0){
487  if(ch[0]==45) sgn=1; //sign minus
488  if(ch[0]==43 || ch[0]==45){
489  ch[0]=fgetc(f);
490  if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
491  printf("Error: +- not followed by a number for r=%ld c=%ld, file %s",r,c,join_strings(name,ascii_esri));
492  t_error("Fatal error");
493  }
494  }
495  if(ch[0]>=48 && ch[0]<=57){
496  cont=0;
497  do{
498  cont+=1;
499  ch[cont]=fgetc(f);
500  }while(ch[cont]>=48 && ch[cont]<=57);
501  //while ch[cont] is a number
502  for(j=0;j<=cont-1;j++){
503  dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
504  }
505  ch[0]=ch[cont];
506  }
507  if(ch[0]==46){
508  cont=0;
509  do{
510  ch[0]=fgetc(f);
511  if(ch[0]>=48 && ch[0]<=57){
512  cont+=1;
513  dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
514  }
515  }while(ch[0]>=48 && ch[0]<=57);
516  }else if(ch[0]==42){
517  dtm[(r-1)*nc+c-1]=novalue;
518  ch[0]=fgetc(f);
519  }else if(ch[0]!=32 && ch[0]!=10){
520  ch[0]=fgetc(f);
521  }
522  if(sgn==1) dtm[(r-1)*nc+c-1]*=(-1);
523  }
524  }while(ch[0]!=10 && ch[0]!=-1);
525 
526  if(c<nc && r<=nr){
527  printf("Number of cols less than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
528  t_error("Fatal error");
529  }
530  if(c>nc && r<=nr){
531  printf("Number of cols higher than declared in row %ld in file %s",r,join_strings(name,ascii_esri));
532  t_error("Fatal error");
533  }
534  if(ch[0]==-1 && r<nr){
535  printf("Number of rows less than declared in file %s",join_strings(name,ascii_esri));
536  t_error("Fatal error");
537  }
538  }
539  }
540 
541  fclose(f);
542 
543  return(dtm);
544 
545 }
546 
547 //-----------------------------------------------------------------------------------------------------------------------
548 //-----------------------------------------------------------------------------------------------------------------------
549 //-----------------------------------------------------------------------------------------------------------------------
550 
551 void error_message(short format, long n, long n1, long n2, long n3, char *name)
552 //format=1 grassascii
553 //format=2 esriascii
554 
555 {
556  if(n==n1 || n==n2 || n==n3){
557  if(format==1) printf("File %s incompleted, end of file or end of line reached",join_strings(name,ascii_grass));
558  if(format==2) printf("File %s incompleted, end of file or end of line reached",join_strings(name,ascii_esri));
559  t_error("Fatal error");
560  }
561 }
562 
563 //-----------------------------------------------------------------------------------------------------------------------
564 //-----------------------------------------------------------------------------------------------------------------------
565 //-----------------------------------------------------------------------------------------------------------------------