Exemplo n.º 1
0
    def _fitFocus(self, positions, minmax=None):

        focuser = self.getFocuser()
        initial_position = focuser.getPosition()

        self.log.debug("Initial focus position: %04d" % initial_position)

        fwhm = []
        valid_positions = []

        for i, position in enumerate(positions):

            self.log.debug("Moving focuser to %d" % int(position))

            focuser.moveTo(position)

            frame_path, frame = self._takeImage()
            stars = self._findStars(frame_path)
            star = self._findBrighterStar(stars)

            star["CHIMERA_FLAGS"] = green("OK")

            if abs(star["FWHM_IMAGE"] - 4.18) <= 0.02:
                self.log.debug(
                    "Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f"
                    % (star["XWIN_IMAGE"], star["YWIN_IMAGE"], star["FWHM_IMAGE"], star["FLUX_BEST"])
                )
                star["CHIMERA_FLAGS"] = red("Ignoring, SExtractor FWHM magic number.")
            elif star["FWHM_IMAGE"] <= minmax[0] or star["FWHM_IMAGE"] >= minmax[1]:
                self.log.debug(
                    "Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f"
                    % (star["XWIN_IMAGE"], star["YWIN_IMAGE"], star["FWHM_IMAGE"], star["FLUX_BEST"])
                )
                star["CHIMERA_FLAGS"] = red("Ignoring, FWHM above/below minmax limits.")
            else:
                self.log.debug(
                    "Adding star to curve. (X,Y)=(%d,%d) FWHM=%.3f FLUX=%.3f"
                    % (star["XWIN_IMAGE"], star["YWIN_IMAGE"], star["FWHM_IMAGE"], star["FLUX_BEST"])
                )
                fwhm.append(star["FWHM_IMAGE"])
                valid_positions.append(position)

            self.stepComplete(position, star, frame_path)

        # fit a parabola to the points and save parameters
        try:
            if minmax:
                self.log.debug("Minmax filtering FWHM (%.3f,%.3f)" % minmax)

            try:
                temp = focuser.getTemperature()
            except NotImplementedError:
                temp = None
            fit = FocusFit.fit(N.array(valid_positions), N.array(fwhm), temperature=temp, minmax=minmax)
        except Exception, e:
            focuser.moveTo(initial_position)

            raise FocusNotFoundException(
                "Error trying to fit a focus curve. " "Leaving focuser at %04d" % initial_position
            )
Exemplo n.º 2
0
    def _fitFocus(self, positions, minmax=None):

        focuser = self.getFocuser()
        initial_position = focuser.getPosition()

        self.log.debug("Initial focus position: %04d" % initial_position)

        fwhm = []
        valid_positions = []

        for i, position in enumerate(positions):

            self.log.debug("Moving focuser to %d" % int(position))

            focuser.moveTo(position)

            frame_path, frame = self._takeImage()
            stars = self._findStars(frame_path)
            star = self._findBrighterStar(stars)

            star["CHIMERA_FLAGS"] = green("OK")

            if abs(star["FWHM_IMAGE"] - 4.18) <= 0.02:
                self.log.debug("Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f" % (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                                                                                                     star["FWHM_IMAGE"], star["FLUX_BEST"]))
                star["CHIMERA_FLAGS"] = red("Ignoring, SExtractor FWHM magic number.")
            elif star["FWHM_IMAGE"] <= minmax[0] or star["FWHM_IMAGE"] >= minmax[1]:
                self.log.debug("Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f" % (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                                                                                                     star["FWHM_IMAGE"], star["FLUX_BEST"]))
                star["CHIMERA_FLAGS"] = red("Ignoring, FWHM above/below minmax limits.")
            else:
                self.log.debug("Adding star to curve. (X,Y)=(%d,%d) FWHM=%.3f FLUX=%.3f" % (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                                                                                            star["FWHM_IMAGE"], star["FLUX_BEST"]))
                fwhm.append(star["FWHM_IMAGE"])
                valid_positions.append(position)

            self.stepComplete(position, star, frame_path)

        # fit a parabola to the points and save parameters
        try:
            if minmax:
                self.log.debug("Minmax filtering FWHM (%.3f,%.3f)" % minmax)

            try:
                temp = focuser.getTemperature()
            except NotImplementedError:
                temp = None
            fit = FocusFit.fit(N.array(valid_positions), N.array(fwhm), temperature=temp, minmax=minmax)
        except Exception, e:
            focuser.moveTo(initial_position)

            raise FocusNotFoundException("Error trying to fit a focus curve. "
                                         "Leaving focuser at %04d" % initial_position)
Exemplo n.º 3
0
    def start(self, options):

        self.initLy()

        fp = self.initDB()

        while True:

            data = self.getData()
            self.out(green('[MoniT]:') + ' %s %s %s %s %s' % (data[0], data[1], data[2], data[3], data[4]))
            fp.write('%s %s %s %s %s\n' % (data[0], data[1], data[2], data[3], data[4]))
            fp.flush()

            try:
                self.streamData(data)
            except:
                self.err(red('[MoniT]:') + ' Failed to stream the data. Trying to open streams again.')
                try:
                    self.openStreams()
                    self.streamData(data)
                except:
                    fp.close()
                    self.exit(red('[MoniT]:') + ' Failed to stream the data. Giving up...', -1)
                finally:
                    pass
            finally:
                time.sleep(options.utime)

            # Check if date changed. If yes, start new streams
            if self.year != data[0].year or self.month != data[0].month or self.day != data[0].day:
                fp.close()
                self.closeStreams()
                self.initLy()
                fp = self.initDB()

        return 0
Exemplo n.º 4
0
    def _fitFocus(self, positions, minmax=None):

        focuser = self.getFocuser()
        initial_position = focuser.getPosition()

        self.log.debug("Initial focus position: %04d" % initial_position)

        fwhm = []
        valid_positions = []

        for i, position in enumerate(positions):

            # check for abort signal
            if self._abort.isSet():
                self.log.warning("Aborting autofocus! Moving focuser to initial position: %04d" % initial_position)
                focuser.moveTo(initial_position)
                raise ProgramExecutionAborted()

            aux_fwhm = []

            self.log.debug("Moving focuser to %d" % int(position))

            focuser.moveTo(position)

            frame_path, frame = self._takeImage(position)
            stars = self._findStars(frame_path)
            star = dict()

            star["CHIMERA_FLAGS"] = green("OK")

            for s in stars:  # FIXME: remove this for.
                if s['FLAGS'] == 0:  # and # s["FWHM_IMAGE"] > minmax[0] or s["FWHM_IMAGE"] < minmax[1]:
                    aux_fwhm.append(s['FWHM_IMAGE'])

            if len(aux_fwhm) < 5:
                star["CHIMERA_FLAGS"] = red("Ignoring, Less than 5 good stars found.")
            else:
                mode = stats.mode(aux_fwhm).mode[0]
                mean = N.mean(aux_fwhm)
                median = N.median(aux_fwhm)
                # std = N.std(aux_fwhm)
                # logstr = "Adding point to curve. FWHM mode=%.3f STDEV=%.3f " % (mode, std)
                logstr = "Adding point to curve. FWHM mode=%.3f " % mode
                logstr += " mean=%.3f " % mean
                logstr += " median=%.3f " % median
                try:
                    temp = focuser.getTemperature()
                except (IOError, NotImplementedError) as e:
                    temp = None
                if temp:
                    logstr += "TEMP=%.3f" % temp
                self.log.debug(logstr)
                star["FWHM"] = median
                fwhm.append(median)
                valid_positions.append(position)

            self.stepComplete(position, star, frame_path)

        # fit a parabola to the points and save parameters
        try:
            if minmax:
                self.log.debug("Minmax filtering FWHM (%.3f,%.3f)" % minmax)

            fit = FocusFit.fit(N.array(valid_positions), N.array(fwhm), temperature=temp, minmax=minmax,
                               filter=self.getFilter().getFilter())
        except Exception, e:
            focuser.moveTo(initial_position)

            raise FocusNotFoundException("Error trying to fit a focus curve. "
                                         "Leaving focuser at %04d" % initial_position)
Exemplo n.º 5
0
 def __str__(self):
     return red(str(self.msg))
Exemplo n.º 6
0
    def _fitFocus(self, positions, minmax=None):

        focuser = self.getFocuser()
        initial_position = focuser.getPosition()

        self.log.debug("Initial focus position: %04d" % initial_position)

        fwhm = []
        valid_positions = []

        for i, position in enumerate(positions):

            self.log.debug("Moving focuser to %d" % int(position))

            focuser.moveTo(position)

            frame_path, frame = self._takeImage()
            stars = self._findStars(frame_path)
            star = self._findBrighterStar(stars)

            star["CHIMERA_FLAGS"] = green("OK")

            if abs(star["FWHM_IMAGE"] - 4.18) <= 0.02:
                self.log.debug(
                    "Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f"
                    % (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                       star["FWHM_IMAGE"], star["FLUX_BEST"]))
                star["CHIMERA_FLAGS"] = red(
                    "Ignoring, SExtractor FWHM magic number.")
            elif star["FWHM_IMAGE"] <= minmax[0] or star[
                    "FWHM_IMAGE"] >= minmax[1]:
                self.log.debug(
                    "Ignoring star at (X,Y)=(%d,%d) FWHM magic number=%.3f, FLUX=%.3f"
                    % (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                       star["FWHM_IMAGE"], star["FLUX_BEST"]))
                star["CHIMERA_FLAGS"] = red(
                    "Ignoring, FWHM above/below minmax limits.")
            else:
                self.log.debug(
                    "Adding star to curve. (X,Y)=(%d,%d) FWHM=%.3f FLUX=%.3f" %
                    (star["XWIN_IMAGE"], star["YWIN_IMAGE"],
                     star["FWHM_IMAGE"], star["FLUX_BEST"]))
                fwhm.append(star["FWHM_IMAGE"])
                valid_positions.append(position)

            self.stepComplete(position, star, frame_path)

        # fit a parabola to the points and save parameters
        try:
            if minmax:
                self.log.debug("Minmax filtering FWHM (%.3f,%.3f)" % minmax)

            try:
                temp = focuser.getTemperature()
            except NotImplementedError:
                temp = None
            fit = FocusFit.fit(N.array(valid_positions),
                               N.array(fwhm),
                               temperature=temp,
                               minmax=minmax)
        except Exception as e:
            focuser.moveTo(initial_position)

            raise FocusNotFoundException("Error trying to fit a focus curve. "
                                         "Leaving focuser at %04d" %
                                         initial_position)

        fit.plot(
            os.path.join(SYSTEM_CONFIG_DIRECTORY, self.currentRun,
                         "autofocus.plot.png"))
        fit.log(
            os.path.join(SYSTEM_CONFIG_DIRECTORY, self.currentRun,
                         "autofocus.plot.dat"))

        # leave focuser at best position
        try:
            if N.isnan(fit.best_focus[0]):
                raise FocusNotFoundException(
                    "Focus fitting error: fitting do not converges (NaN result). See logs for more info."
                )

            self.log.debug("Best focus position: %.3f" % fit.best_focus[0])
            focuser.moveTo(int(fit.best_focus[0]))
        except InvalidFocusPositionException as e:
            focuser.moveTo(initial_position)
            raise FocusNotFoundException(
                "Best guess was %s, but could not move the focuser.\n"
                "%s\n"
                "Returning to initial position." %
                (str(fit.best_focus[0]), str(e)))

        return fit