ó
šßÈ[c           @   sÝ   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z e j d ƒ Z	 e j
 d d d ƒe j
 d d d	 ƒe j
 d
 d d ƒd „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s¶  
``fitscheck`` is a command line script based on astropy.io.fits for verifying
and updating the CHECKSUM and DATASUM keywords of .fits files.  ``fitscheck``
can also detect and often fix other FITS standards violations.  ``fitscheck``
facilitates re-writing the non-standard checksums originally generated by
astropy.io.fits with standard checksums which will interoperate with CFITSIO.

``fitscheck`` will refuse to write new checksums if the checksum keywords are
missing or their values are bad.  Use ``--force`` to write new checksums
regardless of whether or not they currently exist or pass.  Use
``--ignore-missing`` to tolerate missing checksum keywords without comment.

Example uses of fitscheck:

1. Verify and update checksums, tolerating non-standard checksums, updating to
   standard checksum::

    $ fitscheck --checksum either --write *.fits

2. Write new checksums,  even if existing checksums are bad or missing::

    $ fitscheck --write --force *.fits

3. Verify standard checksums and FITS compliance without changing the files::

    $ fitscheck --compliance *.fits

4. Verify original nonstandard checksums only::

    $ fitscheck --checksum nonstandard *.fits

5. Only check and fix compliance problems,  ignoring checksums::

    $ fitscheck --checksum none --compliance --write *.fits

6. Verify standard interoperable checksums::

    $ fitscheck *.fits

7. Delete checksum keywords::

    $ fitscheck --checksum none --write *.fits
iÿÿÿÿNi   (   t   fitst	   fitscheckt   errort   messages   Checksum verification faileds   Datasum verification failedt   ignores   Overwriting existing filec         C   su  t  |  ƒ s d g }  n  t j d t j d j ƒ  ƒ ƒ } | j d d d d d d	 d
 d d d d g d d d d d d ƒ| j d d d d d d d t d d ƒ| j d d d d d d d t d d ƒ| j d d d d  d d! d t d d ƒ| j d" d# d d$ d d% d t d d ƒ| j d& d' d d( d d) d t d d ƒ| j |  ƒ \ a	 } t	 j
 d k rqt t	 _
 n  | S(*   Ns   -ht   usages  
        fitscheck [options] <.fits files...>

        .e.g. fitscheck example.fits

        Verifies and optionally re-writes the CHECKSUM and DATASUM keywords
        for a .fits file.
        Optionally detects and fixes FITS standard compliance problems.
        s   -ks
   --checksumt   destt   checksum_kindt   typet   choicet   choicest   standardt   nonstandardt   eithert   nonet   helps6   Choose FITS checksum mode or none.  Defaults standard.t   defaultt   metavars(   [standard | nonstandard | either | none]s   -ws   --writet
   write_files6   Write out file checksums and/or FITS compliance fixes.t   actiont
   store_trues   -fs   --forcet   forces1   Do file update even if original checksum was bad.s   -cs   --compliancet
   compliances-   Do FITS compliance checking; fix if possible.s   -is   --ignore-missingt   ignore_missings   Ignore missing checksums.s   -vs	   --verboset   verboses   Generate extra output.(   t   lent   optparset   OptionParsert   textwrapt   dedentt   stript
   add_optiont   Falset
   parse_argst   OPTIONSR   (   t   argst   parsert
   fits_files(    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   handle_options@   s@    c          C   s_   t  j r t j t j ƒ n t j t j ƒ t j ƒ  }  |  j t j	 d ƒ ƒ t j
 |  ƒ d  S(   Ns   %(message)s(   R"   R   t   logt   setLevelt   loggingt   INFOt   WARNINGt   StreamHandlert   setFormattert	   Formattert
   addHandler(   t   handler(    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   setup_loggingu   s    	c         C   s  d } y t  j |  d t j ƒ} WnY t k
 r} } d d j t | ƒ j d ƒ d ƒ j ƒ  } t	 j
 d j |  | ƒ ƒ d SXt j só xi t | ƒ D]X \ } } | j sÆ t	 j
 d j |  | ƒ ƒ d S| j s” t	 j
 d j |  | ƒ ƒ d Sq” Wn  | st	 j d	 j |  ƒ ƒ n  | S(
   sR   
    Prints a message if any HDU in `filename` has a bad checksum or datasum.
    i    t   checksums   .. t    i   s   BAD {!r} {}s-   MISSING {!r} .. Checksum not found in HDU #{}s,   MISSING {!r} .. Datasum not found in HDU #{}s   OK {!r}(   R    t   openR"   R   t   UserWarningt   joint   strt   splitR   R'   t   warningt   formatR   t	   enumeratet	   _checksumt   _datasumt   info(   t   filenamet   errorst   hdulistt   wt	   remaindert   it   hdu(    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   verify_checksums€   s(    ,			c         C   si   t  j |  ƒ } y | j d ƒ WnB t  j k
 rd } t j d j |  ƒ t | ƒ j d d ƒ ƒ d SXd S(   s#   Check for FITS standard compliance.t	   exceptions   NONCOMPLIANT {!r} .. {}s   
R3   i   i    (	   R    R4   t   verifyt   VerifyErrorR'   R9   R:   R7   t   replace(   R?   RA   t   exc(    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   verify_complianceœ   s    c         C   s}   t  j |  d t ƒ} zV y; t j r* d n d } | j |  d t j d t d | ƒWn t  j k
 ri n XWd | j ƒ  Xd S(   sœ   
    Sets the ``CHECKSUM`` and ``DATASUM`` keywords for each HDU of `filename`.

    Also updates fixes standards violations if possible and requested.
    t   do_not_scale_image_datat	   silentfixR   R2   t	   overwritet   output_verifyN(	   R    R4   t   TrueR"   R   t   writetoR   RI   t   close(   R?   RA   RP   (    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   update©   s     c         C   s•   ya t  |  ƒ } t j r' t |  ƒ } n d } t j rB | d k sK t j rX t |  ƒ n  | | SWn- t k
 r } t j	 d j
 |  | ƒ ƒ d SXd S(   sa   
    Handle a single .fits file,  returning the count of checksum and compliance
    errors.
    i    s   EXCEPTION {!r} .. {}i   N(   RF   R"   R   RL   R   R   RT   t	   ExceptionR'   R   R:   (   R?   t   checksum_errorst   compliance_errorst   e(    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   process_file»   s    	c          C   sp   d }  t  t j d ƒ } t ƒ  x | D] } |  t | ƒ 7}  q' W|  r` t j d j |  ƒ ƒ n  t t	 |  ƒ ƒ S(   sš   
    Processes command line parameters into options and files,  then checks
    or update FITS DATASUM and CHECKSUM keywords for the specified files.
    i    i   s	   {} errors(
   R&   t   syst   argvR1   RY   R'   R9   R:   t   intt   bool(   R@   R%   R?   (    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   mainÏ   s    (   t   __doc__R)   R   RZ   R   t   warningst    R    t	   getLoggerR'   t   filterwarningsR&   R1   RF   RL   RT   RY   R^   (    (    (    s@   lib/python2.7/site-packages/astropy/io/fits/scripts/fitscheck.pyt   <module>,   s"   	5					