197 input_ref_dict = butlerQC.get(inputRefs)
199 tract = butlerQC.quantum.dataId[
'tract']
201 source_table_refs = input_ref_dict[
'source_table_visit']
203 self.log.info(
'Running with %d source_table_visit dataRefs',
204 len(source_table_refs))
206 source_table_ref_dict_temp = {source_table_ref.dataId[
'visit']: source_table_ref
for
207 source_table_ref
in source_table_refs}
209 bands = {source_table_ref.dataId[
'band']
for source_table_ref
in source_table_refs}
211 if band
not in self.config.band_order:
212 self.log.warning(
'Input data has data from band %s but that band is not '
213 'configured for matching', band)
217 source_table_ref_dict = {visit: source_table_ref_dict_temp[visit]
for
218 visit
in sorted(source_table_ref_dict_temp.keys())}
220 struct = self.
run(input_ref_dict[
'skymap'], tract, source_table_ref_dict)
222 butlerQC.put(pd.DataFrame(struct.star_source_cat),
223 outputRefs.isolated_star_sources)
224 butlerQC.put(pd.DataFrame(struct.star_cat),
225 outputRefs.isolated_star_cat)
227 def run(self, skymap, tract, source_table_ref_dict):
228 """Run the isolated star association task.
232 skymap : `lsst.skymap.SkyMap`
236 source_table_ref_dict : `dict`
237 Dictionary of source_table refs. Key is visit, value is dataref.
241 struct : `lsst.pipe.base.struct`
242 Struct with outputs for persistence.
246 primary_bands = self.config.band_order
251 if len(primary_star_cat) == 0:
252 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
253 star_cat=np.zeros(0, primary_star_cat.dtype))
258 if len(primary_star_cat) == 0:
259 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
260 star_cat=np.zeros(0, primary_star_cat.dtype))
263 inner_tract_ids = skymap.findTractIdArray(primary_star_cat[self.config.ra_column],
264 primary_star_cat[self.config.dec_column],
266 use = (inner_tract_ids == tract)
267 self.log.info(
'Total of %d isolated stars in inner tract.', use.sum())
269 primary_star_cat = primary_star_cat[use]
271 if len(primary_star_cat) == 0:
272 return pipeBase.Struct(star_source_cat=np.zeros(0, star_source_cat.dtype),
273 star_cat=np.zeros(0, primary_star_cat.dtype))
278 len(primary_star_cat))
281 star_source_cat, primary_star_cat = self.
_match_sources(primary_bands,
285 return pipeBase.Struct(star_source_cat=star_source_cat,
286 star_cat=primary_star_cat)
289 """Make a catalog of all the star sources.
293 tract_info : `lsst.skymap.TractInfo`
294 Information about the tract.
295 source_table_ref_dict : `dict`
296 Dictionary of source_table refs. Key is visit, value is dataref.
300 star_source_cat : `np.ndarray`
301 Catalog of star sources.
307 poly = tract_info.outer_sky_polygon
310 for visit
in source_table_ref_dict:
311 source_table_ref = source_table_ref_dict[visit]
312 df = source_table_ref.get(parameters={
'columns': all_columns})
313 df.reset_index(inplace=
True)
315 goodSrc = self.source_selector.selectSources(df)
317 table = df[persist_columns][goodSrc.selected].to_records()
321 table = np.lib.recfunctions.append_fields(table,
324 [np.where(goodSrc.selected)[0],
325 np.zeros(goodSrc.selected.sum(), dtype=np.int32)],
331 tract_use = poly.contains(np.deg2rad(table[self.config.ra_column]),
332 np.deg2rad(table[self.config.dec_column]))
334 tables.append(table[tract_use])
337 star_source_cat = np.concatenate(tables)
339 return star_source_cat
342 """Get the list of sourceTable_visit columns from the config.
346 all_columns : `list` [`str`]
348 persist_columns : `list` [`str`]
349 Columns to persist (excluding selection columns)
351 columns = [self.config.id_column,
353 self.config.ra_column, self.config.dec_column,
354 self.config.physical_filter_column, self.config.band_column,
355 self.config.inst_flux_field, self.config.inst_flux_field +
'Err']
356 columns.extend(self.config.extra_columns)
358 all_columns = columns.copy()
359 if self.source_selector.config.doFlags:
360 all_columns.extend(self.source_selector.config.flags.bad)
361 if self.source_selector.config.doUnresolved:
362 all_columns.append(self.source_selector.config.unresolved.name)
363 if self.source_selector.config.doIsolated:
364 all_columns.append(self.source_selector.config.isolated.parentName)
365 all_columns.append(self.source_selector.config.isolated.nChildName)
366 if self.source_selector.config.doRequirePrimary:
367 all_columns.append(self.source_selector.config.requirePrimary.primaryColName)
369 return all_columns, columns
372 """Match primary stars.
376 primary_bands : `list` [`str`]
377 Ordered list of primary bands.
378 star_source_cat : `np.ndarray`
379 Catalog of star sources.
383 primary_star_cat : `np.ndarray`
384 Catalog of primary star positions
386 ra_col = self.config.ra_column
387 dec_col = self.config.dec_column
391 primary_star_cat =
None
392 for primary_band
in primary_bands:
393 use = (star_source_cat[
'band'] == primary_band)
395 ra = star_source_cat[ra_col][use]
396 dec = star_source_cat[dec_col][use]
398 with Matcher(ra, dec)
as matcher:
401 idx = matcher.query_groups(self.config.match_radius/3600., min_match=1)
402 except AttributeError:
404 idx = matcher.query_self(self.config.match_radius/3600., min_match=1)
409 self.log.info(
'Found 0 primary stars in %s band.', primary_band)
412 band_cat = np.zeros(count, dtype=dtype)
413 band_cat[
'primary_band'] = primary_band
419 if ra.min() < 60.0
and ra.max() > 300.0:
420 ra_temp = (ra + 180.0) % 360. - 180.
426 for i, row
in enumerate(idx):
428 band_cat[ra_col][i] = np.mean(ra_temp[row])
429 band_cat[dec_col][i] = np.mean(dec[row])
433 band_cat[ra_col] %= 360.0
436 if primary_star_cat
is None or len(primary_star_cat) == 0:
437 primary_star_cat = band_cat
439 with Matcher(band_cat[ra_col], band_cat[dec_col])
as matcher:
440 idx = matcher.query_radius(primary_star_cat[ra_col],
441 primary_star_cat[dec_col],
442 self.config.match_radius/3600.)
444 match_indices = np.array([i
for i
in range(len(idx))
if len(idx[i]) > 0])
445 if len(match_indices) > 0:
446 band_cat = np.delete(band_cat, match_indices)
448 primary_star_cat = np.append(primary_star_cat, band_cat)
449 self.log.info(
'Found %d primary stars in %s band.', len(band_cat), primary_band)
452 if primary_star_cat
is None:
453 primary_star_cat = np.zeros(0, dtype=dtype)
455 return primary_star_cat
458 """Remove neighbors from the primary star catalog.
462 primary_star_cat : `np.ndarray`
463 Primary star catalog.
467 primary_star_cat_cut : `np.ndarray`
468 Primary star cat with neighbors removed.
470 ra_col = self.config.ra_column
471 dec_col = self.config.dec_column
473 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
478 idx = matcher.query_groups(self.config.isolation_radius/3600., min_match=2)
479 except AttributeError:
481 idx = matcher.query_self(self.config.isolation_radius/3600., min_match=2)
484 neighbor_indices = np.concatenate(idx)
486 neighbor_indices = np.zeros(0, dtype=int)
488 if len(neighbor_indices) > 0:
489 neighbored = np.unique(neighbor_indices)
490 self.log.info(
'Cutting %d objects with close neighbors.', len(neighbored))
491 primary_star_cat = np.delete(primary_star_cat, neighbored)
493 return primary_star_cat
496 """Match individual sources to primary stars.
500 bands : `list` [`str`]
502 star_source_cat : `np.ndarray`
503 Array of star sources.
504 primary_star_cat : `np.ndarray`
505 Array of primary stars.
509 star_source_cat_sorted : `np.ndarray`
510 Sorted and cropped array of star sources.
511 primary_star_cat : `np.ndarray`
512 Catalog of isolated stars, with indexes to star_source_cat_cut.
514 ra_col = self.config.ra_column
515 dec_col = self.config.dec_column
519 n_source_per_band_per_obj = np.zeros((len(bands),
520 len(primary_star_cat)),
524 with Matcher(primary_star_cat[ra_col], primary_star_cat[dec_col])
as matcher:
525 for b, band
in enumerate(bands):
526 band_use, = np.where(star_source_cat[
'band'] == band)
528 idx = matcher.query_radius(star_source_cat[ra_col][band_use],
529 star_source_cat[dec_col][band_use],
530 self.config.match_radius/3600.)
531 n_source_per_band_per_obj[b, :] = np.array([len(row)
for row
in idx])
533 band_uses.append(band_use)
535 n_source_per_obj = np.sum(n_source_per_band_per_obj, axis=0)
537 primary_star_cat[
'nsource'] = n_source_per_obj
538 primary_star_cat[
'source_cat_index'][1:] = np.cumsum(n_source_per_obj)[:-1]
540 n_tot_source = primary_star_cat[
'source_cat_index'][-1] + primary_star_cat[
'nsource'][-1]
543 source_index = np.zeros(n_tot_source, dtype=np.int32)
544 obj_index = np.zeros(n_tot_source, dtype=np.int32)
547 for i
in range(len(primary_star_cat)):
548 obj_index[ctr: ctr + n_source_per_obj[i]] = i
549 for b
in range(len(bands)):
550 source_index[ctr: ctr + n_source_per_band_per_obj[b, i]] = band_uses[b][idxs[b][i]]
551 ctr += n_source_per_band_per_obj[b, i]
553 source_cat_index_band_offset = np.cumsum(n_source_per_band_per_obj, axis=0)
555 for b, band
in enumerate(bands):
556 primary_star_cat[f
'nsource_{band}'] = n_source_per_band_per_obj[b, :]
559 primary_star_cat[f
'source_cat_index_{band}'] = primary_star_cat[
'source_cat_index']
562 primary_star_cat[f
'source_cat_index_{band}'] = (primary_star_cat[
'source_cat_index']
563 + source_cat_index_band_offset[b - 1, :])
565 star_source_cat = star_source_cat[source_index]
566 star_source_cat[
'obj_index'] = obj_index
568 return star_source_cat, primary_star_cat