#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "rtbp.h"
#include "ctants.h"

#define MXL 128

int main (int argc, char *argv[]) {
   int nt;
   double mu=MU, r0, v0, alf, dstM, z0[4], zf[4], pmax;
   char sr0[MXL], sv0[MXL], salf[MXL], snt[MXL];
   FILE *fp;
#define FITXTRJ argv[2]
#define NARGS 3
   if (argc<NARGS || sscanf(argv[1], "%lf", &pmax)!=1) {
      fprintf(stderr,"%s pmax fitxtrj\n", argv[0]);
      return -1;
   }
   while (scanf("%s %s %s %s",sr0,sv0,salf,snt)==4) {
      if ( sr0[0]!='-')  assert(  sscanf(sr0,"%lf",&r0)==1);
      if ( sv0[0]!='-')  assert(  sscanf(sv0,"%lf",&v0)==1);
      if (salf[0]!='-')  assert(sscanf(salf,"%lf",&alf)==1);
      if ( snt[0]!='-')  assert(   sscanf(snt,"%d",&nt)==1);
      rva2z(mu,r0,v0,cos(alf),sin(alf),z0);
      fp=fopen(FITXTRJ,"a");
      proptraj(mu,nt,z0,zf,NULL,pmax,fp);
      fclose(fp);
      dstM=((mu-1)-zf[0])*AMOON;
      dstM+=( dstM<0 ) ? RMOON : -RMOON;
      printf("%.16g %.16g %.3f\n", alf, zf[2], dstM);
   }
   return 0;
}
