-
Notifications
You must be signed in to change notification settings - Fork 9
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
Migrate unit validation to desiutil; support adding units to FITS files. #201
Conversation
* main: update dev version after 3.4.0 tag update release notes and version for desiutil/3.4.0
Basic functionality appears to work as advertised to make it easier to add units/comments to a fits file. Thanks. I have several comments, followed by my opinions on the questions you posed in the original PR post. In approximate order of descending priority:
That sounds fine.
For this PR, let's just stick with
My preference for this PR: if a comment is too long, log an error (or critical) message and stop, unless a new option If that is too much of a hassle, just printing a warning message and moving on would be ok too, as long as there is some way to completely turn off adding comments while still adding units (see earlier bullet).
I think it is fine to add a CHECKSUM no matter what.
Let's do a best effort to hide specific units warnings about cases that we know about and still want to consider valid (nanomaggie, nannomaggy, nanomaggys, nanomaggies, possibly others) while still generically letting warnings through to catch common mistakes like 'deg' vs. 'degree' vs. 'degrees'. |
Thank you @sbailey, these are all good comments. I think that satisfies all the questions I had. It will probably take another day or so for final implementation. |
@sbailey, in the current implementation, the I'm considering not supporting a |
When you made that observation, did you have |
Additional detail on the last comment: As I mentioned, a warning about truncating a comment is already emitted, but the warning doesn't say which comment, so we could add that, or convert the warning to an exception while also providing more information about the comment that triggered it. I checked and the warning is a real |
Indeed, I was using
astropy appears to issue a single "WARNING: VerifyWarning: Card is too long, comment will be truncated. [astropy.io.fits.card]" even if it is truncating multiple comments, which implies that it is a warnings.warn() and is perhaps the motivation for why it doesn't include which comment is being truncated. In this case, I think it would be very helpful to know which comment is being truncated so let's add that info with our own warning message. Resummarizing what I think we want regarding comments:
i.e. make truncating comments "opt-in", and augment the logging to say which keys/comments are being truncated. |
Thank you. As I noted above, it might be easier to eliminate |
@sbailey, et al., this is ready for final review. I removed the |
Dropping One remaining gotcha: I appreciate the concept of reporting all comments that are too long before exiting so that the user doesn't have to find,fix,rerun one-by-one, but as currently implemented the code will error+exit for long comments on columns that aren't even in the input file, i.e. long comments that don't matter. I think it should pre-select only the columns that are in the input file and check only those. Since this is a little more surgery than just adding verbose logging, I'm checking with you first. How about something like: with fits.open(filename, mode='readonly') as hdulist:
...
column_comments = dict()
for i in range(1, 1000):
ttype = f"TTYPE{i:d}"
if ttype in hdu.header:
colname = hdu.header[ttype]
if comments and colname in comments:
column_comments[colname] = comments[colname]
else:
break
n_long = check_comment_length(column_comments, error=(not truncate))
... |
Yes, that's fair to check only the comments on columns that are actually in the file. However, both that change and the change to the logging in |
Actually, I'm having second thoughts about that code snippet. Please do not attempt to insert that, I don't think it actually works with the way that function is planned out. |
Among other things, raising an exception inside a |
And yes, there are other places where exceptions can be raised inside the |
OK, I fixed the tests I broke by changing the details of the log messages, but I'll leave it to you @weaverba137 to investigate how to only check the columns that actually appear in the data file rather than all of them in the csv/yaml file whether they are needed or not. |
Ah, I just discovered another gotcha: independently of checking the comments, there is also nowhere where the units are validated, either in |
* main: update dev version after 3.4.1 tag update release notes and version for desiutil/3.4.1 update changelog for PR #202 fix codestyle whitespace I think add support for removing dependencies
Rechecking latest version:
|
Sounds reasonable. I'll try to get that out tomorrow. |
@sbailey, last suggested change is in. I will plan to merge later today. |
This PR
Please test by running the script
annotate_fits
. Input column definitions can be in CSV format (like desidatamodel'scolumn_descriptions.csv
), YAML (like desitarget'sunits.yaml
) or directly on the command line with aCOLUMN=value:COLUMN=value
notation. Seeannotate_fits --help
for more information.Additional questions that need to be answered before this PR can be completed:
BUNIT
), but this gets into tricky territory, because then do we also have to support adding units to compressed images? I'm leaning toward only supporting adding units to tables, i.e. strictlyfits.BinTableHDU
.TTYPEnnn= 'COLUMN_NAME' / COMMENT
convention, but we could also supportTCOMMnnn= 'COMMENT'
. The latter is not officially part of the FITS standard, but might be in the near future.TCOMMnnn
convention, then a long comment can be split across multiple lines, but then theLONGSTRN
keyword has to be present.annotate_fits()
adds a checksum to the HDU it is modifying, but should it only do so if a checksum is already present?