# adds/fixes a few headers for the ppakm31 frames.  Sore spot: CRVAL3,
# which is constant as the things came in.  We're setting it to the wavelength
# observed.  And give the unit, too.

import re


from gavo import api


class LinemapFixer(api.ImmediateHeaderProcessor):
	def _isProcessed(self, srcName, hdr):
		return "CUNIT3" in hdr
	
	def _changeHeader(self, srcName, hdr):
		if "cube" in srcName:
			hdr.set("CUNIT3", "Angstrom", "TOPOCENTER Air Wavelength")
		else:
			lineCentre = float(re.search(r"[^\d](\d+)\.fits", srcName).group(1))
			hdr.set("CRVAL3", lineCentre, "Laboratory wavelength of extracted line")
			hdr.set("CUNIT3", "Angstrom")
			if "CDELT3" in hdr:
				del hdr["CDELT3"]

		hdr.set("CUNIT1", "deg")
		hdr.set("CUNIT2", "deg")

		api.addHistoryCard(hdr,
			"ppakm31: added CUNITn, CRVAL3",
			"CUNITn")


if __name__=="__main__":
	api.procmain(LinemapFixer, "ppakm31/q", "import")
