Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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)inputDSMfile名.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 file
- if((fpr=fopen(argv[1],"r"))==NULL)
- {
- printf("ファイルのオープンに失敗しました。プログラムを終了します。\n");
- exit(1);
- }
- //write file
- if((fpw=fopen(argv[2],"w"))==NULL)
- {
- fprintf(stderr,"DSM by GSI データファイル.raw\n");
- exit(1);
- }
- /*****************************************************
- データの読み取り書き込み
- ******************************************************/
- /* データ処理 */
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement