1212import json
1313import galsim
1414from skycatalogs .objects .base_object import LSST_BANDS , load_lsst_bandpasses
15+ from skycatalogs .objects .base_object import load_roman_bandpasses
1516from skycatalogs .objects .trilegal_object import TrilegalConfigFragment
1617from .utils .config_creator_utils import assemble_provenance
1718from .utils .config_creator_utils import assemble_file_metadata
19+ from .utils .parquet_schema_utils import make_star_flux_schema
1820from skycatalogs .utils .trilegal_utils import get_trilegal_hp_nrows
1921from skycatalogs .utils .trilegal_utils import find_trilegal_subpixels
2022
@@ -254,14 +256,16 @@ def _do_trilegal_flux_chunk(send_conn, collection, instrument_needed,
254256 id = collection .get_native_attribute ('id' )
255257 imag = collection .get_native_attribute ('imag' )
256258 out_dict ['id' ] = id [l_bnd : u_bnd ]
259+
260+ # Compute LSST fluxes
257261 fluxes = []
258262 sed_ix = 0
259263 for ix in range (l_bnd , u_bnd ):
260264 obj_fluxes = []
261265 sed = seds [sed_ix ]
262266
263267 if sed is None :
264- obj_fluxes = [0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ]
268+ obj_fluxes = [0.0 ] * len ( LSST_BANDS )
265269 else :
266270 # Apply extinction; calculate fluxes
267271 sed = extinguisher .extinguish (sed , av [ix ])
@@ -277,6 +281,32 @@ def _do_trilegal_flux_chunk(send_conn, collection, instrument_needed,
277281 out_dict .update (flux_dict )
278282 del fluxes_transpose
279283
284+ # Compute Roman fluxes if requested
285+ if 'roman' in instrument_needed :
286+ roman_bandpasses = load_roman_bandpasses (include_all_bands = True )
287+ roman_fluxes = []
288+ sed_ix = 0
289+ for ix in range (l_bnd , u_bnd ):
290+ obj_fluxes = []
291+ sed = seds [sed_ix ]
292+
293+ if sed is None :
294+ obj_fluxes = [0.0 ] * len (roman_bandpasses )
295+ else :
296+ # Apply extinction; calculate Roman fluxes
297+ sed = extinguisher .extinguish (sed , av [ix ])
298+ for band in roman_bandpasses :
299+ obj_fluxes .append (collection [ix ].get_roman_flux (
300+ band , sed = sed , cache = False ))
301+ roman_fluxes .append (obj_fluxes )
302+ sed_ix += 1
303+
304+ colnames = [f'roman_flux_{ band } ' for band in roman_bandpasses ]
305+ roman_fluxes_transpose = zip (* roman_fluxes )
306+ roman_flux_dict = dict (zip (colnames , roman_fluxes_transpose ))
307+ out_dict .update (roman_flux_dict )
308+ del roman_fluxes_transpose
309+
280310 if debug :
281311 now = datetime .now ().isoformat ()[:19 ]
282312 print (f'{ now } Leaving _do_trilegal_flux_chunk, l_bnd={ l_bnd } , row_group={ row_group } ' , flush = True )
@@ -288,7 +318,7 @@ def _do_trilegal_flux_chunk(send_conn, collection, instrument_needed,
288318
289319
290320class TrilegalFluxCatalogCreator :
291- def __init__ (self , catalog_creator ):
321+ def __init__ (self , catalog_creator , include_roman_flux = False ):
292322 '''
293323 Parameters
294324 ----------
@@ -297,28 +327,10 @@ def __init__(self, catalog_creator):
297327 self ._catalog_creator = catalog_creator
298328 self ._output_dir = catalog_creator ._output_dir
299329 self ._logger = catalog_creator ._logger
330+ self ._include_roman_flux = include_roman_flux
300331 global tri_lsst_bandpasses
301332 tri_lsst_bandpasses = load_lsst_bandpasses ()
302333
303- def _create_flux_schema (self , metadata_input = None ,
304- metadata_key = 'provenance' ):
305- # id and 6 flux fields (for now. Maybe later also Roman)
306- fields = [
307- pa .field ('id' , pa .string ()),
308- pa .field ('lsst_flux_u' , pa .float32 (), True ),
309- pa .field ('lsst_flux_g' , pa .float32 (), True ),
310- pa .field ('lsst_flux_r' , pa .float32 (), True ),
311- pa .field ('lsst_flux_i' , pa .float32 (), True ),
312- pa .field ('lsst_flux_z' , pa .float32 (), True ),
313- pa .field ('lsst_flux_y' , pa .float32 (), True )]
314- if metadata_input :
315- metadata_bytes = json .dumps (metadata_input ).encode ('utf8' )
316- final_metadata = {metadata_key : metadata_bytes }
317- else :
318- final_metadata = None
319-
320- return pa .schema (fields , metadata = final_metadata )
321-
322334 def _create_trilegal_flux_pixel (self , pixel , arrow_schema ):
323335 output_filename = f'trilegal_flux_{ pixel } .parquet'
324336 output_path = os .path .join (self ._catalog_creator ._output_dir ,
@@ -340,6 +352,9 @@ def _create_trilegal_flux_pixel(self, pixel, arrow_schema):
340352 writer = pq .ParquetWriter (output_path , arrow_schema )
341353
342354 instrument_needed = ['lsst' ]
355+ if self ._include_roman_flux :
356+ instrument_needed .append ('roman' )
357+
343358 rg_written = 0
344359 writer = None
345360 fields_needed = arrow_schema .names
@@ -436,13 +451,17 @@ def create_trilegal_flux_catalog(self):
436451 self ._main_template = trilegal_config ['file_template' ]
437452
438453 thru_v = {'lsst_throughputs_version' : self ._cat ._lsst_thru_v }
454+ if self ._include_roman_flux :
455+ thru_v ['roman_throughputs_version' ] = self ._cat ._roman_thru_v
439456 file_metadata = assemble_file_metadata (
440457 self ._catalog_creator ._pkg_root ,
441458 run_options = self ._catalog_creator ._run_options ,
442459 flux_file = True ,
443460 throughputs_versions = thru_v )
444461
445- arrow_schema = self ._create_flux_schema (metadata_input = file_metadata )
462+ arrow_schema = make_star_flux_schema (self ._logger .name ,
463+ include_roman_flux = self ._include_roman_flux ,
464+ metadata_input = file_metadata )
446465
447466 self ._logger .info ('Creating trilegal flux files' )
448467
0 commit comments