I wrote this code to edit a fits file (clean data) I reduced some data from 'WAVE' and 'FLUX' columns. So their dimensions has changed. Then I want to make a new fits file with same headers of the first fits file but new wave and flux columns. When i run this code, i receive error and ChatGPT said it is beacuse new fits file has not made correctly.
'''
import os
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.table import Table
import numpy as np
#Reading a file and its basic features and plot it!
filename = '/home/marziefaraji/Documents/data_optic/extest/ADP.2020-06- 10T16_42_09.348.fits'
data = fits.open(filename)
data1 = Table.read(data, hdu=1)
wave = data1['WAVE'][0]
flux = data1['FLUX'][0]
plt.plot(wave, flux, label='Original Data')
plt.xlabel('Wavelength')
plt.ylabel('Flux')
plt.title('Select Points')
plt.grid(True)
#Use plt.ginput() to select points
print("Select points on the plot. Press Enter when done.")
points = plt.ginput(-1, -1)
#Close the plot window
plt.close()
#Print the selected points
print("Selected points:")
for point in points:
print(f"X: {point[0]}, Y: {point[1]}")
#Extract the x and y coordinates of the selected points
wave_selected = [point[0] for point in points]
flux_selected = [point[1] for point in points]
#Remove the data between the selected points
wave_filtered = []
flux_filtered = []
for x, y in zip(wave, flux):
if x < wave_selected[0] or x > wave_selected[1]:
wave_filtered.append(x)
flux_filtered.append(y)
#wave_filtered = np.array(wave_filtered)
#flux_filtered = np.array(flux_filtered)
#Plot the new filtered data
plt.plot(wave_filtered, flux_filtered, 'r-', label='Filtered Data')
plt.xlabel('Wavelength')
plt.ylabel('Flux')
plt.title('Filtered Data')
plt.grid(True)
plt.legend()
plt.show()
#Close the plot window
plt.close()
#Create a new FITS file with the filtered data
new_filename = os.path.splitext(filename)[0] + '_new.fits'
#Create a new primary HDU with the filtered data
primary_hdu = fits.PrimaryHDU()
#Copy the header from the original file to the new primary HDU
primary_hdu.header = data[0].header
#Create a new BinTable HDU with the filtered wave and flux columns
col1_wave = np.array(wave_filtered)
col2_flux = np.array(flux_filtered)
columns = fits.ColDefs([fits.Column(name='WAVE', format='D', array=col1_wave),
fits.Column(name='FLUX', format='D', array=col2_flux)])
filtered_hdu = fits.BinTableHDU.from_columns(columns)
#Copy the header from the original BinTable HDU to the new BinTable HDU
filtered_hdu.header = data[1].header
#Create a new HDU list with the primary and filtered HDUs
hdul = fits.HDUList([primary_hdu, filtered_hdu])
hdul.writeto(new_filename, overwrite=True)
#Save the new FITS file
print(f"New FITS file '{new_filename}' created.")
'''
The error i got: WARNING: File may have been truncated: actual file length (2315520) is smaller than the expected size (1650971016000) [astropy.io.fits.file] WARNING:astropy:File may have been truncated: actual file length (2315520) is smaller than the expected size (1650971016000)