def test_pretty_time(): t0 = '2016-08-13 10:00:00' os.environ['POCSTIME'] = t0 t1 = current_time(pretty=True) assert t1 == t0 # This will increment one second - see docs t2 = current_time(flatten=True) assert t2 != t0 assert t2 == '20160813T100001' # This will increment one second - see docs t3 = current_time(datetime=True) assert t3 == dt(2016, 8, 13, 10, 0, 2)
def send_message(self, topic, message): """ Responsible for actually sending message across a topic Args: topic(str): Name of topic to send on. The name must match topic_name_re. message: Message to be sent (a string or a dict). """ if not isinstance(topic, str): raise ValueError('Topic name must be a string') elif not self.topic_name_re.fullmatch(topic): raise ValueError('Topic name ("{}") is not valid'.format(topic)) if isinstance(message, str): message = { 'message': message, 'timestamp': current_time(pretty=True), } elif isinstance(message, dict): message = self.scrub_message(message) else: raise ValueError('Message value must be a string or dict') msg_object = dumps(message, skipkeys=True) full_message = '{} {}'.format(topic, msg_object) if topic == 'PANCHAT': self.logger.info("{} {}".format(topic, message['message'])) # Send the message self.socket.send_string(full_message, flags=zmq.NOBLOCK)
def test_countdown_timer_bad_input(): with pytest.raises(ValueError): assert CountdownTimer('d') with pytest.raises(ValueError): assert CountdownTimer(current_time()) with pytest.raises(AssertionError): assert CountdownTimer(-1)
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 data = hdu[0].data data = focus_utils.mask_saturated(data) wcs = WCS(header) if not title: field = header.get('FIELD', 'Unknown field') exptime = header.get('EXPTIME', 'Unknown exptime') filter_type = header.get('FILTER', 'Unknown filter') 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, exptime, filter_type, date_time) norm = ImageNormalize(interval=PercentileInterval(clip_percent), stretch=LogStretch()) fig = Figure() FigureCanvas(fig) fig.set_size_inches(*figsize) fig.dpi = dpi if wcs.is_celestial: ax = fig.add_subplot(1, 1, 1, projection=wcs) ax.coords.grid(True, color='white', ls='-', alpha=alpha) ra_axis = ax.coords['ra'] ra_axis.set_axislabel('Right Ascension') ra_axis.set_major_formatter('hh:mm') ra_axis.set_ticks(number=number_ticks, color='white', exclude_overlapping=True) dec_axis = ax.coords['dec'] dec_axis.set_axislabel('Declination') dec_axis.set_major_formatter('dd:mm') dec_axis.set_ticks(number=number_ticks, color='white', exclude_overlapping=True) else: ax = fig.add_subplot(111) ax.grid(True, color='white', ls='-', alpha=alpha) ax.set_xlabel('X / pixels') ax.set_ylabel('Y / pixels') im = ax.imshow(data, norm=norm, cmap=palette, origin='lower') fig.colorbar(im) fig.suptitle(title) new_filename = fname.replace('.fits', '.jpg') fig.savefig(new_filename, bbox_inches='tight') # explicitly close and delete figure fig.clf() del fig return new_filename
def create_storage_obj(collection, data, obj_id=None): """Returns the object to be stored in the database""" obj = dict(data=data, type=collection, date=current_time(datetime=True)) if obj_id: obj['_id'] = obj_id return obj