def add_object_info(directory='.', object_list=None, match_radius=20.0, new_file_ext='new', overwrite=False, detailed_history=True): """ Automagically add object information to FITS files. `directory` is the directory containing the FITS files to be fixed up and an `object_list`. Set `object_list` to `None` to use default object list name. `match_radius` is the maximum distance, in arcmin, between the RA/Dec of the image and a particular object for the image to be considered an image of that object. """ from astro_object import AstroObject import numpy as np from fitskeyword import FITSKeyword images = ImageFileCollection(directory, keywords=['imagetyp', 'RA', 'Dec', 'object']) summary = images.summary_info print summary['file'] try: observer, object_names = read_object_list(directory) except IOError: print 'No object list in directory %s, skipping.' % directory return # ra_dec_obj = {'er ori':(93.190,12.382), 'm101':(210.826,54.335), 'ey uma':(135.575,49.810)} object_names = np.array(object_names) ra_dec = [] for object_name in object_names: obj = AstroObject(object_name) ra_dec.append(obj.ra_dec) object_ra_dec = np.array(ra_dec) for header in images.headers(save_with_name=new_file_ext, clobber=overwrite, object='', RA='*', Dec='*'): image_ra_dec = coords.coordsys.FK5Coordinates(header['ra'], header['dec']) distance = [(ra_dec - image_ra_dec).arcmin for ra_dec in object_ra_dec] distance = np.array(distance) matches = (distance < match_radius) if matches.sum() > 1: raise RuntimeError("More than one object match for image") if not matches.any(): raise RuntimeWarning("No object foundn for image") continue object_name = (object_names[matches])[0] obj_keyword = FITSKeyword('object',value=object_name) obj_keyword.addToHeader(header, history=True)
def patch_headers(dir='.', new_file_ext='new', overwrite=False, detailed_history=True): """ Add minimal information to Feder FITS headers. `dir` is the directory containing the files to be patched. `new_file_ext` is the name added to the FITS files with updated header information. It is added to the base name of the input file, between the old file name and the `.fit` or `.fits` extension. `overwrite` should be set to `True` to replace the original files. detailed_history : bool If `True`, write name and value of each keyword changed to output FITS files. If `False`, write only a list of which keywords changed. """ images = ImageFileCollection(location=dir, keywords=['imagetyp']) latitude.value = sexagesimal_string(feder.latitude.dms) longitude.value = sexagesimal_string(feder.longitude.dms) obs_altitude.value = feder.altitude for header in images.headers(save_with_name=new_file_ext, clobber=overwrite, do_not_scale_image_data=True): run_time = datetime.now() header.add_history(history(patch_headers, mode='begin', time=run_time)) header.add_history('patch_headers.py modified this file on %s' % run_time) add_time_info(header, history=detailed_history) if not detailed_history: header.add_history('patch_headers.py updated keywords %s' % keyword_names_as_string(keywords_for_all_files)) if header['imagetyp'] == 'LIGHT': try: add_object_pos_airmass(header, history=detailed_history) if not detailed_history: header.add_history('patch_headers.py updated keywords %s' % keyword_names_as_string(keywords_for_light_files)) except ValueError: print 'Skipping file %s' % image continue header.add_history(history(patch_headers, mode='end', time=run_time))
def add_overscan(dir='.', new_file_ext='new', overwrite=False, detailed_history=True): """ Add overscan information to Feder FITS headers. `dir` is the directory containing the files to be patched. `new_file_ext` is the name added to the FITS files with updated header information. It is added to the base name of the input file, between the old file name and the `.fit` or `.fits` extension. `overwrite` should be set to `True` to replace the original files. detailed_history : bool If `True`, write name and value of each keyword changed to output FITS files. If `False`, write only a list of which keywords changed. """ feder_info = Feder() images = ImageFileCollection(location=dir, keywords=['imagetyp', 'instrume']) for header in images.headers(save_with_name=new_file_ext, clobber=overwrite, do_not_scale_image_data=True): image_dim = [header['naxis1'], header['naxis2']] instrument = feder_info.instrument[header['instrume']] run_time = datetime.now() header.add_history(history(add_overscan, mode='begin', time=run_time)) overscan_present.value = instrument.has_overscan(image_dim) overscan_present.addToHeader(header, history=detailed_history) modified_keywords = [overscan_present] if overscan_present.value: overscan_axis.value = instrument.overscan_axis overscan_start.value = instrument.overscan_start overscan_axis.addToHeader(header, history=detailed_history) overscan_start.addToHeader(header, history=detailed_history) modified_keywords.extend([overscan_axis, overscan_start]) if not detailed_history: header.add_history('add_overscan updated keywords %s' % keyword_names_as_string(modified_keywords)) header.add_history(history(add_overscan, mode='end', time=run_time))
#logger.info("Moving file %s to raw folder" % \ # (os.path.basename(afile))) try: os.rename(afile, os.path.join(rawpath, \ os.path.basename(afile))) except: logger.error("Couldn't move file %s to raw folder." % \ (todayspath)) #Enter here file to database #Reduce each file from image_collection import ImageFileCollection #Create an image file collection storing the following keys keys = ['imagetyp', 'object', 'filter', 'exptime'] allfits = ImageFileCollection(rawpath, keywords=keys) #Collect all dark files and make a dark frame for each diff co time dark_matches = np.ma.array(['dark' in atype.lower() \ for atype in allfits.summary['imagetyp']]) darkexp_set = set(allfits.summary['exptime'][dark_matches]) darklists = {} for anexp in darkexp_set: my_darks = allfits.summary['file'][(\ allfits.summary['exptime'] == anexp) & dark_matches] my_darks = [os.path.join(rawpath, adark) for adark in my_darks] darklists[anexp] = combineDarks(my_darks) #Collect all science files sciencelist = allfits.files_filtered(imagetyp='light') sciencelist = [os.path.join(rawpath, afile) for afile in sciencelist]
def preprocess(fits_path): """Apply basic CCD reduction tasks. Ask for the path to all the calibration and science files, and perform Bias, Dark and Flat combinations and proper substractions. Pipeline que aplica una reduccion basica a imagenes de tipo CCD. Realiza combinacion de Bias, de Darks, y Flats. Luego realiza las restas inherentes. """ import os import sys from image_collection import ImageFileCollection #Collect all FITS files in path in fitslist #fitslist = [] #for root, dirs, files in os.walk(fits_path, topdown=True): # fitslist.extend([os.path.join(root, name) for name in files if '.fit' in name]) # ##Dictionary with the image type (value) for each image file name (key) #imagetypes = {img:fits.getval(img, 'IMAGETYP') for img in fitslist} # now we make different lists for different image types #biaslist = [] #sciencelist = [] #flatlist = [] #darklist = [] #unknown = [] #for k, v in imagetypes.iteritems(): # if 'LIGHT' in v.upper() or v.upper()=='OBJECT': # sciencelist.append(k) # elif 'BIAS' in v.upper() or v.upper()=='ZERO': # biaslist.append(k) # elif 'FLAT' in v.upper(): # flatlist.append(k) # elif 'DARK' in v.upper(): # darklist.append(k) # else: # unknown.append(k) #Create an image file collection storing the following keys keys = ['imagetyp', 'object', 'filter', 'exptime'] allfits = ImageFileCollection('.', keywords=keys) #Collect all dark files and make a dark frame for each different exposure time dark_matches = np.ma.array(['dark' in typ.lower() for typ in allfits.summary['imagetyp']]) darkexp_set = set(allfits.summary['exptime'][dark_matches]) darklists = {} for anexp in darkexp_set: my_darks = allfits.summary['file'][(allfits.summary['exptime'] == anexp) & dark_matches] darklists[anexp] = combineDarks(my_darks) #Collect all science files sciencelist = allfits.files_filtered(imagetyp='light') #Collect all bias files #bias_matches = ([('zero' in typ.lower() or 'bias' in typ.lower()) for typ in allfits.summary['imagetyp']] #biaslist = allfits.summary['file'][bias_matches] #Create the flat master flatlist = allfits.files_filtered(imagetyp='flat') exptime = fits.getval(flatlist[0], 'exptime') darkmaster = chooseClosestDark(darklists, exptime) flatmaster = combineFlats(flatlist, dark=darkmaster) for ascience in sciencelist: sci_image = ccdproc.CCDData.read(ascience, unit='adu') exp_time = fits.getval(ascience, 'exptime') darkmaster = chooseClosestDark(darklists, exp_time) try: sci_darksub = ccdproc.subtract_dark(sci_image, darkmaster, exposure_time='exptime', exposure_unit=u.second) sci_flatcorrected = ccdproc.flat_correct(sci_darksub, flatmaster) outpath = os.path.join('/Users/utb/Desktop/reduced/', 'reduced_' + os.path.basename(ascience)) except: sci_flatcorrected = sci_image outpath = os.path.join('/Users/utb/Desktop/reduced/', 'failed_' + os.path.basename(ascience)) hdulist = sci_flatcorrected.to_hdu() hdulist.writeto(outpath, clobber=True) return
def preprocess(fits_path): """Apply basic CCD reduction tasks. Ask for the path to all the calibration and science files, and perform Bias, Dark and Flat combinations and proper substractions. Pipeline que aplica una reduccion basica a imagenes de tipo CCD. Realiza combinacion de Bias, de Darks, y Flats. Luego realiza las restas inherentes. """ import os import sys from image_collection import ImageFileCollection #Collect all FITS files in path in fitslist #fitslist = [] #for root, dirs, files in os.walk(fits_path, topdown=True): # fitslist.extend([os.path.join(root, name) for name in files if '.fit' in name]) # ##Dictionary with the image type (value) for each image file name (key) #imagetypes = {img:fits.getval(img, 'IMAGETYP') for img in fitslist} # now we make different lists for different image types #biaslist = [] #sciencelist = [] #flatlist = [] #darklist = [] #unknown = [] #for k, v in imagetypes.iteritems(): # if 'LIGHT' in v.upper() or v.upper()=='OBJECT': # sciencelist.append(k) # elif 'BIAS' in v.upper() or v.upper()=='ZERO': # biaslist.append(k) # elif 'FLAT' in v.upper(): # flatlist.append(k) # elif 'DARK' in v.upper(): # darklist.append(k) # else: # unknown.append(k) #Create an image file collection storing the following keys keys = ['imagetyp', 'object', 'filter', 'exptime'] allfits = ImageFileCollection('.', keywords=keys) #Collect all dark files and make a dark frame for each different exposure time dark_matches = np.ma.array( ['dark' in typ.lower() for typ in allfits.summary['imagetyp']]) darkexp_set = set(allfits.summary['exptime'][dark_matches]) darklists = {} for anexp in darkexp_set: my_darks = allfits.summary['file'][ (allfits.summary['exptime'] == anexp) & dark_matches] darklists[anexp] = combineDarks(my_darks) #Collect all science files sciencelist = allfits.files_filtered(imagetyp='light') #Collect all bias files #bias_matches = ([('zero' in typ.lower() or 'bias' in typ.lower()) for typ in allfits.summary['imagetyp']] #biaslist = allfits.summary['file'][bias_matches] #Create the flat master flatlist = allfits.files_filtered(imagetyp='flat') exptime = fits.getval(flatlist[0], 'exptime') darkmaster = chooseClosestDark(darklists, exptime) flatmaster = combineFlats(flatlist, dark=darkmaster) for ascience in sciencelist: sci_image = ccdproc.CCDData.read(ascience, unit='adu') exp_time = fits.getval(ascience, 'exptime') darkmaster = chooseClosestDark(darklists, exp_time) try: sci_darksub = ccdproc.subtract_dark(sci_image, darkmaster, exposure_time='exptime', exposure_unit=u.second) sci_flatcorrected = ccdproc.flat_correct(sci_darksub, flatmaster) outpath = os.path.join('/Users/utb/Desktop/reduced/', 'reduced_' + os.path.basename(ascience)) except: sci_flatcorrected = sci_image outpath = os.path.join('/Users/utb/Desktop/reduced/', 'failed_' + os.path.basename(ascience)) hdulist = sci_flatcorrected.to_hdu() hdulist.writeto(outpath, clobber=True) return