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))