HI~ guys , I have a program here (Sorry it is very long about 240
lines.) It can read and write the header information successfully but
it cannot write the array successfully. I guess there is something
wrong with the Write2DArrayInt function there. Can any one henlp me
to find it out? Thank you~ (BTW : The printf function in line 114 and
139 implys that the data is right, but after that , in the new
generated file, the values are all zero.....:( )
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5
6 typedef struct _GRIDDEF
7 {
8 double CentX;
9 double CentY;
10 double CentLat;
11 double CentLon;
12 double TlX;
13 double TlY;
14 double DeltaX;
15 double DeltaY;
16 long ProjectionID;
17 double ProjectionParam eters[15];
18 long (*inv_trans[100])(double,double ,double *, double *);
19 long (*for_trans[100])(double,double ,double *, double *);
20 long MapZone;
21 long InDatum;
22 long stat;
23 int NumX;
24 int NumY;
25 } GRIDDEF;
26
27
28 short **Allocate2DInt (int rows,int columns);
29 float **Allocate2DFlo at(int rows,int columns);
30 void Write2DArrayInt (short **Array, int Columns, int Rows,
FILE *fp);
31 void WriteHeader(GRI DDEF *, FILE *);
32 void free_2D_float(f loat **);
33 void free_2D_Int(sho rt **);
34
35 int main()
36 {
37 GRIDDEF psgrid;
38
39 psgrid.MapZone = 0;
40 psgrid.InDatum = -1;
41 psgrid.Projecti onParameters[0] = 6371007.181;
42 psgrid.CentLat = -25.0;
43 psgrid.CentLon = 108;
44 psgrid.NumX = 800;
45 psgrid.NumY = 800;
46 psgrid.DeltaX = 1000.0;
47 psgrid.DeltaY = 1000.0;
48 psgrid.Projecti onID = 10;
49 54 float **latitude,**lo ntitude;
55 short
**clear,**tcumu lus,**cowacloud ,**cumulus,**wa wacloud,**thick cicloud,**verys mallcumu,**cirr us,**solidwa,** thwacloud1,**ci rrordeepcon,**s tratu
s,**Cb;
56
57 FILE *in;
58 FILE *out;
59 FILE *lat,*lon;
60 lat =
fopen("MOD021KM .A2005243.0255. 005.20080271153 45.lat", "r");
61 lon =
fopen("MOD021KM .A2005243.0255. 005.20080271153 45.lon", "r");
62
in=fopen("MOD02 1KM.A2005243.02 55.005.20080271 15345.200","r") ;
63 out=fopen("clas sified.dat","w" );
64
65 class=Allocate2 DInt(nrows, ncolumns);
66 clear=Allocate2 DInt(nrows, ncolumns);
67 tcumulus=Alloca te2DInt(nrows, ncolumns);
68 cowacloud=Alloc ate2DInt(nrows, ncolumns);
69 cumulus=Allocat e2DInt(nrows, ncolumns);
70 wawacloud=Alloc ate2DInt(nrows, ncolumns);
71 thickcicloud=Al locate2DInt(nro ws, ncolumns);
72 verysmallcumu=A llocate2DInt(nr ows, ncolumns);
73 cirrus=Allocate 2DInt(nrows, ncolumns);
74 solidwa=Allocat e2DInt(nrows, ncolumns);
75 thwacloud1=Allo cate2DInt(nrows , ncolumns);
76 cirrordeepcon=A llocate2DInt(nr ows, ncolumns);
77 stratus=Allocat e2DInt(nrows, ncolumns);
78 Cb=Allocate2DIn t(nrows, ncolumns);
79
80
81 if(in != NULL){
82 printf ("Opened file named successfully\n" );
83 }
84 latitude=Alloca te2DFloat(nrows , ncolumns);
85 lontitude=Alloc ate2DFloat(nrow s, ncolumns);
86
87 for(i=0; i<nrows; i++){
88 for(j=0; j<ncolumns; j++){
89 latitude[i][j] = 0.0;
90 lontitude[i][j] = 0.0;
91 class[i][j]=0;
92 clear[i][j]=0;
93 tcumulus[i][j]=0;
94 cowacloud[i][j]=0;
95 cumulus[i][j]=0;
96 wawacloud[i][j]=0;
97 thickcicloud[i][j]=0;
98 verysmallcumu[i][j]=0;
99 cirrus[i][j]=0;
100 solidwa[i][j]=0;
101 cirrordeepcon[i][j]=0; 104 thwacloud1[i]
[j]=0;
105 } 106 }
107 for(i=0; i<nrows; i++){
108 fread(latitude[i],sizeof(float), ncolumns,lat);
109
fread( lontitude[i],sizeof(float), ncolumns,lon);
110 fread(class[i],sizeof(short), ncolumns,in);
111 }
112 for(i=0;i<nrows ;i++){
113 for(j=0;j<ncolu mns;j++){
114 /* printf("The lat is %hd\t",class[i][j]);*/
115 }
116 }
117
118 for(i=0; i<nrows; i++){
119 for(j=0; j<ncolumns; j++){
120 /*printf("the data is %hd",class[i][j]); */
121 if (class[i][j]==1){ clear[i][j]=1;}else {clear[i][j]=0;}
122 if (class[i][j]==2){ tcumulus[i][j]=1;}else {tcumulus[i]
[j]=0;}
123 if (class[i][j]==3){ cowacloud[i][j]=1;}else {cowacloud[i]
[j]=0;}
124 if (class[i][j]==4){ cumulus[i][j]=1;}else {cumulus[i][j]=0;}
125 if (class[i][j]==5){ wawacloud[i][j]=1;}else {wawacloud[i]
[j]=0;}
126 if (class[i][j]==6){ thickcicloud[i][j]=1;}else
{thickcicloud[i][j]=0;}
127 if (class[i][j]==7){ verysmallcumu[i][j]=1;}else
{verysmallcumu[i][j]=0;}
128 if (class[i][j]==8){ cirrus[i][j]=1;}else {cirrus[i][j]=0;}
129 if (class[i][j]==9){ solidwa[i][j]=1;}else {solidwa[i][j]=0;}
130 if (class[i][j]==10){ thwacloud1[i][j]=1;}else {thwacloud1[i]
[j]=0;}
131 if (class[i][j]==11){cirrordee pcon[i][j]=1;}else
{cirrordeepcon[i][j]=0;}
132 if (class[i][j]==12){ stratus[i][j]=1;}else {stratus[i]
[j]=0;}
133 if (class[i][j]==13){ Cb[i][j]=1;}else {Cb[i][j]=0;}
134 /* printf("The lat is %hd\t",tcumulus[i][j]);*/
135 }
136 }
137
138 for(i=0; i<10; i++){
139 for(j=0; j<10; j++){printf("Th e lat is %d
\t",cumulus[i][j]);
140 }}
141
142 WriteHeader(&ps grid, out);
143 :wq
144
145 Write2DArrayInt (tcumulus, ncolumns,nrows, out);
146 Write2DArrayInt (clear, ncolumns,nrows, out);
147 Write2DArrayInt (cowacloud, ncolumns,nrows, out);
148 Write2DArrayInt (cumulus, ncolumns,nrows, out);
149 Write2DArrayInt (wawacloud, ncolumns,nrows, out);
150 Write2DArrayInt (thickcicloud, ncolumns,nrows, out); 155
Write2DArrayInt (cirrordeepcon, ncolumns,nrows, out);
156 Write2DArrayInt (stratus, ncolumns,nrows, out);
157 Write2DArrayInt (Cb, ncolumns,nrows, out);
158
159 free_2D_Int(tcu mulus);
160 free_2D_Int(cle ar);
161 free_2D_Int(cow acloud);
162 free_2D_Int(cum ulus);
163 free_2D_Int(waw acloud);
164 free_2D_Int(thi ckcicloud);
165 free_2D_Int(ver ysmallcumu);
166 free_2D_Int(cir rus);
167 free_2D_Int(sol idwa);
168 free_2D_Int(thw acloud1);
169 free_2D_Int(cir rordeepcon);
170 free_2D_Int(str atus);
171 free_2D_Int(Cb) ;
172
173
174 fclose(in);
175 fclose(out);
176 fclose(lat);
177 fclose(lon);
178
179 return(0);
180 }
181
182
183 void WriteHeader(GRI DDEF *psgrid, FILE *out)
184 {
185 fwrite(&(psgrid->CentLat),sizeo f(double),1,out );
186 fwrite(&(psgrid->CentLon),sizeo f(double),1,out );
187 fwrite(&(psgrid->DeltaX),sizeof (double),1,out) ;
188 fwrite(&(psgrid->DeltaY),sizeof (double),1,out) ;
189 fwrite(&(psgrid->ProjectionID), sizeof(long),1, out);
190 fwrite(psgrid->ProjectionPara meters,sizeof(d ouble),15,out);
191 fwrite(&(psgrid->MapZone),sizeo f(long),1,out);
192 fwrite(&(psgrid->NumX),sizeof(i nt),1,out);
193 fwrite(&(psgrid->NumY),sizeof(i nt),1,out);
194 }
195
196
197 /* Function to create a 2D float array of pointers*/
198 short **Allocate2DInt (int rows,int columns) 206 return
pntr;
199 {
200 short **pntr;
201 int i;
202
203 pntr = (short **)malloc(sizeo f(short *)*rows);
204 pntr[0] = (short *)malloc(sizeof (short )*rows*columns) ;
205 for(i=1; i<rows; i++)pntr[i] = pntr[i-1]+columns;
206 return pntr;
207 }
208
209 float **Allocate2DFlo at(int rows,int columns)
210 {
211 float **pntr;
212 int i;
213
214 pntr = (float **)malloc(sizeo f(float *)*rows);
215 pntr[0] = (float *)malloc(sizeof (float )*rows*columns) ;
216 for(i=1; i<rows; i++)pntr[i] = pntr[i-1]+columns;
217 return pntr;
218 }
219
220 void Write2DArrayInt (short **Array, int Columns, int Rows,
FILE *fp)
221 {
222
223 int i;
224
225 for(i=0; i<Rows; i++){
226 fwrite(Array[i], sizeof(short),C olumns, fp);
227 }
228 /*fclose(fp);*/
229 }
230
231 void free_2D_float(f loat **pntr)
232 {
233
234 free(pntr[0]);
235
236 free(pntr);
237 }
238
239 void free_2D_Int(sho rt **pntr)
240 {
241
242 free(pntr[0]);
243
244 free(pntr);
245 }
lines.) It can read and write the header information successfully but
it cannot write the array successfully. I guess there is something
wrong with the Write2DArrayInt function there. Can any one henlp me
to find it out? Thank you~ (BTW : The printf function in line 114 and
139 implys that the data is right, but after that , in the new
generated file, the values are all zero.....:( )
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5
6 typedef struct _GRIDDEF
7 {
8 double CentX;
9 double CentY;
10 double CentLat;
11 double CentLon;
12 double TlX;
13 double TlY;
14 double DeltaX;
15 double DeltaY;
16 long ProjectionID;
17 double ProjectionParam eters[15];
18 long (*inv_trans[100])(double,double ,double *, double *);
19 long (*for_trans[100])(double,double ,double *, double *);
20 long MapZone;
21 long InDatum;
22 long stat;
23 int NumX;
24 int NumY;
25 } GRIDDEF;
26
27
28 short **Allocate2DInt (int rows,int columns);
29 float **Allocate2DFlo at(int rows,int columns);
30 void Write2DArrayInt (short **Array, int Columns, int Rows,
FILE *fp);
31 void WriteHeader(GRI DDEF *, FILE *);
32 void free_2D_float(f loat **);
33 void free_2D_Int(sho rt **);
34
35 int main()
36 {
37 GRIDDEF psgrid;
38
39 psgrid.MapZone = 0;
40 psgrid.InDatum = -1;
41 psgrid.Projecti onParameters[0] = 6371007.181;
42 psgrid.CentLat = -25.0;
43 psgrid.CentLon = 108;
44 psgrid.NumX = 800;
45 psgrid.NumY = 800;
46 psgrid.DeltaX = 1000.0;
47 psgrid.DeltaY = 1000.0;
48 psgrid.Projecti onID = 10;
49 54 float **latitude,**lo ntitude;
55 short
**clear,**tcumu lus,**cowacloud ,**cumulus,**wa wacloud,**thick cicloud,**verys mallcumu,**cirr us,**solidwa,** thwacloud1,**ci rrordeepcon,**s tratu
s,**Cb;
56
57 FILE *in;
58 FILE *out;
59 FILE *lat,*lon;
60 lat =
fopen("MOD021KM .A2005243.0255. 005.20080271153 45.lat", "r");
61 lon =
fopen("MOD021KM .A2005243.0255. 005.20080271153 45.lon", "r");
62
in=fopen("MOD02 1KM.A2005243.02 55.005.20080271 15345.200","r") ;
63 out=fopen("clas sified.dat","w" );
64
65 class=Allocate2 DInt(nrows, ncolumns);
66 clear=Allocate2 DInt(nrows, ncolumns);
67 tcumulus=Alloca te2DInt(nrows, ncolumns);
68 cowacloud=Alloc ate2DInt(nrows, ncolumns);
69 cumulus=Allocat e2DInt(nrows, ncolumns);
70 wawacloud=Alloc ate2DInt(nrows, ncolumns);
71 thickcicloud=Al locate2DInt(nro ws, ncolumns);
72 verysmallcumu=A llocate2DInt(nr ows, ncolumns);
73 cirrus=Allocate 2DInt(nrows, ncolumns);
74 solidwa=Allocat e2DInt(nrows, ncolumns);
75 thwacloud1=Allo cate2DInt(nrows , ncolumns);
76 cirrordeepcon=A llocate2DInt(nr ows, ncolumns);
77 stratus=Allocat e2DInt(nrows, ncolumns);
78 Cb=Allocate2DIn t(nrows, ncolumns);
79
80
81 if(in != NULL){
82 printf ("Opened file named successfully\n" );
83 }
84 latitude=Alloca te2DFloat(nrows , ncolumns);
85 lontitude=Alloc ate2DFloat(nrow s, ncolumns);
86
87 for(i=0; i<nrows; i++){
88 for(j=0; j<ncolumns; j++){
89 latitude[i][j] = 0.0;
90 lontitude[i][j] = 0.0;
91 class[i][j]=0;
92 clear[i][j]=0;
93 tcumulus[i][j]=0;
94 cowacloud[i][j]=0;
95 cumulus[i][j]=0;
96 wawacloud[i][j]=0;
97 thickcicloud[i][j]=0;
98 verysmallcumu[i][j]=0;
99 cirrus[i][j]=0;
100 solidwa[i][j]=0;
101 cirrordeepcon[i][j]=0; 104 thwacloud1[i]
[j]=0;
105 } 106 }
107 for(i=0; i<nrows; i++){
108 fread(latitude[i],sizeof(float), ncolumns,lat);
109
fread( lontitude[i],sizeof(float), ncolumns,lon);
110 fread(class[i],sizeof(short), ncolumns,in);
111 }
112 for(i=0;i<nrows ;i++){
113 for(j=0;j<ncolu mns;j++){
114 /* printf("The lat is %hd\t",class[i][j]);*/
115 }
116 }
117
118 for(i=0; i<nrows; i++){
119 for(j=0; j<ncolumns; j++){
120 /*printf("the data is %hd",class[i][j]); */
121 if (class[i][j]==1){ clear[i][j]=1;}else {clear[i][j]=0;}
122 if (class[i][j]==2){ tcumulus[i][j]=1;}else {tcumulus[i]
[j]=0;}
123 if (class[i][j]==3){ cowacloud[i][j]=1;}else {cowacloud[i]
[j]=0;}
124 if (class[i][j]==4){ cumulus[i][j]=1;}else {cumulus[i][j]=0;}
125 if (class[i][j]==5){ wawacloud[i][j]=1;}else {wawacloud[i]
[j]=0;}
126 if (class[i][j]==6){ thickcicloud[i][j]=1;}else
{thickcicloud[i][j]=0;}
127 if (class[i][j]==7){ verysmallcumu[i][j]=1;}else
{verysmallcumu[i][j]=0;}
128 if (class[i][j]==8){ cirrus[i][j]=1;}else {cirrus[i][j]=0;}
129 if (class[i][j]==9){ solidwa[i][j]=1;}else {solidwa[i][j]=0;}
130 if (class[i][j]==10){ thwacloud1[i][j]=1;}else {thwacloud1[i]
[j]=0;}
131 if (class[i][j]==11){cirrordee pcon[i][j]=1;}else
{cirrordeepcon[i][j]=0;}
132 if (class[i][j]==12){ stratus[i][j]=1;}else {stratus[i]
[j]=0;}
133 if (class[i][j]==13){ Cb[i][j]=1;}else {Cb[i][j]=0;}
134 /* printf("The lat is %hd\t",tcumulus[i][j]);*/
135 }
136 }
137
138 for(i=0; i<10; i++){
139 for(j=0; j<10; j++){printf("Th e lat is %d
\t",cumulus[i][j]);
140 }}
141
142 WriteHeader(&ps grid, out);
143 :wq
144
145 Write2DArrayInt (tcumulus, ncolumns,nrows, out);
146 Write2DArrayInt (clear, ncolumns,nrows, out);
147 Write2DArrayInt (cowacloud, ncolumns,nrows, out);
148 Write2DArrayInt (cumulus, ncolumns,nrows, out);
149 Write2DArrayInt (wawacloud, ncolumns,nrows, out);
150 Write2DArrayInt (thickcicloud, ncolumns,nrows, out); 155
Write2DArrayInt (cirrordeepcon, ncolumns,nrows, out);
156 Write2DArrayInt (stratus, ncolumns,nrows, out);
157 Write2DArrayInt (Cb, ncolumns,nrows, out);
158
159 free_2D_Int(tcu mulus);
160 free_2D_Int(cle ar);
161 free_2D_Int(cow acloud);
162 free_2D_Int(cum ulus);
163 free_2D_Int(waw acloud);
164 free_2D_Int(thi ckcicloud);
165 free_2D_Int(ver ysmallcumu);
166 free_2D_Int(cir rus);
167 free_2D_Int(sol idwa);
168 free_2D_Int(thw acloud1);
169 free_2D_Int(cir rordeepcon);
170 free_2D_Int(str atus);
171 free_2D_Int(Cb) ;
172
173
174 fclose(in);
175 fclose(out);
176 fclose(lat);
177 fclose(lon);
178
179 return(0);
180 }
181
182
183 void WriteHeader(GRI DDEF *psgrid, FILE *out)
184 {
185 fwrite(&(psgrid->CentLat),sizeo f(double),1,out );
186 fwrite(&(psgrid->CentLon),sizeo f(double),1,out );
187 fwrite(&(psgrid->DeltaX),sizeof (double),1,out) ;
188 fwrite(&(psgrid->DeltaY),sizeof (double),1,out) ;
189 fwrite(&(psgrid->ProjectionID), sizeof(long),1, out);
190 fwrite(psgrid->ProjectionPara meters,sizeof(d ouble),15,out);
191 fwrite(&(psgrid->MapZone),sizeo f(long),1,out);
192 fwrite(&(psgrid->NumX),sizeof(i nt),1,out);
193 fwrite(&(psgrid->NumY),sizeof(i nt),1,out);
194 }
195
196
197 /* Function to create a 2D float array of pointers*/
198 short **Allocate2DInt (int rows,int columns) 206 return
pntr;
199 {
200 short **pntr;
201 int i;
202
203 pntr = (short **)malloc(sizeo f(short *)*rows);
204 pntr[0] = (short *)malloc(sizeof (short )*rows*columns) ;
205 for(i=1; i<rows; i++)pntr[i] = pntr[i-1]+columns;
206 return pntr;
207 }
208
209 float **Allocate2DFlo at(int rows,int columns)
210 {
211 float **pntr;
212 int i;
213
214 pntr = (float **)malloc(sizeo f(float *)*rows);
215 pntr[0] = (float *)malloc(sizeof (float )*rows*columns) ;
216 for(i=1; i<rows; i++)pntr[i] = pntr[i-1]+columns;
217 return pntr;
218 }
219
220 void Write2DArrayInt (short **Array, int Columns, int Rows,
FILE *fp)
221 {
222
223 int i;
224
225 for(i=0; i<Rows; i++){
226 fwrite(Array[i], sizeof(short),C olumns, fp);
227 }
228 /*fclose(fp);*/
229 }
230
231 void free_2D_float(f loat **pntr)
232 {
233
234 free(pntr[0]);
235
236 free(pntr);
237 }
238
239 void free_2D_Int(sho rt **pntr)
240 {
241
242 free(pntr[0]);
243
244 free(pntr);
245 }
Comment