void Pulsar::TimerArchive::unpack_extensions () try
{
  if (verbose == 3)
    cerr << "Pulsar::TimerArchive::unpack_extensions" << endl;

  Receiver* receiver = getadd<Receiver>();
  unpack (receiver);

  Telescope* telescope = getadd<Telescope>();

  try
  {
    telescope->set_coordinates (get_telescope());
    Telescopes::set_telescope_info (telescope, this);
  }
  catch (Error& error)
  {
    if (verbose > 2)
      warning << "Pulsar::TimerArchive::unpack_extensions " 
              << error.get_message().c_str() << endl;
  }
  
  TapeInfo* tape = getadd<TapeInfo>();
  unpack (tape);

  if (verbose == 3)
    cerr << "Pulsar::TimerArchive::unpack_extensions set Backend" << endl;

  Backend* backend = get<Backend>();

  if (!backend)
  {
    Backend* ben = getadd<Backend>();
    ben->set_name (hdr.machine_id);

    ben->set_corrected (hdr.corrected & BE_PHS_CORRECTED);
    ben->set_downconversion_corrected (hdr.corrected & BE_DCC_CORRECTED);

    backend = ben;
  }

  if (verbose == 3)
    cerr << "Pulsar::TimerArchive::unpack_extensions " 
	 << backend->get_extension_name() << " name="
	 << backend->get_name() << endl;

}
catch (Error& error)
{
  throw error += "Pulsar::TimerArchive::unpack_extensions";
}
Esempio n. 2
0
void Pulsar::FITSArchive::load_header (const char* filename) try
{
  int status = 0;
  fitsfile* fptr = 0;
  
  // Open the data file  
  if (verbose > 2)
    cerr << "FITSArchive::load_header fits_open_file ("<< filename <<")"<<endl;
  
  fits_open_file (&fptr, filename, READONLY, &status);
  
  if (status != 0)
    throw FITSError (status, "FITSArchive::load_header", 
		     "fits_open_file(%s)", filename);

  // These Extensions must exist in order to load

  ObsExtension*     obs_ext = getadd<ObsExtension>();
  FITSHdrExtension* hdr_ext = getadd<FITSHdrExtension>();

  // Pulsar FITS header definiton version

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading FITS header version" << endl;

  string tempstr;
  string dfault;

  dfault = hdr_ext->hdrver;
  psrfits_read_key (fptr, "HDRVER", &tempstr, dfault, verbose > 2);
  hdr_ext->hdrver = tempstr;
  psrfits_version = fromstring<float>( tempstr );
  if (sscanf (hdr_ext->hdrver.c_str(), "%d.%d", 
	      &hdr_ext->major_version, &hdr_ext->minor_version) != 2)
    throw Error (InvalidParam, "FITSARchive::load_header",
		 "could not parse header version from " + hdr_ext->hdrver);
 
  if (verbose > 2)
    cerr << "Got: Version " << tempstr << endl;
  
  // File creation date
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading file creation date" << endl;

  dfault = hdr_ext->creation_date;
  psrfits_read_key (fptr, "DATE", &tempstr, dfault, verbose > 2);
  hdr_ext->creation_date = tempstr;

  // Name of observer
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading observer name" << endl;

  dfault = obs_ext->observer;
  psrfits_read_key (fptr, "OBSERVER", &tempstr, dfault, verbose > 2);
  obs_ext->observer = tempstr;
  
  if (verbose > 2)
    cerr << "Got observer: " << tempstr << endl;
  
  // Project ID
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading project ID" << endl;

  dfault = obs_ext->project_ID;
  psrfits_read_key (fptr, "PROJID", &tempstr, dfault, verbose > 2);
  obs_ext->project_ID = tempstr;

  if (verbose > 2)
    cerr << "Got PID: " << tempstr << endl;
  
  // Telescope name
    
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading telescope name" << endl;

  dfault = get_telescope();
  psrfits_read_key (fptr, "TELESCOP", &tempstr, dfault, verbose > 2);

  tempstr = stringtok (tempstr, " ");
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header TELESCOP=" << tempstr << endl;
  
  set_telescope ( tempstr );

  Telescope* telescope = getadd<Telescope>();

  try
  {
    Telescopes::set_telescope_info (telescope, this);
  }
  catch (Error& error)
  {
    if (verbose > 2)
      cerr << "FITSArchive::load_header WARNING"
	" could not set telescope info for " << get_telescope() << "\n\t"
	   << error.get_message() << endl;
  }

  // RA
    
  psrfits_read_key( fptr, "RA", &tempstr, dfault, verbose > 2 );
  hdr_ext->set_ra( tempstr );
  
  // DEC
  
  psrfits_read_key( fptr, "DEC", &tempstr, dfault, verbose > 2 );
  hdr_ext->set_dec( tempstr );

  // Antenna ITRF coordinates

  load_ITRFExtension (fptr);

  // Receiver parameters

  load_Receiver (fptr);

  // WidebandCorrelator parameters

  load_WidebandCorrelator (fptr);

  Backend* backend = get<Backend>();
  if (backend && strstr (backend->get_name().c_str(), "BPP"))
  {
    if (verbose > 3)
      cerr << "FITSArchive::load_header using BPP matching policy" << endl;
    ThresholdMatch::set_BPP (this);
  }

  //
  //
  // Figure out what kind of observation it was
  //
  //

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading OBS_MODE" << endl;

  psrfits_read_key (fptr, "OBS_MODE", &tempstr);
  string obs_mode = tempstr;
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header OBS_MODE='" << obs_mode << "'" << endl;

  hdr_ext->set_obs_mode( obs_mode );

  if (obs_mode == "PSR" || obs_mode == "LEVPSR")
  {
    set_type ( Signal::Pulsar );
    if (verbose > 2)
      cerr << "FITSArchive::load_header using Signal::Pulsar" << endl;
  }
  else if (obs_mode == "CAL" || obs_mode == "LEVCAL")
    set_type ( Signal::PolnCal );
  else if (obs_mode == "FOF")
    set_type ( Signal::FluxCalOff );
  else if (obs_mode == "FON")
    set_type ( Signal::FluxCalOn );
  else if (obs_mode == "PCM")
    set_type ( Signal::Calibrator );

  else if (obs_mode == "SEARCH" || obs_mode == "SRCH")
  {
    search_mode = true;
    set_type ( Signal::Unknown );
    if (verbose > 2)
      cerr << "FITSArchive::load_header search mode file" << endl;
  }
  else
  {
    if (verbose > 2)
      cerr << "FITSArchive::load_header WARNING unknown OBSTYPE = " 
	   << tempstr <<endl;
    set_type ( Signal::Unknown );
  }

  //
  //
  // Read the centre frequency of the observation
  //
  //
  {
    double dfault = 0.0;
    double centre_frequency;

    psrfits_read_key (fptr, "OBSFREQ", &centre_frequency, dfault, verbose > 2);
    set_centre_frequency( centre_frequency );
    hdr_ext->set_obsfreq( centre_frequency );
  }

  // Read the bandwidth of the observation
  {
    double dfault = 0.0;
    double bandwidth;

    psrfits_read_key (fptr, "OBSBW", &bandwidth, dfault, verbose > 2);
    set_bandwidth( bandwidth );
    hdr_ext->set_obsbw( bandwidth );
  }

  // Read the number of channels of the observation
  {
    int obsnchan;

    psrfits_read_key (fptr, "OBSNCHAN", &obsnchan, 0, verbose > 2);
    hdr_ext->set_obsnchan( obsnchan );
  }

  // Read the name of the source

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading source name" << endl;

  psrfits_read_key (fptr, "SRC_NAME", &tempstr);
  set_source ( tempstr );
  
  // Read where the telescope was pointing
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading coordinates" << endl;

  dfault = hdr_ext->coordmode;
  psrfits_read_key (fptr, "COORD_MD", &tempstr, dfault, verbose > 2);
  hdr_ext->coordmode = tempstr;

  if (verbose > 2)
    cerr << "Got coordinate type: " << tempstr << endl;

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading equinox" << endl;

  dfault = "pre version 2.8";
  psrfits_read_key (fptr, "EQUINOX", &tempstr, dfault, verbose > 2);

  if ((tempstr == dfault) || (tempstr.empty()))
  {
    //
    // The file was created before the EQUINOX attribute was added,
    // or was mistakenly created with a null equinox.
    //
    hdr_ext->equinox = "2000.0";
  }
  else
  {
    hdr_ext->equinox = tempstr;

    if (verbose > 2)
      cerr << "Got equinox: " << tempstr << endl;
  }

  //
  // PSRFITS definitons versions before 2.8 have
  //   COORD_MD = J2000
  //   and no EQUINOX attribute
  //
  // PSRFITS definition versions 2.8 to 2.10 have
  //   COORD_MD = EQUAT
  //   EQUINOX = J2000
  //
  // PSRFITS definition version 2.11 has
  //   COORD_MD = J2000 (again)
  //   EQUINOX = 2000.0
  //

  if (hdr_ext->coordmode == "EQUAT")
    hdr_ext->coordmode = "J2000";

  if (hdr_ext->equinox == "J2000")
    hdr_ext->equinox = "2000.0";

  //
  // ... also, since PSRFITS definition version 2.8, "Gal" is "GAL"
  //
  if (hdr_ext->coordmode == "Gal" || hdr_ext->coordmode == "GALACTIC")
    hdr_ext->coordmode = "GAL";

  //
  // ... also, if unset assume J2000
  //
  if (hdr_ext->coordmode == "UNSET")
  {
    warning << "FITSArchive::load_header WARNING"
      " assuming J2000 coordinates" << endl;
    hdr_ext->coordmode = "J2000";
  }

  sky_coord coord;
  
  if (hdr_ext->coordmode == "J2000")
  {
    dfault = "";

    psrfits_read_key (fptr, "RA", &tempstr, dfault, verbose > 2);

    string stt_crd1 = "";
    psrfits_read_key (fptr, "STT_CRD1", &stt_crd1, dfault, verbose > 2);
    hdr_ext->set_stt_crd1(stt_crd1);

    // If RA exists, set the ra from the value of RA.
    // Otherwise, set the ra to the value of STT_CRD1
    const string hms = !tempstr.empty() ? tempstr : stt_crd1;

    psrfits_read_key (fptr, "DEC", &tempstr, dfault, verbose > 2);

    string stt_crd2 = "";
    psrfits_read_key (fptr, "STT_CRD2", &stt_crd2, dfault, verbose > 2);
    hdr_ext->set_stt_crd2(stt_crd2);

    // If DEC exists, set the dec from the value of DEC.
    // Otherwise, set the dec to the value of STT_CRD2
    const string dec = !tempstr.empty() ? tempstr : stt_crd2;
    coord.setHMSDMS (hms.c_str(), dec.c_str());

    psrfits_read_key (fptr, "STP_CRD1", &tempstr, dfault, verbose > 2);
    if (!tempstr.empty())
      hdr_ext->set_stp_crd1(tempstr);

    psrfits_read_key (fptr, "STP_CRD2", &tempstr, dfault, verbose > 2);
    if (!tempstr.empty())
      hdr_ext->set_stp_crd2(tempstr);
  }
  else if (hdr_ext->coordmode == "GAL")
  {
    double dfault = 0.0;
    double co_ord1, co_ord2;

    psrfits_read_key (fptr, "STT_CRD1", &co_ord1, dfault, verbose > 2);
    psrfits_read_key (fptr, "STT_CRD2", &co_ord2, dfault, verbose > 2);
    AnglePair temp;
    temp.setDegrees(co_ord1,co_ord2);
    coord.setGalactic(temp);
  }
  else if (verbose > 2)
    cerr << "FITSArchive::load_header WARNING COORD_MD="
	 << hdr_ext->coordmode << " not implemented" << endl;
  
  set_coordinates (coord);
  
  if (get_type() != Signal::Pulsar && get_type() != Signal::Unknown)
    load_CalInfoExtension (fptr);

  // Track mode

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading track mode" << endl;

  dfault = hdr_ext->trk_mode;
  psrfits_read_key (fptr, "TRK_MODE", &tempstr, dfault, verbose > 2);
  hdr_ext->trk_mode = tempstr;

  //
  // Since PSRFITS version 2.8, the UTC date and time are stored in one string
  //

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading observation date" << endl;

  dfault = "pre version 2.8";
  psrfits_read_key (fptr, "DATE-OBS", &tempstr, dfault, verbose > 2);

  if ((tempstr == dfault) || (tempstr.empty()))
  {
    //
    // Before version 2.8, the UTC date and time were stored separately
    //

    // Read the start UT date

    if (verbose > 2)
      cerr << "FITSArchive::load_header reading start date" << endl;

    dfault = hdr_ext->stt_date;
    psrfits_read_key (fptr, "STT_DATE", &tempstr, dfault, verbose > 2);
    hdr_ext->stt_date = tempstr;
    
    // Read the start UT

    if (verbose > 2)
      cerr << "FITSArchive::load_header reading start UT" << endl;
    
    dfault = hdr_ext->stt_time;
    psrfits_read_key (fptr, "STT_TIME", &tempstr, dfault, verbose > 2);
    
    // strip off any fractional seconds, if present
    size_t decimal = tempstr.find('.');
    if (decimal != string::npos)
      tempstr = tempstr.substr (0, decimal);

    hdr_ext->stt_time = tempstr;
  }
  else
  {
    //
    // Since version 2.8, the UTC date and time are stored as one string
    //

    //
    // Separate the date and time at the expected point in the string
    //
    const unsigned date_length = strlen ("YYYY-MM-DD");
    if (tempstr.length() >= date_length)
      hdr_ext->stt_date = tempstr.substr(0,date_length);
    if (tempstr.length() > date_length)
      hdr_ext->stt_time = tempstr.substr(date_length+1);

    if (verbose > 2)
      cerr << "FITSArchive::load_header DATE-0BS parsed into\n"
	" date='" << hdr_ext->stt_date << "'\n"
	" time='" << hdr_ext->stt_time << "'\n";
  }
  
  // Read the bpa
   
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading BPA" << endl;
    
  dfault = "0.0";
  psrfits_read_key( fptr, "BPA", &tempstr, dfault, verbose > 2 );
  if( tempstr == "*" )
    hdr_ext->set_bpa( 0 );
  else
    hdr_ext->set_bpa( fromstring<double>(tempstr) );
    
    // Read the bmaj
    
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading BMAJ" << endl;
    
  dfault = "0.0";
  psrfits_read_key( fptr, "BMAJ", &tempstr, dfault, verbose > 2 );
  if( tempstr == "*" )
    hdr_ext->set_bpa( 0 );
  else
    hdr_ext->set_bmaj( fromstring<double>(tempstr) );
    
    // Read the bmin
    
  if(verbose > 2 )
    cerr << "FITSArchive::load_header reading BMIN" << endl;
    
  dfault = "0.0";
  psrfits_read_key( fptr, "BMIN", &tempstr, dfault, verbose > 2 );
  if( tempstr == "*" )
    hdr_ext->set_bpa( 0 );
  else
    hdr_ext->set_bmin( fromstring<double>(tempstr) );

  // /////////////////////////////////////////////////////////////////
  
  // Read start MJD  

  long day;
  long sec;
  double frac;
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header reading MJDs" << endl;
  
  psrfits_read_key (fptr, "STT_IMJD", &day, (long)0, verbose > 2);
  psrfits_read_key (fptr, "STT_SMJD", &sec, (long)0, verbose > 2);
  psrfits_read_key (fptr, "STT_OFFS", &frac, 0.0, verbose > 2);
  
  hdr_ext->set_start_time( MJD ((int)day, (int)sec, frac) );

  if (verbose > 2)
    cerr << "FITSArchive::load_header"
      " MJD=" << hdr_ext->get_start_time().printall() << endl;

  // Read the start LST (in seconds)

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading start LST" << endl;

  psrfits_read_key (fptr, "STT_LST", &(hdr_ext->stt_lst), 0.0, verbose > 2);

  // Read the IBEAM value (for multibeam data)

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading IBEAM" << endl;

  // If the keyword does not exist, set ibeam value to a blank string

  dfault = "";
  psrfits_read_key (fptr, "IBEAM", &tempstr, dfault, verbose > 2);
  hdr_ext->ibeam = tempstr;

  if (verbose > 2)
    cerr << "FITSArchive::load_header IBEAM='" << hdr_ext->ibeam << "'" << endl;

  // Read the PNT_ID (for multibeam data)

  if (verbose > 2)
    cerr << "FITSArchive::load_header reading PNT_ID" << endl;

  // If PNT_ID does not exist, set pnt_id to ""

  string default_pnt_id_value  = "";
  psrfits_read_key (fptr, "PNT_ID", &(hdr_ext->pnt_id), default_pnt_id_value,
      verbose > 2);

  if (verbose > 2)
    cerr << "FITSArchive::load_header PNT_ID='" << hdr_ext->pnt_id << "'" << endl;



  // ////////////////////////////////////////////////////////////////
  
  // Finished with primary header information   
  
  // ////////////////////////////////////////////////////////////////
  
  if (verbose > 2)
    cerr << "FITSArchive::load_header finished with primary HDU" << endl;
  
  // Load the processing history
  load_ProcHistory (fptr);

  // Load the digitiser statistics
  load_DigitiserStatistics (fptr);
  
  // Load the digitiser counts
  load_DigitiserCounts(fptr );
  
  // Load the original bandpass data
  load_Passband (fptr);

  // Load the coherent dedispersion extension
  load_CoherentDedispersion (fptr);

  // Load the flux calibrator extension
  load_FluxCalibratorExtension (fptr);

  // Load the calibrator stokes parameters
  load_CalibratorStokes (fptr);

  // Load the calibration model description
  load_PolnCalibratorExtension (fptr);
  
  // Load the parameters from the SUBINT HDU
  load_FITSSUBHdrExtension( fptr );

  // Load the pulsar parameters
  if (get_type() == Signal::Pulsar)
    load_Parameters (fptr);

  // Load the pulse phase predictor
  load_Predictor (fptr);
  hdr_model = model;

  if (correct_P236_reference_epoch)
    P236_reference_epoch_correction ();

  load_integration_state (fptr);

  status = 0;
  
  // Finished with the file for now
  fits_close_file (fptr, &status);
  
  if (status)
    throw FITSError (status, "Pulsar::FITSArchive::load_header",
		     "fits_close_file");

  loaded_from_fits = true;

  if (verbose > 2)
    cerr << "FITSArchive::load_header exit" << endl;
  
}
catch (Error& error)
{
  throw error += "FITSArchive::load_header";
}