#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "../src/param.h"
#include "../src/tdef.h"
#include "../src/common.c"

#include "../src/libio_serial/io_serial.h"
#include "../src/libutility/utility.h"

int main(argc,argv)
int argc;
char **argv;
{
   long unsigned i, BUFSIZE;
   int           j;
   double        dw[7], dx[3], dv[3], dm, x,y,z,vx,vy,vz,m;
   float         fw[7], fx[3], fv[3], fm;
   double        v_mean;
   double        x_fac, v_fac, m_fac;
   char          outdata[MAXSTRING];
   FILE          *fpin, *fpout;
   float         frac;
   int           iseed, SWAPBYTES;
   
   if(argc<4)
    {
      fprintf(stderr,"Use: %s AMIGA_file fraction iseed\n",*argv);
      exit(1);
    }

   printf("=======================================================================\n");
   printf("  Read AMIGA binary and write a random fraction of it to an ASCII file\n");
   printf("=======================================================================\n");

   /*
   printf("Please give fraction of particles:      ");
   scanf("%lf",&frac);
   printf("%g\n",frac);
   printf("Please give random seed:                ");
   scanf("%d",&iseed);
   printf("%d\n",iseed);
    */
   
   frac  = (float) atof(argv[2]);
   iseed = (int)   atoi(argv[3]);

   strcpy(outdata,argv[1]);
   strcat(outdata,".asciiSample");
   fprintf(stderr,"writing ASCII file: %s\n\n",outdata);
	  
   if((fpout=fopen(outdata,"w"))==NULL)
    {
      printf("I cannot open %s\n", outdata);
      exit(1);
    }
   
   /*-------------------------------------------------------
    * read simulation raw data
    *-------------------------------------------------------*/
   input(argv[1]);
   
  /* print some information */
  printf("some information about input file:\n");
  printf("==================================\n");
  printf("%s\n\n",io.header.header);
  printf("multi-mass       = %d\n",io.header.multi_mass);
  printf("double precision = %d\n",io.header.double_precision);
  printf("no_part          = %ld\n",io.header.no_part);
  printf("no_vpart         = %g\n",io.header.no_vpart);
  printf("boxsize          = %g\n",io.header.boxsize);
  printf("omega0           = %g\n",io.header.omega0);
  printf("omegab           = %g\n",io.header.omegab);
  printf("lambda0          = %g\n",io.header.lambda0);   
  printf("gamma            = %g\n",io.header.gamma);
  printf("H_frac           = %g\n",io.header.H_frac);
  printf("T_init           = %g\n",io.header.T_init);
  printf("z_initial        = %g (a=%lf)\n",
		 1.0/io.header.a_initial-1.0,io.header.a_initial);
  printf("z_current        = %g (a=%lf)\n",
		 1.0/io.header.a_current-1.0,io.header.a_current);
  printf("no_timestep      = %d\n",io.header.no_timestep);
   
#ifdef HEADER 
  fprintf(fpout,"#%s\n",io.header.header);
  fprintf(fpout,"#no_part     = %ld\n",io.header.no_part);
  fprintf(fpout,"#omega0      = %g\n", io.header.omega0);
  fprintf(fpout,"#lambda0     = %g\n", io.header.lambda0);
  fprintf(fpout,"#omegab      = %g\n", io.header.omegab);
  fprintf(fpout,"#gamma       = %g\n", io.header.gamma);
  fprintf(fpout,"#H_frac      = %g\n", io.header.H_frac);
  fprintf(fpout,"#T_init      = %g\n", io.header.T_init);
  fprintf(fpout,"#boxsize     = %g\n", io.header.boxsize);
  fprintf(fpout,"#pmass       = %g\n", io.header.pmass);
  fprintf(fpout,"#t_unit      = %g\n", io.header.t_unit);
  fprintf(fpout,"#z_initial   = %g (a=%g)\n",
          1.0/io.header.a_initial-1.0,io.header.a_initial);
  fprintf(fpout,"#z_current   = %g (a=%g)\n",
          1.0/io.header.a_current-1.0,io.header.a_current);
  fprintf(fpout,"#no_timestep = %d\n",io.header.no_timestep);
#endif
  
  x_fac  = io.header.boxsize;
  v_fac  = io.header.boxsize * H0 / io.header.a_current;
#ifdef XDOT
  v_fac  = io.header.boxsize * H0 / io.header.a_current / io.header.a_current;
#endif
  m_fac  = io.header.pmass;
   
  v_mean = 0.0;
  for(i=0;i<io.header.no_part;i++)
   {
	 
	 /* scale coordinates to Mpc/h */
	 x  = (io.fst_part+i)->pos[X]*x_fac;
	 y  = (io.fst_part+i)->pos[Y]*x_fac;
	 z  = (io.fst_part+i)->pos[Z]*x_fac;
	 
    /* scale velocities to km/sec */
	 vx = (io.fst_part+i)->mom[X]*v_fac;
	 vy = (io.fst_part+i)->mom[Y]*v_fac;
	 vz = (io.fst_part+i)->mom[Z]*v_fac;

    /* scale masses to Msun/h */
#ifdef MULTIMASS
	 m  = (io.fst_part+i)->weight*m_fac;
#else
    m  = m_fac;
#endif
	 
	 v_mean += sqrt(pow2(vx) + pow2(vy) + pow2(vz));
	 
    if(ran3(&iseed) < frac)
      {
#ifdef MULTIMASS
          fprintf(fpout,"%g %g %g %g %g %g %g\n",x,y,z,vx,vy,vz,m);
#else
          fprintf(fpout,"%g %g %g %g %g %g\n",x,y,z,vx,vy,vz);
#endif
      }
	 
   } /* npart-loop */
	 
  fclose(fpout);
	 
  printf("\nmean velocity = %g\n", v_mean/(double)io.header.no_part);
}

