33 double *
read_grassascii(
double *header,
double novalue,
char *name,
long max_figures)
37 long cont,i,j,ch[max_figures],r,c,nr,nc;
44 printf(
"\nFile %s doesn't exist",grass);
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);
63 }
while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
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);
73 if(ch[0]>=48 && ch[0]<=57){
76 if(cont>=max_figures){
77 printf(
"Increase max_figures_in_a_number in file %s",grass);
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);
91 if(ch[0]>=48 && ch[0]<=57){
93 header[i]+=(ch[0]-48)*pow(10,-cont);
95 }
while(ch[0]>=48 && ch[0]<=57);
103 if(sgn==1) header[i]*=-1;
105 if(header[1]>=header[0]){
106 printf(
"In file %s south larger than or equal to north",grass);
109 if(header[3]>=header[2]){
110 printf(
"In file %s west larger than or equal to east",grass);
113 if(header[4]<=0 || header[5]<=0){
114 printf(
"In file %s nrows or ncols negative or null",grass);
121 dtm = (
double *) malloc(nr*nc*
sizeof(
double));
124 dtm[(r-1)*nc+c-1]=0.0;
139 printf(
"Number of cols less than declared in row %ld in file %s",r,grass);
144 if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
149 printf(
"Number of rows less than declared in file %s",grass);
154 printf(
"Header cannot consist of more than 6 lines, error in file %s",grass);
157 }
while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
161 if(ch[0]==43 || ch[0]==45){
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);
168 if(ch[0]>=48 && ch[0]<=57){
173 }
while(ch[cont]>=48 && ch[cont]<=57);
175 for(j=0;j<=cont-1;j++){
176 dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
184 if(ch[0]>=48 && ch[0]<=57){
186 dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
188 }
while(ch[0]>=48 && ch[0]<=57);
190 dtm[(r-1)*nc+c-1]=novalue;
192 }
else if(ch[0]!=32 && ch[0]!=10){
195 if(sgn==1) dtm[(r-1)*nc+c-1]*=(-1);
197 }
while(ch[0]!=10 && ch[0]!=-1);
200 printf(
"Number of cols less than declared in row %ld in file %s",r,grass);
204 printf(
"Number of cols higher than declared in row %ld in file %s",r,grass);
207 if(ch[0]==-1 && r<nr){
208 printf(
"Number of rows less than declared in file %s",grass);
223 double *
read_esriascii(
double *header,
double novalue,
char *name,
long max_figures)
227 long cont,i,j,ch[max_figures],r,c,nr,nc;
245 if(ch[0]==32 && cont==0){
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));
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));
259 }
while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
264 if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
269 if(ch[0]>=48 && ch[0]<=57){
272 if(cont>=max_figures){
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);
287 if(ch[0]>=48 && ch[0]<=57){
289 header[i]+=(ch[0]-48)*pow(10,-cont);
291 }
while(ch[0]>=48 && ch[0]<=57);
299 if(sgn==1) header[i]*=-1;
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]);
309 dtm = (
double *) malloc(nr*nc*
sizeof(
double));
312 dtm[(r-1)*nc+c-1]=0.0;
328 }
while(ch[0]<=44 || ch[0]==47 || ch[0]>=58);
332 if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
337 if(ch[0]>=48 && ch[0]<=57){
340 if(cont>=max_figures){
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);
355 if(ch[0]>=48 && ch[0]<=57){
357 header[5]+=(ch[0]-48)*pow(10,-cont);
359 }
while(ch[0]>=48 && ch[0]<=57);
367 if(sgn==1) header[i]*=-1;
386 if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
395 }
while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
399 if(ch[0]==43 || ch[0]==45){
401 if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
406 if(ch[0]>=48 && ch[0]<=57){
411 }
while(ch[cont]>=48 && ch[cont]<=57);
413 for(j=0;j<=cont-1;j++){
414 dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
422 if(ch[0]>=48 && ch[0]<=57){
424 dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
426 }
while(ch[0]>=48 && ch[0]<=57);
428 dtm[(r-1)*nc+c-1]=novalue;
430 }
else if(ch[0]!=32 && ch[0]!=10){
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;
436 }
while(ch[0]!=10 && ch[0]!=-1);
446 if(ch[0]==-1 && r<nr){
475 if( ((r==nr)&&(c==nc+1)) || (r>nr) ){
484 }
while(((ch[0]>=0)&&(ch[0]<=41))||(ch[0]==44)||(ch[0]==47)||(ch[0]>=58));
488 if(ch[0]==43 || ch[0]==45){
490 if(ch[0]<=45 || ch[0]==47 || ch[0]>=58){
495 if(ch[0]>=48 && ch[0]<=57){
500 }
while(ch[cont]>=48 && ch[cont]<=57);
502 for(j=0;j<=cont-1;j++){
503 dtm[(r-1)*nc+c-1]+=(ch[j]-48)*pow(10,cont-j-1);
511 if(ch[0]>=48 && ch[0]<=57){
513 dtm[(r-1)*nc+c-1]+=(ch[0]-48)*pow(10,-cont);
515 }
while(ch[0]>=48 && ch[0]<=57);
517 dtm[(r-1)*nc+c-1]=novalue;
519 }
else if(ch[0]!=32 && ch[0]!=10){
522 if(sgn==1) dtm[(r-1)*nc+c-1]*=(-1);
524 }
while(ch[0]!=10 && ch[0]!=-1);
534 if(ch[0]==-1 && r<nr){
551 void error_message(
short format,
long n,
long n1,
long n2,
long n3,
char *name)
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));