Skip to content

astropy.wcs: presence of keywords like "PV1_5" causes error #299

Closed

Description

@kbarbary

I have a FITS file with an SIP transformation, and also, a series of keywords of the form PV?_? (PV1_1, PV1_2, etc). The WCS constructor seems to think these are coordinate transformation parameters. Perhaps it is searching for the SIP coefficients by looking for anything of the form ???_? ?

To reproduce, grab
http://supernova.lbl.gov/~kbarbary/tmp/PTF_200906253621_i_p_mask_t084123_u012052003_f02_p002899_c07.fits (17MB) (This is the same example file I used in #257).

>>> from astropy.io import fits
>>> from astropy import wcs
>>> hdulist = fits.open('PTF_200906253621_i_p_mask_t084123_u012052003_f02_p002899_c07.fits')
>>> w = wcs.WCS(hdulist[0].header)
WARNING: FITSFixedWarning: 'celfix' made the change 'PV1_5 : Unrecognized coordinate transformation parameter'. This FITS header contains non-standard content. [astropy.wcs.wcs]
>>> w.wcs.print_contents()
ERROR: InvalidTransformError: ERROR 6 in wcsset() at line 1561 of file astropy/wcs/src/wcslib/C/wcs.c:
PV1_5 : Unrecognized coordinate transformation parameter.
 [unknown]

... or perhaps there is something I don't know about these PV?_? keywords. But it looks like they are SCAMP-specific and not intended to interact with wcs or SIP transformations.

Activity

  1. mdboom commented on Jun 21, 2012

    @mdboom
    Contributor

    PVi_ma keywords are used to store extra parameters for certain projection types. Their meaning is specific to the projection being used. For example, there is this note in the wcslib docs:

           Many projections use pv[1] (PVi_1a), some also use
    *     pv[2] (PVi_2a) and SZP uses pv[3] (PVi_3a).  ZPN is currently the only
    *     projection that uses any of the others.
    

    What wcslib is doing here is seeing a greater value for m than what it expects for any of the projections it knows about. It's not clear to me whether it should be raising an error here, or it would make more sense to just silently ignore these keywords. On the other hand, the CTYPE in the wcs is TAN, which has no PV parameters, so it's not necessarily wrong that it should complain that there are PV parameters when none would be expected.

  2. mdboom commented on Jun 21, 2012

    @mdboom
    Contributor

    Also -- do you know what these extra parameters are used for?

  3. kbarbary commented on Jun 21, 2012

    @kbarbary
    MemberAuthor

    Thanks for the explanation. I believe that these are keywords written and used by SCAMP and SWARP. From the SCAMP manual (available at http://www.astromatic.net/software/scamp ; bottom of page 4):

    The astrometric engine at the heart of SCAMP and SWarp is based on M. Calabretta’s WCSlib
    library, to which we added the handling of polynomial distortion parameters (FITS keywords
    PV_xx_xx) as proposed in one of the earlier WCS documents.

    This header contains PV_1_0 through PV_1_16 and PV_2_0 through PV_2_16, in a section commented as "SCAMP distortion keywords". I interpreted this as a representation of distortion alternative to the SIP coefficients, mainly for the sole purpose of being read by SWARP.

    To be honest I'm not familiar with how the PV parameters are used in the FITS standard. Perhaps this use is in conflict with the standard?

    By the way, this file is a data product from IPAC (for the PTF survey). I'm quite surprised they are creating headers that can't be parsed by wcslib!

  4. mdboom commented on Jun 21, 2012

    @mdboom
    Contributor

    This is getting curiouser. The SCAMP documentation refers to PV_xx_xx keywords (note the underscore after the PV), which would not conflict with WCS keywords, but of course also aren't defined in the "earlier WCS document" that it links to. Perhaps that's just a typo in the SCAMP documentation. The file you link to has PVi_ma keywords, which, of course, do conflict with WCS keywords. As far as I can tell, their use is in conflict with the WCS standard, since the projection type is declared as TAN, but the tangent projection should have no PVi_ma keywords.

    Also telling in the SCAMP documentation is that they are using a very old copy of wcslib from 1999 that has then been patched to handle the PV keywords in a special way.

    In WCS Paper I, section 2.1.4, the WCS standard says that PVi_ma values are for extra parameters needed by certain projections. In WCS Paper II, where the projection types are actually defined, it says what they mean, and their meaning is dependent on the specific projection being used. The spec doesn't really say what should happen in the case of extra keywords. there's an argument to be made here that wcslib should be raising an error for all PVi_ma keywords in this case (since the projection is declared as TAN), but at present wcslib always stores the first four, which is the most it would ever need for any of its builtin projections.

    I went back through the history of wcslib to sanity check that this isn't simply a new bug introduced when the better error handling was put in. Indeed, this behavior (to not accept PVi_ma keywords where m > 4 goes back to at least wcslib 4.7).

    Also, as an experiment, it's quite simple to remove the error check and just blindly continue when encountering one of the extra PV keywords. Unfortunately, you run into problems later because, even though it doesn't need them for this particular projection, wcslib (in celset) validates the PVi_ma keywords numerically anyway, and that fails.

    But this is all somewhat naive conjecture on my part, having read the specs, but not written them and being somewhat late to the WCS party. I fired off an e-mail to Mark Calabretta to look at this issue and provide feedback if able.

  5. kbarbary commented on Jun 21, 2012

    @kbarbary
    MemberAuthor

    Yes, you're right, the SCAMP manual says PV_xx_xx but in fact, it uses PVx_xx. I would guess that parts of the manual are probably way out of date. Here is an example file that is the direct output of SWARP: http://supernova.lbl.gov/~kbarbary/tmp/PTF201105082378_1_o_48428_09.w.head (text, 4.4k). Indeed the keywords are PVx_xx.

    I suppose this comes down to the fact that in SCAMP and SWARP, Emmanuel Bertin has used a modified version of wcslib so that these keywords work, with the result being that they don't work with the standard version. This is really a shame - it seems that no file containing SCAMP keywords can be read with the standard wcslib! I suppose we could email Emmanuel to check that this is indeed the case.

  6. ghost assigned on Jun 27, 2012
  7. mdboom commented on Jul 3, 2012

    @mdboom
    Contributor

    From Mark Calabretta:

    The FITS header is indeed in error and wcsset() correctly rejects it.
    
    The WCS standard defines a fixed interpretation, independent of
    projection type, for the first five PVi_ma keywords attached to the
    *longitude* axis.  That's axis 1 here, so PV1_0, PV1_1, PV1_2, PV1_3,
    and PV1_4 have predefined meanings as stipulated in Sects. 2.5 and
    2.6 of WCS Paper II.  In addition, no more than these five are
    recognised by WCSLIB, thus prompting the "Unrecognized coordinate
    transformation parameter" error message.
    
    SIP gets around this simply by using its own keywords for the
    distortion parameters.  SCAMP should do likewise.
    
  8. kbarbary commented on Jul 3, 2012

    @kbarbary
    MemberAuthor

    Thanks for checking into this @mdboom. Hopefully this discussion will serve as guidance for others that run into this problem when using SCAMP in the future.

  9. lpsinger commented on Jan 29, 2013

    @lpsinger
    Contributor

    I am encountering the same error messages trying to open PTF images using astropy and APLpy. How was this resolved? Do I need to manually chop out the SCAMP part of the FITS headers before reading them in?

  10. kbarbary commented on Jan 29, 2013

    @kbarbary
    MemberAuthor

    Personally I gave up on trying to read these images with astropy. But if you find a workaround, please do post it here.

  11. eteq commented on Jan 29, 2013

    @eteq
    Member

    @mdboom - is this something that might be worth somehow working around? Seeing as how SCAMP is a pretty commonly-used utility, this seems like it would be a nice thing to make work, even if it isn't quite up-to-spec...

  12. mdboom commented on Jan 29, 2013

    @mdboom
    Contributor

    It's a tough one -- SCAMP appears to be reusing the PVi_ma keywords for a purpose completely different from how they were originally intended. Does anyone know where to find the documentation about these keywords in SCAMP? The SCAMP documentation refers briefly to PV_xxx_xxx keywords (which are different and thus should work fine), but the code uses PVi_m.

    I looked at what wcstools is doing (understanding we can't use it directly, because it's LGPL) -- it does have explicit SCAMP support, but it's a mess. It loads the SCAMP distortion coefficients, but only when the CTYPE is TPV, but all of the SCAMP files I've seen in the wild have a CTYPE of TAN, so wcstools is ignoring whatever distortion parameters might be in these files. Personally, I think the wcslib behavior is better here because at least it isn't silently ignoring something in the file.

    I think we can work to try to support this, but in lieu of a clear standard, it will probably require some coordination with the SCAMP folks. At the very minimum, we need some sort of mathematical definition of what these keywords are storing, which may give us a running start at converting them to SIP.

  13. eteq commented on Jan 29, 2013

    @eteq
    Member

    If it's not in the scamp manual (https://www.astromatic.net/pubsvn/software/scamp/trunk/doc/scamp.pdf) you might glance at the manual for swarp (https://www.astromatic.net/pubsvn/software/swarp/trunk/doc/swarp.pdf) - my understanding is that SCAMP was basically written to do intermediate work for SWARP.

    If the info isn't there, I think your best bet is to contact Emmanuel Bertin - I don't know how responsive he is, but for some of the astromatic utilities (at least, that I've worked with) there simply isn't documentation on intermediate steps.

  14. stargaser commented on Jan 30, 2013

    @stargaser
    Contributor

    Hi all,

    I'm afraid I have some culpability here. PTF asked me to convert PV to SIP and vice-versa, then decided they wanted both in their headers. We wrote an SPIE paper on the converter software for the conference in Amsterdam last June. Someone (@lpsinger) reminded me that it is posted here: http://web.ipac.caltech.edu/staff/fmasci/home/astro_refs/SIP_To_PV_2012.pdf

    I meant to find out if SPIE allows me to post it to astro-ph...then lost my nerve thinking maybe it is too provocative (the part about TPV being superfluous).

    Anyway, to be clear, PTF decided they wanted both SIP and PV keywords in their headers so that's what drove this.

    David

  15. mdboom commented on Jan 30, 2013

    @mdboom
    Contributor

    @stargaser: Thanks for the reference -- I think from that we should be able to move forward on this. I think for astropy we can probably get away with adding a relax mode that converts these non-standard PV keywords into SIP on the fly and then uses that. Whether we want to write non-standard PV back out, I think, is less important than just getting some way to read these extant files. I'm going to go ahead and reopen the issue for this.

  16. reopened this on Jan 30, 2013
  17. ijiraq commented on Apr 14, 2013

    @ijiraq

    New to this thread.

    I am trying to make use of the astropy packages for a CFHT MEGACAM images. High precision WCS information in those images is created using a keyword mapping that matches swarp. They raise the PV1_5 error, due to swarps non-standard keywords, but we can't blame swarp entirely as the standard appears to have been flexible at the time of authorship.

    Perhaps, when projection is 'TAN' and there are PV?_? values in the header the wcs could apply the swarp usage?

    The keyword definitions appear in the CFHT MEGACAM headers:

    http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/data/pub/CFHTSG/821543p.head

    The use of the keywords is documented in the MegaPipe paper (Gwyn 2008)

    """The higher order part of the WCS has no standard representation as of yet but the most common is the PVn_n keywords of M. R. Calabretta et al. (2004, private communication2)."""

    I know that Stephen Gwyn wrote an appendix for the paper that describes how to use the PV?_? keywords but that section was removed at the request of the reviewer.

  18. embray commented on Apr 15, 2013

    @embray
    Member

    I'm starting to think there needs to just be a "special case" registry for this.

    wcs.pva_b_exception_registry.append(lambda header: header['ORIGIN'] == 'CFHT' and header['DETECTOR'] == 'MegaCam')

    (half tongue-in-cheek?)

  19. ijiraq commented on Apr 15, 2013

    @ijiraq

    The documentation of how to use the PV keywords is well describe in Appendix A of Gwyn 2008, which is available from his web page:

    http://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/megapipe/docs/CD_PV_keywords.pdf

    Provided in addition to information linked to paper from @stargaser.

    Since this issue was reopened 3 months ago with the decision to allow this as a relax mode, is there a patch that might be available for this issue?

  20. vterron commented on Jun 13, 2013

    @vterron

    I've just come across this explanation by Emmanuel Bertin in the Astromatic forums:

    The reason for these apparent inconsistencies is that both SCAMP and SWarp distortion descriptions are based on an old draft version of the WCS proposal (circa 2000). At the time I started working on this I had to choose a standard, and it was the most promising at the time. Unfortunately, since then, the WCS proposal has evolved, but for operational reasons SWarp and SCAMP sticked to the original description. The most recent "official" draft concerning distortions is dated 2004/04/22, and differs significantly from SCAMP and SWarp's implementation. Given the amount of work it will require, my plans are to offer compatibility with the official WCS proposal once it is definitely endorsed.

    He also discusses some details of the implementation, in case they can be of any help.

  21. lpsinger commented on Jul 23, 2013

    @lpsinger
    Contributor

    FYI, I now have a patch ready; I'm just checking with the PTF project to find out which image or header thereof could be released as a test case.

  22. added a commit that references this issue on Jul 23, 2013
    11f3647
  23. added a commit that references this issue on Oct 17, 2013
    2325a09
  24. eteq commented on Oct 18, 2013

    @eteq
    Member

    @lpsinger - if we merge #1278, then this can be closed, right?

  25. lpsinger commented on Oct 18, 2013

    @lpsinger
    Contributor

    @lpsinger - if we merge #1278, then this can be closed, right?

    Yes.

  26. pkgw commented on Oct 18, 2021

    @pkgw

    A little note on the exact 8th anniversary of the last comment on this issue (!): Astropy added support for TPV a while ago, but I've just discovered that you'll still get these wcslib complaints about an unrecognized PV1_5 parameter if your header specifies a TAN projection type (or presumably anything other than TPV). If that's the case, you need to change RA---TAN to RA---TPV, etc.

    (Discovered with a FITS file from DASCH.)

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions