@@ -227,6 +227,12 @@ fitter_t::fitter_t(const options_t& opt, const input_state_t& inp, const gridder
227227
228228 for (uint_t is : range (input.id )) {
229229 chi2_filename[is] = odir+file::get_basename (opts.catalog )+" _" +input.id [is]+" .chi2.grid" ;
230+
231+ if (!input.good [is]) {
232+ file::remove (chi2_filename[is]);
233+ continue ;
234+ }
235+
230236 std::ofstream out;
231237 out.open (chi2_filename[is], std::ios::binary | std::ios::out | std::ios::trunc);
232238 if (!out.is_open ()) {
@@ -352,6 +358,8 @@ void fitter_t::write_chi2(uint_t igrid, const vec1f& chi2, const vec2f& props, u
352358 for (uint_t cis : range (chi2)) {
353359 uint_t is = cis + i0;
354360
361+ if (!input.good .safe [is]) continue ;
362+
355363 if (chi2.safe [cis] > best_chi2.safe [is] + opts.save_bestchi ) continue ;
356364
357365 if (chi2.safe [cis] < best_chi2.safe [is]) {
@@ -477,6 +485,11 @@ void fitter_t::fit_galaxies(const model_t& model, uint_t i0, uint_t i1) {
477485 for (uint_t i : range (i1-i0)) {
478486 uint_t is = i + i0;
479487
488+ if (!input.good .safe [is]) {
489+ // Skip this galaxy
490+ continue ;
491+ }
492+
480493 // Apply constraints on redshift
481494 bool dofit = (idzl.safe [is] <= iz && iz <= idzu.safe [is]);
482495 bool keepfit = true ;
@@ -729,6 +742,11 @@ void fitter_t::fit_galaxies(const model_t& model, uint_t i0, uint_t i1) {
729742
730743 for (uint_t i : range (i1-i0)) {
731744 uint_t is = i + i0;
745+ if (!input.good .safe [is]) {
746+ // Skip this galaxy
747+ continue ;
748+ }
749+
732750 ++output.num_models [is];
733751 if (output.best_chi2 .safe [is] > wsp.chi2 [i]) {
734752 output.best_chi2 .safe [is] = wsp.chi2 [i];
@@ -843,16 +861,34 @@ void fitter_t::find_best_fits() {
843861
844862 if (opts.verbose ) note (" finding best fits..." );
845863
864+ std::string best_fits_odir = opts.output_dir +" best_fits/" ;
865+ bool save_sim = opts.save_sim ;
866+ if (save_sim) {
867+ if (!file::mkdir (best_fits_odir)) {
868+ warning (" could not save simulations" );
869+ warning (" the output directory '" , best_fits_odir, " ' could not be created" );
870+ save_sim = false ;
871+ }
872+ }
873+
846874 for (uint_t is : range (input.id )) {
875+ std::string best_fits_output_file =
876+ best_fits_odir+file::get_basename (opts.catalog )+" _" +input.id [is]+" .sims.fits" ;
877+
847878 if (!is_finite (output.best_chi2 [is])) {
848- if (!silence_invalid_chi2) {
879+ if (input. good [is] && !silence_invalid_chi2) {
849880 warning (" galaxy " , input.id [is], " has no best fit solution" );
850881 warning (" there is probably a problem with the models, please re-run FAST++ with DEBUG=1" );
851882 warning (" (further occurences of this warning for other galaxies will be suppressed)" );
852883 silence_invalid_chi2 = true ;
853884 }
854885
855886 output.best_params (is,_,_) = dnan;
887+
888+ if (save_sim) {
889+ file::remove (best_fits_output_file);
890+ }
891+
856892 continue ;
857893 }
858894
@@ -881,17 +917,11 @@ void fitter_t::find_best_fits() {
881917 bparams.safe (gridder.nparam +ip,_) = output.mc_best_props .safe (is,ip,_);
882918 }
883919
884- if (opts.save_sim ) {
885- std::string odir = opts.output_dir +" best_fits/" ;
886- if (!file::mkdir (odir)) {
887- warning (" could not save simulations" );
888- warning (" the output directory '" , odir, " ' could not be created" );
889- } else {
890- fits::write_table (odir+file::get_basename (opts.catalog )+" _" +input.id [is]+" .sims.fits" ,
891- " result" , bparams, " params" , output.param_names ,
892- " chi2" , output.mc_best_chi2 (is,_)
893- );
894- }
920+ if (save_sim) {
921+ fits::write_table (best_fits_output_file,
922+ " result" , bparams, " params" , output.param_names ,
923+ " chi2" , output.mc_best_chi2 (is,_)
924+ );
895925 }
896926
897927 if (!opts.interval_from_chi2 ) {
0 commit comments