def read_exif(fname, exiftool='exiftool'): # pragma: no cover """ Read the EXIF information Gets the EXIF information using exiftool Note: Assumes the `exiftool` is installed Args: fname {str} -- Name of file (CR2) to read Keyword Args: exiftool {str} -- Location of exiftool (default: {'/usr/bin/exiftool'}) Returns: dict -- Dictonary of EXIF information """ assert os.path.exists(fname), warn("File does not exist: {}".format(fname)) exif = {} try: # Build the command for this file command = '{} -j {}'.format(exiftool, fname) cmd_list = command.split() # Run the command exif = loads(subprocess.check_output(cmd_list).decode('utf-8')) except subprocess.CalledProcessError as err: raise error.InvalidSystemCommand( msg="File: {} \n err: {}".format(fname, err)) return exif[0]
def solve_field(fname, timeout=15, solve_opts=[], **kwargs): """ Plate solves an image. Args: fname(str, required): Filename to solve in either .cr2 or .fits extension. timeout(int, optional): Timeout for the solve-field command, defaults to 60 seconds. solve_opts(list, optional): List of options for solve-field. verbose(bool, optional): Show output, defaults to False. """ verbose = kwargs.get('verbose', False) if verbose: print("Entering solve_field") if fname.endswith('cr2'): if verbose: print("Converting cr2 to FITS") fname = cr2_to_fits(fname, **kwargs) if verbose: print("Solved filename: ", fname) solve_field_script = "{}/scripts/solve_field.sh".format(os.getenv('POCS'), '/var/panoptes/POCS') if not os.path.exists(solve_field_script): raise error.InvalidSystemCommand("Can't find solve-field: {}".format(solve_field_script)) # Add the options for solving the field if solve_opts: options = solve_opts else: options = [ '--guess-scale', '--cpulimit', str(timeout), '--no-verify', '--no-plots', '--crpix-center', '--downsample', '4', ] if kwargs.get('clobber', True): options.append('--overwrite') if kwargs.get('skip_solved', True): options.append('--skip-solved') if 'ra' in kwargs: options.append('--ra') options.append(str(kwargs.get('ra'))) if 'dec' in kwargs: options.append('--dec') options.append(str(kwargs.get('dec'))) if 'radius' in kwargs: options.append('--radius') options.append(str(kwargs.get('radius'))) if os.getenv('PANTEMP'): options.append('--temp-dir') options.append(os.getenv('PANTEMP')) cmd = [solve_field_script, ' '.join(options), fname] if verbose: print("Cmd: ", cmd) try: proc = subprocess.Popen(cmd, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) except OSError as e: raise error.InvalidCommand("Can't send command to solve_field.sh. {} \t {}".format(e, cmd)) except ValueError as e: raise error.InvalidCommand("Bad parameters to solve_field.sh. {} \t {}".format(e, cmd)) except Exception as e: raise error.PanError("Timeout on plate solving: {}".format(e)) return proc
def cr2_to_pgm(cr2_fname, pgm_fname=None, dcraw='dcraw', clobber=True, **kwargs): # pragma: no cover """ Convert CR2 file to PGM Converts a raw Canon CR2 file to a netpbm PGM file via `dcraw`. Assumes `dcraw` is installed on the system Note: This is a blocking call Arguments: cr2_fname {str} -- Name of CR2 file to convert **kwargs {dict} -- Additional keywords to pass to script Keyword Arguments: pgm_fname {str} -- Name of PGM file to output, if None (default) then use same name as CR2 (default: {None}) dcraw {str} -- Path to installed `dcraw` (default: {'dcraw'}) clobber {bool} -- A bool indicating if existing PGM should be clobbered (default: {True}) Returns: str -- Filename of PGM that was created """ assert subprocess.call('dcraw', stdout=subprocess.PIPE),\ "could not execute dcraw in path: {}".format(dcraw) assert os.path.exists(cr2_fname), "cr2 file does not exist at {}".format( cr2_fname) verbose = kwargs.get('verbose', False) if pgm_fname is None: pgm_fname = cr2_fname.replace('.cr2', '.pgm') if os.path.exists(pgm_fname) and not clobber: if verbose: print("PGM file exists, returning existing file: {}".format( pgm_fname)) else: try: # Build the command for this file command = '{} -t 0 -D -4 {}'.format(dcraw, cr2_fname) cmd_list = command.split() if verbose: print("PGM Conversion command: \n {}".format(cmd_list)) # Run the command if subprocess.check_call(cmd_list) == 0: if verbose: print("PGM Conversion command successful") except subprocess.CalledProcessError as err: raise error.InvalidSystemCommand( msg="File: {} \n err: {}".format(cr2_fname, err)) return pgm_fname