1

I have some data given to me by my mentor. The data consists of thousands of .fits files. Some of the .fits files are older versions of the others and the way the data tables are constructed are different. Here is what I mean:

Let's say I have two .fits files: FITS1.fits and FITS2.fits

$ python
>>> import pyfits
>>> a = pyfits.getdata('FITS1.fits')
>>> b = pyfits.getdata('FITS2.fits')
>>> a.names
['time', 'timeerr', 'sap_flux', 'sap_flux_err']
>>> b.names
['time', 'sap_flux', 'timeerr', 'sap_flux_err']

Does anyone know of a way that I can switch around the columns in the data tables? so that FITS2.fits's format is similar to FITS1.fits ?

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Dax Feliz
  • 12,220
  • 8
  • 30
  • 33
  • Not being familiar with FITS and pyfits, I have two notes: 1) if pyfits allows _creation_ of FITS tables, then it should be feasible; 2) if you only need to process the data, then you could once read it (play with try-except or something) and keep the data in an unordered structure such as dict. – Lev Levitsky Jun 20 '12 at 18:21

2 Answers2

1

Your best bet is to not use pyfits directly, but to use the newer, shinier Astropy Table interface. You can read in a FITS table like:

from astropy.table import Table
table = Table.read('FITS1.fits')

As demonstrated in the section on modifying tables, you can then reorder the columns like:

table = table[['time', 'timeerr', 'sap_flux', 'sap_flux_err']]

(technically this creates a new copy of the table, with the columns selected in the order you wanted them to be in; however IIRC this does not copy the underlying column arrays and should still be a fast operation).

It is also perfectly possible to do this with the legacy pyfits interface, but I wouldn't recommend it for most cases.

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
-1

You shouldn't write code which depends on the order of keys in a dictionary - a dictionary is a hash table and the order that they are stored is essentialy arbitrary. If you need to match or compare the entries you should get a list of the keys and sort them.

It's probable that pyfits builds a dictionary in the order that the keys are stored in the FITS header, but that isn't necessarily true.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
  • Can you tell me or point me to a resource of how to get a list of and sort the keys? I'm new to programming and I'm not familiar with useful websites for python. – Dax Feliz Jun 25 '12 at 18:50
  • @DaxFeliz - it doesn't look like pyfits returns a standard python dictionary of name:value pairs. you will have to look at what the pyfits lib suggests – Martin Beckett Jun 25 '12 at 19:02
  • No, this has nothing to do with dictionaries. – Iguananaut Jan 27 '15 at 16:28
  • @Iguananaut the user wants to reorder the table entries. Presumably because they want to hard code which fields appear in which order - this is likely to prove a bad idea later. – Martin Beckett Jan 27 '15 at 21:12
  • Not necessarily. FITS as a data format for astronomy data, and specific observatories may have requirements for what order data columns appear in (maybe not with good reasons, but also maybe...) but regardless if a table is not in the correct format it is desirable to be able to reformat the table as needed. – Iguananaut Jan 27 '15 at 23:36