astropy
astropy copied to clipboard
Avoid logger info in CCDData.read when unit specifies matches that in FITS file
At the moment, when reading a FITS file with BUNIT set to adu with CCDData.read, one gets the following info message:
INFO:astropy:using the unit adu passed to the FITS reader instead of the unit adu in the FITS file.
it might make sense to avoid emitting this message if the units match? (cc @mwcraig)
But to avoid not emitting this warning, you will have to try to parse the header regardless and risk extra complexity in the code. Sometimes, there is a reason why people pass in their own unit to bypass a bad header, etc. And what if the user-defined ADU is not really the ADU you think it is?
https://github.com/astropy/astropy/blob/f4615bfafafa4bf75dd4a6b52de1695c1b9f8ea2/astropy/nddata/ccddata.py#L666
Perhaps a more general question is whether we should be emitting a warning at all if the unit is provided explicitly? How should users silence the warning if they definitely want to override any units, other than resorting to using filterwarnings?
whether we should be emitting a warning at all
Personally, I would say there is no need for warning but I don't know if it was added for a reason, so I'll have to defer to @mwcraig et al.
IIRC (and it has been a while) the reason for emitting a warning was to ensure the user was aware that they were overriding the unit already in the file.
I think that if the unit is the same there should definitely not be a warning.
My initial inclination is to say users who want to remove the warning should use filterwarnings -- if they routinely need to manually fix the units in their images the problem is not really CCDData, it is their images.
But if I provide a unit, I am already aware that I am overwriting whatever is in the file... 🤷
Oh I just realized that of course this is not a warning, it is a logger message so it is a bit trickier to filter out for users. But good point that fixing the file is the correct thing to do to get rid of the message. In any case I do think we should not show the message if the two units are the same.
Just for reference, this issue is related to https://github.com/astropy/astroquery/issues/2485 (though there I think we want to remove the message regardless of the units)
Oh I just realized that of course this is not a warning, it is a logger message
Good point -- it would be easy enough to change the logging level to DEBUG. I'm a little surprised the INFO messages are showing up in the logs. I assume in this use case logging is set to display INFO and more severe messages?
I know there is logger filtering but it has been a long, long time since I've used that.
Oh yeah... why is it even using the logger? Can we just change it into a warning?
I'm pretty sure I decided based on this: https://docs.python.org/3/howto/logging.html#when-to-use-logging
I'm fine with switching to warning if that is preferred.
Switching to warning is probably a breaking change and should not be backported?
Actually will switching to warning help or hurt the astroquery use case, @bsipocz ?
@astrofrog @pllim Hi! I'm interested in contributing to this issue, but after reviewing the discussion above, I see that there hasn't been a clear consensus on whether the logger message should be removed entirely or switched to a warning. Please let me know how you would like to proceed, and I'll be glad to contribute further.
@Mubin17 -- thanks for your interest!
I think the first step is to stop a message from being generated if the units are the same. If I recall correctly, and it has been a while so I may be wrong, this message is currently generated whenever an explicit unit is given and there is a unit in the FITS file, even when the units are the same.
When the units are the same, there should be no message generated at all.