vai
  #read input file
  data "uploads/file.dat"
  #data sat26867.txt
  lines 1 1
  read {yy 1 mm 2 dd 3}
  lines 2 0
  read {hr 1 min 2 sec 3 ra 4 dec 5} 
  
  define giorno (dd[0])
  define mese (mm[0])
  define anno (yy[0])
 
  #!rm convertidata.sh
  #write  convertidata.sh "year="$anno 
  #write + convertidata.sh "month="$mese 
  #write + convertidata.sh "day="$giorno 
  #write + convertidata.sh "echo data:  $day $month  $year"
  #!chmod ugo+x convertidata.sh
  #!convertidata.sh
  #echo data: $giorno $mese $anno 
  
  
  #compute JD from input data with php script
  !rm  "output/calcolaJD" 
  write + "output/calcolaJD" "<?php"
  write + "output/calcolaJD" "$jd = GregorianToJD("$mese"," $giorno"," $anno");"
  write + "output/calcolaJD" "echo \"$jd\n\";"
  write + "output/calcolaJD" "$gregorian = JDToGregorian($jd);"
  write + "output/calcolaJD" "echo \"$gregorian\n\";"
  write + "output/calcolaJD" "?>"
  !rm "output/JD.dat" 
  !php "output/calcolaJD" > "output/JD.dat"  

  data "output/JD.dat" 
  lines 1 1 
  read {mioJD 1}
  print '%7.6f' {mioJD}



 #compute ut time, decimal format
 set tempout = hr+min/60+sec/3600

 define n dimen(ra)
 echo $n
 define nn ($n)
 set  dimen(diffRA)=$nn
 set  dimen(diffRAcheck)=$nn
 set  dimen(diffDEC)=$nn
 set  dimen(dt)=$nn
 set  dimen(jdfull)=$nn

               #computing delta RA and DEC (not yet per hour)
 do i=0,$n-2,1{
               #set dt[$i+1]=tempout[$i+1]-tempout[$i]

               set diffRA[$i+1]=(ra[$i+1]-ra[$i])
               #I assume that if delta RA is < -350, the object crossed RA=360 (increasing)  
               # I assume that if delta RA is > 350, the object crossed RA=360 (decreasing)
               define checkRA (ra[$i+1]-ra[$i])
               #echo $checkRA
               if($checkRA<-350){set diffRAcheck[$i+1]=($checkRA+360)
                                 echo RA crossing 360 deg, increasing}
               if($checkRA<-350){set diffRA[$i+1]=($checkRA+360)}
               if($checkRA>350){set diffRAcheck[$i+1]=($checkRA-360)
				echo RA crossing 360 deg, dcreasing}
               if($checkRA>350){set diffRA[$i+1]=($checkRA-360)}

               set diffDEC[$i+1]=(dec[$i+1]-dec[$i])  

	         }

  #computing JD with fraction of the day
  do i=0,$n-1,1{             
               set jdfull[$i]=mioJD+(tempout[$i]-12)/24
               #I assume that if 12 <= ut <24, MJD is the same as JD in input
               #I assume that if 0 <= ut <12, MJD is JD input + 1
               #if(tempout[$i]>=12 && tempout[$i]<24){set mjdfull[$i]=mioJD+(tempout[$i])/24-2450000.5}
               #if(tempout[$i]>=0  && tempout[$i]<12){set mjdfull[$i]=mioJD+(tempout[$i]+24)/24-2450000.5
               #                                     echo "cambio giorno"}  
                }

  # computing dt
  do i=0,$n-2,1{ 
               set dt[$i+1]=(jdfull[$i+1]-jdfull[$i])*24    
                 }

  #print{tempout ra dec diffRA diffDEC dt}
   
  #rejecting all data corresponding to delta t = 0 
   set dt1=dt if(dt>0)
   set hr1 = hr if(dt>0)
   set min1 = min if(dt>0)
   set sec1 = sec if(dt>0)
   set tempout1=tempout if(dt>0)
   set jdfull1=jdfull if(dt>0)
   set ra1=ra if(dt>0)
   set dec1=dec if(dt>0)
   set diffRA0=diffRA if(dt>0)
   set diffDEC0=diffDEC if(dt>0)
 
   #computing RA and DEC differentia velociti in arcsec/hr
   
   
   set diffRA1=diffRA0*3600/dt1
   set diffDEC1=diffDEC0*3600/dt1 
   #print{tempout1 ra1 dec1 diffRA1 diffDEC1 dt1}

   !rm  "output/dati1.dat" 
   print "output/dati1.dat"  '%12.6f %2.0f %2.0f %2.0f  %2.6f %3.8f %3.8f %3.8f %3.8f \n'  {jdfull1 hr1 min1 sec1 tempout1 ra1 dec1 diffRA1 diffDEC1 }
   !rm "output/output.txt" 
   !cat "output/JD.dat" "output/dati1.dat" > "output/output.txt" 
       
