def getNearest(self, time): """ Returns an allskyImage object corresponding to the image in the dataset which has a creation time closest to the specified time. The time argument should be a datetime object. """ filename, site_info_filename = self.getNearestFilename(time) return allskyImage.new(filename, site_info_filename)
def getImagesInRange(self, time1, time2): """ Returns a list of allskyImage objects containing all the images that correspond to times between time1 and time2 (inclusive). The list will be ordered in chronological order. The time arguments should be datetime objects. This function creates a new instance of the allskyImage class for each image in the range. """ filenames = self.getFilenamesInRange(time1, time2) return [allskyImage.new(*x) for x in filenames]
def getImage(self, time): """ Returns an allskyImage object containing the image data for the specified time. If no image exists for the specified time then None is returned. The time argument should be a datetime object. """ try: index = self.__times.index(time) except ValueError: return None return allskyImage.new(self.__filenames[index], self.__site_info_files[index])
def __next__(self): """ Required for the iterator protocol. Returns the next allskyImage in the dataset. """ if self.__current_index < self.__largest_index: im = allskyImage.new(self.__filenames[self.__current_index][0], site_info_file=self.__filenames[self.__current_index][1]) self.__current_index += 1 # return image return im else: # all images have been returned, raise an exception from now on. raise StopIteration
parser.add_option("-f", "--field_of_view", dest="angle", action="store", default=None, help="crop the image at a set angle (in degrees) from the zenith") (options, args) = parser.parse_args() if len(args) < 1: parser.error("No input file") return (options, args) if __name__ == "__main__": options, args = __parse_cmd_line() if not os.path.exists(args[0]): print "Cannot open file \'"+args[0]+"\'. No such file." sys.exit() try: im = allskyImage.new(args[0]) except: print "\'"+args[0]+"\' is not a recognised PASKIL image file." sys.exit() if options.angle is not None: im = im.binaryMask(float(options.angle)) proj = im.projectToHeight(options.proj_height,background=options.background) proj.default() show()
def fromList(file_names, wavelength, filetype, site_info_file=None): """ Creates a dataset from a list of filenames. The file_names argument should be a list of strings specifying the filenames of the files to be included. The wavelength argument should be a string that matches the value of the 'Wavelength' field in the image metadata see allskyImagePlugins module for details. The site_info_file option should be the filename of the site information file (if one is required). The filetype argument is a list of filetypes (e.g. ["png","jpg"]), so a dataset spanning many filetypes can be prodcued using this function (if only a single filetype is desired then it must be specified as ["filetype"]). A dataset spanning images with different site info files can only be produced by combining several datasets with different site info files. Note that images in the list supplied which do not conform to the dataset's parameters will be ignored. If no images are found that can be added to the dataset then ValueError is raised. """ data = [] mode = None found_wavelengths = set([]) # set of wavelengths found during the search # check that filetypes argument is a list - this is a common user error! if type(filetype) is not list: raise TypeError("Filetype argument should be a list.") for filename in file_names: # check if file is of correct type if not filename.endswith(tuple(filetype)): continue # if not then skip it # attempt to create an allskyImage object, if there is no plugin for it, # then skip this file try: current_image = allskyImage.new(filename, site_info_file) except TypeError: continue # skip file if PASKIL cannot open it except IOError: continue # skip file if PIL cannot decode the image # get the image info info = current_image.getInfo() # check if the image has the correct wavelength current_image_wavelength = info['header']['Wavelength'] if current_image_wavelength.find(wavelength) == -1: found_wavelengths.add(current_image_wavelength) continue # if image has wrong wavelength then skip it # check the image has the correct mode, calib factor and colour table try: current_colour_table = info['processing']['applyColourTable'] except KeyError: current_colour_table = None try: current_calib_factor = info['processing']['absoluteCalibration'] except KeyError: current_calib_factor = None current_lens_projection = info['camera']['lens_projection'] if mode == None: mode = current_image.getMode() colour_table = current_colour_table calib_factor = current_calib_factor lens_projection = current_lens_projection if current_image.getMode() != mode: print(("Warning! allskyData.fromList(): Skipping file " + filename + ". Incorrect image mode.")) continue if current_colour_table != colour_table: print(("Warning! allskyData.fromList(): Skipping file " + filename + ". Incorrect colour table.")) continue if current_calib_factor != calib_factor: print(("Warning! allskyData.fromList(): Skipping file " + filename + ". Incorrect absolute calibration factor.")) continue if current_lens_projection != lens_projection: print(("Warning! allskyData.fromList(): Skipping file " + filename + ". Incorrect lens projection.")) continue # read creation time from header time_str = info['header']['Creation Time'] # convert to datetime object try: time = datetime.datetime.strptime(time_str, "%d %b %Y %H:%M:%S %Z") except ValueError: time = datetime.datetime.strptime(time_str.rstrip() + " GMT", "%d %b %Y %H:%M:%S %Z") # store data associated with this image in the data list as a tuple data.append((time, filename, site_info_file, float(info['camera']['Radius']), float(info['camera']['fov_angle']))) # check to make sure the dataset is not empty if len(data) == 0: raise ValueError("No images were compatible with the dataset format," " ensure you have imported the required plugins,that the wavelength " "string matches that in the image header, and that you have specified " "any relevant site info files. Images with the following wavelengths " "were found " + str(found_wavelengths)) # sort the list into chronological order if sys.version_info[0] > 2: data.sort() else: data.sort(misc.tupleCompare) # return a dataset object return dataset(data, wavelength, filetype, mode, colour_table, calib_factor, lens_projection)
def execute(self, settings_manager_proxy, network_manager_proxy): """ Runs the function defined in the outputs.py file for this output type """ try: # start the proxies settings_manager_proxy.start() if (network_manager_proxy is not None): network_manager_proxy.start() # load the image using PASKIL self.image = allskyImage.new(self.image[0], self.image[1]) # work out where the output should be saved remove_file_on_host = False if self.output.filename_format is not None: # the 'normal' case where we want the output saved on the host # machine capture_time_string = self.image.getInfo()['header'][ 'Creation Time'] capture_time = datetime.datetime.strptime( capture_time_string, "%d %b %Y %H:%M:%S %Z") filename = capture_time.strftime(self.output.filename_format) if hasattr(self.output, "abs_path_on_host") and self.output.abs_path_on_host: if not os.path.exists(os.path.normpath(self.output.folder_on_host)): try: os.makedirs( os.path.normpath(self.output.folder_on_host)) except OSError: pass self.output_filename = os.path.normpath( self.output.folder_on_host + "/" + filename) else: if self.output.folder_on_host != "" and self.output.folder_on_host != None: self.output_filename = os.path.normpath( self._folder_on_host + "/" + self.output.folder_on_host + "/" + filename) else: self.output_filename = os.path.normpath( self._folder_on_host + "/" + filename) elif ((self.output.filename_format is None) and (self.file_on_server is not None)): # the case where we only want the file copied to the server temp_folder = settings_manager_proxy.get( ['tmp dir'])['tmp dir'] self.output_filename = os.path.normpath( temp_folder + "/" + self.file_on_server) remove_file_on_host = True else: # the case where we don't want any output saved self.output_filename = None # run the subtask execution function (this is what actually # produces the output) output = self.function( self.image, self.output, settings_manager_proxy) # save the output on the host if output is not None and self.output_filename is not None: try: output.save(self.output_filename) except AttributeError: # this is added for compatibility with matplotlib figure # objects output.savefig(self.output_filename) # the following seems to be needed to break circular references in # matplolib and prevent mem leaks output.clf() del output matplotlib._pylab_helpers.Gcf().destroy_all() matplotlib._pylab_helpers.gc.collect() # copy the output to the server if required if ((self.file_on_server is not None) and (network_manager_proxy is not None)): network_manager_proxy.copy_to_server( self.output_filename, self.file_on_server) del self.image #del output # if we only wanted the output on the server, then remove the copy # on the host if remove_file_on_host: os.remove(self.output_filename) except Exception as ex: traceback.print_exc() raise ex finally: # shutdown the proxies settings_manager_proxy.exit() if (network_manager_proxy is not None): network_manager_proxy.exit()
# Import the plugin needed to open this type of image. from PASKIL.plugins import DSLR_NEF # Declare filenames as global variables for convenience. nef_file = "LYR-SLR-20080201_184252.NEF" info_file = "NEF_site_info.txt" background_image = "LYR-SLR-20010101_000000.NEF" ffcalib_file = "D80_flat_field.txt" # Open the raw image files. print("Decoding NEF files") raw_image = allskyImage.new(nef_file, site_info_file=info_file) bkgd_image = allskyImage.new(background_image, site_info_file=info_file) # The raw image object contains pixel data from all four colour # channels of the camera. We are only interested in one of the # green channels, so we extract that now. For the D80, the # different colour channels are as follows: 0=red, 1=1st green, # 2=blue, 3=2nd green. The green channels are essentially # identical. print("Extracting green channel") green_image = raw_image.getChannel(1) green_bkgd = bkgd_image.getChannel(1) # Subtract the background image. Most methods return
# Import the plugin needed to open this type of image. from PASKIL.plugins import DSLR_LYR_JPG # Import the pylab functions we need to save the map projection from pylab import savefig # Declare filenames as global variables for convenience. jpg_file = "LYR-SLR-20081223_194416.JPG" info_file = "jpg_site_info.txt" # Open the image file print("Opening image file") image = allskyImage.new(jpg_file, info_file) # The D80 has a 180 degree field of view. However, mountains # obscure the view at high angles from zenith. To remove # them we crop the image to only have a 150 degree field of # view. Note that with PASKIL you have to specify the angle from # zenith that you want the image cropped at. Most methods return # a new allskyImage object - here we simply overwrite the # original with the cropped version. print("Cropping field of view") image = image.binaryMask(75) # Crop the image size to only include the field of view. print("Cropping image to field of view size")