mzechmeister / serval

calculate radial velocities from stellar spectra
MIT License
36 stars 9 forks source link

ResourceWarning: unclosed file _io.FileIO #52

Closed mzechmeister closed 2 years ago

mzechmeister commented 2 years ago

In Python 3 the following warning occurs, when serval exits:

sys:1: ResourceWarning: unclosed file <_io.FileIO name='/.../51Peg/HARPN.2015-10-28T01-02-30.901_e2ds_A.fits' mode='rb' closefd=True>

It only happens, when coadding is perfomed (thus not with option -last). The files are open e.g. in https://github.com/mzechmeister/serval/blob/9fdde0df34e68b16a95de0d2478d37d09d6acac5/src/inst_HARPS.py#L157-L163 In particular since coadding is perfomed order wise, the file shall remain open, while serval is running.

mzechmeister commented 2 years ago

A small script to reproduce the warning in Python 3:

from __future__ import print_function

import glob
from read_spec import Spectrum, warnings, pyfits
import inst_HARPN as inst

Spectrum.brvref = 'DRS'

files = sorted(glob.glob('/home/astro115/carmenes/data/HARPN/DRS/51Peg/*[0-9]_A.fits'))
splist = []
for filename in files:
    sp = Spectrum(filename, inst=inst, drs=1, verb=True)
    splist.append(sp)

tpl = 'test.tpl.fits'

hdu = pyfits.PrimaryHDU(header=sp.header)
warnings.resetwarnings() # supress nasty overwrite warning http://pythonhosted.org/pyfits/users_guide/users_misc.html
warnings.filterwarnings('ignore', category=UserWarning, append=True)
hdu.writeto(tpl, overwrite=True, output_verify='fix')

print('\ntemplate written to ', tpl)

The warnings lines occur when writing the template via the write_mfits function in https://github.com/mzechmeister/serval/blob/9fdde0df34e68b16a95de0d2478d37d09d6acac5/src/read_spec.py#L274-L275 Uncommenting the warnings lines does not throw warnings anymore (Python 2+3).

It seems, the supression of overwrite warnings is not needed in newer astropy.

mzechmeister commented 2 years ago

The right way to solve the issue would be to enhance the Spectrum class with a close functionality. But this would be needed then for each inst file...