예제 #1
0
main (int argc, char *argv[])
{
  int i, j, k, val;
  int curr_line;
  int last_dwp, this_dwp;
  int last_start;
  SEASAT_header_ext *hdr;
  FILE *fpin_dat, *fpin_hdr;
  FILE *fpout_dat, *fpout_hdr;
  FILE *fptmp;
  unsigned char buf[SAMPLES_PER_LINE];
  double tbuf[O_SAMP_PER_LINE], fbuf[SAMPLES_PER_LINE], fbuf2[SAMPLES_PER_LINE];
  double total[O_SAMP_PER_LINE];
  
  int    dwp_positions[256];
  double ave;
  int    ndwps;
  double ovpulse[10][O_SAMP_PER_LINE];

  complexFloat *b;
  fftwf_plan   b_longb, b_longf;
  double op[O_SAMP_PER_LINE];
  
  char indat[256], inhdr[256];
  char outdat[256], outhdr[256], outdis[256];
  char tmp[256];

  int ocnt, this_line;
  double ocal[20];
  char intmp[256];

  if (argc != 3) {
     printf("Usage: %s <in> <out>\n",argv[0]);
     printf("\tin         - input data and header file base name\n");
     printf("\tout        - output data and header file base name\n\n");
     exit(1);
  }

  strcpy(indat,argv[1]); strcat(indat,".dat");
  strcpy(inhdr,argv[1]); strcat(inhdr,".hdr");
  strcpy(outdat,argv[2]); strcat(outdat,".dat");
  strcpy(outhdr,argv[2]); strcat(outhdr,".hdr");

  hdr = (SEASAT_header_ext *) malloc(sizeof(SEASAT_header_ext));

  printf("\t%s: opening input files...\n",argv[0]);
  fpin_dat = fopen(indat,"rb");
  if (fpin_dat == NULL) {printf("ERROR: Unable to open input data file %s\n",indat); exit(1);}  
  fpin_hdr = fopen(inhdr,"r");
  if (fpin_hdr == NULL) {printf("ERROR: Unable to open input header file %s\n",inhdr); exit(1);}  

  b = (complexFloat *) fftwf_malloc(sizeof(fftwf_complex)*O_SAMP_PER_FFT);
  b_longb = fftwf_plan_dft_1d(O_SAMP_PER_FFT, (fftwf_complex *) b, (fftwf_complex *)b, FFTW_BACKWARD, FFTW_MEASURE);
  b_longf = fftwf_plan_dft_1d(O_SAMP_PER_FFT, (fftwf_complex *) b, (fftwf_complex *)b, FFTW_FORWARD, FFTW_MEASURE);

  for (i=0; i<O_SAMP_PER_LINE; i++) { tbuf[i] = 0.0; total[i] = 0;}

  /* Start reading the input file header - get the first DWP */
  val=get_values(fpin_hdr,hdr);
  if (val!=20) {printf("ERROR: unable to read from header file\n"); exit(1);}
  last_dwp = hdr->delay;
  printf("\tfound initial dwp of %i\n",last_dwp);

  this_dwp = last_dwp;
  curr_line = 0;
  ndwps = 0;
  dwp_positions[ndwps++] = 0;
  printf("Set dwp position %i to %i\n",ndwps-1,dwp_positions[ndwps-1]);
  
  last_start = 1;

  printf("\treading file; creating oversampled average\n");
  double sum = 0.0;

  /* Loop through the entire file creating averaged calibration pulses */
  while (val == 20) {
      this_line = 0;
      /* read, oversample, and sum lines until DWP shift is found in the hdr file */
      while (this_dwp == last_dwp) {
        fread(buf,SAMPLES_PER_LINE,1,fpin_dat);
	if (this_line < 10000) {
          for (i=0; i<SAMPLES_PER_LINE; i++) fbuf[i] = (double) buf[i];
	  fft_oversamp(fbuf,SAMPLES_PER_LINE,EXPANSION_FACTOR,op);
          for (i=0; i<O_SAMP_PER_LINE; i++) /* if (op[i]>=4.0 && op[i]<28.0) */ { tbuf[i] += op[i]; total[i]++; }
	  this_line++;
   	  if ((int)this_line%100==0) printf("\toversampling line %i\n",curr_line);
	}
	
        curr_line++;
        val=get_values(fpin_hdr,hdr);
        if (val!=20) break;
        this_dwp = hdr->delay;
	if (curr_line%1000==1) printf("\tskipping line %i\n",curr_line);
      }

      /* check if we found a new DWP or hit end of file */
      if (this_dwp != last_dwp) {
        printf("\tread to line %i.  New DWP is %i\n",curr_line,this_dwp);
        dwp_positions[ndwps++] = curr_line;
        printf("Set dwp position %i to %i\n",ndwps-1,dwp_positions[ndwps-1]);
      } else if (val!=20) {
        printf("\tread to end of file\n");
	dwp_positions[ndwps] = curr_line;
        printf("Set dwp position %i to %i\n",ndwps,dwp_positions[ndwps]);
      }

      /* average the summed up pulse */
      sum = 0.0; 
      for (i=0; i<O_SAMP_PER_LINE; i++) { 
          if (total[i] != 0) tbuf[i] = tbuf[i]/total[i]; 
	  else tbuf[i] = 0.0;
          sum += tbuf[i];
        }
      sum = sum / (double) O_SAMP_PER_LINE;

      /* write the averaged normalized pulse to output file */
      sprintf(tmp,"%s%.7i.ov.pulse",argv[1],last_start); 
      fptmp = fopen(tmp,"w");
      if (fptmp == NULL) {printf("ERROR: Unable to open output data file %s\n",tmp); exit(1);}  
      for (i=0; i<O_SAMP_PER_LINE; i++) { 
          fprintf(fptmp,"%lf\n",(tbuf[i]-sum));
	  ovpulse[ndwps-1][i] = tbuf[i]-sum;  /* save averaged pulses as we go */
      }
      fclose(fptmp);

      /* calculate the spectra - just for fun (?) */
      spectra(fpin_dat,dwp_positions[(ndwps-1)],10002,ave,&ocnt,ocal);
      sprintf(intmp,"%s%.7i.spectra.out",argv[1],dwp_positions[(ndwps-1)]); 
      rename("spectra.out",intmp);
      sprintf(intmp,"%s%.7i.spectra.fixed",argv[1],dwp_positions[(ndwps-1)]); 
      rename("spectra.fixed",intmp);

      /* Since the spectra ALWAYS shows a peak at 0.25, we remove that here */
      for (k=0; k<O_SAMP_PER_LINE; k++) { b[k].real = tbuf[k]; b[k].imag = 0.0; }
      for (k=O_SAMP_PER_LINE; k<O_SAMP_PER_FFT; k++) { b[k].real = 0.0; b[k].imag = 0.0; }
      fftwf_execute(b_longf);

      fptmp = fopen("b.freq","w");
      for (k=0; k<O_SAMP_PER_LINE; k++) {
         tbuf[k] = sqrt(b[k].real*b[k].real+b[k].imag*b[k].imag)/(double)O_SAMP_PER_LINE;
         fprintf(fptmp,"%i %lf\n",k,tbuf[k]);
      }
      fclose(fptmp);

//      printf("Removing freq at location %i (value %lf)\n",O_SAMP_PER_FFT/4,
//                sqrt(b[O_SAMP_PER_FFT/4].real*b[O_SAMP_PER_FFT/4].real+
//		     b[O_SAMP_PER_FFT/4].imag*b[O_SAMP_PER_FFT/4].imag)/(double)O_SAMP_PER_FFT);
//      b[O_SAMP_PER_FFT/4].real = 0.0;
//      b[O_SAMP_PER_FFT/4].imag = 0.0;

//      printf("Removing freq at location %i (value %lf)\n",3*O_SAMP_PER_FFT/4,
//     		sqrt(b[3*O_SAMP_PER_FFT/4].real*b[3*O_SAMP_PER_FFT/4].real+
//		     b[3*O_SAMP_PER_FFT/4].imag*b[3*O_SAMP_PER_FFT/4].imag)/(double)O_SAMP_PER_FFT);
//      b[3*O_SAMP_PER_FFT/4].real = 0.0;
//      b[3*O_SAMP_PER_FFT/4].imag = 0.0;
  
      printf("Removing freq at location 4095 - 4097\n");
 
//      b[4093].real = 0.0;
//      b[4093].imag = 0.0;
//      b[4094].real = 0.0;
//      b[4094].imag = 0.0;
      b[4095].real = 0.0;
      b[4095].imag = 0.0;
      b[4096].real = 0.0;
      b[4096].imag = 0.0;
      b[4097].real = 0.0;
      b[4097].imag = 0.0;
//      b[4098].real = 0.0;
//      b[4098].imag = 0.0;
//      b[4099].real = 0.0;
//      b[4099].imag = 0.0;

      printf("Removing freq at location 1365\n");

      b[1365].real = 0.0;
      b[1365].imag = 0.0;

      fptmp = fopen("b.freq2","w");
      for (k=0; k<O_SAMP_PER_LINE; k++) {
         tbuf[k] = sqrt(b[k].real*b[k].real+b[k].imag*b[k].imag)/(double)O_SAMP_PER_LINE;
         fprintf(fptmp,"%i %lf\n",k,tbuf[k]);
      }
      fclose(fptmp);
    
      fftwf_execute(b_longb);
    
      /* average the summed up pulse */
      sum = 0.0;
      for (k=0; k<O_SAMP_PER_LINE; k++) 
        { 
          tbuf[k] = sqrt(b[k].real*b[k].real+b[k].imag*b[k].imag)/(double)O_SAMP_PER_LINE;
          sum += tbuf[k];
        }
      sum = sum / (double) O_SAMP_PER_LINE;
      fptmp = fopen("b.time2","w");
      for (k=0; k<O_SAMP_PER_LINE; k++) {
         tbuf[k] -= sum;
         fprintf(fptmp,"%i %lf\n",k,tbuf[k]);
         ovpulse[ndwps-1][k] = tbuf[k];  /* save averaged pulses as we go */
      }
      fclose(fptmp);

      last_dwp = this_dwp;
      last_start = curr_line;
  }
  
  printf("\n");
  fclose(fpin_dat);
  fclose(fpin_hdr);
  fpin_dat = fopen(indat,"rb");
  if (fpin_dat == NULL) {printf("ERROR: Unable to open input data file %s\n",indat); exit(1);}  
  
  printf("Opening output file\n");
  fpout_dat = fopen(outdat,"wb");
  if (fpout_dat == NULL) {printf("ERROR: Unable to open output data file %s\n",outdat); exit(1);}  

  printf("Number of DWPs is %i\n",ndwps);

  /* Now that we have the pulses, apply them to the data file */
  sum = 0.0; curr_line =0;
  for (k=0; k<ndwps; k++) {

    printf("%i : trying range of %i to %i\n",k,dwp_positions[k],dwp_positions[k+1]);
	
    for (j=dwp_positions[k]; j<dwp_positions[k+1]; j++) {
      fread(buf,SAMPLES_PER_LINE,1,fpin_dat);
      sum =0.0; 
      for (i=0; i<SAMPLES_PER_LINE; i++) { fbuf[i] = (double) buf[i]; sum += fbuf[i]; }
      sum = sum / SAMPLES_PER_LINE;
      for (i=0; i<SAMPLES_PER_LINE; i++) fbuf[i] -= sum;
      match_pulse(fbuf,ovpulse[k],fbuf2);
      for (i=0; i<SAMPLES_PER_LINE; i++) { buf[i] = (int) (fbuf2[i]+sum); }
      fwrite(buf,SAMPLES_PER_LINE,1,fpout_dat); 
      curr_line+=1.0;
    }
  }

  sprintf(tmp,"cp %s %s\n",inhdr,outhdr);
  system(tmp);
  printf("Done correcting file, wrote %i lines of output\n\n",curr_line);
  fclose(fpout_dat);
  exit(0);
}
예제 #2
0
/**
 * Read the instrument group
 * @param mtd_entry :: The node for the current workspace
 * @param local_workspace :: The workspace to attach the instrument
 */
void LoadNexusProcessed::readInstrumentGroup(NXEntry & mtd_entry, API::MatrixWorkspace_sptr local_workspace)
{
  //Instrument information
  NXInstrument inst = mtd_entry.openNXInstrument("instrument");
  if ( ! inst.containsGroup("detector") )
  {
    g_log.information() << "Detector block not found. The workspace will not contain any detector information.\n";
    return;
  }

  //Populate the spectra-detector map
  NXDetector detgroup = inst.openNXDetector("detector");

  //Read necessary arrays from the file
  // Detector list contains a list of all of the detector numbers. If it not present then we can't update the spectra
  // map
  int ndets(-1);
  boost::shared_array<int> det_list(NULL);
  try
  {
    NXInt detlist_group = detgroup.openNXInt("detector_list");
    ndets = detlist_group.dim0();
    detlist_group.load();
    det_list.swap(detlist_group.sharedBuffer());
  }
  catch(std::runtime_error &)
  {
    g_log.information() << "detector_list block not found. The workspace will not contain any detector information."
        << std::endl;
    return;
  }

  //Detector count contains the number of detectors associated with each spectra
  NXInt det_count = detgroup.openNXInt("detector_count");
  det_count.load();
  //Detector index - contains the index of the detector in the workspace
  NXInt det_index = detgroup.openNXInt("detector_index");
  det_index.load();
  int nspectra = det_index.dim0();

  //Spectra block - Contains spectrum numbers for each workspace index
  // This might not exist so wrap and check. If it doesn't exist create a default mapping
  bool have_spectra(true);
  boost::shared_array<int> spectra(NULL);
  try
  {
    NXInt spectra_block = detgroup.openNXInt("spectra");
    spectra_block.load();
    spectra.swap(spectra_block.sharedBuffer());
  }
  catch(std::runtime_error &)
  {
    have_spectra = false;
  }

  //Now build the spectra list
  int *spectra_list = new int[ndets];
  API::Axis *axis1 = local_workspace->getAxis(1);
  int index=0;

  for(int i = 1; i <= nspectra; ++i)
  { 
      int spectrum(-1);
      if( have_spectra ) spectrum = spectra[i-1];
      else spectrum = i+1 ;

      if ((i >= m_spec_min && i < m_spec_max )||(m_list && find(m_spec_list.begin(), m_spec_list.end(),
        i) != m_spec_list.end()))
      {
        if( m_axis1vals.empty() )
        {
          axis1->spectraNo(index) = spectrum;
        }
        else
        {
          axis1->setValue(index, m_axis1vals[i-1]);
        }
        ++index;
      }

      int offset = det_index[i-1];
      int detcount = det_count[i-1];
      for(int j = 0; j < detcount; j++)
      {
        spectra_list[offset + j] = spectrum;
      }
     
  }
  local_workspace->replaceSpectraMap(new SpectraDetectorMap(spectra_list, det_list.get(), ndets));
  delete[] spectra_list;
}
예제 #3
0
    /**
    * Reads the data from the file. It is assumed that the provided file stream has its position
    * set such that the first call to getline will be give the first line of data
    * @param file :: A reference to a file stream
    * @returns A pointer to a new workspace
    */
    API::Workspace_sptr LoadAscii::readData(std::ifstream & file) const
    {
      // Get the first line and find the number of spectra from the number of columns
      std::string line;
      getline(file,line);
      boost::trim(line);

      std::list<std::string> columns;
      const int numCols = splitIntoColumns(columns, line);
      if( numCols < 2 ) 
      {
        g_log.error() << "Invalid data format found in file \"" << getPropertyValue("Filename") << "\"\n";
        throw std::runtime_error("Invalid data format. Fewer than 2 columns found.");
      }
      size_t numSpectra(0);
      bool haveErrors(false);
      bool haveXErrors(false);
      // Assume single data set with no errors
      if( numCols == 2 )
      {
        numSpectra = numCols/2;
      }
      // Data with errors
      else if( (numCols-1) % 2 == 0 )
      {
        numSpectra = (numCols - 1)/2;
        haveErrors = true;
      }
      // Data with errors on both X and Y (4-column file)
      else if( numCols == 4 )
      {
        numSpectra = 1;
        haveErrors = true;
        haveXErrors = true;
      }
      else
      {
        g_log.error() << "Invalid data format found in file \"" << getPropertyValue("Filename") << "\"\n";
        g_log.error() << "LoadAscii requires the number of columns to be an even multiple of either 2 or 3.";
        throw std::runtime_error("Invalid data format.");
      }

      // A quick check at the number of lines won't be accurate enough as potentially there
      // could be blank lines and comment lines
      int numBins(0), lineNo(0);
      std::vector<DataObjects::Histogram1D> spectra(numSpectra);
      std::vector<double> values(numCols, 0.);
      do
      {
        ++lineNo;
        boost::trim(line);
        if( this->skipLine(line) ) continue;
        columns.clear();
        int lineCols = this->splitIntoColumns(columns, line); 
        if( lineCols != numCols )
        {
          std::ostringstream ostr;
          ostr << "Number of columns changed at line " << lineNo;
          throw std::runtime_error(ostr.str());
        }

        try
        {
          fillInputValues(values, columns); //ignores nans and replaces them with 0
        }
        catch(boost::bad_lexical_cast&)
        {
          g_log.error() << "Invalid value on line " << lineNo << " of \""
            << getPropertyValue("Filename") << "\"\n";
          throw std::runtime_error("Invalid value encountered.");
        }

        for (size_t i = 0; i < numSpectra; ++i)
        {
          spectra[i].dataX().push_back(values[0]);
          spectra[i].dataY().push_back(values[i*2+1]);
          if( haveErrors )
          {
            spectra[i].dataE().push_back(values[i*2+2]);
          }
          else
          {
            spectra[i].dataE().push_back(0.0);
          }
          if( haveXErrors )
          {
            // Note: we only have X errors with 4-column files.
            // We are only here when i=0.
            spectra[i].dataDx().push_back(values[3]);
          }
          else
          {
            spectra[i].dataDx().push_back(0.0);
          }
        }
        ++numBins;
      }
      while(getline(file,line));

      MatrixWorkspace_sptr localWorkspace = boost::dynamic_pointer_cast<MatrixWorkspace>
        (WorkspaceFactory::Instance().create("Workspace2D",numSpectra,numBins,numBins));
      try 
      {
        localWorkspace->getAxis(0)->unit() = UnitFactory::Instance().create(getProperty("Unit"));
      } 
      catch (Exception::NotFoundError&) 
      {
        // Asked for dimensionless workspace (obviously not in unit factory)
      }

      for (size_t i = 0; i < numSpectra; ++i)
      {
        localWorkspace->dataX(i) = spectra[i].dataX();
        localWorkspace->dataY(i) = spectra[i].dataY();
        localWorkspace->dataE(i) = spectra[i].dataE();
        // Just have spectrum number start at 1 and count up
        localWorkspace->getAxis(1)->spectraNo(i) = static_cast<specid_t>(i+1);
      }
      return localWorkspace;
    }