#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/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;
   int           SWAPBYTES;
  
   if(argc<2)
    {
      fprintf(stderr,"Use: %s AMIGA_file\n",*argv);
      exit(1);
    }

   printf("===============================================\n");
   printf("  Read AMIGA binary and convert to ASCII file\n");
   printf("===============================================\n");

   strcpy(outdata,argv[1]);
   strcat(outdata,".ascii");
	  
   if((fpin=fopen(argv[1],"rb"))==NULL)
    {
      printf("I cannot open %s\n", argv[1]);
      exit(1);
    }
   
   if((fpout=fopen(outdata,"w"))==NULL)
    {
      printf("I cannot open %s\n", outdata);
      exit(1);
    }
   
   /* read AMIGA header */
   read_amiga_header(fpin, &io, &SWAPBYTES);
   
  /* print some information */
  printf("\nsome 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("pmass            = %g\n",io.header.pmass);   
  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("B_init           = %g\n",io.header.B_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,"#%ld\n",io.header.no_part);
  fprintf(fpout,"#%g\n", io.header.boxsize);
  fprintf(fpout,"#%g\n", io.header.pmass);
  fprintf(fpout,"#%g\n", io.header.omega0);
  fprintf(fpout,"#%g\n", io.header.lambda0);
  fprintf(fpout,"#%g\n", io.header.omegab);
  fprintf(fpout,"#%g\n", io.header.gamma);
  fprintf(fpout,"#%g\n", io.header.H_frac);
  fprintf(fpout,"#%g\n", io.header.T_init);
  fprintf(fpout,"#%g\n", io.header.B_init);
  fprintf(fpout,"#%g\n", 1.0/io.header.a_initial-1.0,io.header.a_initial);
  fprintf(fpout,"#%g\n", 1.0/io.header.a_current-1.0,io.header.a_current);
  fprintf(fpout,"#%d\n",io.header.no_timestep);
#endif
   
  x_fac  = io.header.boxsize;
  v_fac  = io.header.boxsize * H0 / io.header.a_current;
  m_fac  = io.header.pmass;
   
  if(io.header.multi_mass == 1)
	 BUFSIZE = 7;
  else
	 BUFSIZE = 6;

  v_mean = 0.0;
  for(i=0;i<io.header.no_part;i++)
   {
	 
	 if(io.header.double_precision == 1)
	  {		  
		for(j=0; j<BUFSIZE; j++)
		   ReadDouble(fpin,&(dw[j]),SWAPBYTES);
	  }
	 else /* double precision */
	  {
		for(j=0; j<BUFSIZE; j++)
		 {
		   ReadFloat(fpin,&(fw[j]),SWAPBYTES);
		   dw[j] = (double) fw[j];
		 }
	  }
     
	 /* scale coordinates to Mpc/h */
	 x  = dw[0]*x_fac;
	 y  = dw[2]*x_fac;
	 z  = dw[4]*x_fac;
	 
    /* scale velocities to km/sec */
	 vx = dw[1]*v_fac;
	 vy = dw[3]*v_fac;
	 vz = dw[5]*v_fac;

    /* scale masses to Msun/h */
	 m  = dw[6]*m_fac;
	 
	 v_mean += sqrt(pow2(vx) + pow2(vy) + pow2(vz));
	 
    if(io.header.multi_mass == 1)
       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);
	 
   } /* npart-loop */
	 
  fclose(fpin);
  fclose(fpout);
	 
  fprintf(stderr,"\nmean velocity     = %g\n\n", v_mean/(double)io.header.no_part);

  fprintf(stderr,"wrote ASCII file:  %s\n",outdata);

}

