<commit_before>import os
import subprocess
from threading import Event
from threading import Timer

from astropy import units as u
from panoptes.pocs.camera.gphoto import AbstractGPhotoCamera
from panoptes.utils import error
from panoptes.utils.images import cr2 as cr2_utils
from panoptes.utils.time import CountdownTimer
from panoptes.utils.time import current_time
from panoptes.utils.utils import get_quantity_value


class Camera(AbstractGPhotoCamera):

    def __init__(self, *args, **kwargs):
        kwargs['readout_time'] = 6.0
        kwargs['file_extension'] = 'cr2'
        super().__init__(*args, **kwargs)

        # Hold on to the exposure process for polling.
        self._exposure_proc = None

        self.logger.debug("Connecting GPhoto2 camera")
        self.connect()
        self.logger.debug(f"{self.name} connected")

    @property
    def bit_depth(self):
        return 12 * u.bit

    @property
    def egain(self):
        return 1.5

    def connect(self):
        """Connect to Canon DSLR

        Gets the serial number from the camera and sets various settings
        """
        self.logger.debug('Connecting to camera')

        # Get serial number
        _serial_number = self.get_property('serialnumber')
        if not _serial_number:
            raise error.CameraNotFound(f"Camera not responding: {self}")

        self._serial_number = _serial_number

        # Properties to be set upon init.
        prop2index = {
            '/main/actions/viewfinder': 1,  # Screen off
            '/main/capturesettings/autoexposuremode': 3,  # 3 - Manual; 4 - Bulb
            '/main/capturesettings/continuousaf': 0,  # No auto-focus
            '/main/capturesettings/drivemode': 0,  # Single exposure
            '/main/capturesettings/focusmode': 0,  # Manual (don't try to focus)
            '/main/capturesettings/shutterspeed': 0,  # Bulb
            '/main/imgsettings/imageformat': 9,  # RAW
            '/main/imgsettings/imageformatcf': 9,  # RAW
            '/main/imgsettings/imageformatsd': 9,  # RAW
            '/main/imgsettings/iso': 1,  # ISO 100
            '/main/settings/autopoweroff': 0,  # Don't power off
            '/main/settings/capturetarget': 0,  # Capture to RAM, for download
            '/main/settings/datetime': 'now',  # Current datetime
            '/main/settings/datetimeutc': 'now',  # Current datetime
            '/main/settings/reviewtime': 0,  # Screen off after taking pictures
        }

        owner_name = 'Project PANOPTES'
        artist_name = self.get_config('pan_id', default=owner_name)
        copy_right = f'{owner_name} {current_time().datetime:%Y}'

        prop2value = {
            '/main/settings/artist': artist_name,
            '/main/settings/copyright': copy_right,
            '/main/settings/ownername': owner_name,
        }

        self.set_properties(prop2index, prop2value)
        self._connected = True

    def take_observation(self, observation, headers=None, filename=None, *args, **kwargs):
        """Take an observation

        Gathers various header information, sets the file path, and calls
        `take_exposure`. Also creates a `threading.Event` object and a
        `threading.Timer` object. The timer calls `process_exposure` after the
        set amount of time is expired (`observation.exptime + self.readout_time`).

        Note:
            If a `filename` is passed in it can either be a full path that includes
            the extension, or the basename of the file, in which case the directory
            path and extension will be added to the `filename` for output

        Args:
            observation (~pocs.scheduler.observation.Observation): Object
                describing the observation
            headers (dict): Header data to be saved along with the file
            filename (str, optional): Filename for saving, defaults to ISOT time stamp
            **kwargs (dict): Optional keyword arguments (`exptime`)

        Returns:
            threading.Event: An event to be set when the image is done processing
        """
        # To be used for marking when exposure is complete (see `process_exposure`)
        observation_event = Event()<commit_msg>Set DSLR camera time correctly (#892)<commit_after>import os
import subprocess
from threading import Event
from threading import Timer

from astropy import units as u
from panoptes.pocs.camera.gphoto import AbstractGPhotoCamera
from panoptes.utils import error
from panoptes.utils.images import cr2 as cr2_utils
from panoptes.utils.time import CountdownTimer
from panoptes.utils.time import current_time
from panoptes.utils.utils import get_quantity_value


class Camera(AbstractGPhotoCamera):

    def __init__(self, *args, **kwargs):
        kwargs['readout_time'] = 6.0
        kwargs['file_extension'] = 'cr2'
        super().__init__(*args, **kwargs)

        # Hold on to the exposure process for polling.
        self._exposure_proc = None

        self.logger.debug("Connecting GPhoto2 camera")
        self.connect()
        self.logger.debug(f"{self.name} connected")

    @property
    def bit_depth(self):
        return 12 * u.bit

    @property
    def egain(self):
        return 1.5

    def connect(self):
        """Connect to Canon DSLR

        Gets the serial number from the camera and sets various settings
        """
        self.logger.debug('Connecting to camera')

        # Get serial number
        _serial_number = self.get_property('serialnumber')
        if not _serial_number:
            raise error.CameraNotFound(f"Camera not responding: {self}")

        self._serial_number = _serial_number

        # Properties to be set upon init.
        prop2index = {
            '/main/actions/viewfinder': 1,  # Screen off
            '/main/capturesettings/autoexposuremode': 3,  # 3 - Manual; 4 - Bulb
            '/main/capturesettings/continuousaf': 0,  # No auto-focus
            '/main/capturesettings/drivemode': 0,  # Single exposure
            '/main/capturesettings/focusmode': 0,  # Manual (don't try to focus)
            '/main/imgsettings/imageformat': 9,  # RAW
            '/main/imgsettings/imageformatcf': 9,  # RAW
            '/main/imgsettings/imageformatsd': 9,  # RAW
            '/main/settings/autopoweroff': 0,  # Don't power off
            '/main/settings/capturetarget': 0,  # Capture to RAM, for download
            '/main/settings/reviewtime': 0,  # Screen off after taking pictures
            '/main/imgsettings/iso': 1,  # ISO 100
            '/main/capturesettings/shutterspeed': 0,  # Bulb
        }

        owner_name = 'Project PANOPTES'
        artist_name = self.get_config('pan_id', default=owner_name)
        copy_right = f'{owner_name} {current_time().datetime:%Y}'

        prop2value = {
            '/main/settings/artist': artist_name,
            '/main/settings/copyright': copy_right,
            '/main/settings/ownername': owner_name,
            # Current UTC datetime in seconds since epoch.U
            '/main/settings/datetimeutc': f'{current_time(datetime=True):%s}',
        }

        self.set_properties(prop2index, prop2value)
        self._connected = True

    def take_observation(self, observation, headers=None, filename=None, *args, **kwargs):
        """Take an observation

        Gathers various header information, sets the file path, and calls
        `take_exposure`. Also creates a `threading.Event` object and a
        `threading.Timer` object. The timer calls `process_exposure` after the
        set amount of time is expired (`observation.exptime + self.readout_time`).

        Note:
            If a `filename` is passed in it can either be a full path that includes
            the extension, or the basename of the file, in which case the directory
            path and extension will be added to the `filename` for output

        Args:
            observation (~pocs.scheduler.observation.Observation): Object
                describing the observation
            headers (dict): Header data to be saved along with the file
            filename (str, optional): Filename for saving, defaults to ISOT time stamp
            **kwargs (dict): Optional keyword arguments (`exptime`)

        Returns:
            threading.Event: An event to be set when the image is done processing
        """
        # To be used for marking when exposure is complete (see `process_exposure`)
        observation_event = Event()
{'fraction_non_alphanumeric': 0.09232045508414316, 'fraction_numerical': 0.007940270206210003, 'mean_word_length': 2.4671322925225967, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7137349', 'n_tokens_mistral': 2406, 'n_tokens_neox': 2251, 'n_words': 721}