#include <stdio.h>
#include <stdlib.h>
//#include <math.h>

#include "limits.h"

double get_ref_frame_aperture_from_log_file(){
 char char_MUSOR[512];
 double ap;
 FILE *logfile;
 logfile=fopen("vast_image_details.log","r");
 fscanf(logfile,"%s %s %s %s %s %s %s %s %lf %s", char_MUSOR, char_MUSOR, char_MUSOR, char_MUSOR, char_MUSOR, char_MUSOR, char_MUSOR, char_MUSOR, &ap, char_MUSOR);
 fclose(logfile);
 return ap;
}

int main(){
 FILE *candidates_transients_lst;
 double ap=get_ref_frame_aperture_from_log_file();
 char outfilename[OUTFILENAME_LENGTH];
 char system_command[1024+OUTFILENAME_LENGTH];

 FILE *ref_frame_sextractor_cat;
 int n,flag;
 double flux,flux_err,mag,mag_err,x,y,x_ref,y_ref;
 
 double a_a,a_a_err,a_b,a_b_err;
 double class_star;

 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"no transient candidates found!\n");
  return 1;
 }
 fprintf(stderr,"Removing blends from candidates-transients.lst ... ");
 
 system("cp candidates-transients.lst candidates-transients.tmp_1");
 
 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"ERROR: Can't open candidates-transients.lst \n");
  return 1;
 }
 while(-1<fscanf(candidates_transients_lst,"%s %s %s %s %s %s %lf %lf", outfilename, system_command, system_command, system_command, system_command , system_command, &x_ref, &y_ref)){
  ref_frame_sextractor_cat=fopen("ref_frame_sextractor.cat","r");
  while(-1<fscanf(ref_frame_sextractor_cat,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %d %lf", &n, &flux, &flux_err, &mag, &mag_err, &x, &y, &a_a, &a_a_err, &a_b, &a_b_err, &flag, &class_star)){
   if( (x-x_ref)*(x-x_ref)+(y-y_ref)*(y-y_ref)<ap*ap/4.0 ){
    sprintf(system_command,"grep -v %s candidates-transients.tmp_1 > candidates-transients.tmp_2 ; mv candidates-transients.tmp_2 candidates-transients.tmp_1",outfilename);
    system(system_command);
    break;
   }
  }
  fclose(ref_frame_sextractor_cat);
 }
 
 fclose(candidates_transients_lst);

 system("mv candidates-transients.tmp_1 candidates-transients.lst");

 
 /* Check for nearby bright stars */ 
 /* 1st pass */
 system("sex -c bright_star_blend_check_1.5.sex -PHOT_APERTURES `head -n 1 vast_image_details.log | awk '{print $9}'` `grep \"Ref.  image:\" vast_summary.log | awk '{print $6}'`");
 system("cp candidates-transients.lst candidates-transients.tmp_1");
 
 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"ERROR: Can't open candidates-transients.lst \n");
  return 1;
 }
 while(-1<fscanf(candidates_transients_lst,"%s %s %s %s %s %s %lf %lf", outfilename, system_command, system_command, system_command, system_command , system_command, &x_ref, &y_ref)){
  ref_frame_sextractor_cat=fopen("test.cat","r");
  while(-1<fscanf(ref_frame_sextractor_cat,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %d %lf", &n, &flux, &flux_err, &mag, &mag_err, &x, &y, &a_a, &a_a_err, &a_b, &a_b_err, &flag, &class_star)){
   if( (x-x_ref)*(x-x_ref)+(y-y_ref)*(y-y_ref)<MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*ap*ap ){
    sprintf(system_command,"grep -v %s candidates-transients.tmp_1 > candidates-transients.tmp_2 ; mv candidates-transients.tmp_2 candidates-transients.tmp_1",outfilename);
    system(system_command);
    break;
   }
  }
  fclose(ref_frame_sextractor_cat);
 }
 
 fclose(candidates_transients_lst);

 system("mv candidates-transients.tmp_1 candidates-transients.lst");

 /* 2nd pass */
 system("sex -c bright_star_blend_check_3.0.sex -PHOT_APERTURES `head -n 1 vast_image_details.log | awk '{print $9}'` `grep \"Ref.  image:\" vast_summary.log | awk '{print $6}'`");
 system("cp candidates-transients.lst candidates-transients.tmp_1");
 
 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"ERROR: Can't open candidates-transients.lst \n");
  return 1;
 }
 while(-1<fscanf(candidates_transients_lst,"%s %s %s %s %s %s %lf %lf", outfilename, system_command, system_command, system_command, system_command , system_command, &x_ref, &y_ref)){
  ref_frame_sextractor_cat=fopen("test.cat","r");
  while(-1<fscanf(ref_frame_sextractor_cat,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %d %lf", &n, &flux, &flux_err, &mag, &mag_err, &x, &y, &a_a, &a_a_err, &a_b, &a_b_err, &flag, &class_star)){
   if( (x-x_ref)*(x-x_ref)+(y-y_ref)*(y-y_ref)<MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*ap*ap ){
    sprintf(system_command,"grep -v %s candidates-transients.tmp_1 > candidates-transients.tmp_2 ; mv candidates-transients.tmp_2 candidates-transients.tmp_1",outfilename);
    system(system_command);
    break;
   }
  }
  fclose(ref_frame_sextractor_cat);
 }
 
 fclose(candidates_transients_lst);

 system("mv candidates-transients.tmp_1 candidates-transients.lst");

 /* 3rd pass - detect only the brightest stars and clean wider area around them */
 system("sex -c bright_star_blend_check_7.0.sex -PHOT_APERTURES `head -n 1 vast_image_details.log | awk '{print $9}'` `grep \"Ref.  image:\" vast_summary.log | awk '{print $6}'`");
 system("cp candidates-transients.lst candidates-transients.tmp_1");
 
 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"ERROR: Can't open candidates-transients.lst \n");
  return 1;
 }
 while(-1<fscanf(candidates_transients_lst,"%s %s %s %s %s %s %lf %lf", outfilename, system_command, system_command, system_command, system_command , system_command, &x_ref, &y_ref)){
  ref_frame_sextractor_cat=fopen("test.cat","r");
  while(-1<fscanf(ref_frame_sextractor_cat,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %d %lf", &n, &flux, &flux_err, &mag, &mag_err, &x, &y, &a_a, &a_a_err, &a_b, &a_b_err, &flag, &class_star)){
   if( (x-x_ref)*(x-x_ref)+(y-y_ref)*(y-y_ref)<2*2*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*ap*ap ){
    sprintf(system_command,"grep -v %s candidates-transients.tmp_1 > candidates-transients.tmp_2 ; mv candidates-transients.tmp_2 candidates-transients.tmp_1",outfilename);
    system(system_command);
    break;
   }
  }
  fclose(ref_frame_sextractor_cat);
 }
 
 fclose(candidates_transients_lst);

 system("mv candidates-transients.tmp_1 candidates-transients.lst");

 /* 4th pass - faintest stars and clean small area around them */
 system("sex -c bright_star_blend_check_0.7.sex -PHOT_APERTURES `head -n 1 vast_image_details.log | awk '{print $9}'` `grep \"Ref.  image:\" vast_summary.log | awk '{print $6}'`");
 system("cp candidates-transients.lst candidates-transients.tmp_1");
 
 candidates_transients_lst=fopen("candidates-transients.lst","r");
 if( NULL==candidates_transients_lst ){
  fprintf(stderr,"ERROR: Can't open candidates-transients.lst \n");
  return 1;
 }
 while(-1<fscanf(candidates_transients_lst,"%s %s %s %s %s %s %lf %lf", outfilename, system_command, system_command, system_command, system_command , system_command, &x_ref, &y_ref)){
  ref_frame_sextractor_cat=fopen("test.cat","r");
  while(-1<fscanf(ref_frame_sextractor_cat,"%d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %d %lf", &n, &flux, &flux_err, &mag, &mag_err, &x, &y, &a_a, &a_a_err, &a_b, &a_b_err, &flag, &class_star)){
   if( (x-x_ref)*(x-x_ref)+(y-y_ref)*(y-y_ref)<0.5*0.5*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*MIN_DISTANCE_BETWEEN_STARS_IN_APERTURE_DIAMS*ap*ap ){
    sprintf(system_command,"grep -v %s candidates-transients.tmp_1 > candidates-transients.tmp_2 ; mv candidates-transients.tmp_2 candidates-transients.tmp_1",outfilename);
    system(system_command);
    break;
   }
  }
  fclose(ref_frame_sextractor_cat);
 }
 
 fclose(candidates_transients_lst);

 system("mv candidates-transients.tmp_1 candidates-transients.lst");

  
 fprintf(stderr,"done!  =)\n\n");

 return 0;
}
