Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

U/danielsf/when obs lsst sim breaks #92

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
46 changes: 26 additions & 20 deletions python/lsst/sims/GalSimInterface/galSimCameraWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class that inherits from GalSimCameraWrapper. This class will
"""

import numpy as np
import lsst.obs.lsst.phosim as obs_lsst_phosim
from lsst.afw.cameraGeom import FOCAL_PLANE, PIXELS, TAN_PIXELS
from lsst.afw.cameraGeom import FIELD_ANGLE
import lsst.geom as LsstGeom
Expand Down Expand Up @@ -483,6 +484,11 @@ def pixelCoordsFromRaDec(self, ra, dec, pm_ra=None, pm_dec=None, parallax=None,
class LSSTCameraWrapper(coordUtils.DMtoCameraPixelTransformer,
GalSimCameraWrapper):

def __init__(self):
self._camera = obs_lsst_phosim.PhosimMapper().camera



def getTanPixelBounds(self, detector_name):
"""
Return the min and max pixel values of a detector, assuming
Expand Down Expand Up @@ -544,10 +550,10 @@ def pixelCoordsFromPupilCoords(self, xPupil, yPupil, chipName, obs_metadata,
are defined in the Camera team system, rather than the DM system.
"""
(dm_x_pix,
dm_y_pix) = coordUtils.pixelCoordsFromPupilCoordsLSST(xPupil, yPupil,
chipName=chipName,
band=obs_metadata.bandpass,
includeDistortion=includeDistortion)
dm_y_pix) = coordUtils.pixelCoordsFromPupilCoords(xPupil, yPupil,
chipName=chipName,
camera=self.camera,
includeDistortion=includeDistortion)

cam_y_pix = dm_x_pix
if isinstance(chipName, list) or isinstance(chipName, np.ndarray):
Expand Down Expand Up @@ -609,9 +615,9 @@ def pupilCoordsFromPixelCoords(self, xPix, yPix, chipName, obs_metadata,
else:
cam_center_pix = self.getCenterPixel(chipName)
dm_yPix = 2.0*cam_center_pix.getX()-xPix
return coordUtils.pupilCoordsFromPixelCoordsLSST(dm_xPix, dm_yPix, chipName,
band=obs_metadata.bandpass,
includeDistortion=includeDistortion)
return coordUtils.pupilCoordsFromPixelCoords(dm_xPix, dm_yPix, chipName,
camera=self.camera,
includeDistortion=includeDistortion)

def _raDecFromPixelCoords(self, xPix, yPix, chipName, obs_metadata,
epoch=2000.0, includeDistortion=True):
Expand Down Expand Up @@ -666,11 +672,11 @@ def _raDecFromPixelCoords(self, xPix, yPix, chipName, obs_metadata,
cam_center_pix = self.getCenterPixel(chipName)
dm_yPix = 2.0*cam_center_pix.getX() - xPix

return coordUtils._raDecFromPixelCoordsLSST(dm_xPix, dm_yPix, chipName,
obs_metadata=obs_metadata,
band=obs_metadata.bandpass,
epoch=epoch,
includeDistortion=includeDistortion)
return coordUtils._raDecFromPixelCoords(dm_xPix, dm_yPix, chipName,
obs_metadata=obs_metadata,
camera=self.camera,
epoch=epoch,
includeDistortion=includeDistortion)

def raDecFromPixelCoords(self, xPix, yPix, chipName, obs_metadata,
epoch=2000.0, includeDistortion=True):
Expand Down Expand Up @@ -775,14 +781,14 @@ def _pixelCoordsFromRaDec(self, ra, dec, pm_ra=None, pm_dec=None, parallax=None,
are defined in the Camera team system, rather than the DM system.
"""

dm_xPix, dm_yPix = coordUtils._pixelCoordsFromRaDecLSST(ra, dec,
pm_ra=pm_ra, pm_dec=pm_dec,
parallax=parallax, v_rad=v_rad,
obs_metadata=obs_metadata,
chipName=chipName,
band=obs_metadata.bandpass,
epoch=epoch,
includeDistortion=includeDistortion)
dm_xPix, dm_yPix = coordUtils._pixelCoordsFromRaDec(ra, dec,
pm_ra=pm_ra, pm_dec=pm_dec,
parallax=parallax, v_rad=v_rad,
obs_metadata=obs_metadata,
chipName=chipName,
camera=self.camera,
epoch=epoch,
includeDistortion=includeDistortion)

return self.cameraPixFromDMPix(dm_xPix, dm_yPix, chipName)

Expand Down
23 changes: 10 additions & 13 deletions python/lsst/sims/GalSimInterface/galSimDetector.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import galsim
import numpy as np
import lsst.geom as LsstGeom
import lsst.obs.lsst.translators.lsst
from lsst.afw.cameraGeom import FIELD_ANGLE, PIXELS, FOCAL_PLANE
from lsst.afw.cameraGeom import WAVEFRONT, GUIDER
from lsst.obs.lsstSim import LsstSimMapper
from lsst.sims.utils import arcsecFromRadians
from lsst.sims.GalSimInterface.wcsUtils import tanSipWcsFromDetector
from lsst.sims.GalSimInterface import GalSimCameraWrapper
Expand Down Expand Up @@ -116,13 +116,12 @@ def __init__(self, detectorName, cameraWrapper, obs_metadata, epoch, photParams=
self.fitsHeader.set('SIMULATE', True)
self.fitsHeader.set('ORIGIN', 'IMSIM')
observatory = LsstObservatory()
self.fitsHeader.set('OBS-LONG', observatory.getLongitude().asDegrees())
self.fitsHeader.set('OBS-LAT', observatory.getLongitude().asDegrees())
self.fitsHeader.set('OBS-ELEV', observatory.getElevation())
obs_location = observatory.getLocation()
self.fitsHeader.set('OBSGEO-X', obs_location.geocentric[0].value)
self.fitsHeader.set('OBSGEO-Y', obs_location.geocentric[1].value)
self.fitsHeader.set('OBSGEO-Z', obs_location.geocentric[2].value)
self.fitsHeader.set('OBS-LONG', observatory.lon.degree)
self.fitsHeader.set('OBS-LAT', observatory.lat.degree)
self.fitsHeader.set('OBS-ELEV', observatory.height.value)
self.fitsHeader.set('OBSGEO-X', observatory.geocentric[0].value)
self.fitsHeader.set('OBSGEO-Y', observatory.geocentric[1].value)
self.fitsHeader.set('OBSGEO-Z', observatory.geocentric[2].value)

self.crpix1 = self.fitsHeader.getScalar("CRPIX1")
self.crpix2 = self.fitsHeader.getScalar("CRPIX2")
Expand Down Expand Up @@ -688,7 +687,8 @@ class LsstObservatory:
observatory location information.
"""
def __init__(self):
self.observatory = LsstSimMapper().MakeRawVisitInfoClass().observatory
self.observatory = lsst.obs.lsst.translators.lsst.LSST_LOCATION
self.geocentric = self.observatory.to_geocentric()

def getLocation(self):
"""
Expand All @@ -698,10 +698,7 @@ def getLocation(self):
-------
astropy.coordinates.earth.EarthLocation
"""
return astropy.coordinates.EarthLocation.from_geodetic(
self.observatory.getLongitude().asDegrees(),
self.observatory.getLatitude().asDegrees(),
self.observatory.getElevation())
return self.observatory

def __getattr__(self, attr):
if hasattr(self.observatory, attr):
Expand Down
2 changes: 1 addition & 1 deletion python/lsst/sims/GalSimInterface/galSimInterpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ def __init__(self, obs_metadata=None, detectors=None, bandpassDict=None,
self.local_hour_angle \
= self.getHourAngle(self.obs_metadata.mjd.TAI,
self.obs_metadata.pointingRA)*galsim.degrees
self.obs_latitude = self.observatory.getLatitude().asDegrees()*galsim.degrees
self.obs_latitude = self.observatory.lat.degree*galsim.degrees

# Make a trivial SED to use for faint things.
blue_limit = np.min([bp.blue_limit for bp in self.gs_bandpass_dict.values()])
Expand Down
2 changes: 0 additions & 2 deletions tests/testFitsHeaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

from testUtils import create_text_catalog

from lsst.sims.coordUtils import clean_up_lsst_camera

ROOT = os.path.abspath(os.path.dirname(__file__))

Expand Down Expand Up @@ -67,7 +66,6 @@ class FitsHeaderTest(unittest.TestCase):
@classmethod
def tearDownClass(cls):
sims_clean_up()
clean_up_lsst_camera()

def testFitsHeader(self):
"""
Expand Down
51 changes: 18 additions & 33 deletions tests/testGalSimCameraWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import numpy as np
import lsst.utils.tests

import lsst.obs.lsst.phosim as obs_lsst_phosim

from lsst.sims.utils import ObservationMetaData
from lsst.sims.utils import raDecFromAltAz
from lsst.sims.coordUtils import pixelCoordsFromRaDec
Expand All @@ -13,21 +15,11 @@

from lsst.sims.GalSimInterface import GalSimCameraWrapper
from lsst.sims.GalSimInterface import LSSTCameraWrapper
from lsst.sims.coordUtils import lsst_camera

import lsst.afw.cameraGeom.testUtils as camTestUtils
from lsst.afw.cameraGeom import FOCAL_PLANE
from lsst.afw.cameraGeom import TAN_PIXELS, FIELD_ANGLE, PIXELS

from lsst.sims.coordUtils import chipNameFromPupilCoordsLSST
from lsst.sims.coordUtils import focalPlaneCoordsFromPupilCoordsLSST
from lsst.sims.coordUtils import pupilCoordsFromFocalPlaneCoordsLSST
from lsst.sims.coordUtils import pupilCoordsFromPixelCoordsLSST
from lsst.sims.coordUtils import pixelCoordsFromPupilCoordsLSST
from lsst.sims.coordUtils import raDecFromPixelCoordsLSST
from lsst.sims.coordUtils import lsst_camera

from lsst.sims.coordUtils import clean_up_lsst_camera

def setup_module(module):
lsst.utils.tests.init()
Expand All @@ -37,10 +29,6 @@ class Camera_Wrapper_Test_Class(unittest.TestCase):

longMessage = True

@classmethod
def tearDownClass(cls):
clean_up_lsst_camera()

def test_generic_camera_wrapper(self):
"""
Test that GalSimCameraWrapper wraps its methods as expected.
Expand Down Expand Up @@ -209,7 +197,7 @@ def test_LSST_camera_wrapper(self):
Camera +y = DM +x
Camera +x = DM -y
"""
camera = lsst_camera()
camera = obs_lsst_phosim.PhosimMapper().camera
camera_wrapper = LSSTCameraWrapper()

obs_mjd = ObservationMetaData(mjd=60000.0)
Expand Down Expand Up @@ -290,8 +278,8 @@ def test_LSST_camera_wrapper(self):
# is as expected
x_pup = rng.random_sample(10)*0.005-0.01
y_pup = rng.random_sample(10)*0.005-0.01
x_pix, y_pix = pixelCoordsFromPupilCoordsLSST(x_pup, y_pup, chipName=name,
band=obs.bandpass)
x_pix, y_pix = pixelCoordsFromPupilCoords(x_pup, y_pup, chipName=name,
camera=camera)

(x_pix_wrapper,
y_pix_wrapper) = camera_wrapper.pixelCoordsFromPupilCoords(x_pup, y_pup,
Expand Down Expand Up @@ -324,8 +312,8 @@ def test_LSST_camera_wrapper(self):
x_pix = bbox.getMinX() + rng.random_sample(10)*(bbox.getMaxX()-bbox.getMinX())
y_pix = bbox.getMinY() + rng.random_sample(10)*(bbox.getMaxY()-bbox.getMinY())

ra, dec = raDecFromPixelCoordsLSST(x_pix, y_pix, name, obs_metadata=obs,
band=obs.bandpass)
ra, dec = raDecFromPixelCoords(x_pix, y_pix, name, obs_metadata=obs,
camera=camera)

(ra_wrapper,
dec_wrapper) = camera_wrapper.raDecFromPixelCoords(2.0*center_pix.getY()-y_pix,
Expand Down Expand Up @@ -370,14 +358,13 @@ def test_LSST_camera_wrapper(self):

del camera
del camera_wrapper
del lsst_camera._lsst_camera

def test_dmPixFromCameraPix(self):
"""
Test that the method to return DM pixel coordinates from
Camera Team pixel coordinates works.
"""
camera = lsst_camera()
camera = obs_lsst_phosim.PhosimMapper().camera
camera_wrapper = LSSTCameraWrapper()
obs = ObservationMetaData(bandpassName='u')

Expand All @@ -391,10 +378,10 @@ def test_dmPixFromCameraPix(self):
chip_name_list = rng.choice(name_list, size=npts)

(xPup_list,
yPup_list) = pupilCoordsFromPixelCoordsLSST(dm_x_pix_list,
dm_y_pix_list,
chipName=chip_name_list,
band=obs.bandpass)
yPup_list) = pupilCoordsFromPixelCoords(dm_x_pix_list,
dm_y_pix_list,
chipName=chip_name_list,
camera=camera)

(cam_x_pix_list,
cam_y_pix_list) = camera_wrapper.pixelCoordsFromPupilCoords(xPup_list,
Expand Down Expand Up @@ -425,10 +412,10 @@ def test_dmPixFromCameraPix(self):
chip_name = chip_name_list[10]

(xPup_list,
yPup_list) = pupilCoordsFromPixelCoordsLSST(dm_x_pix_list,
dm_y_pix_list,
chipName=chip_name,
band=obs.bandpass)
yPup_list) = pupilCoordsFromPixelCoords(dm_x_pix_list,
dm_y_pix_list,
chipName=chip_name,
camera=camera)

(cam_x_pix_list,
cam_y_pix_list) = camera_wrapper.pixelCoordsFromPupilCoords(xPup_list,
Expand All @@ -448,7 +435,6 @@ def test_dmPixFromCameraPix(self):

del camera
del camera_wrapper
del lsst_camera._lsst_camera

def test_camPixFromDMpix(self):
"""
Expand All @@ -459,12 +445,11 @@ def test_camPixFromDMpix(self):
npts = 200
cam_x_in = rng.random_sample(npts)*4000.0
cam_y_in = rng.random_sample(npts)*4000.0
dm_x, dm_y = camera_wrapper.dmPixFromCameraPix(cam_x_in, cam_y_in, 'R:1,1 S:2,2')
cam_x, cam_y = camera_wrapper.cameraPixFromDMPix(dm_x, dm_y, 'R:1,1 S:2,2')
dm_x, dm_y = camera_wrapper.dmPixFromCameraPix(cam_x_in, cam_y_in, 'R11_S22')
cam_x, cam_y = camera_wrapper.cameraPixFromDMPix(dm_x, dm_y, 'R11_S22')
np.testing.assert_array_almost_equal(cam_x_in, cam_x, decimal=10)
np.testing.assert_array_almost_equal(cam_y_in, cam_y, decimal=10)
del camera_wrapper
del lsst_camera._lsst_camera


class MemoryTestClass(lsst.utils.tests.MemoryTestCase):
Expand Down
4 changes: 1 addition & 3 deletions tests/testGalSimInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from lsst.sims.catUtils.utils import (calcADUwrapper, testGalaxyBulgeDBObj, testGalaxyDiskDBObj,
testGalaxyAgnDBObj, testStarsDBObj)
import lsst.afw.image as afwImage
from lsst.sims.coordUtils import clean_up_lsst_camera

# Tell astropy not to download this file again, even if it's out of date.
from astropy.utils import iers
Expand Down Expand Up @@ -1121,7 +1120,6 @@ def setUp(self):
self.db_name = os.path.join(self.scratch_dir, 'galsim_test_db')

def tearDown(self):
clean_up_lsst_camera()
if os.path.exists(self.db_name):
os.remove(self.db_name)
if os.path.exists(self.scratch_dir):
Expand All @@ -1143,7 +1141,7 @@ def test_getStampBounds(self):
obs_md.OpsimMetaData['altitude'] = altitude
obs_md.OpsimMetaData['rawSeeing'] = seeing
camera_wrapper = LSSTCameraWrapper()
detector = make_galsim_detector(camera_wrapper, 'R:2,2 S:1,1',
detector = make_galsim_detector(camera_wrapper, 'R22_S11',
PhotometricParameters(), obs_md)
gs_interpreter = make_gs_interpreter(obs_md, [detector],
BandpassDict.loadTotalBandpassesFromFiles(),
Expand Down
Loading