Converting molly files to sets of fits files

The molly file format is very useful and sensible, but as molly is not widespread it is not a format which many people will be able to read. In astronomy the standard format is FITS (Flexible Image Transport System), which is getting old but is by far the most common data interchange format. The FITS standard is maintained by the NASA FITS support office. So it is useful to have a way of converting molly files to FITS files.

If you want a quick and simple conversion then the molly command wfits will write out a set of spectra from within molly. However, the headers of these files are of very limited use and are insufficient in many cases for peoples' needs.

I have therefore written a shell script which uses molly and IDL to create more useful FITS files. The spectra are written as the image, and specific header keywords are extracted one by one to put in the output FITS files. I have successfully used the resulting files to deposit data with the CDS catalogue service, which are quite correctly picky and accept only standard-format FITS files.

To run this shell script, mol2fit you will need to have IDL and the IDL astronomy user's library available on your computer system.

mol2fit

Copy the shell script below into a text file and name this mol2fit. You can run it from the same directory as the molly files are, or put it in your PATH so it appears as a normal command on your system. To run the script enter on the command line:

mol2fit   [file.mol]

or

mol2fit   [file.mol]   verbose

The first option uses molly and IDL to create a set of FITS files from one molly file. The second option does the same but outputs information about the process and leaves behind all the temporary file it creates. Use the second option if there are problems, as it will be easier to work out what's gone wrong.

If you want to investigate the result, the easiest way is probably to use the fv program, which is a part of the NASA/GSFC/HEASARC ftools package but can also be obtained as a stand-alone version.

#!/bin/csh
            # John Taylor 25/02/2007
            # Script to convert molly spectrum files to FITS using IDL and the ASTROLIB library
            # Written to provide basic but usable fits files and headers.

if ( $#argv < 1 ) then
  echo " "
  echo "MOL2FIT                                   John Taylor  18/02/2007"
  echo "This code uses MOLLY and IDL to create fits files from a MOLLY file"
  echo " "
  echo "Usage:   mol2fit   [file.mol]   verbose"
  echo " "
  echo " where verbose is an optional argument to output extra information"
  echo " "
  exit
endif

#==================================================================================================

set mollyfile = $argv[1]

molly << EOF >& tmp.molly
load $mollyfile 1 2000 1
quit
y
EOF

if ( $#argv == 2 ) then
  echo " "
  cat tmp.molly
  echo " "
endif

set filein = `grep $mollyfile tmp.molly`
set nfiles = $filein[2]

echo "Found $nfiles spectra in the molly file"

#==================================================================================================

set nfile = 0

while ( $nfile < $nfiles )
  @ nfile = $nfile + 1
  set outfile = $mollyfile:r"_"$nfile.fit

molly << EOF >& tmp.molly
load $mollyfile 1 1 $nfile
flis 1 1 tmp.object 1 object
flis 1 1 tmp.ra 1 ra
flis 1 1 tmp.dec 1 dec
flis 1 1 tmp.equinox 1 equinox
flis 1 1 tmp.hjd 1 hjd
flis 1 1 tmp.vearth 1 vearth
flis 1 1 tmp.airmass 1 airmass
flis 1 1 tmp.day 1 day
flis 1 1 tmp.month 1 month
flis 1 1 tmp.year 1 year
flis 1 1 tmp.utc 1 utc
flis 1 1 tmp.texp 1 dwell
wasc tmp.dat 1 a m
quit
y
EOF

  if ( $#argv == 2 ) then
    echo " "
    cat tmp.molly
    echo " "
  endif

idl << EOF >& tmp.idl
readfloat, 'tmp.dat', WAVE, FLUX, FLUXE, /double
IMAGE = dblarr(3,n_elements(WAVE))
IMAGE[0,*] = WAVE
IMAGE[1,*] = FLUX
IMAGE[2,*] = FLUXE
mkhdr, HEAD, IMAGE
readcol,'tmp.year',SLOT,IN,form='i,a'   & IN=IN[0] & sxaddpar,HEAD,'YEAR',   IN,' Year this spectrum was obtained'
readcol,'tmp.month',SLOT,IN,form='i,a'  & IN=IN[0] & sxaddpar,HEAD,'MONTH',  IN,' Month this spectrum was obtained'
readcol,'tmp.day',SLOT,IN,form='i,a'    & IN=IN[0] & sxaddpar,HEAD,'DAY',    IN,' Day this spectrum was obtained'
readcol,'tmp.utc',SLOT,IN,form='i,a'    & IN=IN[0] & sxaddpar,HEAD,'UTC',IN,' Universal time of exposure midpoint'
readcol,'tmp.object',SLOT,IN,form='i,a' & IN=IN[0] & sxaddpar,HEAD,'TARGET', IN,' Name of target'
readcol,'tmp.ra',SLOT,IN,form='i,a'     & IN=IN[0] & sxaddpar,HEAD,'RA',     IN,' Right Ascension (hours)'
readcol,'tmp.dec',SLOT,IN,form='i,a'    & IN=IN[0] & sxaddpar,HEAD,'DEC',    IN,' Declination (degrees)'
readcol,'tmp.equinox',SLOT,IN,form='i,a'& IN=IN[0] & sxaddpar,HEAD,'EQUINOX',IN,' RA/Dec equinox'
readcol,'tmp.hjd',SLOT,IN,form='i,a'    & IN=IN[0] & sxaddpar,HEAD,'HJD',    IN,' HJD of exposure midpoint'
readcol,'tmp.vearth',SLOT,IN,form='i,a' & IN=IN[0] & sxaddpar,HEAD,'VEARTH', IN,' Velocity of Earth (km/s)'
readcol,'tmp.airmass',SLOT,IN,form='i,a'& IN=IN[0] & sxaddpar,HEAD,'AIRMASS',IN,' Airmass at observation midpoint'
readcol,'tmp.texp',SLOT,IN,form='i,a'   & IN=IN[0] & sxaddpar,HEAD,'TEXP',   IN,' Exposure time (seconds)'
sxaddhist, 'Image column 1 is wavelength (Angstroms)', HEAD, /comment
sxaddhist, 'Image column 2 is flux (MJy)', HEAD, /comment
sxaddhist, 'Image column 3 is flux uncertainty (MJy)', HEAD, /comment
sxaddhist, 'Vearth keyword is heliocentric velocity correction', HEAD, /comment
sxaddhist, '(if Vearth=0 then correction has been applied', HEAD, /comment
sxaddhist, 'FITS file created from MOLLY format using IDL ASTROLIB library', HEAD, /comment
writefits, 'tmp.fits', IMAGE, HEAD
exit
EOF

  if ( $#argv == 2 ) then
    echo " "
    cat tmp.idl
    echo " "
  endif

  \mv -f tmp.fits $outfile
  echo Molly spectrum $nfile written to fits file $outfile

end

set tarfile = $mollyfile:r".tgz"
tar cvzf $tarfile $mollyfile:r"_"?.fit $mollyfile:r"_"??.fit $mollyfile:r"_"???.fit >& /dev/null
echo "gzipped tar file containing all fits spectra has been created: $tarfile"

if ( $#argv == 1 ) then
  rm tmp.*
endif