PYTHON   18

platesolve py

Guest on 15th August 2022 12:38:48 PM

  1. import os.path
  2. import platform
  3. from subprocess import Popen, PIPE
  4. import tempfile
  5.  
  6. # Point this to the location of the "ps3cli.exe" executable
  7. PS3CLI_EXE = os.path.expanduser("~/ps3cli/ps3cli.exe")
  8.  
  9. # For testing purposes...
  10. #PS3CLI_EXE = r"C:\Users\kmi\Desktop\Planewave work\Code\PWGit\PWCode\ps3cli\bin\Debug\ps3cli.exe"
  11.  
  12.  
  13. # Set this to the path where the PlateSolve catalogs are located.
  14. # The directory specified here should contain "UC4" and "Orca" subdirectories.
  15. # If this is None, we will try to use the default catalog location
  16. PS3_CATALOG = None
  17.  
  18. def is_linux():
  19.     return platform.system() == "Linux"
  20.  
  21. def get_default_catalog_location():
  22.     if is_linux():
  23.         return os.path.expanduser("~/Kepler")
  24.     else:
  25.         return os.path.expanduser("~\\Documents\\Kepler")
  26.  
  27.  
  28. def platesolve(image_file, arcsec_per_pixel):
  29.     stdout_destination = None  # Replace with PIPE if we want to capture the output rather than displaying on the console
  30.  
  31.     output_file_path = os.path.join(tempfile.gettempdir(), "ps3cli_results.txt")
  32.  
  33.     if PS3_CATALOG is None:
  34.         catalog_path = get_default_catalog_location()
  35.     else:
  36.         catalog_path = PS3_CATALOG
  37.  
  38.     args = [
  39.         PS3CLI_EXE,
  40.         image_file,
  41.         str(arcsec_per_pixel),
  42.         output_file_path,
  43.         catalog_path
  44.     ]
  45.  
  46.     if is_linux():
  47.         # Linux systems need to run ps3cli via the mono runtime,
  48.         # so add that to the beginning of the command/argument list
  49.         args.insert(0, "mono")
  50.    
  51.     process = Popen(
  52.             args,
  53.             stdout=stdout_destination,
  54.             stderr=PIPE
  55.             )
  56.  
  57.     (stdout, stderr) = process.communicate()  # Obtain stdout and stderr output from the wcs tool
  58.     exit_code = process.wait() # Wait for process to complete and obtain the exit code
  59.  
  60.     if exit_code != 0:
  61.         raise Exception("Error finding solution.\n" +
  62.                         "Exit code: " + str(exit_code) + "\n" +
  63.                         "Error output: " + stderr)
  64.    
  65.     return parse_platesolve_output(output_file_path)
  66.  
  67. def parse_platesolve_output(output_file):
  68.     f = open(output_file)
  69.  
  70.     results = {}
  71.  
  72.     for line in f.readlines():
  73.         line = line.strip()
  74.         if line == "":
  75.             continue
  76.  
  77.         fields = line.split("=")
  78.         if len(fields) != 2:
  79.             continue
  80.        
  81.         keyword, value = fields
  82.  
  83.         results[keyword] = float(value)
  84.    
  85.     return results

Raw Paste


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