Read Text to Binary

/***************************************************************************

lp2grid-1st pulse data source full set 1

DSM-

Airborne Lidar original Text data to Binary data

スクリーンショット 2013-02-15 13.19.47

 

For Saving DSM float data typed

read

ver. 1.0 2008/1/10

ver.1.1 2009/3/7

 

Important Command

*fscanf

1,-8001.06,-88498.56,74.41
2,-8002.38,-88498.56,74.51
3,-8004.14,-88498.53,74.20
4,-8005.39,-88498.54,74.47
5,-8006.64,-88498.55,74.92

*******************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

 

int main(int argc,char *argv[])

{

 

/*****************************************************

1.Definition of Variables

******************************************************/

FILE *fpr,*fpw;  //file pointer for input&output file

int no,pno;

float x,y,z;

float *file_sht,*file_dsm;

int *file_ct;

int i,j;

int k,l;

float ulx,uly,reso; //ulx:upper light pixel, uly:

int ht,wt; //height,width

float dn1,dn3;

int dn2;

 

 

/*****************************************************

2.command line arguments processing

******************************************************/

if(argc!=8)

{

fprintf(stderr,”Usage: %s (1)inputDSMfilename.txt\n(2)write FILENAME\n(3)ULX(m)\n(4)ULY(m)\n(5)resolution(m)\n(6)height(pxl)\n(7)width(pxl)\n”,argv[0]);

exit(1);

}

printf(“OPEN FILE NAME:%s\n”,argv[1]);

ulx=atof(argv[3]);

uly=atof(argv[4]);

reso=atof(argv[5]); //0.5

ht=atoi(argv[6]);//3000

wt=atoi(argv[7]);//4000

 

/**********************************************************************************

**********************************************************************************

3.   decision the location of the memory. MEMORY ALLOCATION (malloc-sizeof-free)

**********************************************************************************

*************************************************************************************/

file_sht= (float *)malloc(ht*wt*sizeof(float)); //sum of height information

if(file_sht==NULL)

{

printf(“Error on malloc”);

exit(1);

}

 

//initializing

for(k=0;k<ht;k++)

{

for(l=0;l<wt;l++)

{

file_sht[k*wt+l]=0;

}

}

file_ct= (int *)malloc(wt*ht*sizeof(int)); //sum of count

if(file_ct==NULL)

{

printf(“Error on malloc”);

exit(1);

}

//initializing

for(k=0;k<ht;k++)

{

for(l=0;l<wt;l++)

{

file_ct[k*wt+l]=0;

}

}

 

file_dsm= (float *)malloc(wt*ht*sizeof(float)); //for DSM image

if(file_dsm==NULL)

{

printf(“Error on malloc”);

exit(1);

}

 

//initializing

for(k=0;k<ht;k++)

{

for(l=0;l<wt;l++)

{

file_dsm[k*wt+l]=0;

}

}

/**********************************************************************************

**********************************************************************************

4.  FILE OPEN + Binary File Input

**********************************************************************************

*************************************************************************************/

// open input text file

if((fpr=fopen(argv[1],”r”))==NULL)

{

printf(“ファイルのオープンに失敗しました。プログラムを終了します。\n”);

exit(1);

}

//write binary file

if((fpw=fopen(argv[2],”w”))==NULL)

{

fprintf(stderr,”DSM by GSI データファイル.raw\n”);

exit(1);

}

/*****************************************************

Read Text Data

1,32000.00,-179291.24,-0.15,1
2,32000.00,-179193.10,0.00,1
3,32000.00,-179103.05,0.10,1
4,32000.00,-178919.26,0.33,1
5,32000.01,-179307.99,-1.08,1
6,32000.01,-179297.63,-0.61,1
7,32000.01,-179268.18,-1.37,1

******************************************************/

/* データ処理: Comma Separated */

while (fscanf(fpr, “%d,%f,%f,%f,%d”, &no,&x,&y,&z,&pno) != EOF)

{

//printf(“pno=%d\n”,pno);

if(pno==1)

{

//printf(“oi\n”);

if(x-ulx!=0)

{

//printf(“oi2\n”);

j=(int)floor((x-ulx)/reso);//width direction

}

else

{

//printf(“oi3\n”);

j=0;

}

if(y-uly!=0)

{

//printf(“oi4\n”);

i=(int)floor((uly-y)/reso); //height direction

}

else

{

i=0;

}

//printf(“oi5\n”);

//printf(“i=%d¥t,j=%d¥t,z=%f\n”,i,j,z);

file_sht[i*wt+j]=file_sht[i*wt+j]+z;

//printf(“oi6\n”);

file_ct[i*wt+j]=file_ct[i*wt+j]+1;

//printf(“oi7\n”);

//fprintf(fpw, “%f\t%f\t%f\n”,x,y,z);

//printf(“x:%f y:%f z:%f\n”, x,y,z);

}

}

for(i=0;i<ht;i++)

{

for(j=0;j<wt;j++)

{

dn1=file_sht[i*wt+j];//float

dn2=file_ct[i*wt+j];//int

if(dn2!=0)

{

dn3=dn1/(float)dn2; //float

//printf(“dn1=%f\tdn2=height=%f\n”,dn3);

}

else

{

dn3=0;

}

fwrite(&dn3,sizeof(float),1,fpw); /*m単位標高データの書き込み*/

}

}

/* ファイル・クローズ */

fclose(fpr);

fclose(fpw);

free(file_ct);

free(file_sht);

free(file_dsm);

}