def genImageDotHead(image_id): """ Generate a FITS image's .head file. The image file is first accessed to get the number of HDUs (extansions). Then keywords mapping is retrieved from the instrument's ITT in order to get a proper HDU data. @return tuple of hdudata and total number of hdus in this image (primary + extansions) """ db = DB(host = settings.DATABASE_HOST, user = settings.DATABASE_USER, passwd = settings.DATABASE_PASSWORD, db = settings.DATABASE_NAME) # Beginning transaction g = DBGeneric(db.con) if type(image_id) != types.IntType: raise ValueError, "first argument image_id must be an integer" r = g.execute(""" SELECT im.path, im.name, ru.name, ins.itt, ins.name, im.dateobs, cha.name, im.flat, im.airmass, im.equinox, im.object, im.exptime, im.mask, im.alpha, im.delta FROM youpi_image AS im, youpi_instrument AS ins, youpi_channel AS cha, youpi_rel_ri AS ri, youpi_run AS ru WHERE im.id=%d AND im.instrument_id=ins.id AND im.channel_id=cha.id AND ri.image_id = im.id AND ru.id = ri.run_id """ % image_id) if not r: raise LookupError, "no image found with ID: %s" % image_id r = r[0] fname = os.path.join(r[0], r[1] + '.fits') if not os.path.exists(fname): fname = re.sub(r'_\d+\.fits$', '.fits', fname) hdulist = pyfits.open(fname) hdulist.close() # Get instrument translation table itt = marshal.loads(zlib.decompress(base64.decodestring(r[3]))) try: run = r[2] except: run = None data = { 'YRUN' : run, # 'YDETECTOR': , # 'YTELESCOP': , 'YINSTRUMENT' : r[4], 'YDATEOBS' : r[5], 'YFILTER' : r[6], 'YFLAT' : r[7], 'YAIRMASS' : r[8], 'YEQUINOX' : r[9], 'YOBJECT' : r[10], 'YEXPTIME' : r[11], 'YMASK' : r[12], 'YRA' : r[13], 'YDEC' : r[14], } hdudata = {} for k, v in data.iteritems(): try: val = float(str(v)) except ValueError: if isinstance(v, datetime.datetime): v = v.isoformat() val = "'%s'" % v if itt[k].has_key('MAP'): # Unmapped keyword are ignored since they are available in # the image's header hdudata[itt[k]['MAP']] = val missing = [] # Keyword copy feature (+KEYWORD) if itt.has_key('+COPY'): # List of missing keywords (not found in src image) for kw in itt['+COPY']: data = None # Search keyword in all extensions for hdu in hdulist: try: data = str(hdu.header[kw]).strip() except KeyError: # Keyword not found in this extension, continue pass if data: hdudata[kw] = data else: missing.append(kw) return (hdudata, len(hdulist), missing)
return fitsNoExt + FITSEXT if __name__ == '__main__': print "Running from the CLI" # Connection object to MySQL database try: db = DB(host = DATABASE_HOST, user = DATABASE_USER, passwd = DATABASE_PASSWORD, db = DATABASE_NAME) # Beginning transaction g = DBGeneric(db.con) res = g.execute("SELECT id, username FROM auth_user LIMIT 1") debug("Proceeding as user '%s'" % res[0][1]) user_id = res[0][0] # Transaction begins here g.begin() run_stack_ingestion(g, sys.argv[1], user_id) # Commits for that image g.con.commit() except Exception, e: debug(e, FATAL) g.con.rollback() sys.exit(1) sys.exit(0)