/*---------------------------------------------------------------
 *
 *                          mask_cat_pub2.c
 *
 *---------------------------------------------------------------
 *
 * Description:
 * ===========
 *   Reads a mask file and a catalog, and removes objects 
 *   in the masked regions.
 *
 * Revision history:
 * ================
 *   created  Nov 14  2003  Shimasaku
 *
 *---------------------------------------------------------------
*/

#define   LINMAX   500
#define   NDATA   1000

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

/*---------------------------------------------------------------*/

main( argc, argv )

int argc;
char *argv[];

{

   FILE   *fp_in1, *fp_in2, *fp_out;

   char   line[LINMAX], b_or_c[NDATA][100], output[200], 
          ctemp1[100];

   long   i, n_mask, i_b_or_c[NDATA], n_line, itemp1;

   double xx, yy, 
          xc[NDATA], yc[NDATA], 
          x_width[NDATA], y_width[NDATA], radius[NDATA], 
          del_x, del_y, 
          ftemp1;

   /*---- get argv[] --------------------------------------------*/

   if( argc != 3 ) {
      (void) printf("usage: mask_cat_pub2  mask_sdf_pub2.dso  ");
      (void) printf("catalog\n");
      (void) exit(-1);
      }

   sprintf( output, "%s.mask", argv[2] );

   printf("\n");
   printf(" Mask file is        %s.\n", argv[1] );
   printf(" Input catalog is    %s.\n", argv[2] );
   printf(" Output catalog is   %s.\n", output );


   /*---- open output file --------------------------------------*/ 

   if( (fp_out = fopen( output, "w" )) == NULL ) {
      (void) printf(" ERROR: Can't open %s.\n", output );
      (void) exit(-1);
      }

   /*---- open mask file and read mask data ---------------------*/ 

   if( (fp_in1 = fopen( argv[1], "r" )) == NULL ) {
      (void) printf(" ERROR: Can't open %s.\n", argv[1] );
      (void) exit(-1);
      }

   i = 0;
   while( fgets( line, LINMAX, fp_in1 ) != NULL ) { 
      (void) sscanf( line, "%s %lf %lf %lf %s", 
                    b_or_c[i], &xc[i], &yc[i], &ftemp1, ctemp1 );

      /* circular masks */
      if( b_or_c[i][0] == 'C' || b_or_c[i][0] == 'c' || 
          b_or_c[i][1] == 'C' || b_or_c[i][1] == 'c' ) {
         i_b_or_c[i] = 1;
         radius[i] = ftemp1;
         }

      /* boxy masks */
      if( b_or_c[i][0] == 'B' || b_or_c[i][0] == 'b' || 
          b_or_c[i][1] == 'B' || b_or_c[i][1] == 'b' ) {
         i_b_or_c[i] = 2;
         x_width[i] = ftemp1;
         y_width[i] = atof(ctemp1);
         }

      i += 1;
      }

   (void) fclose(fp_in1);

   n_mask = i;

   printf(" Number of mask regions is %4d.\n", n_mask );
   printf("\n");

   /*---- read catalog and remove objects in masked regions -----*/ 

   if( (fp_in2 = fopen( argv[2], "r" )) == NULL ) {
      (void) printf(" ERROR: Can't open %s.\n", argv[2] );
      (void) exit(-1);
      }

   while( fgets( line, LINMAX, fp_in2 ) != NULL ) { 

      /* skip header region of *.cat file */
      if( line[0] == '#' ) {
         goto end;
         }
#if 0
printf(" %s\n", line );
#endif

      (void) sscanf( line, "%d %lf %lf", &itemp1, &xx, &yy );

      /* reject object in masked regions */
      for( i=0; i<n_mask; i++ ) {

         /* circular masks */
         if( i_b_or_c[i] == 1 ) {
            del_x = xx - xc[i];
            del_y = yy - yc[i];
            if( del_x*del_x + del_y*del_y <= radius[i]*radius[i] ) {
               goto end;
               }
            }

         /* boxy masks */
         if( i_b_or_c[i] == 2 ) {
            if( fabs(xx - xc[i]) <= 0.5*x_width[i] && 
                fabs(yy - yc[i]) <= 0.5*y_width[i] ) {
               goto end;
               }
            }
         }

      n_line = sizeof(line);
      line[n_line] = '\0';

      /* output masked catalog */
      fprintf( fp_out, "%s", line );

#if 0
      /* output *.dso file */
      fprintf( fp_out, "CIRCLE( %8.1f %8.1f 3 )\n", xx, yy );
#endif

end:;
      }

   (void) fclose(fp_in2);
   (void) fclose(fp_out);

}

