astropy icon indicating copy to clipboard operation
astropy copied to clipboard

Avoid logger info in CCDData.read when unit specifies matches that in FITS file

Open astrofrog opened this issue 3 years ago • 14 comments

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)

astrofrog avatar Aug 08 '22 10:08 astrofrog

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

pllim avatar Aug 08 '22 16:08 pllim

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?

astrofrog avatar Aug 08 '22 17:08 astrofrog

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.

pllim avatar Aug 08 '22 17:08 pllim

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.

mwcraig avatar Aug 08 '22 21:08 mwcraig

But if I provide a unit, I am already aware that I am overwriting whatever is in the file... 🤷

pllim avatar Aug 08 '22 22:08 pllim

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)

astrofrog avatar Aug 08 '22 23:08 astrofrog

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.

mwcraig avatar Aug 09 '22 13:08 mwcraig

Oh yeah... why is it even using the logger? Can we just change it into a warning?

pllim avatar Aug 09 '22 15:08 pllim

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.

mwcraig avatar Aug 09 '22 16:08 mwcraig

Switching to warning is probably a breaking change and should not be backported?

pllim avatar Aug 10 '22 19:08 pllim

Actually will switching to warning help or hurt the astroquery use case, @bsipocz ?

pllim avatar Aug 10 '22 19:08 pllim

@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 avatar May 16 '23 02:05 Mubin17

@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.

mwcraig avatar May 18 '23 18:05 mwcraig