- 积分
- 1968
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2011-10-16
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 倔强的葱头 于 2012-11-21 08:47 编辑
以下是源代码:
#include "lpj.h"
#define NSLICE 100000
#define NO_DATA -9999
#define USAGE "Usage: %s soilfile.txt coordfile.bin soilfile.bin\n"
typedef struct
{
Real lon,lat;
int soilcode;
} Soildata;
int main(int argc,char **argv)
{
FILE *file;
Coordfile *coordfile;
Coord resol={0.1,0.1};
Coord *coords;
int i,j,n,ncoord,nvalid;
char soilcode;
String line;
Soildata *soildata;
if(argc!=4)
{
fprintf(stderr,"Invalid number of arguments.\n"
USAGE,argv[0]);
return EXIT_FAILURE;
}
file=fopen(argv[1],"r");
if(file==NULL)
{
fprintf(stderr,"Error opening file '%s': %s.\n",argv[1],strerror(errno));
return EXIT_FAILURE;
}
/* skip first 12 lines in ASCII soildata file */
for(i=0;i<12;i++)
fgets(line,STRING_LEN,file);
n=0;
soildata=newvec(Soildata,NSLICE);
while(fscanf(file,"%lf,%lf,%d",&soildata[n].lon,&soildata[n].lat,&soildata[n].soilcode)==3)
{
if(soildata[n].soilcode!=NO_DATA)
{
n++;
if(n % NSLICE==0)
{
soildata=(Soildata *)realloc(soildata,(n+NSLICE)*sizeof(Soildata));
if(soildata==NULL)
{
fprintf(stderr,"Error allocating memory for %d soil data.\n",n+NSLICE);
return EXIT_FAILURE;
}
}
}
}
printf("Data read: %d\n",n);
fclose(file);
coordfile=opencoord(argv[2]);
if(coordfile==NULL)
{
fprintf(stderr,"Error opening file '%s': %s\n",argv[2],strerror(errno));
return EXIT_FAILURE;
}
ncoord=numcoord(coordfile);
coords=newvec(Coord,ncoord);
for(i=0;i<ncoord;i++)
readcoord(coordfile,coords+i,resol);
closecoord(coordfile);
file=fopen(argv[3],"wb");
if(file==NULL)
{
fprintf(stderr,"Error creating file '%s': %s\n",argv[3],strerror(errno));
return EXIT_FAILURE;
}
nvalid=0;
for(i=0;i<ncoord;i++)
{
soilcode=0;
for(j=0;j<n;j++)
if(coords.lon==soildata[j].lon && coords.lat==soildata[j].lat)
{
/*printf("%.2f %.2f %d\n",coords.lon,coords.lat,soildata[j].soilcode); */
soilcode=(char)soildata[j].soilcode;
if(soilcode>0)
nvalid++;
break;
}
fwrite(&soilcode,1,1,file);
if(j==n)
fprintf(stderr,"Soildata not found for cell (%.2f, %.2f)\n",
coords.lon,coords.lat);
}
printf("Valid soilcodes: %d\n",nvalid);
fclose(file);
return EXIT_SUCCESS;
} /* of 'main' */
我运行时老师出现以下错误:
|
-
错误
|