def _takeImage(self): if self._debugging: try: frame = self._debug_images[self._debug_image] self._debug_image += 1 img = Image.fromFile(frame) srv = getImageServer(self.getManager()) return srv.register(img) except IndexError: raise ChimeraException("Cannot find debug images") self.imageRequest["filename"] = os.path.basename( ImageUtil.makeFilename("focus-$DATE")) cam = self.getCam() if self.filter: filter = self.getFilter() filter.setFilter(self.filter) frames = cam.expose(self.imageRequest) if frames: image = frames[0] image_path = image.filename() if not os.path.exists( image_path ): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ':\\' in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename( os.path.join( getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path))) t0 = time.time() self.log.debug('Downloading image from server to %s' % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException( 'Error downloading image %s from %s' % (image_path, image.http())) self.log.debug('Finished download. Took %3.2f seconds' % (time.time() - t0)) return image_path, image else: raise Exception("Could not take an image")
def _takeImage(self, exptime, filter): cam = self._getCam() if self["filterwheel"] is not None: fw = self._getFilterWheel() fw.setFilter(filter) self.log.debug("Start frame") frames = cam.expose(exptime=exptime, frames=1, shutter=Shutter.OPEN, filename=os.path.basename(ImageUtil.makeFilename("skyflat-$DATE"))) self.log.debug("End frame") if frames: image = frames[0] image_path = image.filename() if not os.path.exists(image_path): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ':\\' in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename(os.path.join(getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path))) t0 = time.time() self.log.debug('Downloading image from server to %s' % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException('Error downloading image %s from %s' % (image_path, image.http())) self.log.debug('Finished download. Took %3.2f seconds' % (time.time() - t0)) return image_path, image else: raise Exception("Could not take an image")
def _takeImage(self): if self._debugging: pass try: frame = self._debug_images[self._debug_image] self._debug_image += 1 img = Image.fromFile(frame) srv = getImageServer(self.getManager()) return srv.register(img) except IndexError: raise ChimeraException("Cannot find debug images") self.imageRequest["filename"] = os.path.join(SYSTEM_CONFIG_DIRECTORY, self.currentRun, "autoguider.fits") cam = self.getCam() if self.filter: filter = self.getFilter() filter.setFilter(self.filter) frame = cam.expose(self.imageRequest) if frame: return frame[0] else: raise Exception("Error taking image.")
def _takeImage(self): cam = self.getCam() if self.filter: filter = self.getFilter() filter.setFilter(self.filter) self.imageRequest["filename"] = os.path.basename(ImageUtil.makeFilename("align-$DATE")) frames = cam.expose(self.imageRequest) if frames: image = frames[0] image_path = image.filename() if not os.path.exists(image_path): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ':\\' in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename(os.path.join(getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path))) t0 = time.time() self.log.debug('Downloading image from server to %s' % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException('Error downloading image %s from %s' % (image_path, image.http())) self.log.debug('Finished download. Took %3.2f seconds' % (time.time() - t0)) image = Image.fromFile(image_path) return image #image_path #, image else: raise Exception("Could not take an image")
def _takeImage(self): if self._debugging: try: frame = self._debug_images[self._debug_image] self._debug_image += 1 img = Image.fromFile(frame) srv = getImageServer(self.getManager()) return srv.register(img) except IndexError: raise ChimeraException("Cannot find debug images") self.imageRequest["filename"] = os.path.join(SYSTEM_CONFIG_DIRECTORY, self.currentRun, "focus-$DATE.fits") cam = self.getCam() if self.filter: filter = self.getFilter() filter.setFilter(self.filter) frame = cam.expose(self.imageRequest) if frame: return frame[0] else: raise Exception("Error taking image.")
def _saveImage(self, imageRequest, imageData, extra): (mode, binning, top, left, width, height) = self._getReadoutModeInfo(imageRequest["binning"], imageRequest["window"]) binFactor = extra.get("binning_factor", 1.0) pix_w, pix_h = self.getPixelSize() focal_length = self["telescope_focal_length"] scale_x = binFactor * (((180 / pi) / focal_length) * (pix_w * 0.001)) scale_y = binFactor * (((180 / pi) / focal_length) * (pix_h * 0.001)) full_width, full_height = self.getPhysicalSize() CRPIX1 = ((int(full_width / 2.0)) - left) - 1 CRPIX2 = ((int(full_height / 2.0)) - top) - 1 t0 = time.time() img = Image.create(imageData, imageRequest) img += [ ('DATE-OBS', ImageUtil.formatDate( extra.get("frame_start_time", dt.datetime.utcnow())), 'Date exposure started'), ('CCD-TEMP', extra.get("frame_temperature", -275.0), 'CCD Temperature at Exposure Start [deg. C]'), ("EXPTIME", float(imageRequest['exptime']) or -1, "exposure time in seconds"), ('IMAGETYP', imageRequest['type'].strip(), 'Image type'), ('SHUTTER', str(imageRequest['shutter']), 'Requested shutter state'), ("CRPIX1", CRPIX1, "coordinate system reference pixel"), ("CRPIX2", CRPIX2, "coordinate system reference pixel"), ("CD1_1", scale_x, "transformation matrix element (1,1)"), ("CD1_2", 0.0, "transformation matrix element (1,2)"), ("CD2_1", 0.0, "transformation matrix element (2,1)"), ("CD2_2", scale_y, "transformation matrix element (2,2)"), ('CAMERA', str(self['camera_model']), 'Camera Model'), ('CCD', str(self['ccd_model']), 'CCD Model'), ('CCD_DIMX', self.getPhysicalSize()[0], 'CCD X Dimension Size'), ('CCD_DIMY', self.getPhysicalSize()[1], 'CCD Y Dimension Size'), ('CCDPXSZX', self.getPixelSize()[0], 'CCD X Pixel Size [micrometer]'), ('CCDPXSZY', self.getPixelSize()[1], 'CCD Y Pixel Size [micrometer]') ] # register image on ImageServer server = getImageServer(self.getManager()) proxy = server.register(img) # and finally compress the image img.compress(multiprocess=True) return proxy
def _takeImage(self): if self._debugging: try: frame = self._debug_images[self._debug_image] self._debug_image += 1 img = Image.fromFile(frame) srv = getImageServer(self.getManager()) return srv.register(img) except IndexError: raise ChimeraException("Cannot find debug images") self.imageRequest["filename"] = os.path.basename(ImageUtil.makeFilename("focus-$DATE")) cam = self.getCam() if self.filter: filter = self.getFilter() filter.setFilter(self.filter) frames = cam.expose(self.imageRequest) if frames: image = frames[0] image_path = image.filename() if not os.path.exists(image_path): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ":\\" in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename( os.path.join(getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path)) ) t0 = time.time() self.log.debug("Downloading image from server to %s" % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException("Error downloading image %s from %s" % (image_path, image.http())) self.log.debug("Finished download. Took %3.2f seconds" % (time.time() - t0)) return image_path, image else: raise Exception("Could not take an image")
def _saveImage(self, imageRequest, imageData, extra): (mode, binning, top, left, width, height) = self._getReadoutModeInfo( imageRequest["binning"], imageRequest["window"] ) binFactor = extra.get("binning_factor", 1.0) pix_w, pix_h = self.getPixelSize() focal_length = self["telescope_focal_length"] scale_x = binFactor * (((180 / pi) / focal_length) * (pix_w * 0.001)) scale_y = binFactor * (((180 / pi) / focal_length) * (pix_h * 0.001)) full_width, full_height = self.getPhysicalSize() CRPIX1 = ((int(full_width / 2.0)) - left) - 1 CRPIX2 = ((int(full_height / 2.0)) - top) - 1 t0 = time.time() img = Image.create(imageData, imageRequest) img += [ ( "DATE-OBS", ImageUtil.formatDate(extra.get("frame_start_time", dt.datetime.utcnow())), "Date exposure started", ), ("CCD-TEMP", extra.get("frame_temperature", -275.0), "CCD Temperature at Exposure Start [deg. C]"), ("EXPTIME", float(imageRequest["exptime"]) or -1, "exposure time in seconds"), ("IMAGETYP", imageRequest["type"].strip(), "Image type"), ("SHUTTER", str(imageRequest["shutter"]), "Requested shutter state"), ("CRPIX1", CRPIX1, "coordinate system reference pixel"), ("CRPIX2", CRPIX2, "coordinate system reference pixel"), ("CD1_1", scale_x, "transformation matrix element (1,1)"), ("CD1_2", 0.0, "transformation matrix element (1,2)"), ("CD2_1", 0.0, "transformation matrix element (2,1)"), ("CD2_2", scale_y, "transformation matrix element (2,2)"), ("CAMERA", str(self["camera_model"]), "Camera Model"), ("CCD", str(self["ccd_model"]), "CCD Model"), ("CCD_DIMX", self.getPhysicalSize()[0], "CCD X Dimension Size"), ("CCD_DIMY", self.getPhysicalSize()[1], "CCD Y Dimension Size"), ("CCDPXSZX", self.getPixelSize()[0], "CCD X Pixel Size [micrometer]"), ("CCDPXSZY", self.getPixelSize()[1], "CCD Y Pixel Size [micrometer]"), ] # register image on ImageServer server = getImageServer(self.getManager()) proxy = server.register(img) # and finally compress the image img.compress(multiprocess=True) return proxy
def _saveImage(self, imageRequest, imageData, extras=None): if extras is not None: self.extra_header_info = extras imageRequest.headers += self.getMetadata(imageRequest) img = Image.create(imageData, imageRequest) # register image on ImageServer server = getImageServer(self.getManager()) proxy = server.register(img) # and finally compress the image if asked if imageRequest['compress_format'].lower() != 'no': img.compress(format=imageRequest['compress_format'], multiprocess=True) return proxy
def _takeImage(self, exptime, filter, download=False): cam = self._getCam() if self["filterwheel"] is not None: fw = self._getFilterWheel() fw.setFilter(filter) self.log.debug("Start frame") request = ImageRequest(exptime=exptime, frames=1, shutter=Shutter.OPEN, filename=os.path.basename(ImageUtil.makeFilename("skyflat-$DATE-$TIME")), type='sky-flat') self.log.debug('ImageRequest: {}'.format(request)) frames = cam.expose(request) self.log.debug("End frame") # checking for aborting signal if self._abort.isSet(): self.log.warning('Aborting exposure!') raise ProgramExecutionAborted() if frames: image = frames[0] image_path = image.filename() if download and not os.path.exists(image_path): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ':\\' in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename(os.path.join(getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path))) t0 = time.time() self.log.debug('Downloading image from server to %s' % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException('Error downloading image %s from %s' % (image_path, image.http())) self.log.debug('Finished download. Took %3.2f seconds' % (time.time() - t0)) return image_path, image else: raise Exception("Could not take an image")
def _takeImage(self): cam = self.getCam() if cam["telescope_focal_length"] is None: raise ChimeraException("telescope_focal_length parameter must be set on camera instrument configuration") if self["filterwheel"] is not None: fw = self.getFilterWheel() fw.setFilter(self["filter"]) frames = cam.expose( exptime=self["exptime"], frames=1, shutter=Shutter.OPEN, filename=os.path.basename(ImageUtil.makeFilename("pointverify-$DATE")), ) if frames: image = frames[0] image_path = image.filename() if not os.path.exists(image_path): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ":\\" in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename( os.path.join(getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path)) ) t0 = time.time() self.log.debug("Downloading image from server to %s" % image_path) if not ImageUtil.download(image, image_path): raise ChimeraException("Error downloading image %s from %s" % (image_path, image.http())) self.log.debug("Finished download. Took %3.2f seconds" % (time.time() - t0)) return image_path, image else: raise Exception("Could not take an image")
def _baseExpose(self, request, **kwargs): if request: if isinstance(request, ImageRequest): imageRequest = request elif isinstance(request, dict): imageRequest = ImageRequest(**request) else: if kwargs: imageRequest = ImageRequest(**kwargs) else: imageRequest = ImageRequest() frames = imageRequest['frames'] interval = imageRequest['interval'] # validate shutter if str(imageRequest["shutter"]).lower() == "open": imageRequest["shutter"] = Shutter.OPEN elif str(imageRequest["shutter"]).lower() == "close": imageRequest["shutter"] = Shutter.CLOSE else: imageRequest["shutter"] = Shutter.LEAVE_AS_IS # validate readout mode self._getReadoutModeInfo(imageRequest["binning"], imageRequest["window"]) # use image server if any and save image on server's default dir if # filename given as a relative path. server = getImageServer(self.getManager()) if not os.path.isabs(imageRequest["filename"]): imageRequest["filename"] = os.path.join( server.defaultNightDir(), imageRequest["filename"]) # clear abort setting self.abort.clear() images = [] manager = self.getManager() for frame_num in range(frames): # [ABORT POINT] if self.abort.isSet(): return tuple(images) imageRequest.beginExposure(manager) self._expose(imageRequest) # [ABORT POINT] if self.abort.isSet(): return tuple(images) image = self._readout(imageRequest) if image is not None: images.append(image) imageRequest.endExposure(manager) # [ABORT POINT] if self.abort.isSet(): return tuple(images) if (interval > 0 and frame_num < frames) and (not frames == 1): time.sleep(interval) return tuple(images)
def _baseExpose(self, request, **kwargs): if request: if isinstance(request, ImageRequest): imageRequest = request elif isinstance(request, dict): imageRequest = ImageRequest(**request) else: if kwargs: imageRequest = ImageRequest(**kwargs) else: imageRequest = ImageRequest() frames = imageRequest['frames'] interval = imageRequest['interval'] # validate shutter if str(imageRequest["shutter"]).lower() == "open": imageRequest["shutter"] = Shutter.OPEN elif str(imageRequest["shutter"]).lower() == "close": imageRequest["shutter"] = Shutter.CLOSE else: imageRequest["shutter"] = Shutter.LEAVE_AS_IS # validate readout mode self._getReadoutModeInfo(imageRequest["binning"], imageRequest["window"]) # use image server if any and save image on server's default dir if filename given as a relative path. server = getImageServer(self.getManager()) if not os.path.isabs(imageRequest["filename"]): imageRequest["filename"] = os.path.join(server.defaultNightDir(), imageRequest["filename"]) # clear abort setting self.abort.clear() images = [] manager = self.getManager() for frame_num in range(frames): # [ABORT POINT] if self.abort.isSet(): return tuple(images) imageRequest.beginExposure(manager) self._expose(imageRequest) # [ABORT POINT] if self.abort.isSet(): return tuple(images) image = self._readout(imageRequest) if image is not None: images.append(image) imageRequest.endExposure(manager) # [ABORT POINT] if self.abort.isSet(): return tuple(images) if (interval > 0 and frame_num < frames) and (not frames == 1): time.sleep(interval) return tuple(images)
#Last resort if nothing else could make a picture if (pix == None): pix = N.zeros((100, 100), dtype=N.int32) imageRequest.fetchPostHeaders(self.getManager()) img = Image.create(pix, imageRequest) # update image request imageRequest["filename"] = img.filename() img += [('DATE-OBS', ImageUtil.formatDate( dt.datetime.fromtimestamp(self.__lastFrameStart)))] server = getImageServer(self.getManager()) proxy = server.register(img) self.readoutComplete(proxy) return proxy def abortExposure(self): if not self.isExposing(): return self.__abort.set() # busy waiting for exposure/readout stops while self.isExposing(): time.sleep(0.1)
def _saveImage(self, imageRequest, imageData, extra): (mode, binning, top, left, width, height) = self._getReadoutModeInfo(imageRequest["binning"], imageRequest["window"]) binFactor = extra.get("binning_factor", 1.0) pix_w, pix_h = self.getPixelSize() img = Image.create(imageData, imageRequest) if self["telescope_focal_length"] is not None: # If there is no telescope_focal_length defined, don't store WCS focal_length = self["telescope_focal_length"] scale_x = binFactor * (((180 / pi) / focal_length) * (pix_w * 0.001)) scale_y = binFactor * (((180 / pi) / focal_length) * (pix_h * 0.001)) full_width, full_height = self.getPhysicalSize() CRPIX1 = ((int(full_width / 2.0)) - left) - 1 CRPIX2 = ((int(full_height / 2.0)) - top) - 1 # Adding WCS coordinates according to FITS standard. # Quick sheet: http://www.astro.iag.usp.br/~moser/notes/GAi_FITSimgs.html # http://adsabs.harvard.edu/abs/2002A%26A...395.1061G # http://adsabs.harvard.edu/abs/2002A%26A...395.1077C img += [("CRPIX1", CRPIX1, "coordinate system reference pixel"), ("CRPIX2", CRPIX2, "coordinate system reference pixel"), ("CD1_1", scale_x * cos(self["rotation"]*pi/180.), "transformation matrix element (1,1)"), ("CD1_2", -scale_y * sin(self["rotation"]*pi/180.), "transformation matrix element (1,2)"), ("CD2_1", scale_x * sin(self["rotation"]*pi/180.), "transformation matrix element (2,1)"), ("CD2_2", scale_y * cos(self["rotation"]*pi/180.), "transformation matrix element (2,2)")] img += [('DATE-OBS', ImageUtil.formatDate( extra.get("frame_start_time", dt.datetime.utcnow())), 'Date exposure started'), ('CCD-TEMP', extra.get("frame_temperature", -275.0), 'CCD Temperature at Exposure Start [deg. C]'), ("EXPTIME", float(imageRequest['exptime']) or -1, "exposure time in seconds"), ('IMAGETYP', imageRequest['type'].strip(), 'Image type'), ('SHUTTER', str(imageRequest['shutter']), 'Requested shutter state'), ('INSTRUME', str(self['camera_model']), 'Name of instrument'), ('CCD', str(self['ccd_model']), 'CCD Model'), ('CCD_DIMX', self.getPhysicalSize() [0], 'CCD X Dimension Size'), ('CCD_DIMY', self.getPhysicalSize() [1], 'CCD Y Dimension Size'), ('CCDPXSZX', self.getPixelSize()[0], 'CCD X Pixel Size [micrometer]'), ('CCDPXSZY', self.getPixelSize()[1], 'CCD Y Pixel Size [micrometer]')] # register image on ImageServer server = getImageServer(self.getManager()) proxy = server.register(img) # and finally compress the image if asked if imageRequest['compress_format'].lower() != 'no': img.compress(format=imageRequest['compress_format'], multiprocess=True) return proxy
def run_stats(self, proxy, status): if status == CameraStatus.OK and proxy["IMAGETYP"].upper().rstrip() == "OBJECT" and \ proxy["SHUTTER"].upper().rstrip() == "OPEN": self.log.debug('%s [status:%s]@[%s]' % (proxy.filename(), status, proxy.http())) image_path = proxy.filename() if not os.path.exists( image_path ): # If image is on a remote server, donwload it. # If remote is windows, image_path will be c:\...\image.fits, so use ntpath instead of os.path. if ':\\' in image_path: modpath = ntpath else: modpath = os.path image_path = ImageUtil.makeFilename( os.path.join( getImageServer(self.getManager()).defaultNightDir(), modpath.basename(image_path))) t0 = time.time() self.log.debug('Downloading image from server to %s' % image_path) if not ImageUtil.download(proxy, image_path): raise ChimeraException( 'Error downloading image %s from %s' % (image_path, image.http())) self.log.debug('Finished download. Took %3.2f seconds' % (time.time() - t0)) img = Image.fromFile(image_path) else: img = Image.fromFile(image_path) tmpfile = mktemp() p = self._sex_params p.update({"CATALOG_NAME": mktemp()}) extract = img.extract(p) # os.unlink(tmpfile) # else: # extract = proxy.extract(self.sex_params) if len(extract ) > 0: # Only go ahead if at least one object was detected # stats = np.array( # [[data["CLASS_STAR"], data["FLAGS"], data["FWHM_IMAGE"], data["BACKGROUND"]] for data in # extract]) stats = np.array([[ data["NUMBER"], data["X_IMAGE"], data["Y_IMAGE"], data["XWIN_IMAGE"], data["YWIN_IMAGE"], data["ALPHA_J2000"], data["DELTA_J2000"], data["MAG_AUTO"], data["FLUX_AUTO"], data["BACKGROUND"], data["FWHM_IMAGE"], data["FLAGS"], data["CLASS_STAR"], ] for data in extract]) mask = np.bitwise_and(stats[:, 12] > 0.8, stats[:, 11] == 0) fff = "CLEAR" if "FILTER" in proxy.keys(): fff = proxy["FILTER"] # fff = "R" session = Session() try: log = ImageStatistics( date_obs=datetime.datetime.strptime( proxy["DATE-OBS"], "%Y-%m-%dT%H:%M:%S.%f"), filename=proxy.filename(), filter=fff, fwhm_avg=np.average(stats[:, 10][mask]), fwhm_std=np.std(stats[:, 10][mask]), background=np.average(stats[:, 9][mask]), npts=mask.sum(), exptime=proxy["EXPTIME"]) session.add(log) session.flush() session.refresh(log) # Now add stars to the star catalog # Todo: Solve astrometry cat = [] mag_sort = np.argsort(stats[:, 7])[:self['max_stars_catalog']] for data in stats[mag_sort]: cat.append( ImageCatalog( image_statistics_id=log.id, NUMBER=data[0], X_IMAGE=data[1], Y_IMAGE=data[2], XWIN_IMAGE=data[3], YWIN_IMAGE=data[4], ALPHA_J2000=data[5], DELTA_J2000=data[6], MAG_AUTO=data[7], FLUX_AUTO=data[8], BACKGROUND=data[9], FWHM_IMAGE=data[10], FLAGS=data[11], CLASS_STAR=data[12], )) session.add_all(cat) finally: session.commit() # self.stats.append(s) # print "fwhm stats:", s # self.stats[-1] else: self.log.debug( 'Image %s not good for statistics. [status:%s]@[%s]' % (proxy.filename(), status, proxy.http()))