def _gen_image(in_science_fqn,
                geometry,
                save_fqn,
                scope_param,
                rotate_param,
                zoom_param='to fit',
                pan_param='',
                mosaic_param='',
                mode_param='-mode none',
                scale_param=''):
     # 20-03-20 - seb - always use iraf - do not trust wcs coming from the
     # data acquisition. A proper one needs processing which is often not
     # done on observations.
     cmd = f'xvfb-run -a ds9 {mosaic_param} {in_science_fqn} ' \
           f'{pan_param} ' \
           f'-geometry {geometry} ' \
           f'{rotate_param} ' \
           f'-scale squared ' \
           f'-scale mode {scale_param} ' \
           f'-scale scope {scope_param} ' \
           f'-scale datasec yes ' \
           f'-invert ' \
           f'{mode_param} ' \
           f'-view colorbar no ' \
           f'-zoom {zoom_param} ' \
           f'-saveimage jpeg {save_fqn} ' \
           f'-quit'
     mc.exec_cmd(cmd, timeout=900)  # wait 15 minutes till killing
     count = 0
     if os.path.exists(save_fqn):
         count = 1
     return count
 def _exec_cmd_chdir(self, temp_file, cmd):
     orig_dir = os.getcwd()
     try:
         os.chdir(self._working_dir)
         if os.path.exists(temp_file):
             os.unlink(temp_file)
         mc.exec_cmd(cmd)
     finally:
         os.chdir(orig_dir)
 def _write_files_to_disk(self, wln, flux, x_label, title, thumb_fqn,
                          preview_fqn):
     pylab.clf()
     pylab.grid(True)
     pylab.plot(wln, flux, color='k')
     pylab.xlabel(x_label, color='k')
     pylab.ylabel('Intensity', color='k')
     pylab.xlim(wln.min(), wln.max())
     pylab.ylim(0, flux.max())
     pylab.title(title, color='k', fontweight='bold')
     temp_fn = 'temp.png'
     pylab.savefig(temp_fn, format='png')
     mc.exec_cmd(f'convert -resize 256x256 {temp_fn} {thumb_fqn}')
     mc.exec_cmd(f'convert -resize 1024x1024 {temp_fn} {preview_fqn}')
     self.add_to_delete(f'{self._working_dir}/{temp_fn}')
def _do_prev(file_id, working_dir, plane, cadc_client, stream):
    gem_name = GemName('{}.jpg'.format(file_id))
    preview = gem_name.prev
    preview_fqn = os.path.join(working_dir, preview)
    thumb = gem_name.thumb
    thumb_fqn = os.path.join(working_dir, thumb)

    if not os.access(preview_fqn, 0):
        mc.data_get(cadc_client, working_dir, preview, ARCHIVE)

    if os.access(thumb_fqn, 0):
        os.remove(thumb_fqn)
    convert_cmd = 'convert -resize 256x256 {} {}'.format(
        preview_fqn, thumb_fqn)
    mc.exec_cmd(convert_cmd)

    thumb_uri = gem_name.thumb_uri
    _augment(plane, thumb_uri, thumb_fqn, ProductType.THUMBNAIL)
    if cadc_client is not None:
        mc.data_put(cadc_client, working_dir, thumb, ARCHIVE, stream)
    return 1
def test_exec_cmd():
    test_cmd = 'ls /abc'
    with pytest.raises(mc.CadcException):
        mc.exec_cmd(test_cmd)
    def _do_sci(self, hdu_list, header, storage_name, science_fqn, preview_fqn,
                thumb_fqn):
        logging.debug(f'Do science preview augmentation with {science_fqn}')
        count = 0
        detector = header.get('DETECTOR')
        instrument = header.get('INSTRUME')
        if detector in [
                'SITe-4', 'UBC-1', 'SITe-2', 'SITe-5', 'E2V-1', 'E2V-4'
        ]:
            # unprocessed CCD data
            if detector == 'SITe-4':
                axis = 'NAXIS2'
                naxis1 = mc.to_int(header.get(axis))
                xc = naxis1 / 2
                xs = 512
                xoffset = xc - xs / 2
                rotate = '90.0'
                geometry = '256x' + str(xs) + '+1+' + str(xoffset)
                resize1 = 'x1024'
                resize2 = 'x256'
            else:
                axis = 'NAXIS1'
                naxis1 = mc.to_int(header.get(axis))
                xc = naxis1 / 2
                xs = 512
                xoffset = xc - xs / 2
                rotate = '0.0'
                geometry = str(xs) + 'x256+' + str(xoffset) + '+1'
                resize1 = '1024x1024'
                resize2 = '256'

            if 'Imager' in instrument:
                mc.exec_cmd(f'convert -resize 1024x1024 '
                            f'-normalize -negate {science_fqn} {preview_fqn}')
                mc.exec_cmd(f'convert -resize 256x256 -normalize -negate '
                            f'{science_fqn} {thumb_fqn}')
            else:
                mc.exec_cmd(f'convert -resize {resize1} -rotate {rotate} '
                            f'-normalize -negate {science_fqn} {preview_fqn}')
                mc.exec_cmd(f'convert -crop {geometry} -resize {resize2} '
                            f'-rotate {rotate} -normalize '
                            f'-negate {science_fqn} {thumb_fqn}')
            count = 2
        else:
            # unprocessed RETICON spectrum
            object_type = header.get('OBJECT')
            if object_type is not None:
                naxis1 = header.get('NAXIS1')
                logging.info(f'Object: {object_type}')

                signal = hdu_list[0].data[0]
                baseline = hdu_list[0].data[1]
                flux = np.subtract(signal, baseline)
                wl = []
                for i in range(0, naxis1):
                    wl.append(i + 1)
                wln = np.array(wl)
                self._write_files_to_disk(
                    wln, flux, 'Pixel',
                    f'{storage_name.file_id}: {object_type}', thumb_fqn,
                    preview_fqn)
                count = 2
        return count
def test_exec_cmd():
    test_cmd = 'ls /abc'
    with pytest.raises(mc.CadcException):
        mc.exec_cmd(test_cmd)