[AstroPy] Parse DETSEC etc..

Peter Erwin erwin at mpe.mpg.de
Thu Jul 5 03:41:12 EDT 2018


And, of course, the list comprehension line should look something more like this:

b = [int(q) for p in h[1:-1].split(',') for q in p.split(':’)]

(that’ll teach me to change the intermediate variable names halfway through
writing the post…)

   — Peter

> On Jul 5, 2018, at 8:51 AM, Peter Erwin <erwin at mpe.mpg.de> wrote:
> 
> If you know the names of the headers to parse and you know they will always
> have the format “[x1:x2,y1:y2]”, then a slightly shorter (and possibly more
> idiomatic) way, which avoids regex and mapping, would be:
> 
> ff=fits.open('file.fits')
> h = ff[1].header[‘DETSEC’]
> b = [int(x2) for x1 in h[1:-1].split(",") for x2 in a.split(":”)]
> 
> (Substitute “float” for “int” if you really want floating-point values; I’m
> assuming that you might want to use the values from DETSEC to index
> the data array, in which case they should be integers.)
> 
> But that may not be as flexible as what you want.
> 
> 
> cheers,
> 
> Peter
> 
>> On Jul 5, 2018, at 3:21 AM, Petr Kubánek <petr at kubanek.net> wrote:
>> 
>> Hi Astropy,
>> 
>> I might be stupid, but I don't see any way how to parse DETSEC and
>> similar FITS header you can encounter in (multiframe) FITS files. I of
>> course know how to parse that with e.g. re and mapping result to float
>> array, but is there a better way? See for the code (yes, that will
>> become a function..unless I know about better way):
>> 
>> from astropy.io import fits
>> import re
>> 
>> ff=fits.open('file.fits')
>> r = re.compile('\[(\d+):(\d+),(\d+):(\d+)\]')
>> g = r.match(ff[1].header['DETSEC'])  # DETSEC is '[20:30,40:50]'
>> b=map(float, g.groups())
>> print(b)
>> [20,30,40,50]
>> 
>> 
>> Thanks
>> 
>> Petr Kubánek
>> http://rts2.org
>> _______________________________________________
>> AstroPy mailing list
>> AstroPy at python.org
>> https://mail.python.org/mailman/listinfo/astropy
> 
> =============================================================
> Peter Erwin                   Max-Planck-Insitute for Extraterrestrial 
> erwin at mpe.mpg.de              Physics, Giessenbachstrasse
> tel. +49 (0)176 2481 7713     85748 Garching, Germany
> fax  +49 (0)89 30000 3495     http://www.mpe.mpg.de/~erwin
> 
> 
> 
> _______________________________________________
> AstroPy mailing list
> AstroPy at python.org
> https://mail.python.org/mailman/listinfo/astropy

=============================================================
Peter Erwin                   Max-Planck-Insitute for Extraterrestrial 
erwin at mpe.mpg.de              Physics, Giessenbachstrasse
tel. +49 (0)176 2481 7713     85748 Garching, Germany
fax  +49 (0)89 30000 3495     http://www.mpe.mpg.de/~erwin





More information about the AstroPy mailing list