[AstroPy] Re: HIERARCH

Joe Harrington jh at oobleck.astro.cornell.edu
Wed Mar 30 14:36:45 CST 2005


>It will not be initially as the underlying PyFITS code does not support 
>HIERARCH yet.

It's not hard to support extracting the values of individual HIERARCH
keywords, and it's a very valuable thing to do since there are now a
lot of data that use them.  I just implemented it for IDL's sxpar
using regular expressions in a couple of hours.  I've included a
patch, just for illustrative purposes, so you can see how I cased it
in the regex.  The full routine is here if you care:
http://oobleck.astro.cornell.edu/pub/sxpar.pro

Getting the whole hierarchy in some reasonable structure is more of a
stunt, and less useful.  People care about the individual values.

--jh--

*** sxpar.pro   2005-03-30 15:22:39.436418953 -0500
--- /home/local/share/idlastro-6.0/pro/fits/sxpar.pro   2002-03-12 00:27:25.000000000 -0500
***************
*** 115,121 ****
  ;       W. Landsman Dec 2001, Optional /SILENT keyword to suppress warnings
  ;       W. Landsman/D. Finkbeiner  Mar 2002  Make sure extracted vectors
  ;             of mixed data type are returned with the highest type.
- ;     J. Harrington 30 Mar 2005 Extended to read individual HIERARCH keys.
  ;-
  ;----------------------------------------------------------------------
   if N_params() LT 2 then begin
--- 115,120 ----
***************
*** 151,164 ****
              vector = 1                  ;Flag for vector output
              name_length = strlen(nam)   ;Length of name
              num_length = 8 - name_length        ;Max length of number portion 
!
!             if num_length LE 0 and strmid(nam, 0, 8) NE 'HIERARCH' then  $
                    message, 'Keyword length must be 8 characters or less'

! ;  Otherwise, DO NOT extend NAME with blanks to eight characters.

      endif else begin
!                 ;while strlen(nam) LT 8 do nam = nam + ' ' ;Make 8 chars long
                  vector = 0
      endelse

--- 150,162 ----
              vector = 1                  ;Flag for vector output
              name_length = strlen(nam)   ;Length of name
              num_length = 8 - name_length        ;Max length of number portion 
!             if num_length LE 0 then  $
                    message, 'Keyword length must be 8 characters or less'

! ;  Otherwise, extend NAME with blanks to eight characters.

      endif else begin
!                 while strlen(nam) LT 8 do nam = nam + ' ' ;Make 8 chars long
                  vector = 0
      endelse

***************
*** 167,173 ****
  ;  a number.  Store the positions of the located keywords in NFOUND, and the
  ;  value of the number field in NUMBER.

!         histnam = (nam eq 'HISTORY') or (nam eq 'COMMENT') or (nam eq '')
          if N_elements(start) EQ 0 then start = -1l
          start = long(start[0])
          if (not vector) and (start GE 0) then begin
--- 165,171 ----
  ;  a number.  Store the positions of the located keywords in NFOUND, and the
  ;  value of the number field in NUMBER.

!         histnam = (nam eq 'HISTORY ') or (nam eq 'COMMENT ') or (nam eq '')
          if N_elements(start) EQ 0 then start = -1l
          start = long(start[0])
          if (not vector) and (start GE 0) then begin
***************
*** 176,194 ****
              nheader = N_elements(hdr)
              mn = (start - precheck)  > 0
              mx = (start + postcheck) < nheader-1
!             keyword = stregex(hdr[mn:mx], /extract, /subexpr, $
! '^(........)=|^(HIERARCH[^=]*)|^(COMMENT )|^(HISTORY )|(^        )|(^END     )')
!             keyword = keyword[1:*, *]
!             keyword = keyword[where(keyword ne '')]
          endif else begin
              restart:
              start   = -1l
!             keyword = stregex(hdr, /extract, /subexpr, $
! '^(........)=|^(HIERARCH[^=]*)|^(COMMENT )|^(HISTORY )|(^        )|(^END     )')
!             keyword = keyword[1:*, *]
!             keyword = keyword[where(keyword ne '')]
          endelse
-         keyword = strtrim(keyword)

          if vector then begin
              nfound = where(strpos(keyword,nam) GE 0, matches)
--- 174,185 ----
              nheader = N_elements(hdr)
              mn = (start - precheck)  > 0
              mx = (start + postcheck) < nheader-1
!             keyword = strmid(hdr[mn:mx], 0, 8)
          endif else begin
              restart:
              start   = -1l
!             keyword = strmid( hdr, 0, 8)
          endelse

          if vector then begin
              nfound = where(strpos(keyword,nam) GE 0, matches)
***************
*** 224,230 ****

   if matches GT 0 then begin
    line = hdr[nfound]
!   svalue = strtrim((stregex(line, '[^=]*=(.*)', /extract, /subexpr))[1, *], 2)    if histnam then $
          value = strtrim(strmid(line,8,71),2) else for i = 0,matches-1 do begin        if ( strmid(svalue[i],0,1) EQ "'" ) then begin   ;Is it a string?
--- 215,221 ----

   if matches GT 0 then begin
    line = hdr[nfound]
!   svalue = strtrim( strmid(line,9,71),2)
    if histnam then $
          value = strtrim(strmid(line,8,71),2) else for i = 0,matches-1 do begin        if ( strmid(svalue[i],0,1) EQ "'" ) then begin   ;Is it a string?




More information about the AstroPy mailing list