void CopyImageData(ExtHDU &InHDU, ExtHDU &OutHDU, fitsfile *infptr, fitsfile *outfptr, float MemLimit)
{
    /*  just copy the existing data across */
    const long nFrames = InHDU.axis(0);
    const long nObjects = InHDU.axis(1);
    const long N = nObjects * nFrames;
    int status = 0;

    const long nElementsInMemory = long(MemLimit / sizeof(T));
    cout << "Elements are " << sizeof(T) << " bytes" << endl;
    const long nIterationsRequired = (N / nElementsInMemory) + 1;
    cout << "Can load " << nElementsInMemory << " elements into memory" << endl;
    cout << "Going to take " << nIterationsRequired << " iterations" << endl;

    FITSUtil::MatchType<T> DataType;
    //cout << "Fitsio data type: " << DataType() << endl;



    //valarray<T> buffer(nElementsInMemory);
    vector<T> buffer(nElementsInMemory);
    for (int iter=0; iter<nIterationsRequired; ++iter)
    {
        long FirstElement = 1 + iter * nElementsInMemory;

        if (iter != nIterationsRequired - 1)
        {
            cout << "Iteration " << iter + 1 << ", reading from element " << FirstElement << " to " << FirstElement + nElementsInMemory << endl;

            //InHDU.read(buffer, FirstElement, nElementsInMemory);
            fits_read_img(infptr, DataType(), FirstElement, nElementsInMemory, 0, &buffer[0], 0, &status);
            if (status) { throw FitsioException(status); }

            fits_write_img(outfptr, DataType(), FirstElement, nElementsInMemory, &buffer[0], &status);
            if (status) { throw FitsioException(status); }
        }
        else
        {
            const long nElementsLeft = N - iter * nElementsInMemory;

            /*  resize the buffer */
            buffer.resize(nElementsLeft);

            cout << "Iteration " << iter + 1 << ", reading from element " << FirstElement << " to " << FirstElement + nElementsLeft << endl;

            fits_read_img(infptr, DataType(), FirstElement, nElementsLeft, 0, &buffer[0], 0, &status);
            if (status) { throw FitsioException(status); }

            //InHDU.read(buffer, FirstElement, nElementsLeft);
            fits_write_img(outfptr, DataType(), FirstElement, nElementsLeft, &buffer[0], &status);
            if (status) { throw FitsioException(status); }
        }
    }


}
Exemplo n.º 2
0
bool hist_lib::write_fits(string filename){
  filename = "!"+filename;
  
  static long naxis(2);
  static long naxes[] = {xysize,xysize};

  using namespace CCfits;
  std::unique_ptr<FITS> pFits;
  try{
    pFits.reset(new FITS(filename,DOUBLE_IMG,naxis,naxes));
  }
  catch (FITS::CantCreate){
    return false;
  }
  
  static std::vector<long> extAx(2,xysize);
  static string names[] = {"Model Diagnostic","Observation Diagnostic"};
  static int nelements;
  nelements = xysize*xysize;

  static std::valarray<double> model_1d(nelements);
  static std::valarray<double> obs_1d(nelements);
  static std::valarray<double> comparison_1d(nelements);

  for (int i=0;i<xysize;i++){
    for (int j=0;j<xysize;j++){
      model_1d[i*xysize+j] = model_hist[i][j];
      obs_1d[i*xysize+j] = obs_hist[i][j];
      comparison_1d[i*xysize+j] = comparison_hist[i][j];
    }
  }

  pFits->pHDU().addKey("DIM",xysize,"Side length of square histogram area"); 
  pFits->pHDU().addKey("H_MIN_X",xrange[0],"Minimum Histogram Bin Value");
  pFits->pHDU().addKey("H_MAX_X",xrange[1],"Maximum Histogram Bin Value");
  pFits->pHDU().addKey("H_MIN_Y",yrange[0],"Minimum Histogram Bin Value");
  pFits->pHDU().addKey("H_MAX_Y",yrange[1],"Maximum Histogram Bin Value");
  pFits->pHDU().addKey("BINSIZE_X",xbinsize,"Histogram bin size");
  pFits->pHDU().addKey("BINSIZE_Y",ybinsize,"Histogram bin size");
  pFits->pHDU().addKey("FITSTAT",chisq,"Fitting Statistic");

  pFits->pHDU().write(1,nelements,comparison_1d);

  ExtHDU* modImage = pFits->addImage(names[0],LONG_IMG,extAx);
  ExtHDU* obsImage = pFits->addImage(names[1],LONG_IMG,extAx);

  modImage->write(1,nelements,model_1d);
  obsImage->write(1,nelements,obs_1d);

  return true;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    gsl_rng_env_setup();
    boost::property_tree::ptree pt;
    
    // List of GPU indices
    std::vector<int> IDlist;

    // Read command line arguments
    po::options_description generic("Options");
    generic.add_options()
    ("version,v", "print version string")
    ("help,h", "print help message")
#ifdef CUDA_ACC
    ("gpu,g", po::value< std::string >(), "comma separated list of GPUs to use (e.g: -g 0,1)")
#endif
    ;
    
    po::options_description positional("Arguments");
    positional.add_options()
    ("config", po::value< std::string >()->required(), "configuration file")
    ("data",  po::value< std::string >()->required(), "survey data file")
    ("output", po::value< std::string >()->required(), "output file name");
    po::positional_options_description positionalOptions;
    positionalOptions.add("config", 1);
    positionalOptions.add("data", 1);
    positionalOptions.add("output", 1);
    
    po::options_description cmdline_options;
    cmdline_options.add(generic).add(positional);
    
    po::variables_map vm;

    // Process generic options
    try {
        po::store(po::command_line_parser(argc, argv)
                  .options(generic)
                  .run(), vm);
        po::notify(vm);
    } catch (po::error &e) {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        std::cerr << cmdline_options << std::endl;
        return 1;
    }
    if (vm.count("help")) {
        cout << cmdline_options << "\n";
        return 0;
    }
    
    if (vm.count("version")) {
         cout << VERSION << "\n";
         return 0;
    }
    
    // In case of GPU acceleration, parse the list of GPUs
#ifdef CUDA_ACC
    if (vm.count("gpu")) {
        
        // Check that none of the requested GPU id is larger than nGPU
        int nGpu;
        int whichGPUs[MAX_GPUS];
        cudaGetDeviceCount(&nGpu);
        
        std::vector<std::string> strs;
        boost::split(strs,vm["gpu"].as<std::string>(),boost::is_any_of(",;"));
        
        for(int i =0; i < strs.size(); i++){
            IDlist.push_back(boost::lexical_cast<int>(strs[i]));
        }
        
        if(IDlist.size() > MAX_GPUS){
            cout << "ERROR: Requested more GPUs than maximum number;"<< endl;
            cout << "Maximum size of GPU array " << MAX_GPUS << endl;
            return -1;
        }
        if(IDlist.size() > nGpu){
            cout << "ERROR: Requested more GPUs than available;"<< endl;
            cout << "Number of GPUs available: " << nGpu << endl;
            return -1;
        }
        
        for(int i=0; i < IDlist.size(); i++){
            
            if(IDlist[i] >= nGpu){
                cout << "ERROR: Requested GPU id not available;"<< endl;
                cout << "Maximum GPU id : " << nGpu - 1 << endl;
                return -1;
            }
            whichGPUs[i] = IDlist[i];
        }
        setWhichGPUs( IDlist.size(), whichGPUs);
    }
#endif
    
    // Process positional arguments
    try {
        po::store(po::command_line_parser(argc, argv)
                  .options(cmdline_options)
                  .positional(positionalOptions)
                  .run(), vm);
        po::notify(vm);
    } catch (po::error &e) {
        std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
        std::cerr << cmdline_options << std::endl;
        return 1;
    }

    // Load the configuration file
    try {
        boost::property_tree::ini_parser::read_ini(vm["config"].as<std::string>(), pt);
    } catch (boost::property_tree::ini_parser_error e) {
        std::cout << e.what() << std::endl;
        exit(-1);
    }

    // Create survey object and load data
    survey *surv = new survey(pt);
    surv->load(vm["data"].as<std::string>());

    // Initialize lensing field
    field *f = new field(pt, surv);
    
    
    // Open output fits file before performing the actual reconstruction
    long naxes[3];
    naxes[0] = f->get_npix();
    naxes[1] = f->get_npix();
    naxes[2] = f->get_nlp();
    long naxis = naxes[2] == 1 ? 2 : 3; // Saving surface as 2d image, not 3d cube
    
    std::auto_ptr<FITS> pFits(0);
    try
    {                
        // overwrite existing file if the file already exists.
        std::stringstream fileName;
        fileName << "!" << vm["output"].as<std::string>();

        pFits.reset( new FITS(fileName.str() , DOUBLE_IMG , naxis , naxes));
    }
    catch (FITS::CantCreate)
    {
        std::cerr << "ERROR: Cant create output FITS file" << std::endl;
        return -1;       
    }
    
    // Array holding the reconstruction
    double *reconstruction = (double *) malloc(sizeof(double)* f->get_npix() * f->get_npix() * f->get_nlp());

    // If CUDA is available, give the option to reconstruct in 3D
    if (f->get_nlp() > 1) {
        density_reconstruction rec(pt, f);
        rec.reconstruct();
        
        // Extracts the reconstructed array
        rec.get_density_map(reconstruction);
    } else {
        // Initialize reconstruction object
        surface_reconstruction rec(pt, f);

        rec.reconstruct();

        // Extracts the reconstructed array
        rec.get_convergence_map(reconstruction);
    }
    
    // Number of elements in the array
    long nelements =  naxes[0] * naxes[1] * naxes[2];
    std::valarray<double> pixels(reconstruction, nelements);
    long  fpixel(1);
    
    // Write primary array
    pFits->pHDU().write(fpixel,nelements,pixels);

    
    std::vector<long> extAx(2, naxes[0]);
    string raName ("RA");
    ExtHDU* raExt = pFits->addImage(raName,DOUBLE_IMG,extAx);
    
    string decName ("DEC");
    ExtHDU* decExt = pFits->addImage(decName,DOUBLE_IMG,extAx);
    
    
    double *  ra  = (double *) malloc(sizeof(double) * naxes[0] * naxes[0]);
    double *  dec = (double *) malloc(sizeof(double) * naxes[0] * naxes[0]);
    
    f->get_pixel_coordinates(ra, dec);
    
    long nExtElements = naxes[0] * naxes[0];
    std::valarray<double> raVals(ra, nExtElements);
    std::valarray<double> decVals(dec, nExtElements);
    
    raExt->write(fpixel, nExtElements, raVals);
    decExt->write(fpixel, nExtElements, decVals);
    
    free(reconstruction);
    free(ra);
    free(dec);
    delete f;
    delete surv;

    return 0;
}