Пример #1
0
int
main (int argc, char *argv[])
{
  char *opt;

  char grism_file[MAXCHAR];
  char grism_file_path[MAXCHAR];

  char aper_file[MAXCHAR];
  char aper_file_path[MAXCHAR];

  char obj_PET_file[MAXCHAR];
  char obj_PET_file_path[MAXCHAR];

  char bck_PET_file[MAXCHAR];
  char bck_PET_file_path[MAXCHAR];

  char conf_file[MAXCHAR];
  char conf_file_path[MAXCHAR];

  char SPC_file[MAXCHAR];
  char SPC_file_path[MAXCHAR];

  char SPC_opt_file[MAXCHAR];
  char SPC_opt_file_path[MAXCHAR];

  char WHT_file[MAXCHAR];
  char WHT_file_path[MAXCHAR];

  char label[MAXCHAR];

  int i, index, dobck = 0, noflux = 1;

  object **oblist;

  FITScards      *cards;

  ap_pixel *obj_PET = NULL, *bck_PET = NULL;

  observation *obs;

  //tracestruct *trace;
  aperture_conf *conf;

  spectrum *obj_spec = NULL, *bck_spec = NULL, *sobj_spec = NULL;
  spectrum *resp;
  response_function *resp_func;
  calib_function *wl_calibration;

  fitsfile *OPET_ptr, *BPET_ptr;
  int f_status = 0;

  fitsfile *SPC_ptr, *SPC_opt_ptr, *WHT_ptr;

  gsl_matrix *weights;
  drzstamp     *modvar;

  int obj_aperID, obj_beamID, objindex;
  int bck_aperID, bck_beamID;

  char table[MAXCHAR], table_path[MAXCHAR];
  //char comment[FLEN_COMMENT];
  int empty;
  int drizzle;
  int quant_cont=0;
  int opt_weights=0;
  int for_grism=0;
  int smooth_conv=0;

  d_point smooth_params;

  double exptime;
  double sky_cps;

  drzstamp_dim  dimension;
  gsl_matrix *coverage;

  if (((argc < 3))
      || (opt = get_online_option ("help", argc, argv)))
    {
      fprintf (stdout,
	       "ST-ECF European Coordinating Facility\n"
	       "Copyright (C) 2002 Martin Kuemmel\n"
	       "aXe_PET2SPC Version %s:\n"
	       "             aXe task that produces 1-D, binned spectra using information\n"
	       "             contained in a OPET and a BPET. A 1-D spectrum is generated\n"
	       "             for each beam in each of both the OPET and the BPET files\n"
	       "             The binned background spectra of each beam (order) is then\n"
	       "             subtraced from the corresponding spectra form the OPET. Th\ne"
	       "             background subtraction (and reading a BPET altogether can be\n"
	       "             avoided by using the -noBPET option\n"
	       "             An SPC file, a multi-extension FITS file containing binned\n"
	       "             spectra is produced. Each extension (named after the beam ID,\n"
	       "             e.g. 11B for aperture (object) 11,beam (order) B) contains the\n"
	       "             following columns:\n"
	       "\n"
	       "              LAMBDA      ; the wavelength (in A)\n"
	       "              TCOUNT      ; the total number of counts (in DN)\n"
	       "              TERROR      ; the error in TERRORS (in DN)\n"
	       "              BCOUNT      ; the estimated number of counts from the\n"
	       "                            background (in DN)\n"
	       "              BERROR      ; the error in BCOUNTS (in DN)\n"
	       "              COUNT       ; the estimated number of counts from the\n"
	       "                            object (in DN)\n"
	       "              ERROR       ; the error in COUNTS (in DN)\n"
	       "              FLUX        ; the estimated flux (in erg/s/cm^2/A)\n"
	       "              FERROR      ; the error in FLUX (in erg/s/cm^s/A)\n"
	       "              WEIGHT      ; weight (in pixels)\n"
	       "\n"
	       "             Input FITS mages are looked for in $AXE_IMAGE_PATH\n"
	       "             aXe config file is looked for in $AXE_CONFIG_PATH\n"
	       "             All outputs are writen to $AXE_OUTPUT_PATH\n"
	       "\n"
	       "Usage:\n"
	       "     aXe_PET2SPC [g/prism image filename] [aXe config file name] [options]\n"
	       "\n"
	       "Options:\n"
	       "             -noBPET         - to disable the use of a BPET file\n"
	       "             -noflux         - to disable the flux calibration\n"
	       "             -drz            - use $AXE_DRIZZLE_PATH to locate the grism-, OAF-\n"
	       "                             - and PET-files instead of $AXE_IMAGE/OUTPUT_PATH\n"
	       "             -in_AF=[string] - overwrite the default input Aperture File name\n"
	       "             -OPET=[string]  - overwrite the default input Object PET file name\n"
	       "             -BPET=[string]  - overwrite the default input Background PET\n"
	       "                               file name\n"
	       "             -out_SPC=[string] - overwrite the default output SPC file name\n"
	       "\n"
	       "Example:\n"
	       "       ./aXe_PET2SPC slim_grism.fits SLIM.conf.A.0\n"
	       "\n",RELEASE);
      exit (1);
    }

  // make a general opening statement
  fprintf (stdout, "aXe_PET2SPC: Starting...\n");

  // get the name of the flt-file
  index = 0;
  strcpy (grism_file, argv[++index]);
  if ((opt = get_online_option ("drz", argc, argv)))
    {
      build_path (AXE_DRIZZLE_PATH, grism_file, grism_file_path);
      drizzle=1;
    }
  else
    {
      build_path (AXE_IMAGE_PATH, grism_file, grism_file_path);
      drizzle=0;
    }

  // get the name of the configuration file
  strcpy (conf_file, argv[++index]);
  build_path (AXE_CONFIG_PATH, conf_file, conf_file_path);

  // load the configuration file
  conf = get_aperture_descriptor (conf_file_path);

  // Determine where the various extensions are in the FITS file
  get_extension_numbers(grism_file_path, conf,conf->optkey1,conf->optval1);


  // Build aperture file name
  replace_file_extension (grism_file, aper_file, ".fits",
			  ".OAF", conf->science_numext);
  if (drizzle)
    build_path (AXE_DRIZZLE_PATH, aper_file, aper_file_path);
  else
    build_path (AXE_OUTPUT_PATH, aper_file, aper_file_path);

  // Build object PET file name
  replace_file_extension (grism_file, obj_PET_file, ".fits",
			  ".PET.fits", conf->science_numext);
  // make the total filename
  if (drizzle)
    build_path (AXE_DRIZZLE_PATH, obj_PET_file, obj_PET_file_path);
  else
    build_path (AXE_OUTPUT_PATH, obj_PET_file, obj_PET_file_path);


  // Build background PET file name
  replace_file_extension (grism_file, bck_PET_file, ".fits",
			  ".BCK.PET.fits", conf->science_numext);

  // make the total filename
  if (drizzle)
    build_path (AXE_DRIZZLE_PATH, bck_PET_file, bck_PET_file_path);
  else
    build_path (AXE_OUTPUT_PATH, bck_PET_file, bck_PET_file_path);

  // make a non-standard AF name if necessary
  if ((opt = get_online_option ("in_AF", argc, argv)))
    {
      strcpy(aper_file,opt);
      strcpy(aper_file_path,opt);
    }

  // make a non-standard PET name if necessary
  if ((opt = get_online_option ("OPET", argc, argv)))
    {
      strcpy(obj_PET_file,opt);
      strcpy(obj_PET_file_path,opt);
    }

  // make a non-standard BPET name if necessary
  if ((opt = get_online_option ("BPET", argc, argv)))
    {
      strcpy(bck_PET_file,opt);
      strcpy(bck_PET_file_path,opt);
    }

  // set the flagg for no background subtraction
  if ((opt = get_online_option ("noBPET",argc,argv)))
    {
      dobck = 0;
      strcpy(bck_PET_file,"None");
      strcpy(bck_PET_file_path, "None");
    }
  else
    {
      dobck = 1;
    }

  // set the flagg for flux
  if ((opt = get_online_option ("noflux",argc,argv)))
    noflux = 1;
  else
    noflux = 0;

  // check for the weights flagg,
  // set the file name if the flagg is set
  if ((opt = get_online_option ("opt_weights",argc,argv)))
    opt_weights = 1;
  else
    opt_weights = 0;

  // read the trigger for smoothing the sensitivity
  if ((opt = get_online_option ("smooth_conv",argc,argv)))
    smooth_conv = 1;
  else
    smooth_conv = 0;

  if ((opt = get_online_option ("out_SPC", argc, argv)))
    {
      strcpy (SPC_file, opt);
      strcpy (SPC_file_path, opt);

      if (opt_weights)
	{
	  replace_file_extension (SPC_file, SPC_opt_file, ".fits",
			      "_opt.SPC.fits", -1);
	  replace_file_extension (SPC_file_path, SPC_opt_file_path, ".fits",
			      "_opt.SPC.fits", -1);
	  replace_file_extension (SPC_opt_file, WHT_file, ".SPC.fits",
				  ".WHT.fits", -1);
	  replace_file_extension (SPC_opt_file_path, WHT_file_path, ".SPC.fits",
				  ".WHT.fits", -1);
	}
    }
  else
    {
      replace_file_extension (obj_PET_file, SPC_file, ".PET.fits",
			      ".SPC.fits", -1);
      if (drizzle)
	build_path (AXE_DRIZZLE_PATH, SPC_file, SPC_file_path);
      else
	build_path (AXE_OUTPUT_PATH, SPC_file, SPC_file_path);

      if (opt_weights)
	{
	  replace_file_extension (obj_PET_file, SPC_opt_file, ".PET.fits",
				  "_opt.SPC.fits", -1);
	  replace_file_extension (SPC_opt_file, WHT_file, ".SPC.fits",
				  ".WHT.fits", -1);
	  if (drizzle)
	    {
	      build_path (AXE_DRIZZLE_PATH, SPC_opt_file, SPC_opt_file_path);
	      build_path (AXE_DRIZZLE_PATH, WHT_file, WHT_file_path);
	    }
	  else
	    {
	      build_path (AXE_OUTPUT_PATH, SPC_opt_file, SPC_opt_file_path);
	      build_path (AXE_OUTPUT_PATH, WHT_file, WHT_file_path);
	    }
	}
    }

  // check the configuration file for the smoothin keywords
  if (!check_conf_for_smoothing(conf, smooth_conv))
    aXe_message(aXe_M_FATAL, __FILE__, __LINE__,
        "aXe_PET2SP: Either the configuration file %s does not contain\n"
        "the necessary keywords for the smoothing (POBJSIZE, SMFACTOR),\n"
        "or one of these keywords has an unreasonable value < 0.0!\n",
        conf_file_path);


  // give feedback onto the screen:
  // report on input and output
  // and also on specific parameter
  fprintf (stdout, "aXe_PET2SPC: Input configuration file name:   %s\n",
	   conf_file_path);
  fprintf (stdout, "aXe_PET2SPC: Input Object Aperture file name: %s\n",
	   aper_file_path);
  fprintf (stdout, "aXe_PET2SPC: Input Object PET file name:      %s\n",
	   obj_PET_file_path);
  if (dobck)
    fprintf (stdout, "aXe_PET2SPC: Input Background PET file name:  %s\n",
	     bck_PET_file_path);
  fprintf (stdout, "aXe_PET2SPC: Output SPC file name:            %s\n",
	   SPC_file_path);
  if (opt_weights)
    {
      fprintf (stdout, "aXe_PET2SPC: Computing optimal weights.\n");
      fprintf (stdout, "aXe_PET2SPC: Optimized SPC file name:         %s\n",
	       SPC_opt_file_path);
      fprintf (stdout, "aXe_PET2SPC: Output WHT file name:            %s\n",
	       WHT_file_path);
    }
  if (!noflux)
    {
      fprintf (stdout, "aXe_PET2SPC: Performing flux calibration.\n");
      if (smooth_conv)
	fprintf (stdout, "aXe_PET2SPC: Using smoothed sensitivity curves.\n");
    }
  fprintf (stdout, "\n\n");

  //
  // try to get the descriptor 'exptime' from the 'sci'-extension
  //
  exptime = (double)get_float_from_keyword(grism_file_path, conf->science_numext, conf->exptimekey);
  if (isnan(exptime))
    exptime = (double)get_float_from_keyword(grism_file_path, 1, conf->exptimekey);
  if (isnan(exptime))
    exptime = 1.0;

  //
  // try to get the descriptor 'SKY_CPS' from the 'sci'-extension
  //
  sky_cps = (double)get_float_from_keyword(grism_file_path, conf->science_numext, "SKY_CPS");
  if (isnan(sky_cps))
    sky_cps = 0.0;


  //  Open the OPET file for reading
  fits_open_file (&OPET_ptr, obj_PET_file_path, READONLY, &f_status);
  if (f_status)
    {
      ffrprt (stderr, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "aXe_PET2SPC: Could not open file: %s\n",
		   obj_PET_file_path);
    }

  // check whether the contamination is quantitative
  quant_cont = check_quantitative_contamination(OPET_ptr);

  if (opt_weights && !quant_cont)
    aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		 "aXe_PET2SPC: The optimal extractions needs quantitative contamination! "
		 " Please re-run aXe with Gauss or Fluxcube contamination!");

  obs = load_dummy_observation ();
  /* Loading the object list */
  fprintf (stdout, "aXe_PET2SPC: Loading object aperture list...");
  oblist = file_to_object_list_seq (aper_file_path, obs);
  fprintf (stdout,"%d objects loaded.\n",object_list_size(oblist));

  if (dobck)
    {
      //  Open the file for reading
      fits_open_file (&BPET_ptr, bck_PET_file_path, READONLY, &f_status);
      if (f_status)
	{
	  ffrprt (stderr, f_status);
	  aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		       "aXe_PET2SP: Could not open file: %s\n",
		       bck_PET_file_path);
	}
    }


  /* Copy the header info from the grism image */
  SPC_ptr = create_SPC_opened (SPC_file_path,1);
  cards = get_FITS_cards_opened(OPET_ptr);
  put_FITS_cards_opened(SPC_ptr, cards);
  free_FITScards(cards);

  if (opt_weights)
    {
      cards = get_FITS_cards_opened(OPET_ptr);

      // open the WHT file and add the header keywords
      WHT_ptr = create_FITSimage_opened (WHT_file_path, 1);
      put_FITS_cards_opened(WHT_ptr, cards);

      // open the opt_SPC and add the header keywords
      SPC_opt_ptr = create_SPC_opened (SPC_opt_file_path,1);
      put_FITS_cards_opened(SPC_opt_ptr, cards);

      // delete the header keywords
      free_FITScards(cards);
    }

  // do something only if there
  // exist valid objects
  i = 0;
  if (oblist!=NULL)
    {

      // turn until the end of the PET is reached
      while (1)
	{
	  empty=0;

	  // Get the PET for this object
	  obj_PET = get_ALL_from_next_in_PET(OPET_ptr, &obj_aperID, &obj_beamID);

	  // load the background PET if requested
	  if (dobck)
	    {
	      bck_PET = get_ALL_from_next_in_PET(BPET_ptr, &bck_aperID, &bck_beamID);
	      if ((bck_aperID!=obj_aperID)||(bck_beamID!=obj_beamID))
		aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
			     "Background PET and Object PET extensions are not"
			     " in the same order and cannot be combined.\n");
	    }

	  // end of PET reached: the break condition
	  if ((obj_aperID==-1) && (obj_beamID==-1))
	    break;

	  // signal an empty PET
	  if (obj_PET==NULL)
	    empty=1;

	  // give feedback to the screen
	  fprintf (stdout, "aXe_PET2SPC: BEAM %d; %d%c\n", i, obj_aperID, BEAM(obj_beamID));fflush(stdout);

	  // identify the object which matches the PET
	  objindex =  find_object_in_object_list(oblist,obj_aperID);

	  // look whether we are for grisms or prisms
	  for_grism = check_for_grism (conf_file_path, obj_beamID);
	  wl_calibration  = get_calfunc_for_beam(oblist[objindex]->beams[obj_beamID], for_grism, conf_file_path, conf);

	  // compute the object spectrum
	  obj_spec = bin_naive (obj_PET, oblist[objindex]->beams[obj_beamID].width,
				oblist[objindex]->beams[obj_beamID].orient,	quant_cont);

	  // check for the existence of a background PET
	  if (dobck)
	    {
	      // compute the background spectrum
	      bck_spec = bin_naive (bck_PET,
				    oblist[objindex]->beams[bck_beamID].width,
				    oblist[objindex]->beams[bck_beamID].orient, quant_cont);
	    }
	  else
	    {
	      // create a dummy background spectrum
	      bck_spec = empty_counts_spectrum_copy(obj_spec);
	    }


	  // subtract the background spectrum from the
	  // object (or forground) spectrum
	  sobj_spec = subtract_spectra (obj_spec, bck_spec);

	  if(!noflux)
	    {
	      get_troughput_table_name(conf_file_path,
				       oblist[objindex]->beams[obj_beamID].ID,
				       table);
	      if (strcmp(table,"None"))
		{
		  build_path (AXE_CONFIG_PATH, table, table_path);
		  resp=get_response_function_from_FITS(table_path,2);
		  resp_func = create_response_function(table_path);
		  if (resp->spec_len <2)
		    {
		      aXe_message (aXe_M_WARN1, __FILE__, __LINE__,
				   "Throughput table %s contains only %d"
				   " values. No sensitivity curve was applied.\n",
				   table_path,resp->spec_len);
		    }
		  else
		    {
		      fprintf(stdout,"aXe_PET2SPC: Applying sensitivity contained in %s\n",table_path);
		      smooth_params = get_smooth_pars_for_beam(conf, smooth_conv, oblist[objindex]->beams[obj_beamID]);
		      if (smooth_params.x > 0.0)
		        {
		          // apply a smoothed flux conversion
			  apply_smoothed_response(wl_calibration, for_grism, quant_cont, resp_func, smooth_params, sobj_spec);
			}
		      else
			{
			  // apply a normal flux conversion
			  apply_response_function(sobj_spec,resp, quant_cont);
			}
		    }
		  // free the memory of the
		  // response functions
		  free_spectrum(resp);
		  free_response_function(resp_func);
		}
	    }
	  if (empty!=1)
	    {
	      add_spectra_to_SPC_opened (SPC_ptr, obj_spec, bck_spec,
					 sobj_spec, oblist[objindex]->ID, oblist[objindex]->beams[obj_beamID].ID);


	      /* Copy header from OPET extension into this SPC extension */
	      cards = get_FITS_cards_opened(OPET_ptr);
	      put_FITS_cards_opened(SPC_ptr,cards);
	      free_FITScards(cards);

	    }

	  free_spectrum (bck_spec);
	  free_spectrum (sobj_spec);
	  free_spectrum (obj_spec);


	  if (opt_weights)
	    {
	      // get the dimension in trace length
	      // and crossdispersion
	      dimension = get_all_dims(obj_PET, bck_PET,
				       oblist[objindex]->beams[obj_beamID], dobck);

	      // check for empty PET
	      if (!dimension.resolution)
		{
		  // create dummies in case of empty PET's
		  weights = get_default_weight();
		  modvar  = get_default_modvar();
		}
	      else
		{
		  // prepare the PET's by computing the inverse variance.
		  // Also the trace distances are shifted by 0.5
		  // to get a sampling comparable to the unweighted
		  // extraction
		  prepare_inv_variance(obj_PET, bck_PET, dobck, conf, exptime, sky_cps, 0.0);

		  // compute the inverse variance and the profile
		  // image in the trace distance - crossdispersion plane
		  modvar = compute_modvar(obj_PET, oblist[objindex]->beams[obj_beamID], dimension);

		  // compute the optimal weights
		  weights = comp_allweight(modvar);

		}

	      if (dimension.resolution && empty != 1)
		{
		  sprintf (label, "WHT_%d%c", obj_aperID, BEAM (obj_beamID));
		  gsl_to_FITSimage_opened (weights, WHT_ptr ,0,label);

		  // make and store the default header
		  cards = beam_to_FITScards(oblist[objindex],obj_beamID);
		  put_FITS_cards_opened(WHT_ptr,cards);
		  free_FITScards(cards);
		}

	      // create the optimal weighted
	      // foreground spectrum
	      obj_spec = bin_optimal (obj_PET,oblist[objindex]->beams[obj_beamID],
				      quant_cont, weights, dimension, coverage);



	      // check for the presence of a background PET
	      if (dobck)
		{
		  // create the optimal weighted
		  // background spectrum
		  bck_spec = bin_optimal (bck_PET,oblist[objindex]->beams[obj_beamID],
					  quant_cont, weights, dimension, coverage);
		}
	      else
		{
		  // make a dummy background spectrum
		  bck_spec = empty_counts_spectrum_copy(obj_spec);
		}

	      // release memory
	      gsl_matrix_free(weights);
	      free_drzstamp(modvar);


	  // subtract the background spectrum from the
	  // object (or forground) spectrum
	  sobj_spec = subtract_spectra (obj_spec, bck_spec);

	  if(!noflux)
	    {
	      get_troughput_table_name(conf_file_path,
				       oblist[objindex]->beams[obj_beamID].ID,
				       table);
	    if (strcmp(table,"None"))
	      {
		build_path (AXE_CONFIG_PATH, table, table_path);
		resp=get_response_function_from_FITS(table_path,2);
		resp_func = create_response_function(table_path);
		if (resp->spec_len <2)
		  {
		    aXe_message (aXe_M_WARN1, __FILE__, __LINE__,
				 "Throughput table %s contains only %d"
				 " values. No sensitivity curve was applied.\n",
				 table_path,resp->spec_len);
		  }
		else
		  {
		    fprintf(stdout,"aXe_PET2SPC: Applying sensitivity contained in %s\n",table_path);
		    smooth_params = get_smooth_pars_for_beam(conf, smooth_conv, oblist[objindex]->beams[obj_beamID]);
                    if (smooth_params.x > 0.0)
                      {
                        apply_smoothed_response(wl_calibration, for_grism, quant_cont, resp_func, smooth_params, sobj_spec);
                      }
		    else
		      {
			apply_response_function(sobj_spec,resp, quant_cont);
		      }
		  }
		// free the memory
		free_spectrum(resp);
		free_response_function(resp_func);
	      }
	    }
	  if (empty!=1)
	    {
	      add_spectra_to_SPC_opened (SPC_opt_ptr, obj_spec, bck_spec,
					 sobj_spec, oblist[objindex]->ID, oblist[objindex]->beams[obj_beamID].ID);


	      /* Copy header from OPET extension into this SPC extension */
	      cards = get_FITS_cards_opened(OPET_ptr);
	      put_FITS_cards_opened(SPC_opt_ptr,cards);
	      free_FITScards(cards);

	    }

	  free_spectrum (bck_spec);
	  free_spectrum (sobj_spec);
	  free_spectrum (obj_spec);
	    }

	  // free memory
	  free_calib (wl_calibration);
	  if (bck_PET!=NULL)
	    free (bck_PET);
	  if (obj_PET!=NULL)
	    free (obj_PET);
	  i++;
        }
    }

  fits_close_file (SPC_ptr, &f_status);
  if (f_status)
    {
      ffrprt (stderr, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "aXe_PET2SPC: " "Error closing SPC: %s\n",
		   SPC_file_path);
    }

  if (opt_weights)
    {
      fits_close_file (WHT_ptr, &f_status);
      if (f_status)
	{
	  ffrprt (stderr, f_status);
	  aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		       "aXe_PET2SPC: " "Error closing WHT: %s\n",
		       WHT_file_path);
	}
      fits_close_file (SPC_opt_ptr, &f_status);
      if (f_status)
	{
	  ffrprt (stderr, f_status);
	  aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		       "aXe_PET2SPC: " "Error closing PSC: %s\n",
		       SPC_opt_file_path);
	}
    }

  if (oblist!=NULL)
    free_oblist (oblist);

  fprintf (stdout, "aXe_PET2SPC: Done...\n");
  exit (0);
}
Пример #2
0
int
main (int argc, char *argv[])
{
  char *opt;
  char grism_image[MAXCHAR];
  char grism_image_path[MAXCHAR];

  char sex_catalog[MAXCHAR];
  char sex_catalog_path[MAXCHAR];

  char aper_file[MAXCHAR];
  char aper_file_path[MAXCHAR];

  char conf_file[MAXCHAR];
  char conf_file_path[MAXCHAR];

  float mmag_extract, mmag_mark, mfwhm, dmag;

  int leaveout_ignored = 1;
  int auto_reorient = 1;
  int no_orient = 0;
  int slitless_geom=0;
  int num;
  char ext[MAXCHAR];
  double lambda_mark=0.0;
  int bck_mode=0;

  aperture_conf *conf;
  SexObject **os;
  object **oblist;
  observation *obs;

  if ((argc < 3) || (opt = get_online_option ("help", argc, argv)))
    {
      fprintf (stdout,
               "ST-ECF European Coordinating Facility\n"
               "aXe_GOL2AF Version %s:\n"
               "            aXe task to create an Object Aperture File (OAF)\n"
               "            or a Background Aperture File (BAF) (-bck option)  using an\n"
               "            input Sextractor Grism Object List (GOL).\n"
               "            The AF files contain information about each object (aperture)\n"
               "            and information about the various beams (orders) in each of \n"
               "            these apertures. These are simple text files which can be\n"
               "            edited by hand. The IGNORE keywords can be set to 1 if a\n"
               "            particular beam (order) is to be ignored during the extraction\n"
               "            process. The MMAG_EXTRACT and MMAG_MARK keyword of each beam\n"
               "            listed in the aXe configuration file determine if a particluar\n "
               "            beam is flagged to be extracted or completely ignored.\n"
               "            The -dmag switch can be used to adjust these. See the aXe manual\n"
               "            for more details. An extraction width multiplicative factor can\n"
               "            be set with the -mfwhm option (e.g. to generate BAF containing\n"
               "            broader apertures).\n"
               "            By default, when the extraction angle is too close to the\n"
               "            dispersion direction, 90 degrees are added to the extraction\n"
               "            angle. This can be overwritten by using the -no_auto_orient\n"
               "            online parameter.\n"
               "\n"
               "            Input FITS mages are looked for in $AXE_IMAGE_PATH\n"
               "            aXe config file is looked for in $AXE_CONFIG_PATH\n"
               "            All outputs are writen to $AXE_OUTPUT_PATH\n"
               "\n"
               "Usage:\n"
               "        aXe_GOL2AF [g/prism image filename] [aXe filename] [options]\n"
               "\n"
               "Options:\n"
               "             -bck                - to generate a BAF instead of an OAF file\n"
               "             -SCI_hdu=[integer]  - overwrite the default from the \n"
               "                                   configuration file \n"
               "             -mfwhm=[float]      - an extraction width multiplicative factor.\n"
               "             -exclude_faint      - do not even list faint object in the result.\n"
               "             -out_AF=[string]    - overwrites the default output aper filename.\n"
               "             -in_GOL=[string]    - overwrites the default input catalog name.\n"
               "             -auto_orient=[0/1]  - set to 0 to disable the automatic extraction\n"
               "                                   orientation.\n"
               "             -no_orient          - disable tilted extraction (vertical\n"
               "                                   extraction only).\n"
               "             -dmag=[float]       - A number to add to the MMAG_EXTRACT and\n"
               "                                   MMAG_MARK values.\n"
               "\n"
               "Example: ./aXe_GOL2AF slim_grism.fits -bck -dmag=2 -mfwhm=2\n"
               "\n",RELEASE);
      exit (1);
    }

  fprintf (stdout, "aXe_GOL2AF: Starting...\n");

  /* Get the Grism/Prism image name */
  strcpy (grism_image, argv[1]);
  build_path (AXE_IMAGE_PATH, grism_image, grism_image_path);

  /* Get the name of the configuration file */
  strcpy (conf_file, argv[2]);
  build_path (AXE_CONFIG_PATH, conf_file, conf_file_path);

  /* Read the configuration file */
  conf = get_aperture_descriptor (conf_file_path);

  /* Determine where the various extensions are in the FITS file */
  get_extension_numbers(grism_image_path, conf,conf->optkey1,conf->optval1);

  if ((opt = get_online_option ("SCI_hdu", argc, argv)))
    {
      char str[MAXCHAR];
      strcpy(str,opt);
      conf->science_numext = atoi(str);
    }


  /* Get or set up the name of the output Aperture File */
  if ((opt = get_online_option ("out_AF", argc, argv)))
    {
      /* get it */
      strcpy (aper_file, opt);
      strcpy (aper_file_path, opt);
    }
  else
    {
      /* set the name automatically */
      /* Get the name of the aperture file type, OAF or BAF */
      if ((opt=get_online_option ("bck",argc,argv)))
        {
          strcpy (ext,".BAF");
          bck_mode=1;
        }
      else
        {
          strcpy (ext,".OAF");
        }
      strcpy (aper_file, argv[1]);
      replace_file_extension (grism_image, aper_file, ".fits", ext,
                              conf->science_numext);
      build_path (AXE_OUTPUT_PATH, aper_file, aper_file_path);
    }

     /* Set the option extraction width multiplicative factor */
  if ((opt = get_online_option ("mfwhm", argc, argv)))
    {
      {
        /*char str[MAXCHAR];
          strcpy (str, opt);
          sscanf (str, "%f", &mfwhm);*/
        mfwhm = atof(opt);
      }
    }
  else
    {
      mfwhm = 1.0;
    }

  if ((opt = get_online_option ("dmag", argc, argv)))
    {
      {
        /*char str[MAXCHAR];
          strcpy (str, opt);
          sscanf (str, "%f", &mfwhm);*/
        dmag = atof(opt);
      }
    }
  else
    {
      dmag = 0.0;
    }

  /* Set the option to include object that are too faint into the output
     aperture file.
  */
  if ((opt = get_online_option ("exclude_faint", argc, argv)))
    {
      leaveout_ignored = 1;
    }
  else
    {
      leaveout_ignored = 0;
    }

  /* Set the auto orientation mode for extraction on or off, default is on (=1)

  if ((opt = get_online_option ("auto_orient",argc,argv)))
    {
      if (!atof(opt))
        auto_reorient = 0;
    }
  else
    {
      auto_reorient = 1;
    }
  */

  // set the flag for using slitless geometry,
  // to say extraction parameters optimized for
  // slitless geometry
  if ((opt = get_online_option ("slitless_geom",argc,argv)))
      if (!atof(opt))
        //slitless_geom = 0;
        auto_reorient = 0;
  else
    //slitless_geom = 1;
    auto_reorient = 1;

  /* Set the option to disbale tilted extraction */
  if ((opt = get_online_option ("orient", argc, argv)))
    {
      if (!atof(opt))
        auto_reorient = 2;
    }

  /* Get or generate the name of the sextractor catalog to read */
  if ((opt = get_online_option ("in_GOL", argc, argv)))
    {
      strcpy (sex_catalog, opt);
      strcpy (sex_catalog_path, opt);
    }
  else
    {
      strcpy (sex_catalog, argv[1]);
      replace_file_extension (grism_image, sex_catalog, ".fits", ".cat",
                              conf->science_numext);
      build_path (AXE_OUTPUT_PATH, sex_catalog, sex_catalog_path);
    }

   if ((opt = get_online_option ("lambda_mark", argc, argv)))
    {
      lambda_mark = atof(opt);
    }
   else{
      lambda_mark = 800.0;
   }

   // check the configuration file for the smoothin keywords
   if (!check_conf_for_slitlessgeom(conf, auto_reorient))
   //if (!check_conf_for_slitlessgeom(conf, slitless_geom))
     aXe_message(aXe_M_FATAL, __FILE__, __LINE__,
         "aXe_GOL2AF: Either the configuration file %s does not contain\n"
         "the necessary keywords for the slitless geometry: POBJSIZE,\n"
         "or this keyword has an unreasonable value < 0.0!\n",
         conf_file_path);

  fprintf (stdout,
           "aXe_GOL2AF: Main configuration file name:    %s\n",
           conf_file_path);
  fprintf (stdout,
           "aXe_GOL2AF: Input data file name:            %s\n",
           grism_image_path);
  fprintf (stdout,
           "aXe_GOL2AF: SCI extension number:            %d\n",
           conf->science_numext);
  fprintf (stdout,
           "aXe_GOL2AF: Input grism object list (GOL):   %s\n",
           sex_catalog_path);
  fprintf (stdout,
           "aXe_GOL2AF: Output aperture file name:       %s\n",
           aper_file_path);
  fprintf (stdout,
           "aXe_GOL2AF: FWHM multiplicative factor:      %f\n",
           mfwhm);
  fprintf (stdout,
           "aXe_GOL2AF: Dmag adjustment:      %f\n",
           dmag);
  if (leaveout_ignored)
    fprintf (stdout,
             "aXe_GOL2AF: Ignored object will not "
             "be included in the output aperture file\n");
  if (!leaveout_ignored)
    fprintf (stdout,
             "aXe_GOL2AF: Ignored object will be "
             "included in the output aperture file\n");
  if (auto_reorient != 2)
    fprintf (stdout,
             "aXe_GOL2AF: Tilted extraction will be performed.\n");
  if (auto_reorient == 0)
    fprintf (stdout,
             "aXe_GOL2AF: The extraction geometry follows the object\n");
  if (auto_reorient == 1)
    //if (slitless_geom)
    fprintf (stdout,
        "aXe_GOL2AF: Extraction geometry is optimized for slitless spectroscopy.\n");
    //else
    //  fprintf (stdout,
    //        "aXe_GOL2AF: The extraction geometry follows the object, but may switch.\n");
  if (auto_reorient==2)
    fprintf (stdout,
             "aXe_GOL2AF: NO tilted extraction will be performed (e.g. vertical only).\n");
  fprintf (stdout,
           "aXe_GOL2AF: Wavelength for object selection [nm]: %f\n",
           lambda_mark);
  fprintf(stdout,"\n\n");

  // extend the beams when creating
  // BAF's
  if (bck_mode)
    extend_config_beams(conf);

  fprintf (stdout, "aXe_GOL2AF: Loading object list...");
  os = get_SexObject_from_catalog (sex_catalog_path, lambda_mark);
  fprintf (stdout, "Done.\n");fflush(stdout);

  fprintf (stdout, "aXe_GOL2AF: Reading image...");
  obs = load_empty_image (grism_image_path, conf->science_numext);
  fprintf (stdout, "Done.\n");fflush(stdout);

  fprintf (stdout, "aXe_GOL2AF: Generating aperture list...");

  //if (!slitless_geom)
  //use the old, aXe-1.6  code
  //oblist = SexObjects_to_oblist(os, obs, conf, conf_file_path, mfwhm, dmag,
  //                               auto_reorient, bck_mode);
  // else

  // use the new aXe-1.7 code
  oblist = SexObjects_to_oblistII(os, obs, conf, conf_file_path, mfwhm, dmag, auto_reorient, bck_mode);

  fprintf (stdout, "Done.\n");fflush(stdout);

  fprintf (stdout, "aXe_GOL2AF: Writing aperture file...");fflush(stdout);
  num = object_list_to_file (oblist, aper_file_path, leaveout_ignored);
  fprintf (stdout, "%d beams written.\n", num);

  free_SexObjects (os);
  free_oblist (oblist);

  fprintf (stdout, "aXe_GOL2AF: Done...\n");

  exit (0);
}
Пример #3
0
int
main (int argc, char *argv[])
{
  char *opt;
  char aper_file[MAXCHAR];
  char aper_file_path[MAXCHAR];

  char conf_file[MAXCHAR];
  char conf_file_path[MAXCHAR];

  char grism_file[MAXCHAR];
  char grism_file_path[MAXCHAR];

  char specmod_file[MAXCHAR];
  char specmod_file_path[MAXCHAR];

  char objmod_file[MAXCHAR];
  char objmod_file_path[MAXCHAR];

  aperture_conf *conf;

  int index;
  double lambda_psf=0.0;

  observation *obs;

  if ((argc < 3) || (opt = get_online_option ("help", argc, argv)))
    {
      fprintf (stdout,
    		  "Usage:\n"
    		  "      aXe_DISPIMAGE g/prism_filename configuration_filename"
    		  "\n"
    		  "Options:\n"
    		  "      -in_AF=[string]         - overwrite the automatically generated name\n"
    		  "                                of the input aperture file\n"
    		  "      -model_spectra=[string] - input model spectra"
    		  "      -model_images=[string]  - input model images"
    		  "      -lambda_psf=[float]     - lambda at which psf was measured"
    		  "\n",RELEASE);
      exit (1);
    }

  fprintf (stdout, "aXe_DISPIMAGE: Starting...\n");

  index = 0;

  strcpy (grism_file, argv[++index]);
  build_path (AXE_IMAGE_PATH, grism_file, grism_file_path);

  strcpy (conf_file, argv[++index]);
  build_path (AXE_CONFIG_PATH, conf_file, conf_file_path);

  conf = get_aperture_descriptor (conf_file_path);
  get_extension_numbers(grism_file_path, conf,conf->optkey1,conf->optval1);

 /* Get or set up the name of the output Aperture File */
  if ((opt = get_online_option ("in_AF", argc, argv)))
    {
      /* get it */
      strcpy (aper_file, opt);
      strcpy (aper_file_path, opt);
    }
  else {
    /* Build aperture file name */
    replace_file_extension (grism_file, aper_file, ".fits",
			    ".OAF", conf->science_numext);
    build_path (AXE_OUTPUT_PATH, aper_file, aper_file_path);
  }

  // determine the wavelength
  // the object extend was determined at
  if ((opt = get_online_option ("lambda_psf", argc, argv)))
    lambda_psf = atof(opt);
  else
    lambda_psf = 800.0;

  // check whether a name for the spectral
  // models file is given
  if ((opt = get_online_option ("model_spectra", argc, argv)))
    {
      // get and set up the filename
      strcpy (specmod_file, opt);
      build_path (AXE_IMAGE_PATH, specmod_file, specmod_file_path);
    }
  else
    {
      strcpy (specmod_file, "");
      strcpy (specmod_file_path, "");
    }
  
  // check whether a name for the images
  // models file is given
  if ((opt = get_online_option ("model_images", argc, argv)))
    {
      // get and set up the filename
      strcpy (objmod_file, opt);
      build_path (AXE_IMAGE_PATH, objmod_file, objmod_file_path);
    }
  else
    {
      strcpy (objmod_file, "");
      strcpy (objmod_file_path, "");
    }

  fprintf (stdout, "aXe_DISPIMAGE: grism image file name: %s\n", grism_file_path);
  fprintf (stdout, "aXe_DISPIMAGE: Input Aperture file name: %s\n", aper_file_path);
  fprintf (stdout, "aXe_DISPIMAGE: Using spectral models in table: %s\n", specmod_file_path);
  fprintf (stdout, "aXe_DISPIMAGE: Using direct emission objects in image: %s\n", objmod_file_path);
  fprintf (stdout, "aXe_DISPIMAGE: Object parameters determined at %fnm\n", lambda_psf);

  fprintf (stdout, "aXe_DISPIMAGE: ");
  obs = load_sci_image (grism_file_path, conf->science_numext);

  compute_disp(grism_file_path, aper_file_path, conf_file_path,
		       specmod_file_path,  objmod_file_path, lambda_psf, obs);

  free_observation(obs);
  free_aperture_conf(conf);
  fprintf (stdout, "aXe_DISPIMAGE: Done...\n");
  exit (0);
}
Пример #4
0
int
main (int argc, char *argv[])
{
  char *opt;
  char aper_file[MAXCHAR];
  char aper_file_path[MAXCHAR];

  char conf_file[MAXCHAR];
  char conf_file_path[MAXCHAR];

  char grism_file[MAXCHAR];
  char grism_file_path[MAXCHAR];

  char PET_file[MAXCHAR];
  char PET_file_path[MAXCHAR];

  //  char outputroot[MAXCHAR];
  //  char outputroot_path[MAXCHAR];
  char STP_file[MAXCHAR];
  char STP_file_path[MAXCHAR];

  //  char output_path[MAXCHAR];

  aperture_conf *conf;

  object **oblist;
  observation *obs;

  ap_pixel *PET;

  int index, i;
  char label[MAXCHAR];

  fitsfile *PET_ptr, *STP_ptr;
  int f_status = 0;

  int aperID, beamID, objindex;
  FITScards *cards;
  FITScards *xymin_cards;

  gsl_matrix *rstamp;
  drzstamp *drzstmp;
  drzstamp_dim dim;

  int rectified = 0;
  //int drizzled = 0;
  int drizzle=0;
  int drzstamp=0;
  int for_grism=0;

  d_point stp_min;

  if ((argc < 3) || (opt = get_online_option ("help", argc, argv)))
    {
      fprintf (stdout,
	       "ST-ECF European Coordinating Facility\n"
	       "aXe_STAMPS Version %s:\n"
	       "           Task that generates some Postscript stamp images from the\n"
	       "           content of a Pixel Extraction Table (PET)\n"
	       "           The trace is overplotted and is generated from the group\n"
	       "           of pixels in the PET which have the smallest projected distance\n"
	       "           to the trace in the PET. No analytical a-priori trace function \n"
	       "           is used.\n"
	       "\n"
	       "Usage:\n"
	       "      aXe_STAMPS g/prism_filename configuration_filename [options]\n"
	       "\n"
	       "Options:\n"
	       "      -rectified            - produces rectified stamp image following the\n"
	       "                              direction of the extraction process\n"
	       "      -drz                  - use $AXE_DRIZZLE_PATH to locate the grism-, OAF-\n"
	       "                              and PET-files instead of $AXE_IMAGE/OUTPUT_PATH\n"
	       "      -in_AF=[string]       - overwrite the automatically generated name\n"
	       "                              of the input aperture file\n"
	       "      -in_PET=[string]      - overwrite the automatically generated name\n"
	       "                              of the input PET file\n"
	       "      -out_STP=[string]     - overwrite the automatically generated name\n"
	       "                              of the output stamp FITS image\n"
	       "\n",RELEASE);
      exit (1);
    }

  fprintf (stdout, "aXe_STAMPS: Starting...\n");

  index = 0;
  strcpy (grism_file, argv[++index]);
  if ((opt = get_online_option ("drz", argc, argv))){
    build_path (AXE_DRIZZLE_PATH, grism_file, grism_file_path);
    drizzle=1;
  }
  else{
    build_path (AXE_IMAGE_PATH, grism_file, grism_file_path);
    drizzle=0;
  }

  strcpy (conf_file, argv[++index]);
  build_path (AXE_CONFIG_PATH, conf_file, conf_file_path);


  conf = get_aperture_descriptor (conf_file_path);

  /* Determine where the various extensions are in the FITS file */
  get_extension_numbers(grism_file_path, conf,conf->optkey1,conf->optval1);

  /* Build aperture file name */
  if ((opt = get_online_option("in_AF", argc, argv))){
    /* get it */
    strcpy (aper_file, opt);
    strcpy (aper_file_path, opt);
  }
  else {
    replace_file_extension (grism_file, aper_file, ".fits",
			    ".OAF", conf->science_numext);
    if (drizzle)
      build_path (AXE_DRIZZLE_PATH, aper_file, aper_file_path);
    else
      build_path (AXE_OUTPUT_PATH, aper_file, aper_file_path);
  }

  if ((opt = get_online_option("in_PET", argc, argv))){
    /* get it */
    strcpy (PET_file, opt);
    strcpy (PET_file_path, opt);
  }
  else{
    /* Build object PET file name */
    replace_file_extension (grism_file, PET_file, ".fits",
			    ".PET.fits", conf->science_numext);
    if (drizzle)
      build_path (AXE_DRIZZLE_PATH, PET_file, PET_file_path);
    else
      build_path (AXE_OUTPUT_PATH, PET_file, PET_file_path);
  }

  if ( (opt = get_online_option ("out_STP", argc, argv)) )
    {
      strcpy (STP_file, opt);
      //      strcpy (STP_file_PATH, opt);
    }
  else
    {
      //      replace_file_extension (PET_file, STP_file, ".PET.fits", ".STP.fits",-1);
      replace_file_extension (grism_file, STP_file, ".fits", ".STP.fits",conf->science_numext);
    }


  if (drizzle)
    build_path (AXE_DRIZZLE_PATH, STP_file, STP_file_path);
  else
    build_path (AXE_OUTPUT_PATH, STP_file, STP_file_path);

  if ((opt=get_online_option("rectified",argc,argv)))
    rectified = 1;
  else if ((opt = get_online_option ("drzstamp", argc, argv)))
    drzstamp=1;


  // give feedback onto the screen:
  // report on input and output
  // and also on specific parameters
  fprintf (stdout, "aXe_STAMPS: Input Image file name:               %s\n",
	   grism_file_path);
  fprintf (stdout, "aXe_STAMPS: Input Aperture file name:            %s\n",
	   aper_file_path);
  fprintf (stdout, "aXe_STAMPS: Input PET file name:                 %s\n",
	   PET_file_path);
  fprintf (stdout, "aXe_STAMPS: Name of STP file :                   %s\n",
	   STP_file_path);
  if (rectified)
    fprintf (stdout, "aXe_STAMPS: Producing rectified stamp images.\n");
  else if (drzstamp)
    fprintf (stdout, "aXe_STAMPS: Producing drizzled stamp images.\n");
  else
    fprintf (stdout, "aXe_STAMPS: Producing trace stamp images.\n");


  // Loading the object list
  obs = load_dummy_observation ();
  fprintf (stdout, "\naXe_STAMPS: Loading object aperture list...");
  oblist = file_to_object_list_seq (aper_file_path, obs);
  fprintf (stdout,"%d objects loaded.\n\n",object_list_size(oblist));

  //  Open the OPET file for reading
  fits_open_file (&PET_ptr, PET_file_path, READONLY, &f_status);
  if (f_status)
    {
      ffrprt (stdout, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "aXe_STAMPS: Could not open file: %s\n",
		   PET_file_path);
    }

  STP_ptr = create_FITSimage_opened (STP_file_path, 1);

  i = 0;
  if (oblist!=NULL)
    {
      while (1)
	{

	  /* Get the PET for this object */
	  PET = get_ALL_from_next_in_PET(PET_ptr, &aperID, &beamID);
	  if ((aperID==-1) && (beamID==-1)) break;
	  fprintf (stdout, "aXe_STAMPS: BEAM %d%c.", aperID, BEAM(beamID));
	  objindex =  find_object_in_object_list(oblist,aperID);

	  sprintf (label, "%s.%d%c.ps/CPS", STP_file_path,
		   oblist[objindex]->ID, BEAM (oblist[objindex]->beams[beamID].ID));

	  // determine the minimum in x and y
	  stp_min.x = -1.0;
	  stp_min.y = -1.0;

	  // special treatment for FORS2: dirzzled stamp images
	  if (drzstamp)
	    {

	      for_grism = check_for_grism (conf_file_path, beamID);
	      if (!for_grism)
		aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
			     "aXe_STAMPS: Drizzled stamp images are not\n"
			     "supported for prism images. Please choose a different option!\n");

	      // get the dimensions of the drizzled stamp
	      dim = get_stamp_dim(PET, oblist[objindex]->beams[beamID].width, conf, beamID, &stp_min);

	      // fill the drzstamp structure (counts plus weight matrix)
	      drzstmp = drizzled_stamp_img (PET,oblist[objindex]->beams[beamID].width,
					    oblist[objindex]->beams[beamID].orient,  dim);

	      // does this make sense
	      interpolate_over_NaN (drzstmp->counts);

	      // give the extension name and store the counts
	      sprintf (label, "BEAM_%d%c",oblist[objindex]->ID, BEAM (oblist[objindex]->beams[beamID].ID));
	      gsl_to_FITSimage_opened (drzstmp->counts, STP_ptr ,0,label);

	      // in case that the stamp is no dummy, make and store the WCS header
	      if (dim.resolution)
		{
		  cards = get_WCS_FITScards(dim.xstart*dim.resolution, dim.resolution, dim.ystart);
                  put_FITS_cards_opened(STP_ptr,cards);
                  free_FITScards(cards);
		}

	      // make and store the default header
	      cards = beam_to_FITScards(oblist[objindex],beamID);
              xymin_cards = stpmin_to_FITScards(stp_min);
	      put_FITS_cards_opened(STP_ptr,cards);
              put_FITS_cards_opened(STP_ptr,xymin_cards);
	      free_FITScards(cards);
              free_FITScards(xymin_cards);

	      // give the extension name and store the weight image
	      sprintf (label, "WEIG_%d%c",oblist[objindex]->ID, BEAM (oblist[objindex]->beams[beamID].ID));
	      gsl_to_FITSimage_opened (drzstmp->weight, STP_ptr ,0,label);

	      // in case that the stamp is no dummy, make and store the WCS header
	      if (dim.resolution)
		{
		  cards = get_WCS_FITScards(dim.xstart*dim.resolution, dim.resolution, dim.ystart);
		  put_FITS_cards_opened(STP_ptr,cards);
		  free_FITScards(cards);
		}

	      // make and store the default header
	      cards = beam_to_FITScards(oblist[objindex],beamID);
              xymin_cards = stpmin_to_FITScards(stp_min);
	      put_FITS_cards_opened(STP_ptr,cards);
              put_FITS_cards_opened(STP_ptr,xymin_cards);
	      free_FITScards(cards);
              free_FITScards(xymin_cards);

	      // free the structure with the count and weight matrix
	      free_drzstamp(drzstmp);
	    }
	  else
	    {
	      if (rectified)
		{
		  rstamp = rectified_stamp_img (PET,oblist[objindex]->beams[beamID].width, &stp_min);
		}
	      else
		{
		  rstamp = stamp_img (PET,oblist[objindex]->beams[beamID].width, &stp_min);
		}
	      sprintf (label, "BEAM_%d%c",oblist[objindex]->ID, BEAM (oblist[objindex]->beams[beamID].ID));
	      interpolate_over_NaN (rstamp);
	      gsl_to_FITSimage_opened (rstamp, STP_ptr ,0,label);
	      cards = beam_to_FITScards(oblist[objindex],beamID);
              xymin_cards = stpmin_to_FITScards(stp_min);
	      put_FITS_cards_opened(STP_ptr,cards);
              put_FITS_cards_opened(STP_ptr,xymin_cards);
	      free_FITScards(cards);
              free_FITScards(xymin_cards);

	      free_stamp_img(rstamp);
	    }

	  if (PET!=NULL)
	    free(PET);

	  fprintf (stdout, " Done.\n");
	  i++;
	}

    }
  if (oblist!=NULL) free_oblist (oblist);

  fits_close_file (STP_ptr, &f_status);
  if (f_status)
    {
      ffrprt (stdout, f_status);
      aXe_message (aXe_M_FATAL, __FILE__, __LINE__,
		   "aXe_STAMPS: Could not" " close STP file: %s\n", STP_file_path);
    }

  fprintf (stdout, "aXe_STAMPS: Done...\n");

  /* Copy the header info from the grism image */
  {
    FITScards *cards;
    cards = get_FITS_cards (PET_file_path, 1);
    put_FITS_cards(STP_file_path,1,cards);
    free_FITScards(cards);
  }
  exit (0);
}