192 input_ref_dict = butlerQC.get(inputRefs)
194 tract = butlerQC.quantum.dataId[
'tract']
196 source_table_refs = input_ref_dict[
'source_table_visit']
198 self.log.info(
'Running with %d source_table_visit dataRefs',
199 len(source_table_refs))
201 source_table_ref_dict_temp = {source_table_ref.dataId[
'visit']: source_table_ref
for
202 source_table_ref
in source_table_refs}
204 bands = {source_table_ref.dataId[
'band']
for source_table_ref
in source_table_refs}
206 if band
not in self.config.band_order:
207 self.log.warning(
'Input data has data from band %s but that band is not '
208 'configured for matching', band)
212 source_table_ref_dict = {visit: source_table_ref_dict_temp[visit]
for
213 visit
in sorted(source_table_ref_dict_temp.keys())}
215 struct = self.
run(input_ref_dict[
'skymap'], tract, source_table_ref_dict)
217 butlerQC.put(pd.DataFrame(struct.star_source_cat),
218 outputRefs.isolated_star_sources)
219 butlerQC.put(pd.DataFrame(struct.star_cat),
220 outputRefs.isolated_star_cat)
222 def run(self, skymap, tract, source_table_ref_dict):
223 """Run the isolated star association task.
227 skymap : `lsst.skymap.SkyMap`
231 source_table_ref_dict : `dict`
232 Dictionary of source_table refs. Key is visit, value is dataref.
236 struct : `lsst.pipe.base.struct`
237 Struct with outputs for persistence.
241 primary_bands = self.config.band_order
246 if len(primary_star_cat) == 0:
247 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
248 star_cat=np.zeros(0, primary_star_cat.dtype))
253 if len(primary_star_cat) == 0:
254 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
255 star_cat=np.zeros(0, primary_star_cat.dtype))
258 inner_tract_ids = skymap.findTractIdArray(primary_star_cat[self.config.ra_column],
259 primary_star_cat[self.config.dec_column],
261 use = (inner_tract_ids == tract)
262 self.log.info(
'Total of %d isolated stars in inner tract.', use.sum())
264 primary_star_cat = primary_star_cat[use]
266 if len(primary_star_cat) == 0:
267 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
268 star_cat=np.zeros(0, primary_star_cat.dtype))
273 len(primary_star_cat))
276 star_source_cat, primary_star_cat = self.
_match_sources(primary_bands,
280 return pipeBase.Struct(star_source_cat=star_source_cat,
281 star_cat=primary_star_cat)
284 """Make a catalog of all the star sources.
288 tract_info : `lsst.skymap.TractInfo`
289 Information about the tract.
290 source_table_ref_dict : `dict`
291 Dictionary of source_table refs. Key is visit, value is dataref.
295 star_source_cat : `np.ndarray`
296 Catalog of star sources.
302 poly = tract_info.outer_sky_polygon
305 for visit
in source_table_ref_dict:
306 source_table_ref = source_table_ref_dict[visit]
307 df = source_table_ref.get(parameters={
'columns': all_columns})
308 df.reset_index(inplace=
True)
310 goodSrc = self.source_selector.selectSources(df)
312 table = df[persist_columns][goodSrc.selected].to_records()
316 table = np.lib.recfunctions.append_fields(table,
319 [np.where(goodSrc.selected)[0],
320 np.zeros(goodSrc.selected.sum(), dtype=np.int32)],
326 tract_use = poly.contains(np.deg2rad(table[self.config.ra_column]),
327 np.deg2rad(table[self.config.dec_column]))
329 tables.append(table[tract_use])
332 star_source_cat = np.concatenate(tables)
334 return star_source_cat
337 """Get the list of sourceTable_visit columns from the config.
341 all_columns : `list` [`str`]
343 persist_columns : `list` [`str`]
344 Columns to persist (excluding selection columns)
346 columns = [self.config.id_column,
348 self.config.ra_column, self.config.dec_column,
349 self.config.physical_filter_column, self.config.band_column,
350 self.config.inst_flux_field, self.config.inst_flux_field +
'Err']
351 columns.extend(self.config.extra_columns)
353 all_columns = columns.copy()
354 if self.source_selector.config.doFlags:
355 all_columns.extend(self.source_selector.config.flags.bad)
356 if self.source_selector.config.doUnresolved:
357 all_columns.append(self.source_selector.config.unresolved.name)
358 if self.source_selector.config.doIsolated:
359 all_columns.append(self.source_selector.config.isolated.parentName)
360 all_columns.append(self.source_selector.config.isolated.nChildName)
361 if self.source_selector.config.doRequirePrimary:
362 all_columns.append(self.source_selector.config.requirePrimary.primaryColName)
364 return all_columns, columns
367 """Match primary stars.
371 primary_bands : `list` [`str`]
372 Ordered list of primary bands.
373 star_source_cat : `np.ndarray`
374 Catalog of star sources.
378 primary_star_cat : `np.ndarray`
379 Catalog of primary star positions
381 ra_col = self.config.ra_column
382 dec_col = self.config.dec_column
386 primary_star_cat =
None
387 for primary_band
in primary_bands:
388 use = (star_source_cat[
'band'] == primary_band)
390 ra = star_source_cat[ra_col][use]
391 dec = star_source_cat[dec_col][use]
393 with Matcher(ra, dec)
as matcher:
396 idx = matcher.query_groups(self.config.match_radius/3600., min_match=1)
397 except AttributeError:
399 idx = matcher.query_self(self.config.match_radius/3600., min_match=1)
404 self.log.info(
'Found 0 primary stars in %s band.', primary_band)
407 band_cat = np.zeros(count, dtype=dtype)
408 band_cat[
'primary_band'] = primary_band
414 if ra.min() < 60.0
and ra.max() > 300.0:
415 ra_temp = (ra + 180.0) % 360. - 180.
421 for i, row
in enumerate(idx):
423 band_cat[ra_col][i] = np.mean(ra_temp[row])
424 band_cat[dec_col][i] = np.mean(dec[row])
428 band_cat[ra_col] %= 360.0
431 if primary_star_cat
is None or len(primary_star_cat) == 0:
432 primary_star_cat = band_cat
434 with Matcher(band_cat[ra_col], band_cat[dec_col])
as matcher:
435 idx = matcher.query_radius(primary_star_cat[ra_col],
436 primary_star_cat[dec_col],
437 self.config.match_radius/3600.)
439 match_indices = np.array([i
for i
in range(len(idx))
if len(idx[i]) > 0])
440 if len(match_indices) > 0:
441 band_cat = np.delete(band_cat, match_indices)
443 primary_star_cat = np.append(primary_star_cat, band_cat)
444 self.log.info(
'Found %d primary stars in %s band.', len(band_cat), primary_band)
447 if primary_star_cat
is None:
448 primary_star_cat = np.zeros(0, dtype=dtype)
450 return primary_star_cat
453 """Remove neighbors from the primary star catalog.
457 primary_star_cat : `np.ndarray`
458 Primary star catalog.
462 primary_star_cat_cut : `np.ndarray`
463 Primary star cat with neighbors removed.
465 ra_col = self.config.ra_column
466 dec_col = self.config.dec_column
468 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
473 idx = matcher.query_groups(self.config.isolation_radius/3600., min_match=2)
474 except AttributeError:
476 idx = matcher.query_self(self.config.isolation_radius/3600., min_match=2)
479 neighbor_indices = np.concatenate(idx)
481 neighbor_indices = np.zeros(0, dtype=int)
483 if len(neighbor_indices) > 0:
484 neighbored = np.unique(neighbor_indices)
485 self.log.info(
'Cutting %d objects with close neighbors.', len(neighbored))
486 primary_star_cat = np.delete(primary_star_cat, neighbored)
488 return primary_star_cat
491 """Match individual sources to primary stars.
495 bands : `list` [`str`]
497 star_source_cat : `np.ndarray`
498 Array of star sources.
499 primary_star_cat : `np.ndarray`
500 Array of primary stars.
504 star_source_cat_sorted : `np.ndarray`
505 Sorted and cropped array of star sources.
506 primary_star_cat : `np.ndarray`
507 Catalog of isolated stars, with indexes to star_source_cat_cut.
509 ra_col = self.config.ra_column
510 dec_col = self.config.dec_column
514 n_source_per_band_per_obj = np.zeros((len(bands),
515 len(primary_star_cat)),
519 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
520 for b, band
in enumerate(bands):
521 band_use, = np.where(star_source_cat[
'band'] == band)
523 idx = matcher.query_radius(star_source_cat[ra_col][band_use],
524 star_source_cat[dec_col][band_use],
525 self.config.match_radius/3600.)
526 n_source_per_band_per_obj[b, :] = np.array([len(row)
for row
in idx])
528 band_uses.append(band_use)
530 n_source_per_obj = np.sum(n_source_per_band_per_obj, axis=0)
532 primary_star_cat[
'nsource'] = n_source_per_obj
533 primary_star_cat[
'source_cat_index'][1:] = np.cumsum(n_source_per_obj)[:-1]
535 n_tot_source = primary_star_cat[
'source_cat_index'][-1] + primary_star_cat[
'nsource'][-1]
538 source_index = np.zeros(n_tot_source, dtype=np.int32)
539 obj_index = np.zeros(n_tot_source, dtype=np.int32)
542 for i
in range(len(primary_star_cat)):
543 obj_index[ctr: ctr + n_source_per_obj[i]] = i
544 for b
in range(len(bands)):
545 source_index[ctr: ctr + n_source_per_band_per_obj[b, i]] = band_uses[b][idxs[b][i]]
546 ctr += n_source_per_band_per_obj[b, i]
548 source_cat_index_band_offset = np.cumsum(n_source_per_band_per_obj, axis=0)
550 for b, band
in enumerate(bands):
551 primary_star_cat[f
'nsource_{band}'] = n_source_per_band_per_obj[b, :]
554 primary_star_cat[f
'source_cat_index_{band}'] = primary_star_cat[
'source_cat_index']
557 primary_star_cat[f
'source_cat_index_{band}'] = (primary_star_cat[
'source_cat_index']
558 + source_cat_index_band_offset[b - 1, :])
560 star_source_cat = star_source_cat[source_index]
561 star_source_cat[
'obj_index'] = obj_index
563 return star_source_cat, primary_star_cat