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

Improve code for computing VA scale and offsets #5666

Merged
merged 3 commits into from
Jan 28, 2021

Conversation

mcara
Copy link
Member

@mcara mcara commented Jan 26, 2021

This PR improves code in lib/set_velocity_aberration.py based on suggestions from Colin Cox:

  1. Simplify formula for computing constant scale (neglect higher-order terms);
  2. Improve formula for computing RA, DEC offsets based on example code supplied by Colin Cox;
  3. Overall refactoring.

Closes: #5658
Related to JP-1762

@mcara mcara requested review from nden and hbushouse January 26, 2021 06:36
@mcara mcara self-assigned this Jan 26, 2021
@mcara mcara force-pushed the update_va_corr_computations branch from a5b8ce7 to 1008077 Compare January 26, 2021 06:38
@hbushouse
Copy link
Collaborator

Need to update unit tests in test_velocity_aberration.py, which are causing CI to fail.

Copy link
Collaborator

@hbushouse hbushouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code refactoring looks good to me. Just need to fix the the unit tests that are failing.

@hbushouse hbushouse added this to the Build 7.7.x milestone Jan 26, 2021
@mcara mcara force-pushed the update_va_corr_computations branch from c74ceda to fddc5e7 Compare January 26, 2021 20:44
@mcara
Copy link
Member Author

mcara commented Jan 26, 2021

@hbushouse @ColinRCox @antonkoekemoer @nden Could you please take a look at #5658 and comment on the issues either here or there or ping someone who is knowledgeable in how this code and its computed values are intended to be used.

Thanks!

@codecov
Copy link

codecov bot commented Jan 26, 2021

Codecov Report

Merging #5666 (3106945) into master (e549311) will decrease coverage by 0.00%.
The diff coverage is 84.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5666      +/-   ##
==========================================
- Coverage   74.46%   74.45%   -0.01%     
==========================================
  Files         416      416              
  Lines       38110    38127      +17     
  Branches     5000     5000              
==========================================
+ Hits        28378    28387       +9     
- Misses       9732     9740       +8     
Flag Coverage Δ *Carryforward flag
nightly 76.00% <100.00%> (ø) Carriedforward from e549311
unit 54.16% <81.81%> (-0.01%) ⬇️

*This pull request uses carry forward flags. Click here to find out more.

Impacted Files Coverage Δ
jwst/lib/set_velocity_aberration.py 88.40% <84.00%> (-11.60%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e549311...3106945. Read the comment docs.

@mcara mcara force-pushed the update_va_corr_computations branch from e99dc86 to 1f80777 Compare January 27, 2021 02:47
@mcara mcara requested a review from hbushouse January 27, 2021 02:48
@mcara
Copy link
Member Author

mcara commented Jan 27, 2021

@hbushouse I have made several significant changes to code and schemas. Please re-review this PR. For schema review, maybe @jdavies-st could take a look (don't I need to update a schema version somewhere?)

Here is the outline of changes:

  1. I have combined computation of scale with the computation of apparent RA and DEC;
  2. Instead of "offsets" - the differences between RA (DEC) in the moving telescope and barycentric frames, now the values in the moving frame are reported (apparent RA and DEC);
  3. FITS keywords have been changed to VA_RA and VA_DEC (I wanted to have VA_REF_DEC but it does not fit into 8 characters; the reason for REF is that script computes VA angles of meta.wcsinfo.ra_ref and meta.wcsinfo.dec_ref; the reason I renamed DVA to VA is because D comes from differential which, I believe, does not apply to these quantities).
  4. VA_RA and VA_DEC are returned in degrees and not radians;
  5. In jwst/datamodels/schemas/core.schema.yaml I have renamed ra(dec)_offset to va_ra(dec)_ref;
  6. I have somewhat modified Colin Cox' code to return RA in the range [0, 360) instead of [-180, 180) (by re-using gwcs.geometry.CartesianToSpherical model).
  7. Updated unit tests. Previous tests were using RA_ref=0 and DEC_ref=0 which made test for the case of zero velocity to not be able to distinguish between offsets and apparent angle values.

@mcara mcara force-pushed the update_va_corr_computations branch 2 times, most recently from d37d03e to d10b29b Compare January 27, 2021 03:54
- Update code in ``set_velocity_aberration.py`` functions based on Colin Cox
suggestions (simplify DVA scale computation and improve offset
computation). [#5666]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also now add an entry to the datamodels section above, to indicate the change in the keyword names DVA_RA, DVA_DEC to VA_RA, VA_DEC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I realized that too late. I'll add more to the description.

@@ -1293,15 +1293,15 @@ properties:
title: Velocity aberration correction information
type: object
properties:
ra_offset:
title: "[rad] Velocity aberration correction RA offset"
va_ra_ref:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "REF" suffix in other keywords like RA_REF, DEC_REF indicates that these are the coordinates at the reference point of the aperture/image. If VA_RA, VA_DEC are simply the VA-correction target coords, then the "ref" suffix is probably not appropriate, because the target is not necessarily located at the reference point (e.g. dithers, mosaics, and other intentional offsets move the target off the reference point). So unless these RA, Dec values are at the reference point, we should probably drop the "ref" suffix from their datamodel names and just leave them as "va_ra" and "va_dec".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So unless there RA, Dec values are at the reference point ...

It seems that they are. Take a look at the original code:

ra_ref = float(sheader['RA_REF'])
dec_ref = float(sheader['DEC_REF'])
# compute the velocity aberration information
scale_factor = aberration_scale(jwst_dx, jwst_dy, jwst_dz,
ra_ref, dec_ref)
ra_off, dec_off = aberration_offset(jwst_dx, jwst_dy, jwst_dz,
ra_ref, dec_ref)

In the script in the functions that compute VA effects, input arguments are called targ_ra and targ_dec and I find this confusing. In the function that updates FITS header, targ_ra=ra_ref, ... I think it is important to preserve the actual meaning of the keywords.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ha, so it appears that these coords are in fact the RA, Dec of the reference point, which is not necessarily the same as the coords of the target (if the target isn't at the reference point). Yes, it is unfortunate and confusing when the variable names used are "targ_ra" and "targ_dec", which makes you think it's the same as the TARG_RA, TARG_DEC keywords, but in fact they correspond to keywords RA_REF, DEC_REF. So in that case I withdraw my previous comment and think it's OK to leave the "ref" suffix on the meta attribute names. Too bad the stupid FITS standard won't allow us the room to add "REF" to the keyword names too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was writing #5666 (comment) in parallel with you. I was suggesting to add at least an R in front of RA->RRA and DEC->RDEC just to alert readers and make it obvious that something is different about this RA and DEC.

dec_offset:
title: "[rad] Velocity aberration correction Dec offset"
va_dec_ref:
title: "[dec] Velocity aberration apparent reference Dec"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo in "dec" - should be "deg"

# angle between the velocity vector and the direction toward the target.
sin_theta = math.sqrt(1. - cos_theta**2)
apparent_ra: float
Aparent star position in the moving telescope frame.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: "Aparent" should be "Apparent"

tan_theta_p = sin_theta / (gamma * (cos_theta + beta))
theta_p = math.atan(tan_theta_p)
apparent_dec: float
Aparent star position in the moving telescope frame.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same typo here "Apparent"

beta = np.array([velocity_x, velocity_y, velocity_z]) / SPEED_OF_LIGHT
beta2 = np.dot(beta, beta)
if beta2 == 0.0:
return 1.0, targ_ra, targ_dec
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old version of the code issued a warning when the velocity is zero. That would be good to maintain here, just for traceability in the processing log.

Copy link
Member Author

@mcara mcara Jan 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old warning logger.warning('Speed is zero. Forcing scale to 1.0') is interesting. When speed is 0 then mathematically scale is 1. There is nothing exceptional here. Saying the code is "forcing" scale to be 1 is kind of a forceful statement :) I think from the caller's perspective it should not matter how the code handles special cases. In fact, this if can be safely removed from the code if we are talking only about scale. It is here because of the aberrated RA and Dec - that formula should be taking a limit in mathematical sense when v->0. In addition, it is really an extreme check to make code work if when JWST is "launched" in a garage. We do not realy expect a flying JWST with speed 0.

However, if you are thinking of detecting case when software incorrectly passes 0 speed instead of expected v != 0 - then I hope someone will be reading those log files. Otherwise I would suggest using an assert or raising some other exception.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know what do you prefer.

@ColinRCox
Copy link

A zero speed will not interfere with the scale calculation but the RA and Dec calculation will have a divide by zero problem. By the way beta2 is the square of the velocity so I would prefer to name it something like beta_squared to make it clear.

@mcara
Copy link
Member Author

mcara commented Jan 27, 2021

@hbushouse I addressed (locally) most comments. I have a question about the use of REF(ref). Are you OK with va_ref_ra (or maybe make it va_ra_ref to make it consistent with wcsinfo.ra_ref)? Also, what about changing FITS keywords from VA_RA and VA_DEC to VA_RFDEC (the most I can fit in 8 characters) or VA_RDEC?

The log message is back but modified:

logger.warning('Observatory speed is zero. Setting VA scale to 1.0')

I hope we will never see this message.

@mcara mcara force-pushed the update_va_corr_computations branch from ae56d01 to 6a641fb Compare January 27, 2021 19:56
@mcara mcara force-pushed the update_va_corr_computations branch from 312d2f4 to 3106945 Compare January 27, 2021 20:41
@hbushouse hbushouse merged commit 94723d0 into spacetelescope:master Jan 28, 2021
@ColinRCox
Copy link

The formula for computing the scale is exact. It is fairly simple but no higher terms have been omitted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Computation of velocity aberrated RA, DEC and scale
3 participants