def func_tradeg(filename, hdulist=None): """ Get the FITS header value of 'TELRA' in degrees Parameters ---------- filename : str The file to get the filter keyword from (must be a fits file), only used if hdulist is ``None``. hdulist : astropy.io.fits.HDUList, optional A listing of the HDUs to search for the requested HDU, default is ``None``, in which case `filename` is opened and used. whichhdu : various, optional The HDU being searched for, this can be an int for the HDU index, a string for the HDU name, or ``None`` in which case the primary HDU is used. The default is ``None``. Returns ------- float The value of TELRA in decimal degrees. """ hdulist2 = None if hdulist is None: hdulist2 = fits.open(filename, 'readonly') else: hdulist2 = hdulist telra = fitsutils.get_hdr_value(hdulist2, 'TELRA') if hdulist is None: hdulist2.close() return spmeta.convert_ra_to_deg(telra)
def func_field(filename, hdulist=None, whichhdu=None): """ return the field from OBJECT fits header value """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist try: objectval = fitsutils.get_hdr_value(hdulist, 'OBJECT', whichhdu) except: objectval = fitsutils.get_hdr_value(hdulist, 'OBJECT', 'LDAC_IMHEAD') if hdulist is None: hdulist2.close() return spmeta.create_field(objectval)
def func_field(filename, hdulist=None, whichhdu=None): """ Get the field from the 'OBJECT' FITS header value Parameters ---------- filename : str The file to get the filter keyword from (must be a fits file), only used if hdulist is ``None``. hdulist : astropy.io.fits.HDUList, optional A listing of the HDUs to search for the requested HDU, default is ``None``, in which case `filename` is opened and used. whichhdu : various, optional The HDU being searched for, this can be an int for the HDU index, a string for the HDU name, or ``None`` in which case the primary HDU is used. The default is ``None``. Returns ------- str Contains the generated field value. """ hdulist2 = None if hdulist is None: hdulist2 = fits.open(filename, 'readonly') else: hdulist2 = hdulist try: objectval = fitsutils.get_hdr_value(hdulist, 'OBJECT', whichhdu) except: objectval = fitsutils.get_hdr_value(hdulist, 'OBJECT', 'LDAC_IMHEAD') if hdulist is None: hdulist2.close() return spmeta.create_field(objectval)
def func_nite(filename, hdulist=None, whichhdu=None): """ Create nite from the DATE-OBS keyword """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist date_obs = fitsutils.get_hdr_value(hdulist, 'DATE-OBS', whichhdu) if hdulist is None: hdulist2.close() return spmeta.create_nite(date_obs)
def func_objects(filename, hdulist=None, whichhdu=None): """ return the number of objects in fits catalog """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist objects = fitsutils.get_hdr_value(hdulist, 'NAXIS2', whichhdu) if hdulist is None: hdulist2.close() return objects
def func_camsym(filename, hdulist=None, whichhdu=None): """ Create camsys from the INSTRUME keyword """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist instrume = fitsutils.get_hdr_value(hdulist2, 'INSTRUME', whichhdu) if hdulist is None: hdulist2.close() return spmeta.create_camsym(instrume)
def func_band(filename, hdulist=None, whichhdu=None): """ Create band from the filter keyword """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist filterval = fitsutils.get_hdr_value(hdulist2, 'FILTER', whichhdu) if hdulist is None: hdulist2.close() return spmeta.create_band(filterval)
def func_tdecdeg(filename, hdulist=None, whichhdu=None): """ return the fits header value TELDEC in degrees """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist teldec = fitsutils.get_hdr_value(hdulist, 'TELDEC') if hdulist is None: hdulist2.close() return spmeta.convert_dec_to_deg(teldec)
def func_radeg(filename, hdulist=None, whichhdu=None): """ return the fits header value RA in degrees """ hdulist2 = None if hdulist is None: hdulist2 = pyfits.open(filename, 'readonly') else: hdulist2 = hdulist ra = fitsutils.get_hdr_value(hdulist, 'RA', whichhdu) if hdulist is None: hdulist2.close() return spmeta.convert_ra_to_deg(ra)
def _gather_metadata_from_header(cls, fullname, hdulist, hdname, metakeys): """ Get values from config """ metadata = collections.OrderedDict() datadef = collections.OrderedDict() for key in metakeys: if miscutils.fwdebug_check(6, 'FTMGMT_DEBUG'): miscutils.fwdebug_print(f"INFO: key={key}") try: metadata[key] = fitsutils.get_hdr_value( hdulist, key.upper(), hdname) datadef[key] = fitsutils.get_hdr_extra(hdulist, key.upper(), hdname) except KeyError: if miscutils.fwdebug_check(1, 'FTMGMT_DEBUG'): miscutils.fwdebug_print( f"INFO: didn't find key {key} in {hdname} header of file {fullname}" ) return metadata, datadef
def _gather_metadata_from_header(cls, fullname, hdulist, hdname, metakeys): """ Get values from config Parameters ---------- fullname : str The name of the file hdulist : list List of the HDU's hdname : str The name of the header to look in metakeys : dict Dictionary of the keys to look for Returns ------- tuple The metadata and data definitions """ metadata = OrderedDict() datadef = OrderedDict() for key in metakeys: if miscutils.fwdebug_check(6, 'FTMGMT_DEBUG'): miscutils.fwdebug_print("INFO: key=%s" % (key)) try: metadata[key] = fitsutils.get_hdr_value( hdulist, key.upper(), hdname) datadef[key] = fitsutils.get_hdr_extra(hdulist, key.upper(), hdname) except KeyError: if miscutils.fwdebug_check(1, 'FTMGMT_DEBUG'): miscutils.fwdebug_print("INFO: didn't find key %s in %s header of file %s" %\ (key, hdname, fullname)) return metadata, datadef
def func_objects(filename, hdulist=None, whichhdu=None): """ return the number of objects in a FITS catalog, which is assumed to be the value of the 'NAXIS2' keyword. Parameters ---------- filename : str The file to get the filter keyword from (must be a fits file), only used if hdulist is ``None``. hdulist : astropy.io.fits.HDUList, optional A listing of the HDUs to search for the requested HDU, default is ``None``, in which case `filename` is opened and used. whichhdu : various, optional The HDU being searched for, this can be an int for the HDU index, a string for the HDU name, or ``None`` in which case the primary HDU is used. The default is ``None``. Returns ------- int The number of objects in the catalog. """ hdulist2 = None if hdulist is None: hdulist2 = fits.open(filename, 'readonly') else: hdulist2 = hdulist objects = fitsutils.get_hdr_value(hdulist, 'NAXIS2', whichhdu) if hdulist is None: hdulist2.close() return objects
def func_camsym(filename, hdulist=None, whichhdu=None): """ Create camsym from the 'INSTRUME' keyword value. Parameters ---------- filename : str The file to get the filter keyword from (must be a fits file), only used if hdulist is ``None``. hdulist : astropy.io.fits.HDUList, optional A listing of the HDUs to search for the requested HDU, default is ``None``, in which case `filename` is opened and used. whichhdu : various, optional The HDU being searched for, this can be an int for the HDU index, a string for the HDU name, or ``None`` in which case the primary HDU is used. The default is ``None``. Returns ------- str Contains the generated camsym. """ hdulist2 = None if hdulist is None: hdulist2 = fits.open(filename, 'readonly') else: hdulist2 = hdulist instrume = fitsutils.get_hdr_value(hdulist2, 'INSTRUME', whichhdu) if hdulist is None: hdulist2.close() return spmeta.create_camsym(instrume)
def gather_metadata_file(hdulist, fullname, metadata_defs, extra_info): """ gather metadata for a single file """ # extra_info is "dict" containing any info needed for wcl metadata # computes are already created and stored in hdu, key in headers list if hdulist is None: hdulist = fits.open(fullname, 'r') elif fullname is None: fullname = hdulist.filename() if miscutils.fwdebug_check(3, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print(f"INFO: Beg file={fullname}") if miscutils.fwdebug_check(6, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print(f"INFO: metadata_defs={metadata_defs}") miscutils.fwdebug_print(f"INFO: extra_info={extra_info}") metadata = {'fullname': fullname} if 'wcl' in metadata_defs: if miscutils.fwdebug_check(6, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print(f"INFO: wcl={metadata_defs['wcl']}") wcllist = None if isinstance(metadata_defs['wcl'], str): wcllist = miscutils.fwsplit(metadata_defs['wcl'], ',') else: wcllist = metadata_defs['wcl'] for wclkey in wcllist: metakey = wclkey.split('.')[-1] if metakey == 'fullname': metadata['fullname'] = fullname elif metakey == 'filename': metadata['filename'] = miscutils.parse_fullname( fullname, miscutils.CU_PARSE_FILENAME) else: if miscutils.fwdebug_check(3, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print(f"INFO: wclkey={wclkey}") metadata[metakey] = extra_info[wclkey] if 'headers' in metadata_defs: if miscutils.fwdebug_check(3, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print( f"INFO: headers={metadata_defs['headers']}") for hdu, keys in metadata_defs['headers'].items(): if miscutils.fwdebug_check(6, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print(f"INFO: hdu={hdu}, keys={keys}") keylist = None if isinstance(metadata_defs['wcl'], str): keylist = miscutils.fwsplit(keys, ',') else: keylist = keys for key in keylist: try: metadata[key] = fitsutils.get_hdr_value( hdulist, key.upper(), hdu) except KeyError: if miscutils.fwdebug_check(3, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print( f"INFO: didn't find key {key} in {hdu} header of file {fullname}" ) if miscutils.fwdebug_check(3, 'FM_METAUTILS_DEBUG'): miscutils.fwdebug_print("INFO: end") return metadata
def replace_vars_type(instr, valdict, required, stype, opts=None): """ Search given string for variables of 1 type and replace """ assert isinstance(instr, str) #assert(isinstance(valdict, dict)) keep = {} done = True maxtries = 100 # avoid infinite loop count = 0 newstr = copy.copy(instr) # be careful of nested variables ${RMS_${BAND}} varpat = fr"(?i)\${stype}\{{([^$}}]+)\}}" match_var = re.search(varpat, newstr) while match_var and count < maxtries: count += 1 # the string inside the curly braces var = match_var.group(1) # may be var:# parts = var.split(':') # variable name to replace newvar = parts[0] if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print(f"\t newstr: {newstr} ") miscutils.fwdebug_print(f"\t var: {var} ") miscutils.fwdebug_print(f"\t parts: {parts} ") miscutils.fwdebug_print(f"\t newvar: {newvar} ") # find the variable's value if stype == 'HEAD': if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print( f"\tfound HEAD variable to expand: {newvar} ") varlist = miscutils.fwsplit(newvar, ',') fname = varlist[0] if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print(f"\tHEAD variable fname: {fname} ") hdulist = fits.open(fname, 'readonly') newval = [] for key in varlist[1:]: if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print( f"\tHEAD variable header key: {key} ") newval.append(str(fitsutils.get_hdr_value(hdulist, key))) miscutils.fwdebug_print(f"\tnewval: {newval} ") newval = ','.join(newval) haskey = True hdulist.close() elif stype == 'FUNC': if miscutils.fwdebug_check(1, 'REPL_DEBUG'): miscutils.fwdebug_print( f"\tfound FUNC variable to expand: {newvar} ") varlist = miscutils.fwsplit(newvar, ',') funcinfo = varlist[0] if miscutils.fwdebug_check(1, 'REPL_DEBUG'): miscutils.fwdebug_print(f"\tFUNC info: {funcinfo} ") specf = miscutils.dynamically_load_class(funcinfo) newval = specf(varlist[1:]) haskey = True elif hasattr(valdict, 'search'): (haskey, newval) = valdict.search(newvar, opts) else: haskey = False if newvar in valdict: haskey = True newval = valdict[newvar] if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print(f"\t newvar: {newvar} ") miscutils.fwdebug_print(f"\t haskey: {haskey} ") try: # newval may be undefined miscutils.fwdebug_print(f"\t newval: {newval} ") except: pass if haskey: newval = str(newval) # check if a multiple value variable (e.g., band, ccdnum) if newval.startswith('(') or ',' in newval: if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print( f"\tfound val to expand: {newval} ") miscutils.fwdebug_print( f"\tfound val to expand: opts={opts} ") if opts is not None and 'expand' in opts and opts['expand']: newval = f'$LOOP{{{var}}}' # postpone for later expanding if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print(f"\tLOOP? newval = {newval}") elif len(parts) > 1: prpat = f"{{:0{int(parts[1]):d}d}}" try: keepval = replace_vars_single(newval, valdict, opts) keep[newvar] = keepval newval = prpat.format(int(keepval)) except (TypeError, ValueError) as err: miscutils.fwdebug_print(f"\tError = {str(err)}") miscutils.fwdebug_print(f"\tprpat = {prpat}") miscutils.fwdebug_print(f"\tnewval = {newval}") miscutils.fwdebug_print(f"\topts = {opts}") raise err else: keep[newvar] = newval newstr = re.sub(fr"(?i)\${stype}{{{var}}}", newval, newstr) done = False elif required: raise KeyError(f"Error: Could not find value for {newvar}") else: # missing optional value so replace with empty string newstr = re.sub(fr"(?i)\${stype}{{{var}}}", "", newstr) match_var = re.search(varpat, newstr) return (done, newstr, keep)
def replace_vars_type(instr, valdict, required, stype, opts=None): """ Search given string for variables of 1 type and replace them Parameters ---------- instr : str The input string whose variables need to be replaced valdict : dict Dictionary containing available variables and their values required : bool Whether the replacement must be done stype : str The type of replacement being done opts : dict, optional Additional variable replacement values Returns ------- tuple Three element tuple containing a bool which indicates whether the replacement was complete, the new string with values reaplced, and a dict of updated values. Raises ------ TypeError, ValueError If there is an issue replacing the variables """ assert isinstance(instr, str) #assert(isinstance(valdict, dict)) keep = {} done = True maxtries = 100 # avoid infinite loop count = 0 newstr = copy.copy(instr) # be careful of nested variables ${RMS_${BAND}} varpat = r"(?i)\$%s\{([^$}]+)\}" % stype match_var = re.search(varpat, newstr) while match_var and count < maxtries: count += 1 # the string inside the curly braces var = match_var.group(1) # may be var:# parts = var.split(':') # variable name to replace newvar = parts[0] if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print("\t newstr: %s " % (newstr)) miscutils.fwdebug_print("\t var: %s " % (var)) miscutils.fwdebug_print("\t parts: %s " % (parts)) miscutils.fwdebug_print("\t newvar: %s " % (newvar)) # find the variable's value if stype == 'HEAD': if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print( "\tfound HEAD variable to expand: %s " % (newvar)) varlist = miscutils.fwsplit(newvar, ',') fname = varlist[0] if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print("\tHEAD variable fname: %s " % (fname)) hdulist = fits.open(fname, 'readonly') newval = [] for key in varlist[1:]: if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print("\tHEAD variable header key: %s " % (key)) newval.append(str(fitsutils.get_hdr_value(hdulist, key))) miscutils.fwdebug_print("\tnewval: %s " % (newval)) newval = ','.join(newval) haskey = True hdulist.close() elif stype == 'FUNC': if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print( "\tfound FUNC variable to expand: %s " % (newvar)) varlist = miscutils.fwsplit(newvar, ',') funcinfo = varlist[0] if miscutils.fwdebug_check(0, 'REPL_DEBUG'): miscutils.fwdebug_print("\tFUNC info: %s " % (funcinfo)) specf = miscutils.dynamically_load_class(funcinfo) newval = specf(varlist[1:]) haskey = True elif hasattr(valdict, 'search'): (haskey, newval) = valdict.search(newvar, opts) else: haskey = False if newvar in valdict: haskey = True newval = valdict[newvar] if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print("\t newvar: %s " % (newvar)) miscutils.fwdebug_print("\t haskey: %s " % (haskey)) miscutils.fwdebug_print("\t newval: %s " % (newval)) if haskey: newval = str(newval) # check if a multiple value variable (e.g., band, ccdnum) if newval.startswith('(') or ',' in newval: if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print("\tfound val to expand: %s " % (newval)) miscutils.fwdebug_print("\tfound val to expand: opts=%s " % (opts)) if opts is not None and 'expand' in opts and opts['expand']: newval = '$LOOP{%s}' % var # postpone for later expanding if miscutils.fwdebug_check(6, 'REPL_DEBUG'): miscutils.fwdebug_print("\tLOOP? newval = %s" % newval) elif len(parts) > 1: prpat = "%%0%dd" % int(parts[1]) try: keepval = replace_vars_single(newval, valdict, opts) keep[newvar] = keepval newval = prpat % int(keepval) except (TypeError, ValueError) as err: miscutils.fwdebug_print("\tError = %s" % str(err)) miscutils.fwdebug_print("\tprpat = %s" % prpat) miscutils.fwdebug_print("\tnewval = %s" % newval) miscutils.fwdebug_print("\topts = %s" % opts) raise err else: keep[newvar] = newval newstr = re.sub(r"(?i)\$%s{%s}" % (stype, var), newval, newstr) done = False elif required: raise KeyError("Error: Could not find value for %s" % newvar) else: # missing optional value so replace with empty string newstr = re.sub(r"(?i)\$%s{%s}" % (stype, var), "", newstr) match_var = re.search(varpat, newstr) return (done, newstr, keep)