"""
This removes a few spurious cards (AIRMASS, EXPTIME) and applies manual
calibration to two images with no or bad calibration.
"""

import os

from gavo.helpers import fitstricks
from gavo.helpers import processing
from gavo.utils import fitstools
from gavo.utils import pyfits

class SmakProcessor(processing.HeaderProcessor):

	def _isProcessed(self, srcName):
		return not self.getPrimaryHeader(srcName).has_key("AIRMASS")

	def _mungeHeader(self, srcName, hdr):
		del hdr["AIRMASS"]
		del hdr["EXPTIME"]
		hdr.add_history("More info at http://dc.g-vo.org/smakced/q/cat/form")

		calibName = os.path.join(self.manualPath,
			srcName.split("/")[-1].split(".")[0]+".calibration")
		if os.path.exists(calibName):
			with open(calibName) as f:
				fitstricks.copyFields(hdr, fitstools.parseCards(f.read()))

		return hdr

	def historyFilter(self, hdr):
		return True

	def _createAuxillaries(self, dd):
		self.manualPath = os.path.join(dd.rd.resdir, "res")

if __name__=="__main__":
	processing.procmain(SmakProcessor, "smakced/q", "import_main")
