TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
write_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 "write_ascii.h"
26 #include "extensions.h"
27 
28 
29 void write_fluidturtle(char *name, short type, DOUBLEMATRIX *DTM, T_INIT *UV){
30 
31 // type=0 floating point
32 // type=1 integer
33 
34  FILE *f;
35  long r,c;
36 
37  f=t_fopen(join_strings(name,ascii_ft),"w");
38 
39  fprintf(f,"/** MAP written by write_fluidturtle subroutine */\n");
40  fprintf(f,"index{3}\n");
41  fprintf(f,"1: double array pixels size {%f,%f,%f,%f}\n",UV->U->co[1],UV->U->co[2],UV->U->co[3],UV->U->co[4]);
42  fprintf(f,"2: double array novalues {%ld.0,%ld.0}\n",(long)(UV->V->co[1]),(long)(UV->V->co[2]));
43  fprintf(f,"3: double matrix state_variable {%ld,%ld}\n",DTM->nrh,DTM->nch);
44  for(r=1;r<=DTM->nrh;r++){
45  for(c=1;c<=DTM->nch;c++){
46  if(DTM->co[r][c]==UV->V->co[1]*fabs(UV->V->co[2])){
47  fprintf(f,"%ld.0",(long)(DTM->co[r][c]));
48  }else{
49  if(type==1){
50  fprintf(f,"%ld",(long)(DTM->co[r][c]));
51  }else{
52  fprintf(f,"%f",DTM->co[r][c]);
53  }
54  }
55  if(c!=DTM->nch) fprintf(f," ");
56  }
57  if(r!=DTM->nrh) fprintf(f,"\n");
58  }
59  t_fclose(f);
60 }
61 
62 //-----------------------------------------------------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------------------------------------------------
65 
66 void write_grassascii(char *name, short type, DOUBLEMATRIX *DTM, T_INIT *UV){
67 
68 // type=0 floating point
69 // type=1 integer
70 
71  FILE *f;
72  long r,c;
73  char *filename;
74 
75  filename=join_strings(name,ascii_grass);
76  f=fopen(filename,"w");
77  free(filename);
78  fprintf(f,"north:%f\n",UV->U->co[3]+DTM->nrh*UV->U->co[1]);
79  fprintf(f,"south:%f\n",UV->U->co[3]);
80  fprintf(f,"east:%f\n",UV->U->co[4]+DTM->nch*UV->U->co[2]);
81  fprintf(f,"west:%f\n",UV->U->co[4]);
82  fprintf(f,"rows:%ld\n",DTM->nrh);
83  fprintf(f,"cols:%ld\n",DTM->nch);
84  for(r=1;r<=DTM->nrh;r++){
85  for(c=1;c<=DTM->nch;c++){
86  if(DTM->co[r][c]==UV->V->co[1]*fabs(UV->V->co[2])){
87  fprintf(f,"*");
88  }else{
89  if(type==1){
90  fprintf(f,"%ld",(long)(DTM->co[r][c]));
91  }else{
92  fprintf(f,"%f",DTM->co[r][c]);
93  }
94  }
95  if(c<DTM->nch) fprintf(f," ");
96  }
97  if(r<DTM->nrh) fprintf(f,"\n");
98  }
99  fclose(f);
100 
101 }
102 
103 //-----------------------------------------------------------------------------------------------------------------------
104 //-----------------------------------------------------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------------------------------------------------
106 
107 void write_esriascii(char *name, short type, DOUBLEMATRIX *DTM, T_INIT *UV){
108 
109 // type=0 floating point
110 // type=1 integer
111 
112  FILE *f;
113  long r,c;
114 
115  if(UV->U->co[1]!=UV->U->co[2]){
116  printf("\nCannot export in esriascii, grid not square, Dx=%f Dy=%f \n",UV->U->co[2],UV->U->co[1]);
117  t_error("Fatal error");
118  }
119 
120  f=fopen(join_strings(name,ascii_esri),"w");
121 
122  fprintf(f,"ncols %ld\n",DTM->nch);
123  fprintf(f,"nrows %ld\n",DTM->nrh);
124  fprintf(f,"xllcorner %f\n",UV->U->co[4]);
125  fprintf(f,"yllcorner %f\n",UV->U->co[3]);
126  fprintf(f,"cellsize %f\n",UV->U->co[1]);
127  fprintf(f,"NODATA_value %ld.0\n",(long)(UV->V->co[1]*fabs(UV->V->co[2])));
128  for(r=1;r<=DTM->nrh;r++){
129  for(c=1;c<=DTM->nch;c++){
130  if(DTM->co[r][c]==UV->V->co[1]*fabs(UV->V->co[2])){
131  fprintf(f,"%ld.0",(long)(DTM->co[r][c]));
132  }else{
133  if(type==1){
134  fprintf(f,"%ld",(long)(DTM->co[r][c]));
135  }else{
136  fprintf(f,"%f",DTM->co[r][c]);
137  }
138  }
139  if(c<DTM->nch) fprintf(f," ");
140  }
141  if(r<DTM->nrh) fprintf(f,"\n");
142  }
143  fclose(f);
144 }