#!/usr/local/bin/python
# -*- coding: iso-8859-15 -*-
import os,sys, math
import fnmatch
import pyfits
import  numpy as np
from array import array

# Define the directories for all the Phoenix models. 
d1=['phoenixp03','phoenixp05','phoenixm00','phoenixm05','phoenixm10','phoenixm15','phoenixm20','phoenixm25','phoenixm30','phoenixm35','phoenixm40']
# Define all the log g values for the index values and the filename
ggsP=['[g00]','[g05]','[g10]','[g15]','[g20]','[g25]','[g30]','[g35]','[g40]','[g45]','[g50]','[g55]']
ggs=['0.0','0.5','1.0','1.5','2.0','2.5','3.0','3.5','4.0','4.5','5.0','5.5']

fileout=[]
indexfin=[]

for directory in d1:
# Initialize arrays of filenames, and temperature values

    countf=0
    n1=0
    match1 = '*.fits'
    print 'match=', match1
    f1=[]
    # Starts working one directory at at time
    os.chdir(directory)
    for file in os.listdir('.'): # Lists at all the fits files in that directory
        if fnmatch.fnmatch(file,match1):
            f1.append(file)
            countf += 1.0
            print file
       
    # Column 1 is the INDEX 
    # Column 2 is FILENAME
      
    for file1 in f1: # For each fits file in the directory
       indexfin1=[]
       fileout1=[]
       #extracts the Teff value and stores it in teff1   
       sp=file1.index('.')
       teff1=str(int(file1[11:sp]))
       #Extracts the sign of the metallicity and stores it in sign variable
       if file1[7] == 'm' : 
          sign = '-'  
       else : 
          sign = '+'
       # Extracts the metallicity value and stores it in the variable metal
       metal= file1[8] + '.' + file1[9]
       # Define an output file with all these values
       file11= directory + '/' + file1 
       for g in ggsP: # Makes a line for each of the needed log g values which are columns in the fits file
         fileout.append(file11+g)
       index0 = teff1 + ',' + sign + metal + ','
       for g in ggs:
         indexfin.append(index0+g)

    os.chdir('..')
    i=1


#write the catalog.fits file with the list of all the models 
hdu = pyfits.PrimaryHDU()
hdu.header.update("FILENAME", 'catalog.fits', "Name of file")
hdu.header.update("MAPKEY", "phoecat", "Maping identifiyer for type of file.")
hdu.header.update("CONTACT","R. Diaz","Who created the file")
hdu.header.update("CREATED","March 1, 2011","Date the file was created")
hdu.header.update("DESCRIPT","Catalog for Phoenix Models bu France Allard")
hdu.header.update("FILE_TYP","Atmosphere Grid Model","Type of file")
hdu.header.update("SYSTEMS","etc,cdbs,pysynphot","Systems that will use these files")
hdu.header.update("REASON","Delivered to support JWST","")
hdu.header.update("COMMENT","= 'Files translated to CDBS format by M. McMaster'")
   
col1=pyfits.Column(name='INDEX',format='20A',array=indexfin)
col2=pyfits.Column(name='FILENAME', format='60A', array=fileout)

cols=pyfits.ColDefs([col1, col2])

print 'len indexfin=',len(indexfin),' and len fileout=',len(fileout)
table_hdu=pyfits.new_table(cols, nrows=len(indexfin))
hdulist=pyfits.HDUList([hdu])
hdulist.append(table_hdu)
hdulist.writeto('./catalog.fits')
