査証.

astropy FITSデータがFITS規格に適合しているかどうかを検証する柔軟なシナリオを内蔵している.中の基本的な検証理念 astropy 入力に寛容でなければならないし,出力には厳しくしなければならない.

いつ? astropy FITS規格を満たしていないFITSファイルを読み込み,エラーを起こして退出することはない.それは最も教養のある解釈をしようとし、問題のあるデータがアクセスされて明確な解釈が達成できない場合にのみ、それは放棄されるだろう。

一方,FITSファイルを書き込み出力する場合,デフォルトで記入する内容はFITS基準を厳密に満たしていなければならない.このデフォルト動作は、他のいくつかのオプションによってカバーされることができ、したがって、ユーザは軽微な標準違反のために影響を受けない。

基準に合わせる

FITS基準は“緩い”基準であるため,多くの箇所で違反が発生する可能性があり,ほとんどすべて強制実行することは不可能である.主要な天文台にとって,100%適合しないデータ製品を生成することは珍しくない.いくつかの天文台も独自の非標準方言を開発し、その中のいくつかは非常に流行しており、それらはすでに事実上の標準になっている。例えば、長い文字列値やContinueカードの使用。

基準違反はデータ構造の異なるレベルで発生する可能性がある. astropy このようなレベルで開発された検証案です astropy 検証レベル:

  1. HDUリスト

  2. それぞれのHDU

  3. HDUコネクタの各カード

These three levels correspond to the three categories of objects: HDUList, any HDU (e.g., PrimaryHDU, ImageHDU, etc.), and Card. They are the only objects having the verify() method. Most other classes in astropy.io.fits do not have a verify() method.

もし verify() HDUリストレベルで呼び出されます。3つのレベルすべての標準適合性を検証しますが、呼び出します。 verify() このカードのコンプライアンスのみをチェックします。以来. astropy FITSファイルを読み込む際には寛容であり,No verify() 入力時に呼び出します。出力時には verify() 最も厳しいオプションをデフォルト値として呼び出します。

検証オプション

There are several options accepted by all verify(option) calls in astropy. In addition, they available for the output_verify argument of the following methods: close(), writeto(), and flush(). In these cases, they are passed to a verify() call within these methods. The available options are:

exception

This option will raise an exception if any FITS standard is violated. This is the default option for output (i.e., when writeto(), close(), or flush() is called). If a user wants to overwrite this default on output, the other options listed below can be used.

warn

このオプションは、無視オプションと同じであるが、警告メッセージが送信される。それは修復可能かどうかにかかわらず、FITS標準葛藤を修復しようとしないだろう。

ignore

このオプションはどんな協力基準の衝突も無視するだろう。出力時には,FITS規格を満たしているか否かにかかわらず,HDUリスト内容を出力FITSファイルに書き込む.

選択肢を無視することは以下の場合に有用である:

  1. 非標準フォーマットの入力FITSファイルを読み出し、ユーザは出力ファイルにコピーまたは書き込みを希望する。非標準フォーマットは出力ファイルに保持されます。

  2. ユーザが非標準FITSファイルを意図的に作成することは、一貫性をテストまたは維持するためである可能性がある。

警告メッセージは何も印刷されません。これは、暗黙警告オプション(以下参照)と同様である。

fix

このオプションはどんな協力基準の衝突も修復しようと努力するだろう。このような違反を修復することはいつも可能ではない。一般的に、二つの協力標準違反がある:修復可能で修復不可能だ。例えば、キーワードに浮動小数点数がある場合、その指数表現法が小文字の‘e’(例えば、1.23 e 11)であり、FITS規格が要求する大文字の‘E’ではなく、修復可能な違反である。一方,“P.I.”のようなキーワード名は許されないピリオドを何に置き換えるのか分からないので修復できません。違反が修復可能である場合、このオプションは、違反が修復されたことを示すメッセージを印刷する。もしそれが修復できなければ、それは異常を投げ出すだろう。

修復の背後にある原則は被害を与えないことだ。たとえば,キーワード名“P.I.”を用いて1枚のカードを“修復”することが可能であるようである.削除しますが astropy データの完全性を損なうためにそのような行動は取られないだろう。

すべての修復が“正しい”修復であるわけではありませんが少なくとも astropy 他のFITSリーダを捨てないように修復を試みる.

silentfix

FIXと同様であるが,メッセージを印刷することはできない.これは,ユーザがあまり無害なメッセージを望まない大きなスクリプトで有用である可能性がある.もし違反が修復できなければ、それはまだ異常を投げ出すだろう。

また現在までに astropy バージョン0.4.0は、以下の組み合わせオプションを提供します。

  • 修復+無視

  • 修復+警告

  • 修復+異常

  • サイレント修復+無視

  • サイレント修復+警告

  • サイレント修復+異常

これらのオプションは基本オプションの意味を結合している.例えば silentfix+exception 実際には silentfix 修復可能な誤りは暗黙的に修復されるが、修復不可能な誤りは異常を引き起こすからである。一方で、 silentfix+warn 修復できない誤りには警告するが,修復された誤りには沈黙する.

異なるデータ·オブジェクトレベルの検証

研究してみましょう astropy 検証は3つのレベルで行われています

HDUListでの検証

HDUリストレベルでは、2つの簡単な場合についてのみ検証を行う:

  1. HDUリストの最初のHDUが主HDUであるかどうかを検証します。これは解決できる問題だ。修復方法は,HDUリストに最小のマスタHDUを挿入する.

  2. HDUリストの2番目以上のHDUがプライマリHDUではないことを検証する。違反は修復できないだろう。

それぞれのHDUの検証

各HDUについて、必須キーワード、ヘッダ内のそれらの位置、およびそれらの値が検証される。各FITS HDUは、固定された所望のキーワードのセットを固定順序で有する。例えば、マスタHDUのヘッダは、少なくとも以下のキーワードを有する必要がある。

SIMPLE =                     T /
BITPIX =                     8 /
NAXIS  =                     0

いずれかがキーワードの紛失または順序誤りを記入しなければならない場合、修復オプションはそれらを修復します:

>>> from astropy.io import fits
>>> filename = fits.util.get_testdata_filepath('verify.fits')
>>> hdul = fits.open(filename)
>>> hdul[0].header
SIMPLE  =                    T / conforms to FITS standard
NAXIS   =                    0 / NUMBER OF AXES
BITPIX  =                    8 / BITS PER PIXEL
>>> hdul[0].verify('fix') 
VerifyWarning: Verification reported errors:
VerifyWarning: 'BITPIX' card at the wrong place (card 2).
  Fixed by moving it to the right place (card 1).
VerifyWarning: Note: astropy.io.fits uses zero-based indexing.
>>> hdul[0].header           # voila!
SIMPLE  =                    T / conforms to FITS standard
BITPIX  =                    8 / BITS PER PIXEL
NAXIS   =                    0 / NUMBER OF AXES
>>> hdul.close()

各カードの検証

最低レベルのカードもまた最も複雑な検証可能性を持っている.

実例.

以下は、修復可能カードと非修復可能カードのリストです。

修復可能カード:

  1. 小文字‘e’または‘d’の浮動小数点数::

    >>> from astropy.io import fits
    >>> c = fits.Card.fromstring('FIX1    = 2.1e23')
    >>> c.verify('silentfix')
    >>> print(c)
    FIX1    =               2.1E23
    
  2. 等号は、カード画像の第9列の前:

    >>> c = fits.Card.fromstring('FIX2= 2')
    >>> c.verify('silentfix')
    >>> print(c)
    FIX2    =                    2
    
  3. 引用符なしの文字列値::

    >>> c = fits.Card.fromstring('FIX3    = string value without quotes')
    >>> c.verify('silentfix')
    >>> print(c)
    FIX3    = 'string value without quotes'
    
  4. カード画像では9列目の前に等号が欠けています。

  5. 浮動小数点値の数字とEまたはDの間のスペース:

    >>> c = fits.Card.fromstring('FIX5    = 2.4 e 03')
    >>> c.verify('silentfix')
    >>> print(c)
    FIX5    =               2.4E03
    
  6. 解析できない値を“固定”を文字列にする:

    >>> c = fits.Card.fromstring('FIX6    = 2 10 ')
    >>> c.verify('fix+warn') 
    VerifyWarning: Verification reported errors:
    VerifyWarning: Card 'FIX6' is not FITS standard
     (invalid value string: '2 10').
       Fixed 'FIX6' card to meet the FITS standard.
    VerifyWarning: Note: astropy.io.fits uses zero-based indexing.
    >>> print(c)
    FIX6    = '2 10    '
    

修復できないカード:

  1. キーワード名には不正文字が含まれている.

検証プロセスを“ライフサイクル”の例でまとめます

>>> h = fits.PrimaryHDU()  # create a PrimaryHDU
>>> # Try to add an non-standard FITS keyword 'P.I.' (FITS does no allow
>>> # '.' in the keyword), if using the update() method - doesn't work!
>>> h.header['P.I.'] = 'Hubble' 
VerifyWarning: Keyword name 'P.I.' is greater than 8 characters or
 contains characters not allowed by the FITS standard;
  a HIERARCH card will be created.
>>> # Have to do it the hard way (so a user will not do this by accident)
>>> # First, create a card image and give verbatim card content (including
>>> # the proper spacing, but no need to add the trailing blanks)
>>> c = fits.Card.fromstring("P.I. = 'Hubble'")
>>> h.header.append(c)  # then append it to the header
>>> # Now if we try to write to a FITS file, the default output
>>> # verification will not take it.
>>> h.writeto('pi.fits')  
Traceback (most recent call last):
 ...
VerifyError: HDU 0:
    Card 5:
        Card 'P.I. ' is not FITS standard (equal sign not at column 8).
        Illegal keyword name 'P.I. '
>>> # Must set the output_verify argument to 'ignore', to force writing a
>>> # non-standard FITS file
>>> h.writeto('pi.fits', output_verify='ignore')
>>> # Now reading a non-standard FITS file
>>> # astropy.io.fits is magnanimous in reading non-standard FITS files
>>> hdul = fits.open('pi.fits')
>>> hdul[0].header 
SIMPLE  =            T / conforms to FITS standard
BITPIX  =            8 / array data type
NAXIS   =            0 / number of array dimensions
EXTEND  =            T
HIERARCH P.I. = 'Hubble  '
P.I.    = 'Hubble  '
VerifyWarning: Verification reported errors:
VerifyWarning: Card 'P.I. ' is not FITS standard (equal sign
 not at column 8).  Fixed 'P.I. ' card to meet the FITS standard.
VerifyWarning: Unfixable error: Illegal keyword name 'P.I. '
VerifyWarning: Note: astropy.io.fits uses zero-based indexing.
>>> # even when you try to access the offending keyword, it does NOT
>>> # complain
>>> hdul[0].header['p.i.']
'Hubble'
>>> # But if you want to make sure if there is anything wrong/non-standard,
>>> # use the verify() method
>>> hdul.verify() 
VerifyWarning: Verification reported errors:
VerifyWarning: HDU 0:
VerifyWarning:     Card 5:
VerifyWarning:         Illegal keyword name 'P.I. '
VerifyWarning: Note: astropy.io.fits uses zero-based indexing.
>>> hdul.close()

FITSチェックサムキーワード約束を用いて検証を行う

北米FITS委員会はFITS規格として採用可能なようにFITSチェックサムキーワード約束を審査している。このコミットメントはFITS HDUに含まれる情報の完全性チェックを行う.2つのタイトルキーカードからなることを約束した:CHECKSUMとDATASUM。CHECKSUMキーワードは、HDU中のすべての2880バイトが論理レコードに適合する32ビット1の補コードチェックサムが負のゼロに等しいことを強制するASCII文字列として定義される。DATASUMキーワードは,HDU中のデータレコードの32ビット1のパッチチェックサムを含む符号整数値のない文字列として定義される.累積されたチェックサムが負のゼロに等しいかどうかを検証することは、物理媒体上にファイルを複製または格納するときに、HDUが後続のデータ処理動作によって修正または破損されていないことを決定するためのかなり信頼できる方法を提供する。

性能に影響を与えないようにデフォルトでは astropy ファイルを開く際にHDUチェックサムを検証することもなく,ファイルへの書き込み時にチェックサム値を生成することもない.実際、ファイルがオープンされると、チェックサムカードおよびDATASUMカードはHDUヘッダから自動的に削除され、HDUがファイルに書き込まれると、任意のチェックサムカードまたはDATASUMカードがヘッダから剥離される。ファイルを開く際にHDUのチェックサム値を検証するためには,ユーザはOPEN利便性関数を呼び出す際にTrueの値を持つCHECKSUMキーワードパラメータを提供しなければならない.この動作が完了すると,どのチェックサム検証に失敗しても警告を出す(警告モジュールを通過する).オープン時にチェックサム検証が要求され、HDUヘッダにチェックサムまたはDATASUMカードが存在しない場合、ファイルがオープンされたときに何の注釈もない。同様に、ファイルの書き込み時にHDUヘッダのチェックサムおよびDATASUMカードを出力するためには、ユーザは、Trueの値を有するCHECKSUMキーワードパラメータを呼び出し中に提供しなければならない。 writeto() 機能します。CHECKSUMキーワードパラメータに値‘TATUM’を提供することにより、DATASUMカードのみをヘッダに書き込むことができる。

実例.

ファイルを開く際にHDUのチェックサム値を検証するには、以下の操作を実行してください。

>>> # Open the file checksum.fits verifying the checksum values for all HDUs
>>> filename = fits.util.get_testdata_filepath('checksum.fits')
>>> hdul = fits.open(filename, checksum=True)
>>> hdul.close()
>>> # Open the file in.fits where checksum verification fails
>>> filename = fits.util.get_testdata_filepath('checksum_false.fits')
>>> hdul = fits.open(filename, checksum=True) 
AstropyUserWarning: Checksum verification failed for HDU ('PRIMARY', 1).
AstropyUserWarning: Datasum verification failed for HDU ('PRIMARY', 1).
AstropyUserWarning: Checksum verification failed for HDU ('RATE', 1).
AstropyUserWarning: Datasum verification failed for HDU ('RATE', 1).
>>> # Create file out.fits containing an HDU constructed from data
>>> # containing both CHECKSUM and DATASUM cards.
>>> data = hdul[0].data
>>> fits.writeto('out.fits', data=data, checksum=True)
>>> hdun = fits.open('out.fits', checksum=True)
>>> hdun.close()

>>> # Create file out.fits containing all the HDUs in the HDULIST
>>> # hdul with each HDU header containing only the DATASUM card
>>> hdul.writeto('out2.fits', checksum='datasum')

>>> # Create file out.fits containing the HDU hdu with both CHECKSUM
>>> # and DATASUM cards in the header
>>> hdu = hdul[1]
>>> hdu.writeto('out3.fits', checksum=True)

>>> # Append a new HDU constructed from array data to the end of
>>> # the file existingfile.fits with only the appended HDU
>>> # containing both CHECKSUM and DATASUM cards.
>>> fits.append('out3.fits', data, checksum=True)
>>> hdul.close()