Skip to content

Commit

Permalink
Merge pull request #250 from lephare-photoz/241-the-header-of-the-out…
Browse files Browse the repository at this point in the history
…put-of-mag_gal-t-s-previous-mag_star-in-fortran-is-wrong

add string_to_object function and fix the ascii header
  • Loading branch information
johannct authored Dec 6, 2024
2 parents 4b137d3 + 17fa716 commit f5ecbfa
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/lephare/mag_gal.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"EM_LINES",
"EM_DISPERSION",
"ADD_DUSTEM",
"VERBOSE",
]


Expand Down
11 changes: 1 addition & 10 deletions src/lib/SED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,7 @@ SED::SED(const string nameC, int nummodC, string type) {
nummod = nummodC; // number of the model in the list

// Define nlib to have the type as an interger GAL=0, QSO=1, STAR=2
char t = toupper(type[0]);
if (t == 'S') {
nlib = STAR;
} else if (t == 'Q') {
nlib = QSO;
} else if (t == 'G') {
nlib = GAL;
} else {
throw invalid_argument("Object type not recognized: " + type);
}
nlib = string_to_object(type);

has_emlines = false;
idAge = 0; // index of the age into the SED
Expand Down
29 changes: 29 additions & 0 deletions src/lib/SED.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,37 @@ class SED {
fac_line = p.fac_line;
distMod = p.distMod;
};

//! Convert string to object_type
/*!
\param type String starting with either g, q, or s,
in either lower or upper case. If it is not the case,
throw invalid argument exception.
\return object_type corresponding to input, if valid.
*/
inline static object_type string_to_object(const string &type) {
char t = toupper(type[0]);
if (t == 'S') {
return STAR;
} else if (t == 'Q') {
return QSO;
} else if (t == 'G') {
return GAL;
} else {
throw invalid_argument("Object type not recognized: " + type);
}
}

//! Return true if SED is of object_type GAL, false if not
bool is_gal() { return nlib == GAL ? true : false; }

//! Return true if SED is of object_type QSO, false if not
bool is_qso() { return nlib == QSO ? true : false; }

//! Return true if SED is of object_type STAR, false if not
bool is_star() { return nlib == STAR ? true : false; }

virtual ~SED();
///\brief Read sedFile assumed to be ASCII and build the #lamb_flux vector of
/// oneElLambda elements.
Expand Down Expand Up @@ -147,6 +175,7 @@ class SED {
}
writeSED(sbinOut, sphysOut, sdocOut);
};

virtual void writeMag(bool outasc, ofstream &ofsBin, ofstream &ofsDat,
vector<flt> allFilters, string magtyp) {};

Expand Down
7 changes: 7 additions & 0 deletions src/lib/_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ void applySEDLibTemplate(modT &m, std::string name) {
}

PYBIND11_MODULE(_lephare, mod) {
/*object_type enum for python*/
py::enum_<object_type>(mod, "object_type")
.value("GAL", object_type::GAL)
.value("QSO", object_type::QSO)
.value("STAR", object_type::STAR);

/******** CLASS ONEELLAMBDA *********/
py::class_<oneElLambda>(mod, "oneElLambda")
.def(py::init<double, double, int>(), py::arg("lambin"), py::arg("valin"),
Expand Down Expand Up @@ -152,6 +158,7 @@ PYBIND11_MODULE(_lephare, mod) {
.def_readonly("nummod", &SED::nummod)
.def_readonly("mag", &SED::mag)
.def_readwrite("index_z0", &SED::index_z0)
.def("string_to_object", &SED::string_to_object)
.def("is_gal", &SED::is_gal)
.def("is_star", &SED::is_star)
.def("is_qso", &SED::is_qso)
Expand Down
47 changes: 35 additions & 12 deletions src/lib/mag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Mag::Mag(keymap &key_analysed) {
config = key_analysed["c"].value;

// type of source which is read (Galaxy G, QSO Q, Star S)
typ = key_analysed["t"].value;
object = SED::string_to_object(key_analysed["t"].value);

// Instantiate cosmology
double h0 = (key_analysed["COSMOLOGY"].split_double("70", 3))[0];
Expand Down Expand Up @@ -138,14 +138,33 @@ void Mag::open_files() {
if (!sdatOut) {
throw invalid_argument("Can't open file " + datFile);
}

if (allFlt.size() != 0) {
for (const auto f : allFlt) {
sdatOut << "#" << f.name << "\n";
}
}

// header of the .dat file
sdatOut
<< "# model ext_law E(B-V) L_T(IR) redshift dist_modulus age record "
"N_filt magnitude_vector kcorr_vector em_lines_fluxes_vector "
<< endl;
switch (object) {
case object_type::GAL:
sdatOut
<< "# model ext_law E(B-V) L_T(IR) redshift dist_modulus age "
"N_filt magnitude[N_filt] kcorr[N_filt] em_lines_fluxes[N_filt] "
<< endl;
break;
case object_type::QSO:
sdatOut << "# model ext_law E(B-V) L_T(IR) redshift dist_modulus age "
"N_filt magnitude[N_filt] kcorr[N_filt] "
<< endl;
break;
case object_type::STAR:
sdatOut << "# model N_filt magnitude[N_filt]" << endl;
break;
}
}

cout << " All files opened " << endl;
if (verbose) cout << " All files opened " << endl;
}

// open the opacity files
Expand Down Expand Up @@ -336,12 +355,16 @@ void Mag::print_info() {
// Write the documentation in the GALAXY/QSO/STAR case
void Mag::write_doc() {
sdocOut << "CONFIG_FILE " << config << endl;
if (typ[0] == 'G' || typ[0] == 'g') {
sdocOut << "LIB_TYPE GALAXY" << endl;
} else if (typ[0] == 'Q' || typ[0] == 'q') {
sdocOut << "LIB_TYPE QSO" << endl;
} else if (typ[0] == 'S' || typ[0] == 's') {
sdocOut << "LIB_TYPE STAR" << endl;
switch (object) {
case object_type::GAL:
sdocOut << "LIB_TYPE GALAXY" << endl;
break;
case object_type::QSO:
sdocOut << "LIB_TYPE QSO" << endl;
break;
case object_type::STAR:
sdocOut << "LIB_TYPE STAR" << endl;
break;
}
sdocOut << "LIB_NAME " << lib << endl;
sdocOut << "FILTER_FILE " << filtFile << endl;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/mag.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
/// magnitudes
class Mag {
protected:
string config, typ;
object_type object;
string config;
cosmo lcdm;
string filtFile, magtyp;
bool outasc, verbose;
Expand Down
30 changes: 11 additions & 19 deletions src/lib/mag_gal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,31 +69,23 @@ int main(int argc, char *argv[]) {
// them into a vector
keymap key_analysed = analyse_keywords(argc, argv, list_keywords, nb_ref_key);

// type of source which is read (Galaxy G, QSO Q, Star S)
string typ = key_analysed["t"].value;

// keyword to add the LDUST component to the stellar component (e.g. in BC03)
string addDust = key_analysed["ADD_DUSTEM"].value;

// Define a pointer of the basis class "Mag" which encompasses all the
// elements to create the library
Mag *Magnitude;

// GALAXY CASE
if (typ[0] == 'G' || typ[0] == 'g') {
Magnitude = new GalMag(key_analysed);

// QSO CASE
} else if (typ[0] == 'Q' || typ[0] == 'q') {
Magnitude = new QSOMag(key_analysed);

// STAR CASE
} else if (typ[0] == 'S' || typ[0] == 's') {
Magnitude = new StarMag(key_analysed);

} else {
cout << "The type is not correctly indicated with -t " << endl;
exit(1);
object_type object = SED::string_to_object(key_analysed["t"].value);
switch (object) {
case GAL:
Magnitude = new GalMag(key_analysed);
break;
case QSO:
Magnitude = new QSOMag(key_analysed);
break;
case STAR:
Magnitude = new StarMag(key_analysed);
break;
}

/*
Expand Down
16 changes: 16 additions & 0 deletions tests/lephare/test_sed.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import os
import tempfile

import lephare as lp
import numpy as np
import pytest
from lephare import QSOSED, SED, GalSED, StarSED, flt

TESTDIR = os.path.abspath(os.path.dirname(__file__))
TESTDATADIR = os.path.join(TESTDIR, "../data")


def test_string_to_object():
for t in ["s", "S", "sOap", "STAR"]:
a = lp.SED.string_to_object(t)
assert a == lp.object_type.STAR
for t in ["g", "G", "GAGA", "GAL"]:
a = lp.SED.string_to_object(t)
assert a == lp.object_type.GAL
for t in ["q", "Q", "QUASI", "QSO"]:
a = lp.SED.string_to_object(t)
assert a == lp.object_type.QSO
with pytest.raises(ValueError):
_ = lp.SED.string_to_object("wrong")


def test_sed_constructors():
sed = SED("toto", 10, "GAL")
assert sed.name == "toto"
Expand Down

0 comments on commit f5ecbfa

Please sign in to comment.