Very well. If it helps with troubleshooting, please let me know if you would like the full file/scripts anyway.
This is a code snippet from the file where I am creating a new fits file of the coadded spectra of one star.
# Create new FITS file
# convert to numpy arrays for saving to FITS file
wavelength = coadded_spec.spectral_axis.value
flux = coadded_spec.flux.value
# Create Primary HDU
primary_hdu = fits.PrimaryHDU()
primary_hdu.header = f[0].header # f from the original file reading, assuming it's the last one read
# Create Binary Table HDU
# Using header data of original Binary Table HDU
table_hdu = fits.BinTableHDU.from_columns(columns=f[1].columns, header=f[1].header)
# Create Image HDUs
# Assuming they're zero-filled like in the original FITS files.
# If you have real data to put here, replace np.zeros_like(flux) with your data.
skyback_hdu = fits.ImageHDU(data=np.zeros_like(flux), header=f[2].header)
flux_sky_sub_hdu = fits.ImageHDU(data=flux, header=f[3].header) # replace with co-added flux
# Construct HDU list and write to file
hdulist = fits.HDUList([primary_hdu, table_hdu, skyback_hdu, flux_sky_sub_hdu])
hdulist.writeto('new.fits', overwrite=True)
I am attempting to read this file (news.fits
) into the module file (calc_rv_new_chapman.py) of the ccf code (use_ccf_functions.py). Here the relevant snippet/function in the code that is meant to read in the FITS file I am trying to create:
def ReadSpec_FLAMES_fits(fits_file):
"""Function to read in fits file of high
resolution spectra and then return wlen
and flux arrays"""
# open fits file
image=pf.open(fits_file)
# retrieve flux and header
data = image[0].data
header=image[1].header
# get object data
ispec = 1
#wlen, flux, err = data[0]
star_name = data['OBJECT'][ispec]
flux = image['FLUX_SKY_SUB'].data[:,ispec]
err = np.sqrt(flux)*0.01 #dummy errors, need to be loaded from science_rbnerrors_0000.fits
# get coords
ra_val = data['RA'][ispec]
dec_val = data['DEC'][ispec]
wlen = (header['ESO PRO REBIN WLEN MIN'] + np.arange(header['ESO PRO REBIN NX'])*header['ESO PRO REBIN LAMBDA STEP']) * 10.
print ("Min. - Max. wlen: %i - %i \n" % (wlen[0], wlen[-1]))
# file type to distinguish between
file_type = header['HIERARCH ESO PRO CATG']
# date of observations
mjd_obs = header['MJD-OBS']
# barycentric correction already applied for FEROS
bary_correc = 0
image.close()
# define star_name again with file type and obs date
star_name = "%s_%s" % (star_name.replace(" ", ""), \
file_type)
print (star_name, ra_val, dec_val, mjd_obs, wlen, flux, bary_correc )
return star_name, ra_val, dec_val, mjd_obs, wlen, flux, bary_correc
Here is the error output:
python use_ccf_functions.py
['F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC1.fits', 'F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC2.fits', 'F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC3.fits', 'F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC4.fits', 'F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC5.fits', 'F1-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC6.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC7.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC8.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC9.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC10.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC11.fits', 'F2-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC12.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC13.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC14.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC15.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC16.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC17.fits', 'F3-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC18.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC19.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC20.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC21.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC22.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC23.fits', 'F4-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC24.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC25.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC26.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC27.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC28.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC29.fits', 'F5-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC30.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC31.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC32.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC33.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC34.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC35.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC36.fits', 'F6-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC37.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC38.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC39.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC40.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC41.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC42.fits', 'F7-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC43.fits', 'F8-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC44.fits', 'F8-Carina-FLAMES_SCIENCE_RBNSPECTRA_CRCM=PYCOSMIC45.fits']
45 / 45 still not in output table
Starting CCF and Vsin(i) procedure...
Traceback (most recent call last):
File "/home/nick/wd/2020_04_28_version/use_ccf_functions.py", line 88, in <module>
c_b, ad_sig = master_make_ccf_vsini(files_without_ccf[i], flag='fits', mask_type=mask_name, instrument=instrument)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/nick/wd/2020_04_28_version/./MODULES/calc_rv_new_chapman.py", line 1269, in master_make_ccf_vsini
wlen_obs, flux_obs, bary_correc = ReadSpec_FLAMES_fits(file_location)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/nick/wd/2020_04_28_version/./MODULES/calc_rv_new_chapman.py", line 443, in ReadSpec_FLAMES_fits
star_name = data['OBJECT'][ispec]
~~~~^^^^^^^^^^
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
I believe the issue is how I am creating the new fits file in the first place (hence why I intend to include every relevant code/script for context to the issue). What is my mistake with creating the new FITS file?