I get the error IndexError: list index out of range

42 views Asked by At

I am trying to use the following code in a python script and line 7 produces the error

import numpy as np
import pandas as pd
from astropy.io import fits
import os
import sys
datafile_dir = r'C:\Users\mkboy\OneDrive\PhD\DES Light curves'
y = int(sys.argv[1])
y_head = fits.open(os.path.join(datafile_dir,'DES5YR_SMP','DES_Y0%i_HEAD.FITS.gz'%y))
y_phot = fits.open(os.path.join(datafile_dir,'DES5YR_SMP','DES_Y0%i_PHOT.FITS.gz'%y))

from tqdm import tqdm
head = y_head[1].data
dat = y_phot[1].data
n_rows_done=0
for sn in tqdm(head):
    nobs = sn['NOBS']
    this_sn = []
    for i in range(nobs):
        row = dat[n_rows_done+i]
        if row[0]!=-777:
            this_sn.append(row)
        else:
            n_rows_done+=(nobs+1)
            break
    #print(this_sn)
    df = pd.DataFrame.from_records(this_sn,columns=dat.columns.names)
    #print(sn['SNID'],df)
    df.to_csv(os.path.join(datafile_dir,'DES5YR_SMP','Y%i'%y,'%s_SMP.csv'%sn['SNID']))

tried running in Jupyter notebook and then from terminal

0

There are 0 answers