PYTHON   33

pwi4 build model py

Guest on 15th August 2022 12:40:37 PM

  1. #!/usr/bin/env python
  2.  
  3. """
  4. This script demonstrates how to build a pointing model using the
  5. HTTP interface for PWI4.
  6.  
  7. It is necessary to implement the take_image() function to provide
  8. an image from your camera upon request. The implementation could be
  9. as simple as waiting for an image to be manually taken and for the
  10. FITS image to be placed in the requested location.
  11.  
  12. This script also requires the PlateSolve library and star catalog.
  13. Please contact PlaneWave Instruments for details.
  14. """
  15.  
  16. import time
  17. import pwi4_client
  18. from platesolve import platesolve
  19.  
  20. # NOTE: Replace this with the estimated arcseconds per pixel
  21. # for an image taken with your camera.
  22. # For PWI4 Virtual Camera, the default is 1.0 arcsec/pixel.
  23. IMAGE_ARCSEC_PER_PIXEL = 1.0
  24.  
  25. def main():
  26.     pwi4 = pwi4_client.PWI4()
  27.  
  28.     print("Checking connection to PWI4")
  29.     status = pwi4.status()
  30.  
  31.     if not status.mount.is_connected:
  32.         print("Connecting to mount")
  33.         pwi4.mount_connect()
  34.  
  35.     status = pwi4.status()
  36.     if not status.mount.axis0.is_enabled:
  37.         print("Enabling axis 0")
  38.         pwi4.mount_enable(0)
  39.     if not status.mount.axis1.is_enabled:
  40.         print("Enabling axis 1")
  41.         pwi4.mount_enable(1)
  42.  
  43.     # Construct a grid of 3 x 6 = 18 Alt-Az points
  44.     # ranging from 20 to 80 degrees Altitude, and from
  45.     # 5 to 355 degrees Azimuth.
  46.     points = create_point_list(3, 20, 80, 6, 5, 355)
  47.    
  48.     for (alt, azm) in points:
  49.         map_point(pwi4, alt, azm)
  50.  
  51.     print("DONE!")
  52.  
  53. def create_point_list(num_alt, min_alt, max_alt, num_azm, min_azm, max_azm):
  54.     """
  55.    Build a grid of target points in alt-az coordinate space.
  56.    """
  57.  
  58.     points = []
  59.  
  60.     for i in range(num_azm):
  61.         azm = min_azm + (max_azm - min_azm) * i / float(num_azm)
  62.  
  63.         for j in range(num_alt):
  64.             alt = min_alt + (max_alt - min_alt) * j / float(num_alt-1)
  65.  
  66.             points.append((alt, azm))
  67.  
  68.     return points
  69.  
  70. def take_image(filename, pwi4):
  71.     # TODO: Replace this with your own routine to take an image
  72.     # with your camera and save a FITS file to "image.fits"
  73.     take_image_virtualcam(filename, pwi4)
  74.  
  75. def take_image_virtualcam(filename, pwi4):
  76.     """
  77.    Take an artificial image using PWI4's virtual camera.
  78.    The starfield in the image will be based on the telescope's
  79.    current coordinates.
  80.  
  81.    (NOTE: Depends on the Kepler star catalog being installed
  82.    in the right place!)
  83.    """
  84.  
  85.     pwi4.virtualcamera_take_image_and_save(filename)
  86.  
  87.  
  88. def map_point(pwi4, alt_degs, azm_degs):
  89.     """
  90.    Slew to the target Alt-Az, take an image,
  91.    PlateSolve it, and (if successful) add to the model
  92.    """
  93.  
  94.     print("Slewing to Azimuth %.3f, Altitude %3f..." % (azm_degs, alt_degs))
  95.     pwi4.mount_goto_alt_az(alt_degs, azm_degs)
  96.  
  97.     while True:
  98.         status = pwi4.status()
  99.         if not status.mount.is_slewing:
  100.             break
  101.         time.sleep(0.1)
  102.    
  103.     # Confirm that we actually reached our target.
  104.     # If, for example, the user clicked Stop in the GUI during
  105.     # the slew, we probably don't want to continue building the model.
  106.     status = pwi4.status()
  107.  
  108.     azm_error = abs(status.mount.azimuth_degs - azm_degs)
  109.     alt_error = abs(status.mount.altitude_degs - alt_degs)
  110.  
  111.     if azm_error > 0.1 or alt_error > 0.1:
  112.         raise Exception("Mount stopped at azimuth %.4f, altitude %.4f, which is too far from the target %.4f, %.4f." % (
  113.             status.mount.azimuth_degs,
  114.             status.mount.altitude_degs,
  115.             azm_degs,
  116.             alt_degs
  117.         ))
  118.  
  119.  
  120.     # Mount will be stopped after an alt-az slew, so turn
  121.     # on sidereal tracking before taking an image
  122.     pwi4.mount_tracking_on()
  123.  
  124.     print("Taking image...")
  125.  
  126.     take_image("image.fits", pwi4)
  127.  
  128.     print("Saved FITS image")
  129.  
  130.     print("Running PlateSolve...")
  131.     try:
  132.         match = platesolve("image.fits", IMAGE_ARCSEC_PER_PIXEL)
  133.     except Exception as ex:
  134.         print(ex.message)
  135.         return
  136.    
  137.     pwi4.mount_model_add_point(match["ra_j2000_hours"], match["dec_j2000_degrees"])
  138.     print("Added point")
  139.  
  140. if __name__ == "__main__":
  141.     main()

Raw Paste


Login or Register to edit or fork this paste. It's free.