예제 #1
0
  void setup (Integration* subint, unsigned ichan)
  {
    Pulsar::MoreProfiles* more = NULL;

    unsigned npol = subint->get_npol();
    if (auxiliary)
    {
      more = subint->get_Profile (0,ichan)->get<Pulsar::MoreProfiles>();
      npol = more->get_size();
    }

    try {
      vector<unsigned> pols = ::indeces (npol, indeces);
      profiles.resize (pols.size());

      for (unsigned ipol=0; ipol < pols.size(); ipol++)
      {
	if (more)
	  profiles[ipol] = more->get_Profile( pols[ipol] );
	else
	  profiles[ipol] = subint->get_Profile ( pols[ipol],ichan);
      }
    } 
    catch (...)
    {
      // if parsing indeces fails, then assume that letter codes are used
      Reference::To<const PolnProfile> profile = new_Stokes (subint, ichan);
      profiles.resize (indeces.length());
      for (unsigned ipol=0; ipol < indeces.length(); ipol++)
	profiles[ipol] = new_Profile (profile, indeces[ipol]);
    } 
  }
예제 #2
0
파일: psradd.C 프로젝트: lbaehren/lofarsoft
void psradd::append (Pulsar::Archive* archive) try
{
  if (phase_align)
  {
    Reference::To<Pulsar::Archive> standard;
    standard = total->total();
    Pulsar::Profile* std = standard->get_Profile(0,0,0);
    
    Reference::To<Pulsar::Archive> observation;
    observation = archive->total();
    Pulsar::Profile* obs = observation->get_Profile(0,0,0);
    
    archive->rotate_phase( obs->shift(std).get_value() );
  }

  if (archive->get_state() != total->get_state())
  {
    if (verbose)
      cerr << "psradd: converting state" 
	   << " from " << archive->get_state()
	   << " to " << total->get_state() << endl;
    
    archive->convert_state( total->get_state() );
  }
  
  if (patch)
  {
    if (verbose)
      cerr << "psradd: patching any missing sub-integrations" << endl;
    patch->operate (total, archive);
  }
  
  if (verbose)
    cerr << "psradd: appending " << archive->get_filename() << endl;

  if (time_direction)
    time.append (total, archive);
  else
    frequency.append (total, archive);

  if (log_file)
    fprintf (log_file, " %s", archive->get_filename().c_str());

  if (very_verbose)
    cerr << "psradd: after append, instance count = " 
	 << Reference::Able::get_instance_count() << endl;
}
catch (Error& error)
{
  cerr << "psradd: Archive::append exception:\n" << error << endl;
  if (auto_add)
    reset_total = true;
}
catch (...)
{
  cerr << "psradd: Archive::append exception thrown" << endl;
  if (auto_add)
    reset_total = true;
}
예제 #3
0
파일: paz.C 프로젝트: SkyTian13/psrchive
void paz::setup ()
{
  if (!killfile.empty ())
  {
    FILE *fptr = fopen (killfile.c_str (), "r");

    if (!fptr)
      throw Error (FailedSys, "paz::setup", "fopen " + killfile);

    char *buffer = new char[4096];
    while (fgets (buffer, 4096, fptr))
    {
      char* key = strtok (buffer, whitespace);
      while (key)
      {
	chans_to_zero.push_back (fromstring<unsigned>(key));
	key = strtok (NULL, whitespace);
      }
    }
  }

  zero_channels = chans_to_zero.size()
    || freqs_to_zero.size() || freq_ranges_to_zero.size();

  bool zero_subints = subs_to_zero.size ();

  if (zero_intersection && !(zero_channels && zero_subints))
    throw Error (InvalidState, "paz::setup",
		 "must use -I with both -s|S|k *and* -w|W");
  
  if (mow_all_subints || subints_to_mow.size())
    mower = new Pulsar::LawnMower;
  
  if (median_zap || median_zap_bybin || median_zap_window)
  {
    median_zapper = new Pulsar::ChannelZapMedian;
    median_zapper->set_bybin (median_zap_bybin);
    median_zapper->set_paz_report (true);

    if (median_zap_window)
      median_zapper->set_window_size (median_zap_window);
  }

  if (edge_zap_percent < 0.0 || edge_zap_percent >= 100.0)
    throw Error (InvalidState, "paz::setup",
		 "invalid parameter to option -E");

  if (!std_filename.empty())
  {
    Reference::To<Pulsar::Archive> data = Pulsar::Archive::load (std_filename);
    data->pscrunch ();
    data->fscrunch ();
    data->tscrunch ();
    thestd = data->get_Profile (0, 0, 0);
    
    standard_snr.set_standard (thestd);
    Pulsar::Profile::snr_strategy.get_value().set (&standard_snr,
						   &Pulsar::StandardSNR::get_snr);
  }
}
예제 #4
0
void psrflux::set_standard(Archive *arch)
{
  // Convert
  stdarch = arch->total();
  stdarch->convert_state(Signal::Intensity);

  // Set up DS calculation
  Reference::To<StandardFlux> flux = new StandardFlux;
  flux->set_fit_shift(true); // always init with true, choose later
  flux->set_standard(stdarch->get_Profile(0,0,0));
  ds.set_flux_method(flux);
}
예제 #5
0
void psrflux::process (Pulsar::Archive* archive)
{
  // Convert to total intensity
  archive->convert_state(Signal::Intensity);

  // Set self-standard if needed
  if (stdfile=="") set_standard(archive);

  // Test for single-profile data
  bool single_profile = archive->get_nsubint()==1 && archive->get_nchan()==1;

  // Access to the flux computation
  StandardFlux *flux = dynamic_cast<StandardFlux*>(ds.get_flux_method().get());

  // If shifts not fit, need to dedisperse and possibly align total
  // with standard.
  if (noalign) {
    archive->dedisperse();
    flux->set_fit_shift(false);
  } else if (align==false && single_profile==false) {
    archive->dedisperse();
    Reference::To<Archive> arch_tot = archive->total();
    Estimate<double> shift = 
      arch_tot->get_Profile(0,0,0)->shift(stdarch->get_Profile(0,0,0));
    stdarch->get_Profile(0,0,0)->rotate_phase(-1.0*shift.get_value());
    flux->set_fit_shift(false);
  } else {
    flux->set_fit_shift(true);
  }

  // Compute DS
  ds.set_Archive(archive);
  ds.compute();

  // Unload archive with .sm extension
  std::string outf = archive->get_filename() + "." + ext;
  cerr << "psrflux: unloading " << outf << endl;
  ds.unload(outf, command);

}
예제 #6
0
string Pulsar::SNRatioInterpreter::standard (const string& args) try
{
  if (!standard_snratio)
    standard_snratio = new StandardSNR;

  if (!standard_functor)
    standard_functor.set( standard_snratio.get(), &StandardSNR::get_snr );

  Reference::To<Archive> archive = Archive::load ( args );
  standard_snratio->set_standard( archive->get_Profile (0,0,0) );

  Profile::snr_strategy = standard_functor;
  return "";
}
catch (Error& error) {
  return error.get_message();
}
예제 #7
0
/**
 * Zap a specified range of bins by substituting their values with the
 * baseline mean for each profile.
 */
void zap_bins(Pulsar::Archive* arch, Pulsar::Archive* old_arch,
    const unsigned s, const unsigned start_bin, const unsigned end_bin)
{
  const unsigned npol  = old_arch->get_npol();
  const unsigned nchan = old_arch->get_nchan();

  // Get the baseline region from what is displayed on screen (pscrunched
  // and fscrunched integration).
  Reference::To<Pulsar::Archive> copy = old_arch->clone();
  copy->pscrunch();
  copy->fscrunch();

  Reference::To<Pulsar::PhaseWeight> baseline_weights =
    copy->get_Profile(s,0,0)->baseline();

  vector<float> weights;
  baseline_weights->get_weights(weights);

  for (unsigned ipol = 0; ipol < npol; ++ipol) {
    for (unsigned ichan = 0; ichan < nchan; ++ichan) {
      // Apply the baseline region to each profile and get the mean.
      baseline_weights->set_Profile(old_arch->get_Profile(s,ipol,ichan));
      const double baseline_mean = baseline_weights->get_mean().get_value();
      float* bins = old_arch->get_Profile(s,ipol,ichan)->get_amps();

      // Set all the bins between start_bin and end_bin to the mean of the
      // baseline.
      for (unsigned ibin = start_bin; ibin <= end_bin; ++ibin) {
        bins[ibin] = baseline_mean;
      }
    }
  }

  *arch = *old_arch;
  arch->set_dispersion_measure(0);
  arch->pscrunch();
  arch->fscrunch();
  arch->remove_baseline();
}
예제 #8
0
파일: psrpca.C 프로젝트: lbaehren/lofarsoft
void psrpca::finalize ()
{
  arrival->set_observation ( total );
  arrival->get_toas(toas);

  if ( remove_std_baseline )
    std_archive -> remove_baseline ();
  Reference::To<Profile> std_prof = std_archive->get_Profile(0, 0, 0);
  float *s_amps = std_prof->get_amps ();
  const float nbin = std_prof->get_nbin ();

  double scale, offset, snr;

  if ( total_count < nbin )
    cerr << "WARNING: psrpca::finalize - not enough observations provided, "
	    "covariance matrix will not have full rank" << endl;

  //total->remove_baseline();

  gsl_matrix *profiles = gsl_matrix_alloc ( (unsigned)nbin, total_count );

  for (unsigned i_subint = 0; i_subint < total->get_nsubint(); i_subint++ )
  {
    Reference::To<Profile> prof = total->get_Profile ( i_subint, 0, 0 );
    if ( apply_shift )
      prof->rotate_phase ( toas[i_subint].get_phase_shift() );
    snr = prof->snr ();

    //calculate the scale
    float *p_amps = prof->get_amps ();
    scale = 0.0;

    for ( unsigned i_bin = 0; i_bin < nbin; i_bin++ )
    {
      scale += s_amps[i_bin] * p_amps[i_bin];
    }
    scale = (prof->get_nbin()* scale - prof->sum() * std_prof->sum()) /
	    (prof->get_nbin()* std_prof->sumsq() - std_prof->sum() * std_prof->sum());

    // calculate the baseline offset
    offset = (scale * std_prof->sum() - prof->sum()) / nbin;

    if ( prof_to_std )
    {
      //match the profile to standard and subtract the standard
      if ( apply_offset )
	prof->offset  ( offset );
      if ( apply_scale )
	prof->scale ( 1.0/scale );
      prof->diff ( std_prof );

      double* damps;
      damps = new double [ (unsigned)nbin ];
      transform( prof->get_amps(), prof->get_amps() + (unsigned)nbin, damps, CastToDouble() );

      gsl_vector_const_view view = gsl_vector_const_view_array( damps, nbin );
      gsl_matrix_set_col ( profiles, i_subint, &view.vector );
      t_cov->add_Profile ( prof, snr );
    }
    else
    {// prof_to_std is false
      Reference::To<Profile> diff = prof->clone ();
      diff->set_amps ( std_prof->get_amps () );
      if ( apply_offset ) 
	diff->offset( -offset );
      if ( apply_scale ) 
	diff->scale (scale);
      diff->diff ( prof );
      diff->scale (-1);

      double* damps;
      damps = new double [ (unsigned)nbin ];
      transform( diff->get_amps(), diff->get_amps() + (unsigned)nbin, damps, CastToDouble() );

      gsl_vector_const_view view = gsl_vector_const_view_array( damps, nbin );
      gsl_matrix_set_col ( profiles, i_subint, &view.vector );

      t_cov->add_Profile ( diff, snr );
      prof->set_amps ( diff->get_amps() );
    }
  }

  covariance = gsl_matrix_alloc ( (int) nbin, (int) nbin );
  t_cov->get_covariance_matrix_gsl ( covariance );

  // write the covariance matrix and difference profiles
  FILE *out;
  if ( save_covariance_matrix )
  {
    out = fopen ( (prefix+"_covariance.dat").c_str(), "w" );
    gsl_matrix_fprintf(out, covariance, "%g");
    fclose ( out );
  } // save covariance matrix

  if ( save_diffs ) 
    total->unload ( prefix+"_diffs.ar" );

  //solve the eigenproblem
  gsl_matrix_view m = gsl_matrix_submatrix ( covariance, 0, 0, (int)nbin, (int)nbin );
  gsl_vector *eval = gsl_vector_alloc ( (int)nbin );
  gsl_matrix *evec = gsl_matrix_alloc ( (int)nbin, (int)nbin );

  gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc ( (int)nbin );
  gsl_eigen_symmv ( &m.matrix, eval, evec, w );
  gsl_eigen_symmv_free ( w );
  gsl_eigen_symmv_sort ( eval, evec, GSL_EIGEN_SORT_VAL_DESC );

  // save evectors
  if ( save_evecs )
  {
    Reference::To<Archive> evecs_archive = total->clone();
    gsl_vector *evec_copy = gsl_vector_alloc ( (int)nbin );

    for (unsigned iext=0; iext < evecs_archive->get_nextension(); iext++)
    {
      delete evecs_archive->get_extension(iext);
    }

    evecs_archive->resize ( (unsigned)nbin, 1, 1, (unsigned)nbin );

    for (unsigned i_evec = 0; i_evec < (unsigned)nbin; i_evec++ )
    {
      gsl_vector_view view = gsl_matrix_column(evec, i_evec);

      gsl_vector_memcpy ( evec_copy, &view.vector );
      evecs_archive->get_Profile ( i_evec, 0, 0 ) -> set_amps ( evec_copy->data );
    }
    evecs_archive->unload ( prefix+"_evecs.ar" );
  } // save evectors

  if ( save_evals )
  {
    out = fopen ( (prefix+"_evals.dat").c_str(), "w" );
    gsl_vector_fprintf ( out, eval, "%g" );
    fclose ( out );
  } // save_evals

  // decompose profiles onto eigenvectors
  gsl_matrix *decompositions = gsl_matrix_alloc ( (unsigned)nbin, total_count );

  gsl_blas_dgemm ( CblasTrans, CblasNoTrans, 1.0, evec, profiles, 0.0, decompositions );

  if ( save_decomps )
  {
    out = fopen ( (prefix + "_decomposition.dat").c_str(), "w" );
    gsl_matrix_fprintf ( out, decompositions, "%g");
    fclose ( out );
  } // save decompositions

  if ( !residuals_file.empty() )
  {
    //read in the residuals:
    double tmp;
    unsigned residual_count = 0;

    gsl_vector *mjds = gsl_vector_alloc ( total->get_nsubint() );
    gsl_vector *residuals = gsl_vector_alloc ( total->get_nsubint() );
    gsl_vector *residuals_err = gsl_vector_alloc ( total->get_nsubint() );
    //TODO check the format of the input
    ifstream inFile( residuals_file.c_str() );
    if ( inFile.is_open() )
    {
      while ( inFile.good() )
      {
	inFile >> tmp;
	if ( !inFile.good() )
	  break;
	gsl_vector_set(mjds, residual_count, tmp);

	inFile >> tmp;
	gsl_vector_set(residuals, residual_count, tmp*1e6);

	inFile >> tmp;
	gsl_vector_set(residuals_err, residual_count, tmp);

	residual_count ++ ;
      }
      inFile.close ();
      if ( residual_count != total->get_nsubint() )
      {
	cerr << "psrpca::finalize wrong number of residuals provided. Got " << residual_count 
		<< " while needed " << total->get_nsubint()<< endl;
	exit (-1) ;
      }

    }
    else
    {
예제 #9
0
파일: pam.C 프로젝트: lbaehren/lofarsoft
int main (int argc, char *argv[]) try {
  
    bool verbose = false;
    char* metafile = 0;
 
    string ulpath;

    bool save = false;
    string ext;
  
    bool tscr = false;
    int tscr_fac = 0;

    bool fscr = false;
    int fscr_fac = 0;

    bool bscr = false;
    int bscr_fac = 0;

    bool newdm = false;
    double dm = 0.0;

    bool scattered_power_correction = false;

    bool defaraday = false;

    bool newrm = false;
    double rm = 0.0;

    bool reset_weights = false;
    float new_weight = 1.0;

    float smear_dc = 0.0;

    bool rotate = false;
    double rphase = 0.0;

    bool dedisperse = false;
    bool dededisperse = false;

    bool pscr = false;

    bool invint = false;

    bool stokesify = false;
    bool unstokesify = false;

    bool flipsb = false;
    bool flip_freq = false;
    double flip_freq_mhz = 0.0;

    Pulsar::Parameters* new_eph = 0;

    string command = "pam";

    char* archive_class = 0;

    int new_nchn = 0;
    int new_nsub = 0;
    int new_nbin = 0;

    float tsub = 0.0;

    bool circ = false;
    bool lin = false;

    unsigned ronsub = 0;
    bool cbppo = false;
    bool cbpao = false;
    bool cblpo = false;
    bool cblao = false;

    int subint_extract_start = -1;
    int subint_extract_end = -1;

    bool new_cfreq = false;
    double new_fr = 0.0;
    Signal::Source new_type = Signal::Unknown;
    string instrument;
    bool reverse_freqs = false;
    string site;
    string name;
    float mult = -1.0;
    double new_folding_period = -1.0;

    bool update_dm_from_eph = false;
    double aux_rm = 0.0;

    Reference::To<Pulsar::IntegrationOrder> myio;
    Reference::To<Pulsar::Receiver> install_receiver;

    Pulsar::ReflectStokes reflections;

    int c = 0;

    const int TYPE = 1208;
    const int INST = 1209;
    const int REVERSE_FREQS = 1210;
    const int SITE = 1211;
    const int NAME = 1212;
    const int DD   = 1213;
    const int RR   = 1214;
    const int SPC  = 1215;
    const int RM   = 1216;
    const int MULT = 1218;
    const int PERIOD=1219;
    const int SS   = 1220;
    const int FLIP = 1221;
    const int UPDATE_DM = 1222;
    const int AUX_RM = 1223;

    while (1) {

      int options_index = 0;

      static struct option long_options[] = {
	{"setnchn",    1, 0, 200},
	{"setnsub",    1, 0, 201},
	{"setnbin",    1, 0, 202},
	{"binphsperi", 1, 0, 203},
	{"binphsasc",  1, 0, 204},
	{"binlngperi", 1, 0, 205},
	{"binlngasc",  1, 0, 206},
	{"receiver",   1, 0, 207},
	{"settsub",    1, 0, 208},
	{"type",       1, 0, TYPE},
	{"inst",       1, 0, INST},
	{"reverse_freqs",no_argument,0,REVERSE_FREQS},
	{"flip",       1 ,0, FLIP},
	{"site",       1, 0, SITE},
	{"name",       1, 0, NAME},
	{"DD",         no_argument,      0,DD},
	{"RR",         no_argument,      0,RR},
	{"RM",         required_argument,0,RM},
	{"spc",        no_argument,      0,SPC},
	{"mult",       required_argument,0,MULT},
	{"period",     required_argument,0,PERIOD},
	{"SS",         no_argument,      0,SS},
	{"update_dm",   no_argument,      0,UPDATE_DM},
	{"aux_rm",    required_argument,0,AUX_RM},
	{0, 0, 0, 0}
      };

      c = getopt_long(argc, argv, "hqvViM:mn:a:e:E:TFpIt:f:b:d:o:s:r:u:w:DSBLCx:R:",
		      long_options, &options_index);

      if (c == -1)
	break;

      switch (c) {
      case 'h':
	usage();
	return (0);
	break;
      case 'q':
	Pulsar::Archive::set_verbosity(0);
	break;
      case 'v':
	verbose = true;
	Pulsar::Archive::set_verbosity(2);
	break;
      case 'V':
	verbose = true;
	Pulsar::Archive::set_verbosity(3);
	break;
      case 'i':
	cout << "$Id: pam.C,v 1.101 2010/10/05 23:59:50 jonathan_khoo Exp $" << endl;
	return 0;
      case 'm':
	save = true;
	break;
      case 'M':
        metafile = optarg;
        break;
      case 'L':
	lin = true;
	break;
      case 'C':
	circ = true;
	break;
      case 'a':
	archive_class = optarg;
	break;
      case 'e':
	ext = optarg;
	if( !ext.empty() )
	  save = true;
	break;
      case 'E':

	try {
	  new_eph = factory<Pulsar::Parameters> (optarg);
	}
	catch (Error& error) {
	  cerr << "Could not load new ephemeris from " << optarg << endl;
	  return -1;
	}

	command += " -E";
	break;
      case 'T':
	tscr = true;
	command += " -T";
	break;
      case 'F':
	fscr = true;
	command += " -F";
	break;
      case 'p':
	pscr = true;
	command += " -p";
	break;
      case 'I':
	invint = true;
	pscr = false;
	command += " -I";
	break;
      case 'f':
	fscr = true;
	if (sscanf(optarg, "%d", &fscr_fac) != 1) {
	  cout << "That is not a valid fscrunch factor" << endl;
	  return -1;
	}
	command += " -f ";
	command += optarg;
	break;
	
      case 'n':

	reflections.add_reflection( optarg[0] );

	command += " -n ";
	command += optarg;
	break;

      case 'o':
	new_cfreq = true;
	if (sscanf(optarg, "%lf", &new_fr) != 1) {
	  cout << "That is not a valid centre frequency" << endl;
	  return -1;
	}
	command += " -o ";
	command += optarg;
	break;
      case 't':
	tscr = true;
	if (sscanf(optarg, "%d", &tscr_fac) != 1) {
	  cout << "That is not a valid tscrunch factor" << endl;
	  return -1;
	}
	command += " -t ";
	command += optarg;
	break;
      case 'b':
	bscr = true;
	if (sscanf(optarg, "%d", &bscr_fac) != 1) {
	  cout << "That is not a valid bscrunch factor" << endl;
	  return -1;
	}
	if (bscr_fac <= 0) {
	  cout << "That is not a valid bscrunch factor" << endl;
	  return -1;
	}
	command += " -b ";
	command += optarg;
	break;
      case 'd':
	newdm = true;
	if (sscanf(optarg, "%lf", &dm) != 1) {
	  cout << "That is not a valid dispersion measure" << endl;
	  return -1;
	}
	command += " -d ";
	command += optarg;
	break;
      case 'D':
	dedisperse = true;
	command += " -D ";
	break;
      case 'R':
	if (sscanf(optarg, "%lf", &rm) != 1) {
	  cout << "That is not a valid rotation measure" << endl;
	  return -1;
	}
	newrm = true;
	defaraday = true;
	command += " -R ";
	command += optarg;
	break;
      case 's':
	if (sscanf(optarg, "%f", &smear_dc) != 1) {
	  cout << "That is not a valid smearing duty cycle" << endl;
	  return -1;
	}
	command += " -s ";
	command += optarg;
	break;
      case 'r':
	rotate = true;
	if (sscanf(optarg, "%lf", &rphase) != 1) {
	  cout << "That is not a valid rotation phase" << endl;
	  return -1;
	}
	if (rphase <= -1.0 || rphase >= 1.0) {
	  cout << "That is not a valid rotation phase" << endl;
	  return -1;
	}
	command += " -r ";
	command += optarg;
	break;
      case 'u':
	ulpath = optarg;
	if( !ulpath.empty() )
	{
	  save = true;
	  if (ulpath.substr(ulpath.length()-1,1) != "/")
	    ulpath += "/";
	}
	break;
      case 'w':
	reset_weights = true;
	if (sscanf(optarg, "%f", &new_weight) != 1) {
	  cout << "That is not a valid weight" << endl;
	  return -1;
	}
	command += " -w ";
	command += optarg;
	break;
      case 'S':
	stokesify = true;
	break;
      case SS:
        unstokesify = true;
        break;
      case 'B':
	flipsb = true;
	break;
      case 'x' :
	if (sscanf(optarg, "%d %d", 
		   &subint_extract_start, &subint_extract_end) !=2 ) {
	  cout << "That is not a valid subint range" << endl;
	  return -1;
	}
	subint_extract_end++;
	break;
      case 200:
	fscr = true;
	if (sscanf(optarg, "%d", &new_nchn) != 1) {
	  cout << "That is not a valid number of channels" << endl;
	  return -1;
	}
	if (new_nchn <= 0) {
	  cout << "That is not a valid number of channels" << endl;
	  return -1;
	}
	command += " --setnchn ";
	command += optarg;
	break;
      case 201:
	tscr = true;
	if (sscanf(optarg, "%d", &new_nsub) != 1) {
	  cout << "That is not a valid number of subints" << endl;
	  return -1;
	}
	if (new_nsub <= 0) {
	  cout << "That is not a valid number of subints" << endl;
	  return -1;
	}
	command += " --setnsub ";
	command += optarg;
	break;
      case 202:
	bscr = true;
	if (sscanf(optarg, "%d", &new_nbin) != 1) {
	  cout << "That is not a valid number of bins" << endl;
	  return -1;
	}
	if (new_nbin <= 0) {
	  cout << "That is not a valid number of bins" << endl;
	  return -1;
	}
	command += " --setnbin ";
	command += optarg;
	break;
      case 203: {
	if (cbpao || cblpo || cblao) {
	  cerr << "You can only specify one re-ordering scheme!"
	       << endl;
	  return -1;
	}
	if (sscanf(optarg, "%ud", &ronsub) != 1) {
	  cerr << "Invalid nsub given" << endl;
	  return -1;
	}
	cbppo = true;
	break;
      } 
      case 204: {
	if (cbppo || cblpo || cblao) {
	  cerr << "You can only specify one re-ordering scheme!"
	       << endl;
	  return -1;
	}
	if (sscanf(optarg, "%ud", &ronsub) != 1) {
	  cerr << "Invalid nsub given" << endl;
	  return -1;
	}
	cbpao = true;
	break;
      }      
      case 205: {
	if (cblao || cbppo || cbpao) {
	  cerr << "You can only specify one re-ordering scheme!"
	       << endl;
	  return -1;
	}
	if (sscanf(optarg, "%ud", &ronsub) != 1) {
	  cerr << "Invalid nsub given" << endl;
	  return -1;
	}
	cblpo = true;
	break;
      } 
      case 206: {
	if (cblpo || cbppo || cbpao) {
	  cerr << "You can only specify one re-ordering scheme!"
	       << endl;
	  return -1;
	}
	if (sscanf(optarg, "%ud", &ronsub) != 1) {
	  cerr << "Invalid nsub given" << endl;
	  return -1;
	}
	cblao = true;
	break;
      }

      case 207: try {
	install_receiver = Pulsar::Receiver::load (optarg);
	break;
      }
      catch (Error& error) {
	cerr << "pam: Error loading Receiver from " << optarg << endl
	     << error.get_message() << endl;
	return -1;
      }

      case 208: {
	if (sscanf(optarg, "%f", &tsub) != 1) {
	  cerr << "Invalid tsub given" << endl;
	  return -1;
	}
	tscr = true;
	break;
      }

      case TYPE:
	{
	  string s = optarg;
	  if(s=="Pulsar")     new_type = Signal::Pulsar;
	  else if(s=="PolnCal")    new_type = Signal::PolnCal;
	  else if(s=="FluxCalOn")  new_type = Signal::FluxCalOn;
	  else if(s=="FluxCalOff") new_type = Signal::FluxCalOff;
	  else if(s=="Calibrator") new_type = Signal::Calibrator;
	  else{
	    fprintf(stderr,"Unrecognised argument to --type: '%s'\n",optarg);
	    exit(-1);
	  }
	  command += " --type " + s;
	}
	break;

      case INST: instrument = optarg; break;

      case REVERSE_FREQS: reverse_freqs = true; break;

      case SITE: site = optarg; break;

      case NAME: name = optarg; break;

      case DD: dededisperse = true; break;

      case RM:
        aux_rm = fromstring<double>(optarg);
        newrm = true;
        command += " --RM ";
        command += optarg;
        break;

      case SPC: scattered_power_correction = true; break;
	
      case MULT: mult = atof(optarg); break;

      case PERIOD: new_folding_period = fromstring<double>(optarg); break;

      case FLIP: flip_freq = true; flip_freq_mhz = atof(optarg); break;

      case UPDATE_DM: update_dm_from_eph = true; break;

      case AUX_RM:
        aux_rm = fromstring<double>(optarg);
        command += " --aux_rm ";
        command += optarg;
        break;

      default:
	cout << "Unrecognised option" << endl;
      }
    }

   if (verbose)
     cerr << "pam: parsing filenames" << endl;
 
   vector <string> filenames;

    if (metafile)
      stringfload (&filenames, metafile);
    else
      for (int ai=optind; ai<argc; ai++)
        dirglob (&filenames, argv[ai]);
 
    if (filenames.empty())
    {
      cerr << "pam: no filenames were specified" << endl;
      exit(-1);
    } 
  
    Reference::To<Pulsar::Archive> arch;

    if (!save)
    {
      cout << "Changes will not be saved. Use -m, -u or -e to write results to disk"
	   << endl;
    }

    if (stokesify && unstokesify)
    {
      cerr << "pam: Both -S and --SS options were given.  Poln state will not be changed!" << endl;
      stokesify = false;
      unstokesify = false;
    }

    int flip_option_count=0;
    if (flipsb) flip_option_count++;
    if (flip_freq) flip_option_count++;
    if (reverse_freqs) flip_option_count++;
    if (flip_option_count > 1) {
      cerr << "pam: More than one band-flip option was given, exiting." << endl;
      exit(-1);
    }

    for (unsigned i = 0; i < filenames.size(); i++) try
    {
      if (verbose)
	cerr << "Loading " << filenames[i] << endl;
      
      arch = Pulsar::Archive::load(filenames[i]);

      if( mult > 0.0 ){
	for( unsigned isub=0; isub<arch->get_nsubint();isub++)
	  for( unsigned ichan=0; ichan<arch->get_nchan();ichan++)
	    for( unsigned ipol=0; ipol<arch->get_npol();ipol++)
	      arch->get_Profile(isub,ipol,ichan)->scale( mult );
      }

      if( new_folding_period > 0.0 ){
	Pulsar::counter_drift( arch, new_folding_period, 0.0);
	for( unsigned isub=0; isub<arch->get_nsubint();isub++)
	  arch->get_Integration(isub)->set_folding_period( new_folding_period );
      }

      if (install_receiver) {
	if (verbose)
	  cerr << "pam: Installing receiver: " << install_receiver->get_name()
	       << " in archive" << endl;

	arch->add_extension (install_receiver);
      }

      if (lin || circ) {
	Pulsar::Receiver* receiver = arch->get<Pulsar::Receiver>();

	if (!receiver)
	  cerr << "No Receiver Extension in " << filenames[i] << endl;
	else {
	  if (lin) {
	    receiver->set_basis (Signal::Linear);
	    cout << "Feed basis set to Linear" << endl;
	  }

	  if (circ) {
	    receiver->set_basis (Signal::Circular);
	    cout << "Feed basis set to Circular" << endl;
	  }
	}
      }

      reflections.transform( arch );

      if (new_cfreq)
      {
	double nc = arch->get_nchan();
	double bw = arch->get_bandwidth();
	double cw = bw / nc;

	double fr = new_fr - (bw / 2.0) + (cw / 2.0);
	
	for (unsigned i = 0; i < arch->get_nsubint(); i++) {
	  for (unsigned j = 0; j < arch->get_nchan(); j++) {
	    arch->get_Integration(i)->set_centre_frequency(j,(fr + (j*cw)));
	  }
	}
	
	arch->set_centre_frequency(new_fr);
      }

      if( new_type != Signal::Unknown )
	arch->set_type( new_type );

      if( instrument != string() ){
	Pulsar::Backend* b = arch->get<Pulsar::Backend>();
	if( !b )
	  fprintf(stderr,"Could not change instrument name- archive does not have Backend extension\n");
	else
	  b->set_name(instrument);
      }

      if( site != string() )
	arch->set_telescope( site );

      if( name != string() )
	arch->set_source( name );

      if (new_eph) try
      {
        arch->set_ephemeris(new_eph);

        if (update_dm_from_eph) {
          update_dm(arch);
        }
      }
      catch (Error& error)
      {
	cerr << "Error while installing new ephemeris: " 
	     << error.get_message() << endl;
        continue;
      }

      if (flipsb) {
	for (unsigned i = 0; i < arch->get_nsubint(); i++) {
	  vector<double> labels;
	  labels.resize(arch->get_nchan());
	  for (unsigned j = 0; j < arch->get_nchan(); j++) {
	    labels[j] = arch->get_Integration(i)->get_centre_frequency(j);
	  }
	  for (unsigned j = 0; j < arch->get_nchan(); j++) {
	    double new_frequency = labels[labels.size()-1-j];
	    arch->get_Integration(i)->set_centre_frequency(j,new_frequency);
	  }
	}
	arch->set_bandwidth(-1.0 * arch->get_bandwidth());
      }

      if (flip_freq) {
        for (unsigned isub = 0; isub < arch->get_nsubint(); isub++) {
          Reference::To<Pulsar::Integration> 
            subint = arch->get_Integration(isub);
          for (unsigned ichan = 0; ichan < arch->get_nchan(); ichan++) {
            double new_freq = flip_freq_mhz 
              - (subint->get_centre_frequency(ichan) - flip_freq_mhz);
            subint->set_centre_frequency(ichan, new_freq);
          }
        }
        arch->set_bandwidth(-1.0 * arch->get_bandwidth());
      }

      if( reverse_freqs ) {
	// Of course it would be nice to do this with pointers.... but oh well I guess copying will have to do HSK 27/8/04

	unsigned nchan = arch->get_nchan();

	for( unsigned isub=0; isub<arch->get_nsubint(); isub++){
	  for( unsigned ipol =0; ipol<arch->get_npol(); ipol++){
	    for( unsigned ichan=0; ichan<nchan/2; ichan++){
	      Reference::To<Pulsar::Profile> lo = arch->get_Profile(isub,ipol,ichan);	      
	      Reference::To<Pulsar::Profile> tmp = lo->clone();

	      Reference::To<Pulsar::Profile> hi = arch->get_Profile(isub,ipol,nchan-1-ichan);

	      lo->operator=(*hi);
	      hi->operator=(*tmp);
	    }
	  }
	}
	arch->set_bandwidth( -1.0 * arch->get_bandwidth() );
      }

      if (reset_weights) {
	arch->uniform_weight(new_weight);
	if (verbose)
	  cout << "All profile weights set to " << new_weight << endl;
      }
      
      if (rotate)
	arch->rotate_phase (rphase);

      if (scattered_power_correction) {

	Pulsar::ScatteredPowerCorrection spc;
	if (arch->get_state() == Signal::Stokes)
	  arch->convert_state(Signal::Coherence);

	spc.correct (arch);

      }

      if (newdm)
      {
	arch->set_dispersion_measure(dm);
	if (verbose)
	  cout << "Archive dispersion measure set to " << dm << endl;

	if (arch->get_dedispersed())
        {
	  arch->dedisperse();

	  if (verbose)
	    cout << "Archive re-dedipsersed" << endl;
        }
      }

      if (dedisperse)
      {
	arch->dedisperse();
	if (verbose)
	  cout << "Archive dedipsersed" << endl;
      }

      if (dededisperse)
      {
	Pulsar::Dispersion correction;
	correction.revert (arch);
      }

      if (stokesify) {
	if (arch->get_npol() != 4)
	  throw Error(InvalidState, "Convert to Stokes",
		      "Not enough polarisation information");
	arch->convert_state(Signal::Stokes);
	if (verbose)
	  cout << "Archive converted to Stokes parameters" << endl;
      }

      if (unstokesify) {
	if (arch->get_npol() != 4)
	  throw Error(InvalidState, "Convert to coherence",
		      "Not enough polarisation information");
	arch->convert_state(Signal::Coherence);
	if (verbose)
	  cout << "Archive converted to coherence parameters" << endl;
      }

      if (cbppo) {
	myio = new Pulsar::PeriastronOrder();
	arch->add_extension(myio);
	myio->organise(arch, ronsub);
      }
      
      if (cbpao) {
	myio = new Pulsar::BinaryPhaseOrder();
	arch->add_extension(myio);
	myio->organise(arch, ronsub);
      }
      
      if (cblpo) {
	myio = new Pulsar::BinLngPeriOrder();
	arch->add_extension(myio);
	myio->organise(arch, ronsub);
      }
      
      if (cblao) {
	myio = new Pulsar::BinLngAscOrder();
	arch->add_extension(myio);
	myio->organise(arch, ronsub);
      }
      
      if( subint_extract_start >= 0 && subint_extract_end >= 0 ) {
	vector<unsigned> subints;
	unsigned isub = subint_extract_start;

	while ( isub<arch->get_nsubint() && isub<unsigned(subint_extract_end) ) {
	  subints.push_back( isub );
	  isub++;
	}

	Reference::To<Pulsar::Archive> extracted( arch->extract(subints) );
	extracted->set_filename( arch->get_filename() );

	arch = extracted;
      }

      if (tscr) {
	if (tsub > 0.0) {
	  unsigned factor = 
	    unsigned (tsub / arch->get_Integration(0)->get_duration());
	  if (factor == 0) {
	    cerr << "Warning: subints already too long" << endl;
	  }
	  else {
	    arch->tscrunch(factor);
	  }
	  if (verbose)
	    cout << arch->get_filename() << " tscrunched by a factor of " 
		 << factor << endl;
	}
	else if (new_nsub > 0) {
	  arch->tscrunch_to_nsub(new_nsub);
	  if (verbose)
	    cout << arch->get_filename() << " tscrunched to " 
		 << new_nsub << " subints" << endl;
	}
	else if (tscr_fac > 0) {
	  arch->tscrunch(tscr_fac);
	  if (verbose)
	    cout << arch->get_filename() << " tscrunched by a factor of " 
		 << tscr_fac << endl;
	}
	else {
	  arch->tscrunch();
	  if (verbose)
	    cout << arch->get_filename() << " tscrunched" << endl;
	}
      }
      
      if (pscr) {
	arch->pscrunch();
	if (verbose)
	  cout << arch->get_filename() << " pscrunched" << endl;
      } 

      if (invint) {
	arch->invint();
	if (verbose)
	  cout << arch->get_filename() << " invinted" << endl;
      }

      if (newrm) {
	arch->set_rotation_measure (rm);
	if (verbose)
	  cout << arch->get_filename() << " RM set to " << rm << endl;
      }

      if (defaraday) {
	arch->defaraday();
	if (verbose)
	  cout << arch->get_filename() << " defaradayed" <<endl;
      }

      if (aux_rm)
      {
	if (verbose)
	  cout << "pam: correct auxiliary Faraday rotation; iono RM="
	       << aux_rm << endl;
	correct_auxiliary_rm (arch, aux_rm);
      }

      if (fscr) {
	if (new_nchn > 0) {
	  arch->fscrunch_to_nchan(new_nchn);
	  if (verbose)
	    cout << arch->get_filename() << " fscrunched to " 
		 << new_nchn << " channels" << endl;
	}
	else if (fscr_fac > 0) {
	  arch->fscrunch(fscr_fac);
	  if (verbose)
	    cout << arch->get_filename() << " fscrunched by a factor of " 
		 << fscr_fac << endl;
	}
	else {
	  arch->fscrunch();
	  if (verbose)
	    cout << arch->get_filename() << " fscrunched" << endl;
	}
      }

      if (bscr) {
	if (new_nbin > 0) {
	  arch->bscrunch_to_nbin(new_nbin);
	  if (verbose)
	    cout << arch->get_filename() << " bscrunched to " 
		 << new_nbin << " bins" << endl;
	}
	else {
	  arch->bscrunch(bscr_fac);
	  if (verbose)
	    cout << arch->get_filename() << " bscrunched by a factor of " 
		 << bscr_fac << endl;
	}
      }     

      if (smear_dc) {
	for (unsigned i = 0; i < arch->get_nsubint(); i++) {
	  for (unsigned j = 0; j < arch->get_npol(); j++) {
	    for (unsigned k = 0; k < arch->get_nchan(); k++) {
	      smear (arch->get_Profile(i,j,k), smear_dc);
	    }
	  }
	}
      }
      
      if (save) {

	if (archive_class)  {

	  // unload an archive of the specified class
	  Reference::To<Pulsar::Archive> output;
	  output = Pulsar::Archive::new_Archive (archive_class);
	  output -> copy (*arch);
	  output -> set_filename ( arch->get_filename() );

	  arch = output;

	}


	// See if the archive contains a history that should be updated:
	
	Pulsar::ProcHistory* fitsext = arch->get<Pulsar::ProcHistory>();
	
	if (fitsext) {
	  
	  if (command.length() > 80) {
	    cout << "WARNING: ProcHistory command string truncated to 80 chars" 
		 << endl;
	    fitsext->set_command_str(command.substr(0, 80));
	  }
	  else {
	    fitsext->set_command_str(command);
	  }
	  
	}
	
	string out_filename = arch->get_filename();
	
	if( !ext.empty() )
	  out_filename = replace_extension( out_filename, ext );
	
	if( !ulpath.empty() )
	  out_filename = ulpath + basename(out_filename);
	
	arch->unload( out_filename );
	cout << out_filename << " written to disk" << endl;
      }
    }  
    catch (Error& error) {
      cerr << error << endl;
    } 
  
    return 0;

}
catch(Error& er) 
{
  cerr << er << endl;
  return -1;
}
catch (string& error)
{
  cerr << "exception thrown: " << error << endl;
  return -1;
}
catch (bad_alloc& ba)
{
  cerr << "Caught a bad_alloc: '" << ba.what() << "'" << endl ;
  return -1;
}
catch (exception& e)
{
  cerr << "caught an exception of type '" 
				 << typeid(e).name() << "'" << endl; 
  return -1;
}
catch(...)
{
  fprintf(stderr,"Unknown exception caught\n");
  return -1;
}
Pulsar::PhaseWeight* Pulsar::Integration::baseline () const
{
  Reference::To<const Integration> total = this->total();
  return total->get_Profile(0,0)->baseline ();
}
예제 #11
0
/*! This function assumes that the Integration will have the global
  attributes of the file. */
Pulsar::Integration* 
Pulsar::FITSArchive::load_Integration (const char* filename, unsigned isubint)
try {

  if (!filename)
    throw Error (InvalidParam, "FITSArchive::load_Integration",
		 "filename unspecified");

  if (search_mode)
    throw Error (InvalidParam, "FITSArchive::load_Integration",
		 "SEARCH mode data -- no Integrations to load");

  Reference::To<Pulsar::Integration> integ = new_Integration();
  init_Integration (integ);

  int row = isubint + 1;
  
  int status = 0;  

  if (verbose > 2)
    cerr << "FITSArchive::load_Integration number " << isubint << endl;
  
  double nulldouble = 0.0;
  float nullfloat = 0.0;
  
  int initflag = 0;
  int colnum = 0;
  
  // Open the file

  fitsfile* fptr = 0;
  
  if (verbose > 2)
    cerr << "FITSArchive::load_Integration"
      " fits_open_file (" << filename << ")" << endl;
  
  fits_open_file (&fptr, filename, READONLY, &status);
  
  if (status != 0)
    throw FITSError (status, "FITSArchive::load_Integration", 
		     "fits_open_file(%s)", filename);
  

  // Move to the SUBINT Header Data Unit
  
  fits_movnam_hdu (fptr, BINARY_TBL, "SUBINT", 0, &status);
  if (status != 0)
    throw FITSError (status, "FITSArchive::load_Integration", 
		     "fits_movnam_hdu SUBINT");


  // Load the convention for the epoch definition
  string epoch_def;
  string default_def = "STT_MJD";
  psrfits_read_key (fptr, "EPOCHS", &epoch_def,
		    default_def, verbose > 2);

  if (verbose > 2)
    cerr << "FITSArchive::load_Integration epochs are " << epoch_def << endl;

  // By default, correct epochs using the phase model
  bool correct_epoch_phase = true;

  // By default, correct epochs such that phase(epoch)=phase(start)
  bool phase_match_start_time = true;

  if (epoch_def == "VALID")
    correct_epoch_phase = phase_match_start_time = false;

  if (epoch_def == "MIDTIME")
    phase_match_start_time = false;

  if (get<Pulsar::IntegrationOrder>())
  {
    colnum = 0;
    fits_get_colnum (fptr, CASEINSEN, "INDEXVAL", &colnum, &status);
    
    double value = 0.0;

    fits_read_col (fptr, TDOUBLE, colnum, row, 1, 1, &nulldouble,
		   &value, &initflag, &status);
    
    if (status != 0)
      throw FITSError (status, "FITSArchive::load_Integration", 
		       "fits_read_col INDEXVAL");
    
    get<Pulsar::IntegrationOrder>()->set_Index(row-1,value);
  }
  
  // Get the reference epoch from the primary header
  const Pulsar::FITSHdrExtension* hdr_ext = get<Pulsar::FITSHdrExtension>();
  
  if (!hdr_ext)
  {
    throw Error (InvalidParam, "FITSArchive::load_Integration",
		 "No FITSHdrExtension found");
  }
  
  // Set the duration of the integration
  
  colnum = 0;
  fits_get_colnum (fptr, CASEINSEN, "TSUBINT", &colnum, &status);
  
  double duration = 0.0;
  
  fits_read_col (fptr, TDOUBLE, colnum, row, 1, 1, &nulldouble,
		 &duration, &initflag, &status);
  
  integ->set_duration (duration);

  // Set the start time of the integration
  
  initflag = 0;
  colnum = 0;
  
  fits_get_colnum (fptr, CASEINSEN, "OFFS_SUB", &colnum, &status);
  
  double time = 0.0;
  fits_read_col (fptr, TDOUBLE, colnum, row, 1, 1, &nulldouble,
		 &time, &initflag, &status);
  
  if (status != 0)
    throw FITSError (status, "FITSArchive::load_Integration", 
		     "fits_read_col OFFS_SUB");


  MJD epoch = hdr_ext->get_start_time() + time;

  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration"
      " header epoch=" << hdr_ext->get_start_time().printdays(13) << "\n "
      " offset=" << time << "s epoch=" << epoch.printdays(13) << endl;
  
  // Set a preliminary epoch to avoid problems loading the polyco
  integ->set_epoch (epoch);

  // Set the folding period to 0 until one of three possible methods succeeds
  integ->set_folding_period (0.0);

  /* **********************************************************************

     METHOD 1: folding period defined by a pulse phase model

     ********************************************************************** */

  if (hdr_model)
  {
    // Set the folding period, using the polyco from the file header
    // This was taken out of the condition clause below because period
    // wasn't set when TSUB was 0
    integ->set_folding_period (1.0 / hdr_model->frequency(epoch));

    if (integ->get_folding_period() <= 0.0)
      throw Error( InvalidState, "Pulsar::FITSArchive::load_Integration",
		   "header polyco/predictor corrupted; "
		   "period(epoch=%s)=%lf", epoch.printdays(5).c_str(),
		   integ->get_folding_period() );

    if (integ->get_folding_period() < 1.0e-3)
      warning << "Pulsar::FITSArchive::load_Integration folding_period=" 
	      << integ->get_folding_period() << " is less than 1ms" << endl;

    else if (verbose > 2)
      cerr << "Pulsar::FITSArchive::load_Integration folding_period = "
      	   << integ->get_folding_period () << endl;

    if (duration && correct_epoch_phase)
    {

      if (verbose > 2)
	cerr << "Pulsar::FITSArchive::load_Integration correcting epoch phase"
	     << endl;

      Phase reference_phs = 0.0;

      if (phase_match_start_time)
      {
	// Correct epoch such that its phase equals that of the start time
	if (verbose > 2)
	  cerr << "Pulsar::FITSArchive::load_Integration matching phase(start)"
	       << endl;

	reference_phs = hdr_model->phase(hdr_ext->get_start_time());
      }

      Phase off_phs = hdr_model->phase(epoch);
      Phase dphase  = off_phs - reference_phs;
      
      double dtime = dphase.fracturns() * integ->get_folding_period();
      epoch -= dtime;
      integ->set_epoch (epoch);

      if (verbose > 2)
      {
      	cerr << "Pulsar::FITSArchive::load_Integration row=" << row <<
	  "\n  PRED_PHS=" << predicted_phase;

	if (phase_match_start_time)
	  cerr << "\n  reference epoch=" 
	       << hdr_ext->get_start_time().printdays(13);

	cerr <<
	  "\n  reference phase=" << reference_phs <<
	  "\n      input phase=" << off_phs <<
	  "\n     phase offset=" << dphase << " = " << dtime << "s" 
	  "\n     subint epoch=" << epoch.printdays(13) << 
	  "\n     subint phase=" << hdr_model->phase(epoch) << endl;
      }
    }
  }
  else
  {

    /* *******************************************************************

       METHOD 2: folding period defined by CAL_FREQ in primary header

       ******************************************************************* */

    CalInfoExtension* calinfo = get<CalInfoExtension>();
    if (calinfo && calinfo->cal_frequency > 0.0)
    {
      if (verbose > 2)
        cerr << "FITSArchive::load_Integration CAL_FREQ=" 
	     << calinfo->cal_frequency << endl;
      integ->set_folding_period( 1.0/calinfo->cal_frequency );
    }

    /* *******************************************************************

       METHOD 3: folding period defined by PERIOD column of SUBINT HDU

       ******************************************************************* */

    double period = 0.0;
    status = 0;
    fits_get_colnum (fptr, CASEINSEN, "PERIOD", &colnum, &status);
    fits_read_col (fptr, TDOUBLE, colnum, row, 1, 1, &nulldouble,
                   &period, &initflag, &status);

    if (status == 0 && period > 0.0)
    {
      if (verbose > 2)
	cerr << "FITSArchive::load_Integration PERIOD=" << period << endl;
      integ->set_folding_period (period);
    }

    if (integ->get_folding_period() == 0.0)
      throw FITSError (status, "FITSArchive::load_Integration",
                       "folding period unknown: no model, CAL_FREQ or PERIOD");
  }

  status = 0;

  // Load other useful info

  load_Pointing (fptr,row,integ);
  load_Plasma (fptr,row,integ);

  // Set up the data vector, only Pulsar::Archive base class is friend

  resize_Integration (integ);

  const unsigned nchan = get_nchan();

  if (naux_profile)
    for (unsigned ichan=0; ichan < nchan; ichan++)
    {
      FourthMoments* fourth = new FourthMoments;
      fourth->resize (naux_profile, get_nbin());
      integ->get_Profile(0,ichan)->add_extension (fourth);
    }

  // Load the channel centre frequencies
  
  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration reading channel freqs" 
	 << endl;
  
  int counter = 1;
  vector < float >  chan_freqs(get_nchan());
  
  colnum = 0;
  fits_get_colnum (fptr, CASEINSEN, "DAT_FREQ", &colnum, &status);
  
  fits_read_col (fptr, TFLOAT, colnum, row, counter, get_nchan(),
		 &nullfloat, &(chan_freqs[0]), &initflag, &status);

  if (status != 0)
    throw FITSError (status, "FITSArchive::load_Integration",
                     "fits_read_col DAT_FREQ");

  // Set the profile channel centre frequencies
  
  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration setting frequencies" 
	 << endl;

  bool all_ones = true;
  for (unsigned j = 0; j < get_nchan(); j++)
    if (chan_freqs[j] != 1)
      all_ones = false;
  
  double chanbw = get_bandwidth() / get_nchan();
  
  if ( all_ones )
  {
    if (verbose > 2)
      cerr << "FITSArchive::load_Integration all frequencies unity - reseting"
	   << endl;
    for (unsigned j = 0; j < get_nchan(); j++)
      integ->set_centre_frequency (j, get_centre_frequency()
				   -0.5*(get_bandwidth()+chanbw)+j*chanbw);
  }
  else
  {
    for (unsigned j = 0; j < get_nchan(); j++)
      integ->set_centre_frequency(j, chan_freqs[j]);
  }
  
  // Load the profile weights

  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration reading weights" 
	 << endl;
  
  counter = 1;
  vector < float >  weights(get_nchan());
  
  colnum = 0;
  fits_get_colnum (fptr, CASEINSEN, "DAT_WTS", &colnum, &status);
  
  for (unsigned b = 0; b < get_nchan(); b++) {
    fits_read_col (fptr, TFLOAT, colnum, row, counter, 1, &nullfloat, 
		   &weights[b], &initflag, &status);
    counter ++;
  }

  if (status != 0)
    throw FITSError (status, "FITSArchive::load_Integration",
                     "fits_read_col DAT_WTS");

  // Set the profile weights
  
  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration setting weights" 
	 << endl;
  
  for(unsigned j = 0; j < get_nchan(); j++)
    integ->set_weight(j, weights[j]);
  
  // Load the profile offsets
  
  if (!Profile::no_amps)
  {
    vector<Profile*> profiles;

    setup_profiles_dat (integ, profiles);
    setup_dat (fptr, load_dat_io);

    if (verbose > 2)
      cerr << "FITSArchive::load_Integration dat_io=" << load_dat_io.ptr()
           << endl;

    load_dat_io->load (isubint + 1, profiles);

    if (scale_cross_products && integ->get_state() == Signal::Coherence)
      for (unsigned ichan=0; ichan < get_nchan(); ichan++)
      {
	integ->get_Profile(2, ichan)->scale(2.0);
	integ->get_Profile(3, ichan)->scale(2.0);
      }

    if (naux_profile)
    {
      setup_profiles<MoreProfiles> (integ, profiles);
      setup_aux (fptr, load_aux_io, naux_profile);
      load_aux_io->load (isubint + 1, profiles);
    }
  }

  if (verbose > 2)
    cerr << "Pulsar::FITSArchive::load_Integration load complete" << endl;  
  
  // Finished with the file for now
  
  fits_close_file (fptr, &status);

  return integ.release();
}
catch (Error& error)
{
  throw error += "Pulsar::FITSArchive::load_Integration";
}
예제 #12
0
void Pulsar::PolnProfileFit::add_observation( const PolnProfile* observation )
{
  if (!standard)
    throw Error (InvalidState, "Pulsar::PolnProfileFit::add_observation",
		 "no standard specified.  call set_standard");

  if (!transformation)
    throw Error (InvalidState, "Pulsar::PolnProfileFit::add_observation",
		 "no transformation specified.  call set_transformation");

  if (!observation)
    throw Error (InvalidState, "Pulsar::PolnProfileFit::add_observation",
		 "no observation supplied as argument");

  // ensure that the PolnProfile class is cleaned up
  Reference::To<const PolnProfile> obs = observation;

  unsigned obs_harmonic = observation->get_nbin() / 2;

  if (obs_harmonic < n_harmonic)
    throw Error (InvalidState, "Pulsar::PolnProfileFit::add_observation",
		 "observation n_harmonic=%d < n_harmonic=%d",
		 obs_harmonic, n_harmonic);

  standard_data->set_profile( observation );

  Reference::To<const PolnProfile> fourier = standard_data->get_fourier();

  float phase_guess = ccf_max_phase (standard_fourier->get_Profile(0),
				     fourier->get_Profile(0));

  if (verbose)
    cerr << "Pulsar::PolnProfileFit::add_observation add gradient" << endl;

  unsigned index = 0;
  bool gradient_added = false;

  if (phases)
  {
    if (!shared_phase || phases->get_ngradient() == 0)
    {
      phases->add_gradient();
      gradient_added = true;
    }

    index = phases->get_igradient();

    phases->set_infit (index, !phase_lock);

    // TO-DO: when sharing phase, retain only the best phase_guess
    phases->set_param (index, phase_guess);
  }

  try
  {
    unsigned nbin_std = standard->get_nbin();
    unsigned nbin_obs = observation->get_nbin();

    /* 
       If the standard (template) and observed profiles have different
       numbers of bins, account for the offset between the centres of
       bin 0 of each profile.
       
       This will likely cause trouble if phases have been removed from
       the model.
       
       A potential fix: multiply the observation by the required phase
       gradient.
    */
    if (phases && nbin_std != nbin_obs)
      phases->set_offset (index, 0.5/nbin_std - 0.5/nbin_obs);
    
    // initialize the measurement sets
    for (unsigned ibin=1; ibin<n_harmonic; ibin++)
    {

      Stokes< complex<double> > val;
      Stokes< complex<double> > var;
      valvar( standard_data->get_stokes(ibin), val, var );
      
      Calibration::TemplateUncertainty* error = uncertainty[ibin-1]->clone();
      error -> set_variance (var);
      
#ifdef _DEBUG
      if (error->get_transformation() != transformation)
	{
	  cerr << "error.xform=" << error->get_transformation() << " != "
	       << transformation.get() << endl;
	}
      else
	cerr << "clone ok!" << endl;
#endif
      
      Calibration::CoherencyMeasurement measurement (ibin-1);
      measurement.set_stokes (val, error);
      
      double phase_shift = -2.0 * M_PI * double(ibin);

      Calibration::CoherencyMeasurementSet measurements (measurement_set);

      measurements.add_coordinate ( phase_axis.new_Value(phase_shift) );
      measurements.add_coordinate ( index_axis.new_Value(index) );
      
      measurements.push_back ( measurement );
      
      equation->add_data( measurements );
    }
  }
  catch (Error& error)
  {
    if (gradient_added)
      phases->remove_gradient();

    throw error += "Pulsar::PolnProfileFit::add_observation";
  }
}
예제 #13
0
void Pulsar::PolnSpectrumStats::build () try
{
  if (!profile)
    return;

  fourier = fourier_transform (profile, plan);

  // convert to Stokes parameters and drop the Nyquist bin
  fourier->convert_state (Signal::Stokes);
  fourier->resize( profile->get_nbin() );

  // form the power spectral density
  Reference::To<PolnProfile> psd = fourier_to_psd (fourier);

  // separate fourier into real and imaginary components
  Reference::To<PolnProfile> re = psd->clone();
  Reference::To<PolnProfile> im = psd->clone();

  unsigned npol = 4;
  unsigned nbin = psd -> get_nbin();

  for (unsigned ipol=0; ipol < npol; ipol++)
  {
    float* C_ptr = fourier->get_Profile(ipol)->get_amps();
    float* re_ptr = re->get_Profile(ipol)->get_amps();
    float* im_ptr = im->get_Profile(ipol)->get_amps();

    for (unsigned ibin=0; ibin < nbin; ibin++)
    {
      re_ptr[ibin] = C_ptr[ibin*2];
      im_ptr[ibin] = C_ptr[ibin*2+1];
    }
  }

  if (!regions_set)
  {
    LastHarmonic last;
    last.set_Profile( psd->get_Profile(0) );

    last.get_weight (&onpulse);
    last.get_baseline_estimator()->get_weight (&baseline);

    last_harmonic = last.get_last_harmonic();

#ifdef _DEBUG
    cerr << "Pulsar::PolnSpectrumStats::build last harmonic=" 
	 << last_harmonic << " nbin on=" << onpulse.get_weight_sum() << endl;
#endif

    real->set_regions (onpulse, baseline);
    imag->set_regions (onpulse, baseline);
  }

  if (onpulse.get_nbin () != re->get_nbin())
  {
    PhaseWeight on_temp = onpulse;
    PhaseWeight off_temp = baseline;

    on_temp.resize( re->get_nbin() );
    off_temp.resize( re->get_nbin() );

    if (re->get_nbin() > onpulse.get_nbin ())
    {
      copy_pad( on_temp, onpulse, 0 );
      copy_pad( off_temp, baseline, 1 );
    }

    real->set_regions (on_temp, off_temp);
    imag->set_regions (on_temp, off_temp);
  }

  real->set_profile (re.release());
  imag->set_profile (im.release());

}
catch (Error& error)
{
  throw error += "Pulsar::PolnSpectrumStats::build";
}
예제 #14
0
int main(int argc, char* argv[]) try
{
  if (argc < 2) {
    usage();
    return EXIT_SUCCESS;
  }
  
  int gotc = 0;
  while ((gotc = getopt(argc, argv, "hvV")) != -1) {
    switch (gotc) {
    case 'h':
      usage();
      return EXIT_SUCCESS;
    case 'V':
      Pulsar::Archive::set_verbosity(3);
      break;
    case 'v':
      Pulsar::Archive::set_verbosity(2);
      break;
    }
  }
  
  if (optind >= argc)
    {
      cerr << "pazi: please specify filename" << endl;
      return -1;
    }
  
  string filename = argv[optind];
  string extension = filename.substr(filename.length() - 2, 2);
  
  if (extension == "rf")
    extension = "rz";
  else if (extension == "cf")
    extension = "cz";
  else
    extension = "pazi";
  
  string write_filename = filename + ".";
  write_filename += extension;
  
  cerr << "pazi: loading data" << endl;
  base_archive = Archive::load(filename);
  
  if (base_archive->get_npol() == 4)
  {
    original_state = base_archive->get_state();
    base_archive->convert_state( Signal::Stokes );
  }
  
  backup_archive = base_archive->clone();
  
  cerr << "pazi: making fscrunched clone" << endl;
  mod_archive = base_archive->clone();
  mod_archive->pscrunch();
  mod_archive->remove_baseline();
  mod_archive->dedisperse();
  mod_archive->fscrunch();

  scrunched_archive = mod_archive->clone();
  scrunched_archive->tscrunch();

  ranges.second = get_max_value(base_archive, plot_type);
  positive_direction = base_archive->get_bandwidth() < 0.0;

  time_orig_plot = factory.construct("time");
  time_mod_plot = factory.construct("time");
  time_fui = time_mod_plot->get_frame_interface();

  freq_orig_plot = factory.construct("freq");
  freq_mod_plot = factory.construct("freq");
  freq_fui = freq_mod_plot->get_frame_interface();

  total_plot = factory.construct("flux");
  total_plot->configure("info=1");

  subint_orig_plot = new ProfilePlot;
  subint_mod_plot = new ProfilePlot;
  subint_fui = subint_mod_plot->get_frame_interface();

  subint_orig_plot->configure("info=1");
  subint_mod_plot->configure("info=1");

  unsigned window = 0;
  char device [8];

  for (unsigned i=0; i<2; i++) do {
    window ++;
    snprintf (device, 8, "%u/XS", window);
  }
  while ( cpgopen (device) < 0 );

  cpgask(0);

  cerr << endl << "Total S/N = " <<
    scrunched_archive->get_Profile(0,0,0)->snr() << endl << endl;

  total_plot->plot(scrunched_archive);
  cpgslct(1);
  time_orig_plot->plot(mod_archive);

  do
  {
    cpgswin(0, 1, 0, 1);

    // plot:
    //   frequency = horizontal mouse band
    //   time      = horizontal mouse band
    //   profile   = vertical mouse band

    int band = 0;

    if (prune_start != UNDEF_MOUSE)
    {
      band = BOX_ANCHOR;
      mouse_ref = prune_start;
    }
    else if (mouse_ref != UNDEF_MOUSE)
    {
      if (plot_type == FscrunchedSubint)
	band = VERTICAL_ANCHOR;
      else
	band = HORIZONTAL_ANCHOR;
    }
    else
    {
      if (plot_type == FscrunchedSubint)
	band = VERTICAL_LINE;
      else
	band = HORIZONTAL_LINE;
    }


    cpgband(band, 0, mouse_ref.first, mouse_ref.second, &(mouse.first),
	    &(mouse.second), &ch);

    switch (ch)
    {

    case 'A': // zoom
      {
	constrain_range(mouse.first);
	constrain_range(mouse.second);

	if (mouse_ref == UNDEF_MOUSE)
        {
	  mouse_ref = mouse;
	  continue;
	}


	// store the current range so it can be restored if the user selects
	// a zoom region too small
	const RangeType old_ranges = ranges;
	bool horizontal = plot_type == FscrunchedSubint ? false : true;
	ranges = get_range(mouse_ref, mouse, ranges, horizontal);

	// ignore mouse clicks if the index values are too close (< 1)
	if (ranges.first == ranges.second) {
	  ranges = old_ranges;
	  break;
	}

	zoomed = true;

	const unsigned max_value = get_max_value(base_archive, plot_type);
	const string zoom_option = get_zoom_option(ranges, max_value);

	switch (plot_type) {
	case PhaseVsTime:
	  time_fui->set_value("y:range", zoom_option);
	  redraw(mod_archive, time_orig_plot, time_mod_plot, zoomed);
	  break;
	case PhaseVsFrequency:
	  freq_fui->set_value("y:range", zoom_option);
	  freq_redraw(mod_archive, base_archive, freq_orig_plot,
		      freq_mod_plot, zoomed);
	  break;
	case FscrunchedSubint:
	  subint_fui->set_value("x:range", zoom_option);
	  redraw(mod_archive, subint_orig_plot, subint_mod_plot, zoomed);
	  break;
	}
      }
      break; // case 'A'
    case 'h':
      usage();
      break;

    case 'b': // plot specific subint
      if (plot_type == PhaseVsTime) {
	plot_type = FscrunchedSubint;
	zoomed = false;

	*mod_archive = *base_archive;
	mod_archive->set_dispersion_measure(0);
	mod_archive->fscrunch();
	mod_archive->pscrunch();
	mod_archive->remove_baseline();

	subint = get_indexed_value(mouse);

	ranges.first = 0;
	ranges.second = get_max_value(base_archive, plot_type);

	char add[3];
	sprintf(add, "%d", subint);
	string subint_option = "subint=";
	subint_option += add;

	subint_orig_plot->configure(subint_option);
	subint_mod_plot->configure(subint_option);

	cpgeras();
	subint_orig_plot->plot(mod_archive);
	update_total(scrunched_archive, base_archive, total_plot);
      }
      break;

      /*case 'c': // center pulse
	set_centre(mod_archive, base_archive, centered, plot_type, dedispersed);

	if (plot_type == "freq")
	redraw(mod_archive, freq_orig_plot, freq_mod_plot, zoomed);
	else if (plot_type == "time")
	redraw(mod_archive, time_orig_plot, time_mod_plot, zoomed);

	update_total(scrunched_archive, base_archive, total_plot);
	break;*/

    case 'd': // toggle dedispersion on/off
      set_dedispersion(mod_archive, base_archive, dedispersed);

      if (plot_type == PhaseVsFrequency) {
	mod_archive->tscrunch();
	redraw(mod_archive, freq_orig_plot, freq_mod_plot, zoomed);
      } else if (plot_type == PhaseVsTime) {
	mod_archive->fscrunch();
	redraw(mod_archive, time_orig_plot, time_mod_plot, zoomed);
      }

      update_total(scrunched_archive, base_archive, total_plot);
      break;

    case 'f': // frequency plot
      plot_type = PhaseVsFrequency;
      ranges.first = 0;
      ranges.second = get_max_value(base_archive, plot_type);
      zoomed = false;
      freq_redraw(mod_archive, base_archive, freq_orig_plot, freq_mod_plot, zoomed);
      break;

    case 'm':

      if (plot_type != FscrunchedSubint)
      {
	cerr << "pazi: can only mow lawn in binzap-subint mode" << endl;
	continue;
      }

      cerr << "pazi: mowing lawn" << endl;
      mowlawn (mod_archive, base_archive, subint);

      cerr << "pazi: replotting" << endl;
      redraw(mod_archive, subint_orig_plot, subint_mod_plot, zoomed);
      cerr << "pazi: updating total" << endl;
      update_total(scrunched_archive, base_archive, total_plot);

      break;

    case 'x': // prune

      if (plot_type != FscrunchedSubint)
      {
	cerr << "pazi: can only prune hedge in binzap-subint mode" << endl;
	continue;
      }

      constrain_range(mouse.first);
      constrain_range(mouse.second);

      if (prune_start == UNDEF_MOUSE)
      {
	prune_start = mouse;
	continue;
      }

      prune_end = mouse;

      cerr << "pazi: pruning hedge" << endl;
      prune_hedge (mod_archive, base_archive, subint);

      cerr << "pazi: replotting" << endl;
      redraw(mod_archive, subint_orig_plot, subint_mod_plot, zoomed);
      cerr << "pazi: updating total" << endl;
      update_total(scrunched_archive, base_archive, total_plot);

      break;

    case 'o': // toggle frequency scrunching on/off
      if (plot_type == PhaseVsTime)
      {
	if (fscrunched) {
	  fscrunched = false;
	  *mod_archive = *base_archive;
	}
	else {
	  fscrunched = true;
	  mod_archive->fscrunch();
	}
	redraw(mod_archive, time_orig_plot, time_mod_plot, zoomed);
      }
      break;

    case 'q': // quit
      cpgclos();
      return EXIT_SUCCESS;

    case 'p':
      print_command(channels_to_zap, subints_to_zap, extension, filename);
      break;

    case 'r': // reset zoom
      zoomed = false;
      ranges.first = 0;
      ranges.second = get_max_value(base_archive, plot_type);

      switch (plot_type) {
      case PhaseVsTime:
	redraw(mod_archive, time_orig_plot, time_mod_plot, zoomed);
	break;
      case PhaseVsFrequency:
	freq_redraw(mod_archive, base_archive, freq_orig_plot,
		    freq_mod_plot, zoomed);
	break;
      case FscrunchedSubint:
	redraw(mod_archive, subint_orig_plot, subint_mod_plot, zoomed);
	break;
      }
      break;

    case 's': // save current archive changes:
      {
	Pulsar::ProcHistory* ext = base_archive->get<Pulsar::ProcHistory>();
	if (ext) {
	  ext->set_command_str("pazi");
	}

	if ( base_archive->get_npol() == 4 )
	  base_archive->convert_state( original_state );
	base_archive->unload(write_filename);
	if ( base_archive->get_npol() == 4 )
	  base_archive->convert_state( Signal::Stokes );
	break;
      }

    case 't': // time plot
      plot_type = PhaseVsTime;
      ranges.first = 0;
      ranges.second = get_max_value(base_archive, plot_type);
      zoomed = false;
      time_redraw(mod_archive, base_archive, time_orig_plot, time_mod_plot, zoomed);
      break;

    case 'u': // undo last change

      if (mouse_ref != UNDEF_MOUSE) {
	mouse_ref = UNDEF_MOUSE;
	continue;
      }

      switch (plot_type) {
      case PhaseVsTime:
	{
	  const unsigned value = get_indexed_value(mouse);
	  remove_channel(value, subints_to_zap);
	  time_unzap_subint(base_archive, backup_archive, value);
	  time_redraw(mod_archive, base_archive, time_orig_plot,
		      time_mod_plot, zoomed);
	}
	break;
      case PhaseVsFrequency:
	{
	  const unsigned value = get_indexed_value(mouse);
	  remove_channel(value, channels_to_zap);
	  freq_unzap_chan(base_archive, backup_archive, value);
	  freq_redraw(mod_archive, base_archive, freq_orig_plot,
		      freq_mod_plot, zoomed);
	}
	break;
      case FscrunchedSubint:
	if (bins_to_zap.size()) {
	  bins_to_zap.erase(bins_to_zap.end() - 5, bins_to_zap.end());

	  *base_archive = *backup_archive;
	  *mod_archive = *backup_archive;

	  mod_archive->set_dispersion_measure(0);
	  mod_archive->pscrunch();
	  mod_archive->fscrunch();
	  mod_archive->remove_baseline();
	  redraw(mod_archive, subint_orig_plot, subint_mod_plot, zoomed);
	}
	break;
      }
      update_total(scrunched_archive, base_archive, total_plot);
      break;

    case 'z':
    case 'X': // zap single channel

      if (mouse_ref == UNDEF_MOUSE)
	zap_single ();
      else
	zap_multiple ();

      break;
    }

    prune_start = UNDEF_MOUSE;
    mouse_ref = UNDEF_MOUSE;

  } while (ch != 'q');

  return 0;

 } // end main

 catch (Error& error) {
   cerr << "pazi: " << error << endl;
   return -1;
 }
예제 #15
0
파일: spa.C 프로젝트: lbaehren/lofarsoft
int scan_pulses(Reference::To<Pulsar::Archive> arch, vector<pulse>& data, 
		int method, float cphs, float dcyc)
{
  /* Find and store the flux and phase of each pulse in the file
     to the data vector. */
  
  pulse newentry;

  Reference::To<Pulsar::Profile> prof;
  
  newentry.file = arch->get_filename();

  double nm, nv, vm;
  
  int nbin = arch->get_nbin();
  int bwid = int(float(nbin) * dcyc);
  int cbin = 0;

  for (unsigned i = 0; i < arch->get_nsubint(); i++) {

    newentry.intg = i;
    
    prof = arch->get_Profile(i, 0, 0);
    
    prof->stats(prof->find_min_phase(), &nm, &nv, &vm);
    newentry.err = sqrt(nv);

    switch (method) {
      
    case 0: // Method of total flux
      
      newentry.flx = prof->sum();
      newentry.phs = prof->find_max_phase();
      break;
      
    case 1: // Method of maximum amplitude

      if (dcyc == 0.0) {
	newentry.flx = prof->max();
	newentry.phs  = prof->find_max_phase();
      }
      else {
	cbin = int(prof->find_max_phase(dcyc) * float(nbin));
	newentry.flx = prof->sum(cbin - bwid/2, cbin + bwid/2);
	newentry.phs  = prof->find_max_phase(dcyc);
      }
      break;

    case 2: // User-defined phase centre
      
      cbin = int(float(nbin) * cphs);

      if (dcyc == 0.0) {
	newentry.flx = (prof->get_amps())[cbin];
	newentry.phs = cphs;
      }
      else {
	newentry.flx = prof->sum(cbin - bwid/2, cbin + bwid/2);
	newentry.phs = cphs;
      }
      
      break;
      
    default:
      cerr << "No phase selection method chosen!" << endl;
    }
    
    data.push_back(newentry);
  }
  
  return data.size();
}
예제 #16
0
파일: Archiver.C 프로젝트: steve-ord/dspsr
void dsp::Archiver::set (Pulsar::Integration* integration,
        		 const PhaseSeries* phase,
        		 unsigned isub, unsigned nsub) 
try
{
  if (verbose > 2)
    cerr << "dsp::Archiver::set Pulsar::Integration" << endl;

  const unsigned npol  = phase->get_npol();
  const unsigned nchan = phase->get_nchan();
  const unsigned ndim  = phase->get_ndim();
  const unsigned nbin  = phase->get_nbin();

  const unsigned effective_npol = get_npol (phase);

  if ( integration->get_npol() != effective_npol)
    throw Error (InvalidParam, "dsp::Archiver::set (Pulsar::Integration)",
                 "Integration.npol=%d != PhaseSeries.npol=%d", 
                 integration->get_npol(), effective_npol);

  if ( integration->get_nchan() != nchan)
    throw Error (InvalidParam, "dsp::Archiver::set (Pulsar::Integration)",
                 "Integration.nchan=%d != PhaseSeries.nchan=%d", 
                 integration->get_nchan(), nchan);

  integration-> set_epoch ( phase->get_mid_time() );
  integration-> set_duration ( phase->get_integration_length() );

  integration-> set_folding_period ( phase->get_folding_period () );

  if (verbose > 2)
    cerr << "dsp::Archiver::set"
         << " epoch=" << integration->get_epoch().printdays(13)
         << " duration=" << integration->get_duration()
         << " period=" << integration->get_folding_period() << endl;

  for (unsigned ichan=0; ichan<nchan; ichan++)
  {
    unsigned chan = phase->get_unswapped_ichan (ichan);

    Reference::To<Pulsar::MoreProfiles> more;

    if (fourth_moments)
    {
      more = new Pulsar::FourthMoments;
      more->resize( fourth_moments, nbin );
    }

    if (more)
      integration->get_Profile(0,chan)->add_extension(more);

    corrupted_profiles = 0;

    for (unsigned ipol=0; ipol<npol; ipol++)
    {
      for (unsigned idim=0; idim<ndim; idim++)
      {
        unsigned poln = ipol*ndim+idim;

        if (nsub > 1)
          idim = isub;

        Pulsar::Profile* profile = 0;

        double scale = phase->get_scale ();

        if (more && poln >= effective_npol)
        {
          profile = more->get_Profile (poln - effective_npol);
          scale *= scale; // FourthMoments start with Stokes squared
        }
        else
          profile = integration->get_Profile (poln, chan);

        if (verbose > 2)
          cerr << "dsp::Archiver::set Pulsar::Integration ipol=" << poln
               << " ichan=" << chan << " nbin=" << profile->get_nbin() << endl;

        set (profile, phase, scale, ichan, ipol, idim);

      }
    }

    if (corrupted_profiles)
    {
      cerr << "dsp::Archiver::set Pulsar::Integration ichan=" << ichan
             << " contains corrupted data" << endl;

      integration->set_weight (ichan, 0);
    }
    else if (fourth_moments)
    {
      cerr << "dsp::Archiver::set fourth_moments=true" << endl;
      const unsigned * hits = 0;
      if (phase->get_zeroed_data())
        hits = phase->get_hits(ichan);
      else
        hits = phase->get_hits();
      raw_to_central (chan, more, integration, hits);
    }
  }

  if (hist_unpacker)
  {
    // Add DigitiserCounts histograms for this subint.
    Pulsar::Archive *arch = const_cast<Pulsar::Archive *> 
      ( integration -> expert() -> get_parent() );
    Pulsar::DigitiserCounts *dcnt = arch -> getadd<Pulsar::DigitiserCounts>();
    if (dcnt)
    {
      if (verbose > 2)
	cerr << "dsp::Archiver::set Pulsar::DigitiserCounts extension" << endl;
      set (dcnt, isub);
    }
  }
}

catch (Error& error)
{
  throw error += "dsp::Archiver::set Pulsar::Integration";
}
예제 #17
0
파일: paz.C 프로젝트: SkyTian13/psrchive
void paz::process (Pulsar::Archive* arch)
{
  if (periodic_zap)
  {
    if (arch->get_nchan () == 1)
      cout << periodic_zap_warning << endl;

    for (unsigned pol = 0; pol < arch->get_npol (); pol++)
      for (unsigned chan = 0; chan < arch->get_nchan (); chan++)
	for (unsigned subint = 0; subint < arch->get_nsubint (); subint++)
	  zap_periodic_spikes (arch->get_Profile (subint, pol, chan),
			       periodic_zap_period, periodic_zap_phase,
                               periodic_zap_width);
  }

  if (pol_to_delete != -1)
  {
    /*
      Delete either PP=(pol 0) or QQ=(pol 1) by resizing to npol = 1

      If PP is to be deleted, PP and QQ are first swapped.
    */

    if (pol_to_delete != 0 && pol_to_delete != 1)
      throw Error (InvalidState, "paz::process",
		   "--pol = either 0 or 1, not %d", pol_to_delete);

    if (arch->get_state() == Signal::Stokes)
      arch->convert_state( Signal::Coherence );

    Signal::State state = arch->get_state();

    if (state != Signal::Coherence && state != Signal::PPQQ)
      throw Error (InvalidState, "paz::process",
		   "cannot delete polarization when state = %s", 
		   Signal::state_string(state));

    if (verbose)
      cerr << "paz: deleting ipol=" << pol_to_delete 
	   << " current state=" << Signal::state_string(state) << endl;

    const unsigned nchan = arch->get_nchan ();
    const unsigned nsub = arch->get_nsubint ();

    for (unsigned isub = 0; isub < nsub; isub++)
    {
      // ensure that the sub-integration is loaded from file
      Pulsar::Integration* subint = arch->get_Integration (isub);

      if (pol_to_delete == 0)
      {
        // Swap pol 0 and pol 1 in each sub-integration and frequency channel
	for (unsigned ichan = 0; ichan < nchan; ichan++)
	  subint->expert()->swap_profiles (0, ichan, 1, ichan);
      }
    }

    // resize to npol = 1
    arch->resize (nsub, 1);
    arch->set_state( Signal::Intensity );
  } 

  // To fix early wide-band correlator problem
  if (eightBinZap)
  {
    for (unsigned pol = 0; pol < arch->get_npol (); pol++) {
      for (unsigned chan = 0; chan < arch->get_nchan (); chan++) {
	for (unsigned subint = 0; subint < arch->get_nsubint (); subint++) {
	  Pulsar::Profile * prof = arch->get_Profile (subint, pol, chan);
	  float *amps = prof->get_amps ();
	  for (unsigned i = 5; i < arch->get_nbin (); i += 8) {
	    // Interpolate between previous and later point if possible
	    if (i == arch->get_nbin () - 1)
	      amps[i] = amps[i - 1];
	    else {
	      float amp1 = amps[i - 1];
	      float amp2 = amps[i + 1];
	      amps[i] = 0.5 * (amp1 + amp2);
	    }
	  }
	  prof->set_amps (amps);
	}
      }
    }
  }

  unsigned nchan = arch->get_nchan ();
  double chan_bw = fabs(arch->get_bandwidth() / (double)nchan);

  if (subs_to_delete.size())
  {
    unsigned zapped = 0;
    unsigned orig_nsubint = arch->get_nsubint ();

    for (unsigned i = 0; i < orig_nsubint; i++)
      for (unsigned j = 0; j < subs_to_delete.size (); j++)
	if (subs_to_delete[j] == i)
	{
	  if (verbose)
	    cout << "Deleting subint " << i << endl;

	  // after each subint is zapped, it is necessary to offset the index
	  arch->erase (i - zapped);
	  zapped ++;
	}

    if (verbose)
      cerr << "paz: deletion completed" << endl;
  }

  if (subs_no_delete.size())
  {
    unsigned zapped = 0;
    unsigned orig_nsubint = arch->get_nsubint ();

    for (unsigned i = 0; i < orig_nsubint; i++)
    {
      bool keep = false;
      for (unsigned j = 0; j < subs_no_delete.size (); j++)
	if (subs_no_delete[j] == i)
	  keep = true;

      if (!keep)
      {
	if (verbose)
	  cout << "Deleting subint " << i << endl;

	// after each subint is zapped, it is necessary to offset the index
	arch->erase (i - zapped);
	zapped ++;
      }
    }

    if (verbose)
      cerr << "paz: deletion completed" << endl;
  }

  if (median_zapper)
  {
    cout << "Using median smoothed difference zapper" << endl;
    (*median_zapper) (arch);
  }

  if (modulation_zapper)
  {
    cout << "Using modulation index zapper" << endl;
    (*modulation_zapper) (arch);
  }

  if (simple)
  {
    cout << "Using simple mean offset zapper" << endl;
    zapper->zap_chans (arch);
    cout << "Zapping complete" << endl;
  }

  if (subs_to_zero.size() && !zero_intersection)
  {
    vector<float> mask (nchan, 0.0);
    zapper->zap_very_specific (arch, mask, subs_to_zero);
  }

  if (bins_to_zap.size ())
  {
    for (unsigned i = 0; i < bins_to_zap.size(); ++i)
      zap_bin_in_archive(arch, bins_to_zap[i]);
  }

  if (zero_channels)
  {
    vector<float> mask (nchan, 1.0);
    for (unsigned i = 0; i < chans_to_zero.size (); i++)
    {
      if (chans_to_zero[i] >= nchan || chans_to_zero[i] < 0)
        throw Error (InvalidRange, "paz::process",
            "channel %d is out of range (nchan=%d)",
           chans_to_zero[i], nchan); 
      mask[chans_to_zero[i]] = 0.0;
    }

    for (unsigned i = 0; i < freqs_to_zero.size (); i++)
    {
      for (unsigned ic=0; ic<nchan; ic++)
      {
	double chan_freq=arch->get_Integration(0)->get_centre_frequency(ic);
	if ( freqs_to_zero[i] > chan_freq - chan_bw/2.0 
	     && freqs_to_zero[i] < chan_freq + chan_bw/2.0 )
	  mask[ic] = 0.0;
      }
    }

    for (unsigned i=0; i< freq_ranges_to_zero.size(); i++)
    {
      for (unsigned ic=0; ic<nchan; ic++)
      {
	FreqRange chan;
	chan.lo=arch->get_Integration(0)->get_centre_frequency(ic) 
	  - chan_bw/2.0;
	chan.hi=arch->get_Integration(0)->get_centre_frequency(ic) 
	  + chan_bw/2.0;
	if (chan.lo > freq_ranges_to_zero[i].lo 
	    && chan.lo < freq_ranges_to_zero[i].hi)
	  mask[ic] = 0.0;
	else if (chan.hi > freq_ranges_to_zero[i].lo 
		 && chan.hi < freq_ranges_to_zero[i].hi)
	  mask[ic] = 0.0;
	else if (freq_ranges_to_zero[i].lo > chan.lo 
		 && freq_ranges_to_zero[i].lo < chan.hi)
	  mask[ic] = 0.0;
      }
    }
    
    if (zero_intersection)
      zapper->zap_very_specific (arch, mask, subs_to_zero);
    else
      zapper->zap_specific (arch, mask);
  }
  
  if (edge_zap_percent)
  {
    float fraction = edge_zap_percent / 100.0;
    unsigned buffer = unsigned (float (nchan) * fraction);

    vector < float >mask (nchan, 0.0);

    for (unsigned i = buffer; i < (nchan - buffer); i++)
      mask[i] = 1.0;

    zapper->zap_specific (arch, mask);
  }

  if (ston_cutoff > 0.0)
  {
    double theston = 0.0;
    Reference::To<Pulsar::Archive> cloned = arch->clone();
    cloned->pscrunch ();
    for (unsigned isub = 0; isub < arch->get_nsubint (); isub++) {
      for (unsigned ichan = 0; ichan < arch->get_nchan (); ichan++) {
	theston = cloned->get_Profile (isub, 0, ichan)->snr ();
	if (theston < ston_cutoff) {
	  arch->get_Integration (isub)->set_weight (ichan, 0.0);
	}
      }
    }
  }

  if (dropout_sigma)
  {
    Reference::To < Pulsar::Archive > cloned = arch->clone ();
    Reference::To < Pulsar::Profile > testprof = 0;

    cloned->pscrunch ();
    cloned->fscrunch ();
    cloned->remove_baseline ();

    vector < double >mins;
    for (unsigned isub = 0; isub < arch->get_nsubint (); isub++) {
      testprof = cloned->get_Profile (isub, 0, 0);
      mins.push_back (fabs (testprof->min ()));
    }

    cloned->tscrunch ();

    double mean, vari, varm;

    testprof = cloned->get_Profile (0, 0, 0);
    testprof->stats (&mean, &vari, &varm);

    for (unsigned isub = 0; isub < arch->get_nsubint (); isub++) {
      if (mins[isub] > dropout_sigma * sqrt (vari)) {
	cerr << "Zapping integration " << isub << endl;
	arch->get_Integration (isub)->uniform_weight (0.0);
      }
    }
  }

  if (mower)
  {
    if (mow_all_subints)
    {
      subints_to_mow.resize (arch->get_nsubint ());
      for (unsigned isub = 0; isub < arch->get_nsubint (); isub++)
	subints_to_mow[isub] = isub;
    }

    for (unsigned isub = 0; isub < subints_to_mow.size (); isub++)
    {
      cerr << "paz: mowing subint " << subints_to_mow[isub] << endl;
      mower->transform (arch->get_Integration (subints_to_mow[isub]));
    }
  }
}
예제 #18
0
void fix_single::process (Archive* archive)
{
  if ( !buffer_filled )
  {
    nbin = archive->get_nbin ();
    nchan = archive->get_nchan ();
    npol = archive->get_npol ();
  }

  input_name = basename ( archive->get_filename () );
  inss << input_name.substr( input_name.length() - 14, 11 );
  inss >> input_count;
  inss.clear();
  inss.str ("");

   if ( archive->get_nsubint() != 1 )
  {
    throw Error ( InvalidState, "fix_single::process", "multiple subints not supported" ) ;
  } 

  if ( input_count != previous_count + 1 )
    buffer_filled = false;
  
  if ( buffer_filled )
  {
    output = archive->clone ();
  }

  if ( amps_old_second == NULL )
  {
    amps_old_second = new float [ nbin * nchan * npol / 2 ];
  }

  //currently only one subint input supported
  unsigned i_subint = 0;
  // read two subints at a time, get the amps, rotate etc
  try
  {
    for (unsigned i_chan = 0; i_chan < nchan; i_chan++ )
    {
      for (unsigned i_pol = 0; i_pol < npol; i_pol++ )
      {
	// index of the element containing the i_subint, i_pol, i_chan
	unsigned index = i_subint * nbin * nchan * npol + i_chan * nbin * npol + i_pol * nbin;
	if (buffer_filled)
	{
	  // copy half of one profile into beginning of the output profile and fill the other half with data from the next profile. To preserve alignement of pulsar peak with zero phase the two halfs are swapped.
	  memcpy ( (void *) (output->get_Profile(i_subint, i_pol, i_chan)->get_amps() ), (void *) (archive->get_Profile(i_subint, i_pol, i_chan)->get_amps() ), nbin / 2 * sizeof ( float ) );
	  memcpy ( (void *) ( output->get_Profile(i_subint, i_pol, i_chan)->get_amps() + nbin / 2 ), (void *) ( amps_old_second + index / 2 ), nbin / 2 * sizeof ( float ) );
	}
	//fill the "previous archive" buffer
	memcpy ( (void *) ( amps_old_second + index / 2 ), (void *) ( archive->get_Profile (i_subint, i_pol, i_chan)->get_amps() + nbin / 2), nbin / 2 * sizeof ( float ) );
	previous_count = input_count;
      }
    }
    //save output after adjusting MJD by half a turn
    if ( buffer_filled == true )
    {
      MJD epoch = archive->get_Integration(i_subint)->get_epoch() ;
      epoch -= archive->get_Integration(i_subint)->get_folding_period() / 2;
      output->get_Integration(i_subint)->set_epoch(epoch);
      outss << "pulse_" << input_count << "." << ext;
      out_name = outss.str();
      outss.str ("");
      output->unload ( out_name );
    }
    buffer_filled = true;
  }
  catch (Error& error)
  {
    cerr << "fix_single::process Problem occured with " << archive->get_filename() << endl;
    cerr << error << endl;
    buffer_filled = false;
  }

}
예제 #19
0
파일: psrspa.C 프로젝트: lbaehren/lofarsoft
void psrspa::create_histograms ( Reference::To<Archive> archive )
{
  if ( verbose )
    cerr << "psrspa::create_histograms entered" << endl;
  // ensure Stokes parameters if creating polarisation histograms
  if ( create_polar_degree || create_polar_angle )
  {
    archive->convert_state ( Signal::Stokes );
    if ( verbose )
      cerr << "psrspa::create_histograms converted state of the archive to Stokes" << endl;
  }

  // auxillary vectors
  //vector< Estimate<float > > aux_vec_f;
  vector< Estimate<double > > aux_vec_d;

  // Full Stokes profile
  Reference::To<PolnProfile> profile;
  // Polarized flux
  Reference::To<Profile> P;
  P = new Profile;
  // Total flux
  float *T_amps = new float [ nbin ];
  float *P_amps = new float [ nbin ];

  unsigned bin_min, bin_max;

  if ( verbose && max_bscrunch > 1 )
    cerr << "psrspa::create_histograms entering the bscrunch loop for the " << archive->get_filename () << " archive" << endl;
  // TODO Uh, this should be fixed up. The first idea was to enable the phase resolved histograms to be bscrunch-aware as well, but I think it doesn't make much sense so I decided to keep only the max flux bscrunch aware. This can be done much more neatly probably in such a case
  for ( current_bscrunch = 1 ; current_bscrunch <= max_bscrunch ; current_bscrunch *= 2 )
  {
    // in each passage, we bscrunch by a factor of 2
    if ( current_bscrunch > 1 )
    {
      if ( verbose )
	cerr << "psrspa::create_histograms bscrunching the archive " << archive->get_filename () << " by a factor of 2" << endl;
      archive->bscrunch ( 2 );
    }
    if ( verbose )
      cerr << "psrspa::create_histograms entering the loop through subints of the " << archive->get_filename () << " archive" << endl;
    // loop through subints
    for ( unsigned isub = 0; isub < archive->get_nsubint (); isub++ )
    {
      if ( verbose )
	cerr << "psrspa::create_histograms creating necessary profiles for subint " << isub << " of " << archive->get_filename () << endl;
      if ( create_polar_angle || create_polar_degree && current_bscrunch == 1 )
      {
	profile = archive->get_Integration(isub)->new_PolnProfile(0);
	if ( verbose )
	  cerr << "psrspa::create_histograms retrieved PolnProfile for subint " << isub << " of " << archive->get_filename () << endl;
      }
      if ( create_polar_angle && current_bscrunch == 1 )
      {
	profile->get_orientation ( aux_vec_d, 0 );
	if ( verbose )
	  cerr << "psrspa::create_histograms retrieved polarisation angle for subint " << isub << " of " << archive->get_filename () << endl;
      }
      if ( create_polar_degree || create_flux || find_max_amp_in_range )
      {
	stats.set_profile ( archive->get_Profile ( isub, 0, 0 ) );
	b_sigma = sqrt ( stats.get_baseline_variance ().get_value () );
	T_amps = archive->get_Profile ( isub, 0, 0 )->get_amps (); 
	if ( verbose )
	  cerr << "psrspa::create_histograms retrieved total flux amps for subint " << isub << " of " << archive->get_filename () << endl;
	if ( create_polar_degree && current_bscrunch == 1 )
	{
	  profile->get_polarized ( P );
	  if ( verbose )
	    cerr << "psrspa::create_histograms retrieved polarized flux profile for subint "  << isub << " of " << archive->get_filename () << endl;
	  P_amps = P->get_amps ();
	  if ( verbose )
	    cerr << "psrspa::create_histograms retrieved polarized flux amps for subint " << isub << " of " << archive->get_filename () << endl;
	}
      }

      if ( verbose )
	cerr << "psrspa::create_histograms looping through the provided phase ranges for subint " << isub << " of " << archive->get_filename () << endl;
      unsigned curr_hist = 0;
      // loop through phase ranges
      for ( unsigned irange = 0; irange < phase_range.size () ; irange++ )
      {
	if ( irange%2 == 0)
	{
	  bin_min = unsigned( floor ( phase_range[irange] * float(nbin / current_bscrunch ) + 0.5 ) );
	  if ( verbose ) 
	    cerr << "psrspa::create_histograms set minimal bin to " << bin_min << endl;
	}
	else
	{
	  bin_max = unsigned( floor ( phase_range[irange] * float(nbin / current_bscrunch ) + 0.5 ) );
	  if ( bin_max == nbin )
	    bin_max = nbin - 1 ;
	  if ( verbose ) 
	    cerr << "psrspa::create_histograms set maximum bin to " << bin_max << endl;
	  // loop through bins in the given phase range
	  for ( unsigned ibin = bin_min; ibin <= bin_max; ibin++ )
	  {
	    if ( create_polar_angle && current_bscrunch == 1 )
	    {
	      int result = gsl_histogram_increment ( h_polar_angle_vec[curr_hist], aux_vec_d[ibin].get_value () / 180.0 * M_PI );
	      if ( result == GSL_EDOM )
	      {
		if ( dynamic_histogram )
		{
		  gsl_histogram *temp_hist = gsl_histogram_clone ( h_polar_angle_vec[curr_hist] );
		  h_flux_pr_vec[curr_hist] = update_histogram_range ( temp_hist, aux_vec_d[ibin].get_value () / 180.0 * M_PI );
		}
		else {
		  warn << "WARNING psrspa::create_histograms polarisation angle the histogram range for the bin " << ibin << " in the subint " << isub << " of archive " << archive->get_filename () << endl;
		}
	      }
	    }
	    if ( create_polar_degree && current_bscrunch == 1 )
	    {
	      // if P_amps[ibin] or T_amps[ibin] < 3 sigma, set polar degree to zero
	      int result = gsl_histogram_increment ( h_polar_degree_vec[curr_hist], ( fabs ( P_amps[ibin] ) < 3.0 * b_sigma || fabs ( T_amps[ibin] ) < 3.0 * b_sigma ) ? 0.0 : P_amps[ibin] / T_amps[ibin] );
	      if ( result == GSL_EDOM )
	      {
		if ( dynamic_histogram )
		{
		  gsl_histogram *temp_hist = gsl_histogram_clone ( h_polar_degree_vec[curr_hist] );
		  h_flux_pr_vec[curr_hist] = update_histogram_range ( temp_hist, ( fabs ( P_amps[ibin] ) < 3.0 * b_sigma || fabs ( T_amps[ibin] ) < 3.0 * b_sigma ) ? 0.0 : P_amps[ibin] / T_amps[ibin] );
		}
		else {
		warn << "WARNING psrspa::create_histograms polarisation degree outside the histogram range for the bin " << ibin << " in the subint " << isub << " of archive " << archive->get_filename () << endl;
		}
	      }
	    }
	    if ( create_flux && current_bscrunch == 1 )
	    {
	      int result = 0;
	      if ( log && T_amps[ibin] > 0.0 )
	      {
		result = gsl_histogram_increment ( h_flux_pr_vec[curr_hist], log10f ( T_amps[ibin] ) );
	      }
	      else if ( !log )
	      {
		result = gsl_histogram_increment ( h_flux_pr_vec[curr_hist], T_amps[ibin] );
	      }
	      if ( result == GSL_EDOM )
	      {
		if ( dynamic_histogram )
		{
		  gsl_histogram *temp_hist = gsl_histogram_clone ( h_flux_pr_vec[curr_hist] );
		  h_flux_pr_vec[curr_hist] = update_histogram_range ( temp_hist, log ? log10f ( T_amps[ibin] ) : T_amps[ibin] );
		}
		else {
		  warn << "WARNING psrspa::create_histograms phase resolved flux outside the histogram range for the bin " << ibin << " in the subint " << isub << " of archive " << archive->get_filename () << " flux = " << T_amps[ibin] << endl;
		}
	      }
	    }
	    // increment the histogram id
	    curr_hist ++;
	  } // loop through bins in the given phase range
	  // find the maximum amplitude in given phase range.
	  if ( find_max_amp_in_range )
	  {
	    if ( verbose )
	      cerr << "psrspa::create_histograms finding the maximum amplitude in the phase range " << irange << " of the subint " << isub << " (archive " << archive->get_filename () << ")" << endl;
	    int max_bin = archive->get_Profile ( isub, 0, 0 )->find_max_bin ( (int)bin_min, (int)bin_max );
	    identifier current_identifier = { archive->get_filename (), isub, bin_min, bin_max, current_bscrunch,
	      (unsigned)max_bin, T_amps[max_bin], b_sigma }; 
	    max_amp_info.push_back ( current_identifier );
	  } // find maximal amplitude in the given phase range
	} // handle given phase range
      } // loop through phase ranges
    } // loop through subints
  } // bscrunch loop
  if ( verbose )
    cerr << "psrspa::create_histograms finished" << endl;
}// create_histograms