Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

752 merge sky region #759

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
#### Changed

- Updated changelog release instructions to remove each release having an empty "Unreleased" section at the start [#772](https://github.com/askap-vast/vast-pipeline/pull/772)
- Search in a 10 arcsec radius for an existing SkyRegion when adding new images (#759](https://github.com/askap-vast/vast-pipeline/pull/759)

#### Fixed

Expand All @@ -23,6 +24,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
#### List of PRs

- [#772](https://github.com/askap-vast/vast-pipeline/pull/772): fix, docs: Fixed changelog formatting and updated changelog release instructions
- [#759](https://github.com/askap-vast/vast-pipeline/pull/759): feat: Do a cone search for existing SkyRegions when adding new images

## [1.1.1](https://github.com/askap-vast/vast-pipeline/releases/v1.1.1) (2024-10-15)

Expand Down
34 changes: 34 additions & 0 deletions vast_pipeline/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,38 @@ def __str__(self):
return self.name


class SkyRegionQuerySet(models.QuerySet):

def cone_search(
self, ra: float, dec: float, radius_deg: float
) -> models.QuerySet:
"""
Return all the Sources withing radius_deg of (ra,dec).
Returns a QuerySet of Sources, ordered by distance from
(ra,dec) ascending.

Args:
ra: The right ascension value of the cone search central
coordinate.
dec: The declination value of the cone search central coordinate.
radius_deg: The radius over which to perform the cone search.

Returns:
Sources found withing the cone search area.
"""
return (
self.extra(
select={
"distance": "q3c_dist(centre_ra, centre_dec, %s, %s) * 3600"
},
select_params=[ra, dec],
where=["q3c_radial_query(centre_ra, centre_dec, %s, %s, %s)"],
params=[ra, dec, radius_deg],
)
.order_by("distance")
)


class SkyRegion(models.Model):
run = models.ManyToManyField(Run)

Expand All @@ -238,6 +270,8 @@ class SkyRegion(models.Model):
y = models.FloatField()
z = models.FloatField()

objects = SkyRegionQuerySet.as_manager()

def __str__(self):
return f'{round(self.centre_ra, 3)}, {round(self.centre_dec, 3)}'

Expand Down
29 changes: 20 additions & 9 deletions vast_pipeline/pipeline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,38 @@
dask.config.set({"multiprocessing.context": "fork"})


def get_create_skyreg(image: Image) -> SkyRegion:
def get_create_skyreg(image: Image, radius: float = 10.) -> SkyRegion:
'''
This creates a Sky Region object in Django ORM given the related
image object.
This creates a SkyRegion object in Django ORM given the related
image object. If a SkyRegion already exists and has an image radius
within `radius` arcsec of the input image then use that SkyRegion.

Args:
image: The image Django ORM object.
radius: Search radius (in arcsec) for matching to existing SkyRegion

Returns:
The sky region Django ORM object.
'''
# In the calculations below, it is assumed the image has square
# NOTE: In the calculations below, it is assumed the image has square
# pixels (this pipeline has been designed for ASKAP images, so it
# should always be square). It will likely give wrong results if not
skyregions = SkyRegion.objects.filter(
centre_ra=image.ra,
centre_dec=image.dec,
xtr_radius=image.fov_bmin

# Get SkyRegions and image radii areas within `radius` arcsec
radius_deg = radius/3600.
skyregions = SkyRegion.objects.cone_search(
ra=float(image.ra),
dec=float(image.dec),
radius_deg=float(radius_deg)
).filter(
xtr_radius__range=(
image.fov_bmin - radius_deg/2.,
image.fov_bmin + radius_deg/2.
)
)
if skyregions:
skyr = skyregions.get()
# Get the closest in case of multiple matches.
skyr = skyregions[0]
logger.info('Found sky region %s', skyr)
else:
x, y, z = eq_to_cart(image.ra, image.dec)
Expand Down
Loading