Advertisement
HAYATOTAKAGAKI

hyoukoude-tanoprogram

Oct 8th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int main(int argc,char *argv[])
  7. {
  8.        
  9.     /*****************************************************
  10.          1.Definition of Variables
  11.          ******************************************************/
  12.     FILE *fpr,*fpw;  //file pointer for input&output file
  13.     int no,pno;
  14.     float x,y,z;
  15.         float *file_sht,*file_dsm;
  16.         int *file_ct;
  17.         int i,j;
  18.         int k,l;
  19.         float ulx,uly,reso; //ulx:upper light pixel, uly:
  20.         int ht,wt; //height,width
  21.         float dn1,dn3;
  22.         int dn2;
  23.        
  24.        
  25.     /*****************************************************
  26.          2.command line arguments processing
  27.          ******************************************************/
  28.     if(argc!=8)
  29.     {
  30.         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]);
  31.         exit(1);
  32.     }
  33.         printf("OPEN FILE NAME:%s\n",argv[1]);
  34.        
  35.         ulx=atof(argv[3]);
  36.         uly=atof(argv[4]);
  37.         reso=atof(argv[5]); //0.5
  38.         ht=atoi(argv[6]);//3000
  39.         wt=atoi(argv[7]);//4000
  40.        
  41.         /**********************************************************************************
  42.          **********************************************************************************
  43.          3.   decision the location of the memory. MEMORY ALLOCATION (malloc-sizeof-free)
  44.          **********************************************************************************
  45.          *************************************************************************************/
  46.     file_sht= (float *)malloc(ht*wt*sizeof(float)); //sum of height information
  47.         if(file_sht==NULL)
  48.         {
  49.                 printf("Error on malloc");
  50.                 exit(1);
  51.         }
  52.        
  53.         //initializing
  54.         for(k=0;k<ht;k++)
  55.         {
  56.                 for(l=0;l<wt;l++)
  57.                 {
  58.                         file_sht[k*wt+l]=0;
  59.                 }
  60.         }
  61.        
  62.        
  63.         file_ct= (int *)malloc(wt*ht*sizeof(int)); //sum of count
  64.         if(file_ct==NULL)
  65.         {
  66.                 printf("Error on malloc");
  67.                 exit(1);
  68.         }
  69.        
  70.         //initializing
  71.         for(k=0;k<ht;k++)
  72.         {
  73.                 for(l=0;l<wt;l++)
  74.                 {
  75.                         file_ct[k*wt+l]=0;
  76.                 }
  77.         }
  78.        
  79.         file_dsm= (float *)malloc(wt*ht*sizeof(float)); //for DSM image
  80.         if(file_dsm==NULL)
  81.         {
  82.                 printf("Error on malloc");
  83.                 exit(1);
  84.         }
  85.        
  86.         //initializing
  87.         for(k=0;k<ht;k++)
  88.         {
  89.                 for(l=0;l<wt;l++)
  90.                 {
  91.                         file_dsm[k*wt+l]=0;
  92.                 }
  93.         }
  94.        
  95.         /**********************************************************************************
  96.          **********************************************************************************
  97.          4.  FILE OPEN + Binary File Input
  98.          **********************************************************************************
  99.          *************************************************************************************/
  100.        
  101.     // open input file
  102.     if((fpr=fopen(argv[1],"r"))==NULL)
  103.     {
  104.         printf("ファイルのオープンに失敗しました。プログラムを終了します。\n");
  105.         exit(1);
  106.     }
  107.        
  108.         //write file
  109.     if((fpw=fopen(argv[2],"w"))==NULL)
  110.         {
  111.                 fprintf(stderr,"DSM by GSI データファイル.raw\n");
  112.                 exit(1);
  113.     }
  114.        
  115.     /*****************************************************
  116.          データの読み取り書き込み
  117.          ******************************************************/        
  118.     /* データ処理 */
  119.     while (fscanf(fpr, "%d,%f,%f,%f,%d", &no,&x,&y,&z,&pno) != EOF)
  120.     {
  121.                 //printf("pno=%d\n",pno);
  122.                 if(pno==1)
  123.                 {
  124.                         //printf("oi\n");
  125.                         if(x-ulx!=0)
  126.                         {
  127.                                 //printf("oi2\n");
  128.                                 j=(int)floor((x-ulx)/reso);//width direction
  129.                         }
  130.                        
  131.                        
  132.                         else
  133.                         {
  134.                                 //printf("oi3\n");
  135.                                 j=0;
  136.                         }
  137.                        
  138.                         if(y-uly!=0)
  139.                         {
  140.                                 //printf("oi4\n");
  141.                                 i=(int)floor((uly-y)/reso); //height direction
  142.                         }
  143.                        
  144.                         else
  145.                         {
  146.                                 i=0;
  147.                         }
  148.                         //printf("oi5\n");
  149.                         //printf("i=%d?t,j=%d?t,z=%f\n",i,j,z);
  150.                         file_sht[i*wt+j]=file_sht[i*wt+j]+z;
  151.                         //printf("oi6\n");
  152.                         file_ct[i*wt+j]=file_ct[i*wt+j]+1;
  153.                         //printf("oi7\n");
  154.                         //fprintf(fpw, "%f\t%f\t%f\n",x,y,z);
  155.                         //printf("x:%f y:%f z:%f\n", x,y,z);
  156.                 }
  157.     }
  158.        
  159.         for(i=0;i<ht;i++)
  160.         {
  161.                 for(j=0;j<wt;j++)
  162.                 {
  163.                         dn1=file_sht[i*wt+j];//float
  164.                         dn2=file_ct[i*wt+j];//int
  165.                        
  166.                         if(dn2!=0)
  167.                         {
  168.                                
  169.                                 dn3=dn1/(float)dn2; //float
  170.                                 //printf("dn1=%f\tdn2=height=%f\n",dn3);
  171.                         }
  172.                         else
  173.                         {
  174.                                 dn3=0;
  175.                         }
  176.                         fwrite(&dn3,sizeof(float),1,fpw); /*m単位標高データの書き込み*/
  177.                 }
  178.         }
  179.    
  180.     /* ファイル・クローズ */
  181.     fclose(fpr);
  182.         fclose(fpw);
  183.         free(file_ct);
  184.         free(file_sht);
  185.         free(file_dsm);
  186.        
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement