#include <math.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.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;
{
   char           outdata[1024], dummyline[1024], fstring[1024], *there_is_more;
   FILE          *fpin, *fpout;
   double        *Npart, *Mvir, *Xc, *Yc, *Zc, *VXc, *VYc, *VZc, *Rvir, *sigV, *Vmax, *Rmax, *Xmbp, *Ymbp, *Zmbp, mbp_offset, dummy;
   long           Nhalos, ihalo, jhalo;
   long unsigned *idx;
   if(argc<2)
     {
      fprintf(stderr,"Use: %s .SKID_halos.stats\n",*argv);
      exit(1);
     }
   
   printf("==================================================================\n");
   printf("  Read *.SKID_halos.stats file and convert to .AHF_halos format\n");
   printf("==================================================================\n");
   
   strcpy(outdata,argv[1]);
   strcat(outdata,"_halos");
   fprintf(stderr,"writing _halos file: %s\n\n",outdata);
	  
   if((fpin=fopen(argv[1],"r"))==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);
     }
   
   /* count number of halos */
   Nhalos = 0;
   while((there_is_more = fgets(fstring, 1024, fpin)) != NULL)
      Nhalos++;
   rewind(fpin);
   fprintf(stderr," o found %ld halos\n",Nhalos);
   
   /* allocate memory for halos */
   Npart  = (double *) calloc(Nhalos+1, sizeof(double));
   Mvir   = (double *) calloc(Nhalos, sizeof(double));
   Vmax   = (double *) calloc(Nhalos, sizeof(double));
   Rmax   = (double *) calloc(Nhalos, sizeof(double));
   Rvir   = (double *) calloc(Nhalos, sizeof(double));
   sigV   = (double *) calloc(Nhalos, sizeof(double));
   Xc     = (double *) calloc(Nhalos, sizeof(double));
   Yc     = (double *) calloc(Nhalos, sizeof(double));
   Zc     = (double *) calloc(Nhalos, sizeof(double));
   VXc    = (double *) calloc(Nhalos, sizeof(double));
   VYc    = (double *) calloc(Nhalos, sizeof(double));
   VZc    = (double *) calloc(Nhalos, sizeof(double));
   Xmbp   = (double *) calloc(Nhalos, sizeof(double));
   Ymbp   = (double *) calloc(Nhalos, sizeof(double));
   Zmbp   = (double *) calloc(Nhalos, sizeof(double));
   
   /* read halos from SKID stats-file */
   fprintf(stderr," o reading halos ...");
   for(ihalo=0; ihalo<Nhalos; ihalo++)
     {
      fgets(fstring, 1024, fpin);
      sscanf(fstring,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
             &dummy,         &(Npart[ihalo]), &(Mvir[ihalo]), &dummy,          &dummy,          &(Vmax[ihalo]), &dummy,          &dummy,          &(Rmax[ihalo]), &dummy,
             &(Rvir[ihalo]), &(sigV[ihalo]),  &(Xc[ihalo]),   &(Yc[ihalo]),    &(Zc[ihalo]),    &(VXc[ihalo]),  &(VYc[ihalo]),   &(VZc[ihalo]),   &(Xmbp[ihalo]), &(Ymbp[ihalo]),
             &(Zmbp[ihalo]));   
     }
   fprintf(stderr," done\n");   
   fclose(fpin);
   
   /* sort halos according to number of particles */
   fprintf(stderr," o sorting halos ...");
   idx = (long unsigned *)  calloc(Nhalos+1,   sizeof(long unsigned));
   indexx(Nhalos,Npart-1,idx-1);
   fprintf(stderr," done\n");   
   
   
   /* write halos to file */
   fprintf(stderr," o writing halos ...");
   fprintf(fpout,"#  npart(1)  nvpart(2)      Xc(3)          Yc(4)          Zc(5)      VXc(6)   VYc(7)   VZc(8)    Mvir(9)      Rvir(10) Vmax(11)   Rmax(12) sigV(13) lambda(14)      Lx(15)       Ly(16)       Lz(17)     a(18)     Eax(19)    Eay(20)    Eaz(21)     b(22)     Ebx(23)    Eby(24)    Ebz(25)     c(26)     Ecx(27)    Ecy(28)    Ecz(29) ovdens(30)    Redge(31) nbins(32)     Ekin(33)       Epot(34)   mbp_offset(35)  com_offset(36)   r2(37)      lambdaE(38)\n");
   for(jhalo=Nhalos-1; jhalo>=0; jhalo--)
     {
      ihalo      = idx[jhalo]-1;
      dummy      = 0.0;
      mbp_offset = 1000.*sqrt(pow2(Xc[ihalo]-Xmbp[ihalo])+pow2(Yc[ihalo]-Ymbp[ihalo])+pow2(Zc[ihalo]-Zmbp[ihalo]));      
      fprintf(fpout,"%10ld %10.0f %14.8f %14.8f %14.8f %8.2f %8.2f %8.2f %12.6g %10.2f %8.2f %10.2f %8.2f %10.6f %12.4g %12.4g %12.4g %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %10.6f %8.2f %12.4g %6d       %12.6g   %12.6g  %10.5f      %10.5f     %10.5f  %12.6f\n",
              (long)Npart[ihalo], Npart[ihalo], Xc[ihalo], Yc[ihalo], Zc[ihalo], VXc[ihalo], VYc[ihalo], VZc[ihalo], Mvir[ihalo], Rvir[ihalo]*1000., Vmax[ihalo], Rmax[ihalo]*1000., sigV[ihalo], dummy, dummy, dummy, dummy, dummy, dummy, dummy, dummy,  dummy, dummy, dummy, dummy,  dummy, dummy, dummy, dummy, dummy, dummy, (int)dummy, dummy, dummy, mbp_offset, dummy, dummy, dummy);
     }
   fprintf(stderr," done\n");   
   fclose(fpout);
}
