Commit 79051632 authored by Andrew Strong's avatar Andrew Strong

source/*cc

parent 36842732
#include "Model.h"
ArrayModel::ArrayModel(const Exposure &exposure, const CountsMap &counts, const Psf &psf, const Sources &sources, const Parameters &pars, const Skymap<char> &filter, unsigned int configure) :
BaseModel(counts,exposure,psf,sources,pars, filter, configure), fmodels(0)
{
}
void ArrayModel::setModels( const std::vector<BaseModel*> & models) {
//Check for null pointers in the array
for (int i = 0; i < models.size(); ++i){
if (models[i] == 0) {
throw(ModelError("Null pointer in array model, aborting"));
}
}
//Clear up the variables
fvariables.clear();
//Set the model array to the new models
fmodels = models;
//Join the variables
for (int i = 0; i < fmodels.size(); ++i){
fvariables.add(fmodels[i]->getVariables());
}
}
void ArrayModel::getMap(const Variables & vars, Skymap<double> &map) {
for (int i = 0; i < fmodels.size(); ++i){
fmodels[i]->getMap(vars, map);
}
}
BaseModel::gradMap ArrayModel::getComponents(const Variables & vars, const std::string &prefix){
gradMap output;
for (int i = 0; i < fmodels.size(); ++i){
gradMap tmp(fmodels[i]->getComponents(vars, prefix));
for (gradMap::iterator it = tmp.begin(); it != tmp.end(); ++it){
//Check if something exists, if it does, add the new map
//rather than replace it
gradMap::iterator it2 = output.find((*it).first);
if (it2 == output.end()) {
output[(*it).first] = (*it).second;
} else {
output[(*it).first] += (*it).second;
}
}
}
return output;
}
void ArrayModel::getGrads(const Variables & vars, const std::string & varName, Skymap<double> &map) {
for (int i = 0; i < fmodels.size(); ++i){
fmodels[i]->getGrads(vars,varName, map);
}
}
BaseModel::gradMap ArrayModel::getSecDer(const Variables & vars, const std::string & varName1, const std::string & varName2) {
gradMap output;
//Loop over the models and add to output
for (int i = 0; i < fmodels.size(); ++i){
gradMap tmp(fmodels[i]->getSecDer(vars,varName1,varName2));
for (gradMap::iterator it = tmp.begin(); it != tmp.end(); ++it){
//Check if something exists, if it does, add the new map
//rather than replace it
gradMap::iterator it2 = output.find((*it).first);
if (it2 == output.end()) {
output[(*it).first] = (*it).second;
} else {
output[(*it).first] += (*it).second;
}
}
}
return output;
}
void ArrayModel::modifySources(const Variables & vars, Sources &sources) const {
for (int i = 0; i < fmodels.size(); ++i) {
fmodels[i]->modifySources(vars,sources);
}
}
This diff is collapsed.
using namespace std;
// for slalib version which uses: if defined (__cplusplus) extern "C"
// but this does not work, use version without this
// #define __cplusplus
#include<iostream>
#include"fitsio.h"
#include"slalib.h"
#include"CAT.h"
//////////////////////////////////////////////////////
///////////////////////////////////////////////////////
int CAT:: read (char *directory)
{
char infile[500];
char err_text[100];
char comment[100];
fitsfile *fptr;
int status;
int hdunum,hdutype,total_hdus,colnum;
int n_columns,n_rows;
double nulval=0.;
int anynul =0;
char nulstr[100];strcpy(nulstr,"null");
double dtr=acos(-1.0)/180.;
int i,j,k;
//char catalogue_file[]="gnrl_refr_cat_0020_reformatted.fits";
//char catalogue_file[]="isgriCat_2nd_Tony.fits";
char catalogue_file[]="isgri_2nd_cat_revised.fits";
cout<< "CAT::read (" << directory<<")" <<endl;
strcpy(infile,directory);
strcat(infile,catalogue_file);
cout<<" infile "<<infile<<endl;
status=0; // needed
fits_open_file(&fptr,infile,READONLY,&status) ;
cout<<"FITS read open status= "<<status<<" "<<infile<<endl;
fits_get_errstatus(status,err_text);
cout<<err_text<<endl;
fits_get_num_hdus(fptr,&total_hdus,&status);
cout<<"total number of header units="<<total_hdus<<endl;
hdunum=1;
fits_movabs_hdu(fptr,hdunum+1,&hdutype,&status );
cout<<"FITS movabs_hdu hdunum="<<hdunum
<<" status= "<<status<<" hdutype="<<hdutype<<endl ;
fits_read_key(fptr,TLONG,"TFIELDS",&n_columns,comment,&status);
cout<<" n_columns ="<<n_columns ;
fits_read_key(fptr,TLONG,"NAXIS2",&n_rows ,comment,&status);
cout<<" n_rows ="<<n_rows <<endl;
n_sources=n_rows;
ra =new double[n_rows];
dec =new double[n_rows];
flux =new double[n_rows];//AWS20060110
flux_err=new double[n_rows];//AWS20060110
fits_get_colnum(fptr,CASEINSEN,"RA_OBJ",&colnum,&status);
cout<<"RA_OBJ column ="<< colnum<<endl;
fits_read_col(fptr, TDOUBLE, colnum, 1,1,n_rows,&nulval,ra, &anynul,&status);
fits_get_colnum(fptr,CASEINSEN,"DEC_OBJ",&colnum,&status);
cout<<"DEC_OBJ column ="<< colnum<<endl;
fits_read_col(fptr, TDOUBLE, colnum, 1,1,n_rows,&nulval,dec, &anynul,&status);
// cout<<"ra :";for (i=0;i<n_rows;i++)cout<<" "<<ra [i];cout<<endl;
//cout<<"dec:";for (i=0;i<n_rows;i++)cout<<" "<<dec[i];cout<<endl;
fits_get_colnum(fptr,CASEINSEN,"NAME",&colnum,&status);
cout<<"NAME column ="<< colnum<<endl;
name=new char*[n_rows];
for (i=0;i<n_rows;i++) name[i]=new char[100];
fits_read_col_str(fptr, colnum, 1,1, n_rows,nulstr, name, &anynul,&status);
// for (i=0;i<n_rows;i++) cout<< name[i] <<" ra="<<ra[i]<<" dec="<<dec[i]<<endl;
longitude =new double[n_rows];
latitude =new double[n_rows];
for (i=0;i<n_rows;i++)
{
// NB had to compile sla_c since undefined symbol using lib from osa4.2
slaEqgal(ra[i]*dtr, dec[i]*dtr, &longitude[i], &latitude[i]);
longitude[i]/=dtr;
latitude[i]/=dtr;
}
return 0;
}
///////////////////////////////////////////////////////
int CAT:: read (char *directory,char *filename) //AWS20060110
{
char infile[500];
char err_text[100];
char comment[100];
fitsfile *fptr;
int status;
int hdunum,hdutype,total_hdus,colnum;
int n_columns,n_rows;
double nulval=0.;
int anynul =0;
char nulstr[100];strcpy(nulstr,"null");
double dtr=acos(-1.0)/180.;
int i,j,k;
cout<< "CAT::read (" << directory<<"/"<<filename<<")" <<endl;
strcpy(infile,directory);
strcat(infile,filename);
cout<<"CAT::read infile= "<<infile<<endl;
status=0; // needed
fits_open_file(&fptr,infile,READONLY,&status) ;
cout<<"FITS read open status= "<<status<<" "<<infile<<endl;
fits_get_errstatus(status,err_text);
cout<<err_text<<endl;
fits_get_num_hdus(fptr,&total_hdus,&status);
cout<<"total number of header units="<<total_hdus<<endl;
hdunum=total_hdus-1; // because spimodfit output has also grouping extension, make it thus more general assuming catalogue is last extension
fits_movabs_hdu(fptr,hdunum+1,&hdutype,&status );
cout<<"FITS movabs_hdu hdunum="<<hdunum
<<" status= "<<status<<" hdutype="<<hdutype<<endl ;
fits_read_key(fptr,TLONG,"TFIELDS",&n_columns,comment,&status);
cout<<" n_columns ="<<n_columns ;
fits_read_key(fptr,TLONG,"NAXIS2",&n_rows ,comment,&status);
cout<<" n_rows ="<<n_rows <<endl;
n_sources=n_rows;
ra =new double[n_rows];
dec =new double[n_rows];
flux =new double[n_rows];
flux_err=new double[n_rows];
fits_get_colnum(fptr,CASEINSEN,"RA_OBJ",&colnum,&status);
cout<<"RA_OBJ column ="<< colnum<<endl;
fits_read_col(fptr, TDOUBLE, colnum, 1,1,n_rows,&nulval,ra, &anynul,&status);
fits_get_colnum(fptr,CASEINSEN,"DEC_OBJ",&colnum,&status);
cout<<"DEC_OBJ column ="<< colnum<<endl;
fits_read_col(fptr, TDOUBLE, colnum, 1,1,n_rows,&nulval,dec, &anynul,&status);
fits_get_colnum(fptr,CASEINSEN,"FLUX ",&colnum,&status);
cout<<"FLUX column ="<< colnum<<endl;
// read only first value of spectrum vector
for (i=0;i<n_rows;i++)
fits_read_col(fptr, TDOUBLE, colnum,i+ 1,1,1,&nulval,&flux[i], &anynul,&status);
fits_get_colnum(fptr,CASEINSEN,"FLUX_ERR",&colnum,&status);
cout<<"FLUX_ERR column ="<< colnum<<endl;
// read only first value of spectrum vector
for (i=0;i<n_rows;i++)
fits_read_col(fptr, TDOUBLE, colnum,i+ 1,1,1,&nulval,&flux_err[i],&anynul,&status);
// cout<<"ra :";for (i=0;i<n_rows;i++)cout<<" "<<ra [i];cout<<endl;
//cout<<"dec:";for (i=0;i<n_rows;i++)cout<<" "<<dec[i];cout<<endl;
//cout<<"flux: ";for (i=0;i<n_rows;i++)cout<<" "<<flux[i] ;cout<<endl;
//cout<<"flux_err:";for (i=0;i<n_rows;i++)cout<<" "<<flux_err[i];cout<<endl;
fits_get_colnum(fptr,CASEINSEN,"NAME",&colnum,&status);
cout<<"NAME column ="<< colnum<<endl;
name=new char*[n_rows];
for (i=0;i<n_rows;i++) name[i]=new char[100];
fits_read_col_str(fptr, colnum, 1,1, n_rows,nulstr, name, &anynul,&status);
// for (i=0;i<n_rows;i++) cout<< name[i] <<" ra="<<ra[i]<<" dec="<<dec[i]<<endl;
longitude =new double[n_rows];
latitude =new double[n_rows];
for (i=0;i<n_rows;i++)
{
// NB had to compile sla_c since undefined symbol using lib from osa4.2
slaEqgal(ra[i]*dtr, dec[i]*dtr, &longitude[i], &latitude[i]);
longitude[i]/=dtr;
latitude[i]/=dtr;
}
return 0;
}
////////////////////////////////////////////////////////////////
void CAT:: print ()
{
int i;
cout<<"CAT::print"<<endl;
for (i=0;i<n_sources;i++) cout<<"source #"<<i<<" "<< name[i]
<<" ra = "<<ra[i]<<" dec = "<<dec[i]
<<" l = "<<longitude[i]<<" b = "<<latitude[i]
<<" flux = "<<flux [i]<<" +- "<<flux_err[i]
<<endl;
cout<<"end CAT::print"<<endl;
}
////////////////////////////////////////////////////////////////
void CAT:: find (char *name_, double *ra_, double *dec_, double *longitude_, double *latitude_)
{
int i;
int found;
cout<<"CAT::find"<<endl;
found=0;
for (i=0;i<n_sources;i++)
{
/*
cout<<"source #"<<i<<" "<< name[i]
<<" ra = "<<ra[i]<<" dec = "<<dec[i]
<<" l = "<<longitude[i]<<" b = "<<latitude[i]
<<endl;
*/
if(strcmp(name[i],name_)==0) found=1;
if(strcmp(name[i],name_)==0) break;
}
if(found==0)
{
*ra_ =-999.;
*dec_ =-999.;
*longitude_ =-999.;
*latitude_ =-999.;
cout<<"source "<<name_<< " not found in catalogue !"<<endl;
}
if(found==1)
{
cout<<"found source #"<<i<<" "<< name[i]
<<" ra = "<<ra[i]<<" dec = "<<dec[i]
<<" l = "<<longitude[i]<<" b = "<<latitude[i]
<<endl;
*ra_ =ra[i];
*dec_ =dec[i];
*longitude_=longitude[i];
*latitude_ = latitude[i];
}
cout<<"end CAT::find"<<endl;
}
//**.****|****.****|****.****|****.****|****.****|****.****|****.****|****.****|
// * Configure.cc * galprop package * 4/14/2000
//**"****!****"****!****"****!****"****!****"****!****"****!****"****!****"****|
using namespace std;//AWS20050919
#include<iostream> //AWS20050919
#include <cstring> //AWS20111010 for sles11 gcc 4.3.4
#include"Configure.h"
int Configure::init()
{
cout<<">>>>Configure"<<endl;
directory_length=100;
galdef_directory=new char[directory_length];
fits_directory=new char[directory_length];
adjunct_directory=new char[directory_length];
strcpy(galdef_directory, "../GALDEF/");
strcpy( fits_directory, "../FITS/" );
strcpy(adjunct_directory,"../adjunct/" );
cout<<"Configure: galdef_directory: "<< galdef_directory<<endl;
cout<<"Configure: fits_directory: "<< fits_directory<<endl;
cout<<"Configure:adjunct_directory: "<<adjunct_directory<<endl;
cout<<"<<<<Configure"<<endl;
return 0;
}
#include "Coordinate.h"
#include "pointing.h"
#include "PhysicalConstants.h"
#include <iostream>
SM::Coordinate::Coordinate(const pointing &point) {
m_b = 90 - point.theta/utl::kConvertDegreesToRadians;
m_l = point.phi/utl::kConvertDegreesToRadians;
}
pointing SM::Coordinate::healpixAng() const{
const double theta = utl::kPi/2 - utl::kConvertDegreesToRadians*m_b;
const double phi = utl::kConvertDegreesToRadians*m_l;
return pointing(theta,phi);
}
namespace SM {
std::ostream & operator << (std::ostream &os, const SM::Coordinate &coord) {
os << "(" << coord.l() <<","<<coord.b()<<")";
return os;
}
}
#include "Counts.h"
#include <valarray>
#include <CCfits/CCfits>
#include <cmath>
#include <fstream>
#include "Parameters.h"
CountsMap::CountsMap(const Parameters &pars){
//Start by trying a cached old file
std::string countsOutFile="NONE";
{
//Read from countsFile
std::string countsFile;
pars.getParameter("countsFile", countsFile);
std::cout<<"Reading counts from file "<<countsFile<<std::endl;
//Check the parameters for order and energy binning parameters. If not
//found, assume this is binned healpix output
//TODO check the file for headers.
int order = 0;
try{
pars.getParameter("countOrder", order);
}catch (Parameters::ParameterError){ }
//Check for order, if set we assume FT1 files
if (order) {
std::string energyBinning;
std::valarray<double> eMinArr, eMaxArr;
pars.getParameter("energyBinning", energyBinning);
if (energyBinning == "linear" || energyBinning == "log"){
int nEnergyBins;
double eMin, eMax;
pars.getParameter("nEnergyBins", nEnergyBins);
pars.getParameter("eMin", eMin);
pars.getParameter("eMax", eMax);
eMinArr.resize(nEnergyBins);
eMaxArr.resize(nEnergyBins);
if (energyBinning == "linear" ){
double binsize = (eMax-eMin)/nEnergyBins;
for (int i = 0; i < nEnergyBins; ++i){
eMinArr[i] = eMin+i*binsize;
eMaxArr[i] = eMin+(i+1)*binsize;
}
}else {
double binsize = log(eMax/eMin)/nEnergyBins;
for (int i = 0; i < nEnergyBins; ++i){
eMinArr[i] = eMin*exp(i*binsize);
eMaxArr[i] = eMin*exp((i+1)*binsize);
}
}
}else if(energyBinning == "list"){
std::vector<double> eMinVec;
std::vector<double> eMaxVec;
pars.getParameter("eMinList", eMinVec);
pars.getParameter("eMaxList", eMaxVec);
eMinArr.resize(eMinVec.size());
eMaxArr.resize(eMaxVec.size());
eMinArr = std::valarray<double>(&eMinVec[0], eMinVec.size());
eMaxArr = std::valarray<double>(&eMaxVec[0], eMaxVec.size());
}else{
std::cout<<"Energy binning specified \""<<energyBinning<<"\" not available!"<<std::endl;
std::cout<<"Available values (case sensitive): linear, log and list"<<std::endl;
throw (CountsMapException("Energy binning specified \""+energyBinning+"\" not available!\nAvailable values (case sensitive): linear, log and list"));
}
//Try to get the filter from the parameters
std::string filter;
try{
pars.getParameter("FT1filter", filter);
}catch (Parameters::ParameterError){ }
//Get the energy column from the parameters
fEnergyCol = "ENERGY";
try{
pars.getParameter("FT1EnergyCol", fEnergyCol);
} catch (Parameters::ParameterError) {}
reset(order, eMinArr, eMaxArr);
addFile(countsFile, filter);
}else{
ReadHealpixFits(countsFile);
}
if (countsOutFile != "NONE"){
write(countsOutFile);
}
}
}
CountsMap::CountsMap(const std::string &fileName): fLost(0){
ReadHealpixFits(fileName);
}
CountsMap::CountsMap(int order, std::valarray<double> & eMin, std::valarray<double> & eMax){
reset(order, eMin, eMax);
}
CountsMap::CountsMap(const CountsMap & oldCountsMap){
(*this) = oldCountsMap;
}
CountsMap::CountsMap(const std::string &fileName, int order, std::valarray<double> & eMin, std::valarray<double> & eMax, const std::string & filter) :fLost(0){
reset(order, eMin, eMax);
addFile(fileName, filter);
}
void CountsMap::addPhotons(const std::valarray< std::pair< SM::Coordinate, double > > & photonArray){
for (int i = 0; i < photonArray.size(); ++i){
Add(photonArray[i]);
}
}
void CountsMap::addFile(const std::string & fileName, const std::string & filter){
//Open the file and throw exception if failure occurs
std::ifstream is(fileName.c_str());
if (is.fail()){
throw(CountsMapException("Failed to open file \""+fileName+"\""));
}
//Read the first line and look for SIMPLE keyword. If found, read the file
//directly, otherwise, loop over each line in the file, assuming it is a text
//file containing list of FT1 files
std::string line;
std::getline(is, line);
if (line.compare(0,6,"SIMPLE") == 0) {
//We have a FITS file
ReadFT1Fits(fileName, filter);
}else{
//Assume we have a list of FITS files, one per line
while (true) {
ReadFT1Fits(line, filter);
std::getline(is, line);
if (is.eof()){
break;
}
if (is.fail()){
throw(CountsMapException("Error parsing file \""+fileName+"\" assuming it contains a list of FT1 FITS filenames, one per line"));
}
}
}
}
void CountsMap::reset(int order, std::valarray<double> &eMin, std::valarray<double> & eMax){
//Check the valarrays are the same size and in each case, EMin[i] < EMax[i]
//and EMax[i] <= EMin[i+1];
if (eMin.size() != eMax.size()){
throw(CountsMapException("Boundary array sizes do not conform."));
}else{
if (eMin.size() < 1){
throw(CountsMapException("Must have at least one energy bin."));
}
for (int i = 0; i < eMin.size(); ++i){
if (eMin[i] >= eMax[i]){
throw(CountsMapException("Energy bins must have size > 0"));
}
if (i != eMin.size() - 1 && eMax[i] > eMin[i+1]){
throw(CountsMapException("The energy bins can't ovelap and must be in ascending order!"));
}
}
}