From e2b094fb3f6bffaef34c43098fe3815866fc2128 Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Wed, 19 Sep 2018 22:05:14 +1000 Subject: [PATCH 1/6] * Consistency with `title` parameter. * No default title if nothing passed (making a title is very slow on a raspberry pi) * Sneaking a few cleanup items in --- pocs/utils/images/__init__.py | 60 ++++++++++++++++++----------------- scripts/cr2_to_jpg.sh | 14 ++++---- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/pocs/utils/images/__init__.py b/pocs/utils/images/__init__.py index b8b5c8232..21928d0d0 100644 --- a/pocs/utils/images/__init__.py +++ b/pocs/utils/images/__init__.py @@ -63,32 +63,31 @@ def crop_data(data, box_width=200, center=None, verbose=False): return center -def make_pretty_image(fname, timeout=15, **kwargs): # pragma: no cover - """ Make a pretty image +def make_pretty_image(fname, title=None, timeout=15, **kwargs): # pragma: no cover + """Make a pretty image. This will create a jpg file from either a CR2 (Canon) or FITS file. Notes: - See `$POCS/scripts/cr2_to_jpg.sh` for CR2 process + See `/scripts/cr2_to_jpg.sh` for CR2 process. Arguments: fname {str} -- Name of image file, may be either .fits or .cr2 - **kwargs {dict} -- Additional arguments to be passed to external script - - Keyword Arguments: - timeout {number} -- Process timeout (default: {15}) + title (None|str, optional): Title to be placed on image, default None. + timeout (int, optional): Timeout for conversion, default 15 seconds. + **kwargs {dict} -- Additional arguments to be passed to external script. Returns: - str -- Filename of image that was created + str -- Filename of image that was created. """ assert os.path.exists(fname),\ warn("File doesn't exist, can't make pretty: {}".format(fname)) if fname.endswith('.cr2'): - pretty_path = _make_pretty_from_cr2(fname, timeout=timeout, **kwargs) + pretty_path = _make_pretty_from_cr2(fname, title=title, timeout=timeout, **kwargs) elif fname.endswith('.fits'): - pretty_path = _make_pretty_from_fits(fname, **kwargs) + pretty_path = _make_pretty_from_fits(fname, title=title, **kwargs) else: warn("File must be a Canon CR2 or FITS file.") return None @@ -110,8 +109,14 @@ def make_pretty_image(fname, timeout=15, **kwargs): # pragma: no cover return None -def _make_pretty_from_fits( - fname=None, figsize=(10, 10 / 1.325), dpi=150, alpha=0.2, number=7, **kwargs): +def _make_pretty_from_fits(fname=None, + title=None, + figsize=(10, 10 / 1.325), + dpi=150, + alpha=0.2, + number_ticks=7, + clip_percent=99.9, + **kwargs): with open_fits(fname) as hdu: header = hdu[0].header @@ -119,16 +124,15 @@ def _make_pretty_from_fits( data = focus_utils.mask_saturated(data) wcs = WCS(header) - title = kwargs.get('title', header.get('FIELD', 'Unknown')) - exp_time = header.get('EXPTIME', 'Unknown') - - filter_type = header.get('FILTER', 'Unknown filter') - date_time = header.get('DATE-OBS', current_time(pretty=True)).replace('T', ' ', 1) + if not title: + field = header.get('FIELD', 'Unknown') + exp_time = header.get('EXPTIME', 'Unknown') + filter_type = header.get('FILTER', 'Unknown filter') + date_time = header.get('DATE-OBS', current_time(pretty=True)).replace('T', ' ', 1) - percent_value = kwargs.get('normalize_clip_percent', 99.9) + title = '{} ({}s {}) {}'.format(field, exp_time, filter_type, date_time) - title = '{} ({}s {}) {}'.format(title, exp_time, filter_type, date_time) - norm = ImageNormalize(interval=PercentileInterval(percent_value), stretch=LogStretch()) + norm = ImageNormalize(interval=PercentileInterval(clip_percent), stretch=LogStretch()) fig = plt.figure(figsize=figsize, dpi=dpi) @@ -140,7 +144,7 @@ def _make_pretty_from_fits( ra_axis.set_axislabel('Right Ascension') ra_axis.set_major_formatter('hh:mm') ra_axis.set_ticks( - number=number, + number=number_ticks, color='white', exclude_overlapping=True ) @@ -149,7 +153,7 @@ def _make_pretty_from_fits( dec_axis.set_axislabel('Declination') dec_axis.set_major_formatter('dd:mm') dec_axis.set_ticks( - number=number, + number=number_ticks, color='white', exclude_overlapping=True ) @@ -171,16 +175,14 @@ def _make_pretty_from_fits( return new_filename -def _make_pretty_from_cr2(fname, timeout=15, **kwargs): # pragma: no cover +def _make_pretty_from_cr2(fname, title=None, timeout=15, **kwargs): # pragma: no cover verbose = kwargs.get('verbose', False) - title = '{} {}'.format(kwargs.get('title', ''), current_time().isot) - - solve_field = "{}/scripts/cr2_to_jpg.sh".format(os.getenv('POCS')) - cmd = [solve_field, fname, title] + script_name = os.path.join(os.getenv('POCS'), 'scripts', 'cr2_to_jpg.sh') + cmd = [script_name, fname] - if kwargs.get('primary', False): - cmd.append('link') + if title: + cmd.append(title) if verbose: print(cmd) diff --git a/scripts/cr2_to_jpg.sh b/scripts/cr2_to_jpg.sh index 37401fd80..934653f2a 100755 --- a/scripts/cr2_to_jpg.sh +++ b/scripts/cr2_to_jpg.sh @@ -7,13 +7,13 @@ usage() { # If exiftool is present this merely extracts the thumbnail from # the CR2 file, otherwise use dcraw to create a jpeg. # -# If present the CAPTION is added as a caption to the jpeg. +# If present the TITLE is added as a title to the jpeg. ################################################## - $ $(basename $0) FILENAME [CAPTION] + $ $(basename $0) FILENAME [TITLE] Options: FILENAME Name of CR2 file that holds jpeg. - CAPTION Optional caption to be placed on jpeg. + TITLE Optional tilte to be placed on jpeg. Example: scripts/cr2_to_jpg.sh /var/panoptes/images/temp.cr2 \"M42 (Orion's Nebula)\" @@ -26,7 +26,7 @@ if [ $# -eq 0 ]; then fi FNAME=$1 -CAPTION="${2}" +TITLE="${2}" JPG="${FNAME%.cr2}.jpg" @@ -45,12 +45,12 @@ else fi fi -if [[ -n "$CAPTION" ]] +if [[ -n "$TITLE" ]] then - echo "Adding caption \"${CAPTION}\"" + echo "Adding title \"${TITLE}\"" # Make thumbnail from jpg. convert "${JPG}" -background black -fill red \ - -font ubuntu -pointsize 60 label:"${CAPTION}" \ + -font ubuntu -pointsize 60 label:"${TITLE}" \ -gravity South -append "${JPG}" fi From 35562b307d687b67955b2246080c3730a19d2c34 Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Wed, 19 Sep 2018 22:11:51 +1000 Subject: [PATCH 2/6] Cleanup of image title. --- pocs/camera/camera.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/pocs/camera/camera.py b/pocs/camera/camera.py index ed42bb888..4ab9ab4c4 100644 --- a/pocs/camera/camera.py +++ b/pocs/camera/camera.py @@ -208,7 +208,8 @@ def take_observation(self, observation, headers=None, filename=None, *args, **kw observation.exposure_list[image_id] = file_path # Process the exposure once readout is complete - t = Thread(target=self.process_exposure, args=(metadata, observation_event, exposure_event)) + t = Thread(target=self.process_exposure, args=( + metadata, observation_event, exposure_event)) t.name = '{}Thread'.format(self.name) t.start() @@ -238,12 +239,15 @@ def process_exposure(self, info, observation_event, exposure_event=None): image_id = info['image_id'] seq_id = info['sequence_id'] file_path = info['file_path'] - self.logger.debug("Processing {}".format(image_id)) + field_name = info['field_name'] + + image_title = '{} {} {}'.format(field_name, seq_id.split('_'), current_time(pretty=True)) + self.logger.debug("Processing {}".format(image_title)) try: self.logger.debug("Extracting pretty image") img_utils.make_pretty_image(file_path, - title=info['field_name'], + title=image_title, primary=info['is_primary']) except Exception as e: self.logger.warning('Problem with extracting pretty image: {}'.format(e)) From bcd86b5e3937de8a690cd2ba46d3a5bacddfd16d Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Wed, 19 Sep 2018 22:26:38 +1000 Subject: [PATCH 3/6] Fix for the format call --- pocs/camera/camera.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pocs/camera/camera.py b/pocs/camera/camera.py index 4ab9ab4c4..ce4df22ec 100644 --- a/pocs/camera/camera.py +++ b/pocs/camera/camera.py @@ -241,11 +241,12 @@ def process_exposure(self, info, observation_event, exposure_event=None): file_path = info['file_path'] field_name = info['field_name'] - image_title = '{} {} {}'.format(field_name, seq_id.split('_'), current_time(pretty=True)) - self.logger.debug("Processing {}".format(image_title)) + image_title = '{} {} {}'.format(field_name, + seq_id.replace('_', ' '), + current_time(pretty=True)) try: - self.logger.debug("Extracting pretty image") + self.logger.debug("Processing {}".format(image_title)) img_utils.make_pretty_image(file_path, title=image_title, primary=info['is_primary']) From 61d4c1ae9155e198cf57d6c7315ae539ed90ba29 Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Fri, 21 Sep 2018 09:30:39 +1000 Subject: [PATCH 4/6] Fix comment --- pocs/utils/images/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pocs/utils/images/__init__.py b/pocs/utils/images/__init__.py index 21928d0d0..9f1dd42bf 100644 --- a/pocs/utils/images/__init__.py +++ b/pocs/utils/images/__init__.py @@ -69,7 +69,7 @@ def make_pretty_image(fname, title=None, timeout=15, **kwargs): # pragma: no co This will create a jpg file from either a CR2 (Canon) or FITS file. Notes: - See `/scripts/cr2_to_jpg.sh` for CR2 process. + See `$POCS/scripts/cr2_to_jpg.sh` for CR2 process. Arguments: fname {str} -- Name of image file, may be either .fits or .cr2 From 6844e4fded9d5a7eb6a92c84dca7f8fa396084d0 Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Fri, 21 Sep 2018 09:37:43 +1000 Subject: [PATCH 5/6] If `DATE-OBS` is present, try to parse filename for date. If not, use now. --- pocs/utils/images/__init__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pocs/utils/images/__init__.py b/pocs/utils/images/__init__.py index 9f1dd42bf..a566eb150 100644 --- a/pocs/utils/images/__init__.py +++ b/pocs/utils/images/__init__.py @@ -12,6 +12,7 @@ from glob import glob from copy import copy +from dateutil import parser as date_parser from pocs.utils import current_time from pocs.utils import error @@ -125,10 +126,22 @@ def _make_pretty_from_fits(fname=None, wcs = WCS(header) if not title: - field = header.get('FIELD', 'Unknown') - exp_time = header.get('EXPTIME', 'Unknown') + field = header.get('FIELD', 'Unknown field') + exp_time = header.get('EXPTIME', 'Unknown exptime') filter_type = header.get('FILTER', 'Unknown filter') - date_time = header.get('DATE-OBS', current_time(pretty=True)).replace('T', ' ', 1) + + try: + date_time = header['DATE-OBS'] + except KeyError: + # If we don't have DATE-OBS, check filename for date + try: + basename = os.path.splitext(os.path.basename(fname))[0] + date_time = date_parser.parse(basename).isoformat() + except Exception: + # Otherwise use now + date_time = current_time(pretty=True) + + date_time = date_time.replace('T', ' ', 1) title = '{} ({}s {}) {}'.format(field, exp_time, filter_type, date_time) From 831b6bc58f7534147158285ee7fa950d907e0313 Mon Sep 17 00:00:00 2001 From: Wilfred Gee Date: Fri, 21 Sep 2018 09:43:11 +1000 Subject: [PATCH 6/6] Added exptime to title --- pocs/camera/camera.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pocs/camera/camera.py b/pocs/camera/camera.py index ce4df22ec..1956ba75b 100644 --- a/pocs/camera/camera.py +++ b/pocs/camera/camera.py @@ -239,11 +239,13 @@ def process_exposure(self, info, observation_event, exposure_event=None): image_id = info['image_id'] seq_id = info['sequence_id'] file_path = info['file_path'] + exptime = info['exp_time'] field_name = info['field_name'] - image_title = '{} {} {}'.format(field_name, - seq_id.replace('_', ' '), - current_time(pretty=True)) + image_title = '{} [{}] {} {}'.format(field_name, + exptime, + seq_id.replace('_', ' '), + current_time(pretty=True)) try: self.logger.debug("Processing {}".format(image_title))