def open_config_file(self, config_file): """Load one configuration file into internal storage. If the config_file is a relative path string and can't be found it will be loaded from a package relative location. If it can't be found in the package an exception is raised. """ # If we were provided a string filepath then open the file if isinstance(config_file, str): if not os.path.isabs(config_file): # Its not an absolute path, lets see if its relative path cwd_config = os.path.join(os.path.curdir, config_file) if os.path.exists(cwd_config): config_file = cwd_config config_file = open(config_file, 'r') else: # they have specified a package provided file LOG.debug( "Loading package provided configuration file: '%s'" % (config_file, )) try: parts = config_file.split(":") mod_part, file_part = parts if len(parts) == 2 else ( "", parts[0]) mod_part = mod_part or self.__module__ config_str = get_resource_string(mod_part, file_part) except StandardError: LOG.error("Configuration file '%s' was not found" % (config_file, )) raise config_file = StringIO(config_str) else: config_file = open(config_file, 'r') return config_file
def read_grids_config(config_filepath): """Read the "grids.conf" file and create dictionaries mapping the grid name to the necessary information. There are two dictionaries created, one for gpd file grids and one for proj4 grids. Format for gpd grids: grid_name,gpd,gpd_filename where 'gpd' is the actual text 'gpd' to define the grid as a gpd grid. Format for proj4 grids: grid_name,proj4,proj4_str,pixel_size_x,pixel_size_y,origin_x,origin_y,width,height where 'proj4' is the actual text 'proj4' to define the grid as a proj4 grid. """ full_config_filepath = os.path.realpath(os.path.expanduser(config_filepath)) if not os.path.exists(full_config_filepath): try: config_str = get_resource_string(__name__, config_filepath) return read_grids_config_str(config_str) except StandardError: LOG.error("Grids configuration file '%s' does not exist" % (config_filepath,)) LOG.debug("Grid configuration error: ", exc_info=1) raise config_file = open(full_config_filepath, "r") config_str = config_file.read() return read_grids_config_str(config_str)
def read_grids_config(config_filepath, convert_coords=True): """Read the "grids.conf" file and create dictionaries mapping the grid name to the necessary information. There are two dictionaries created, one for gpd file grids and one for proj4 grids. Format for gpd grids: grid_name,gpd,gpd_filename where 'gpd' is the actual text 'gpd' to define the grid as a gpd grid. Format for proj4 grids: grid_name,proj4,proj4_str,pixel_size_x,pixel_size_y,origin_x,origin_y,width,height where 'proj4' is the actual text 'proj4' to define the grid as a proj4 grid. """ full_config_filepath = os.path.realpath( os.path.expanduser(config_filepath)) if not os.path.exists(full_config_filepath): try: config_str = get_resource_string(__name__, config_filepath).decode() return read_grids_config_str(config_str, convert_coords=convert_coords) except ValueError: LOG.error("Grids configuration file '%s' does not exist" % (config_filepath, )) LOG.debug("Grid configuration error: ", exc_info=1) raise config_file = open(full_config_filepath, "r") config_str = config_file.read() return read_grids_config_str(config_str, convert_coords=convert_coords)
def open_config_file(self, config_file): """Load one configuration file into internal storage. If the config_file is a relative path string and can't be found it will be loaded from a package relative location. If it can't be found in the package an exception is raised. """ # If we were provided a string filepath then open the file if isinstance(config_file, str): if not os.path.isabs(config_file): # Its not an absolute path, lets see if its relative path cwd_config = os.path.join(os.path.curdir, config_file) if os.path.exists(cwd_config): config_file = cwd_config config_file = open(config_file, 'r') else: # they have specified a package provided file LOG.debug("Loading package provided configuration file: '%s'" % (config_file,)) try: parts = config_file.split(":") mod_part, file_part = parts if len(parts) == 2 else ("", parts[0]) mod_part = mod_part or self.__module__ config_str = get_resource_string(mod_part, file_part) except StandardError: LOG.error("Configuration file '%s' was not found" % (config_file,)) raise config_file = StringIO(config_str) else: config_file = open(config_file, 'r') return config_file
def read_atms_coeff_to_string(fn): """Read the coefficients into a string.""" if os.path.isfile(fn): coeff_str = open(fn, "r").readlines() else: parts = fn.split(":") mod_part, file_part = parts if len(parts) == 2 else ("", parts[0]) mod_part = mod_part or __package__ # self.__module__ coeff_str = get_resource_string(mod_part, file_part).decode().split("\n") return coeff_str
def read_atms_limb_correction_coefficients(fn): if os.path.isfile(fn): coeff_str = open(fn, "r").readlines() else: parts = fn.split(":") mod_part, file_part = parts if len(parts) == 2 else ("", parts[0]) mod_part = mod_part or __package__ # self.__module__ coeff_str = get_resource_string(mod_part, file_part).decode().split("\n") # make it a generator coeff_str = (line.strip() for line in coeff_str) all_coeffs = np.zeros((22, 96, 22), dtype=np.float32) all_amean = np.zeros((22, 96, 22), dtype=np.float32) all_dmean = np.zeros(22, dtype=np.float32) all_nchx = np.zeros(22, dtype=np.int32) all_nchanx = np.zeros((22, 22), dtype=np.int32) all_nchanx[:] = 9999 # There should be 22 sections for chan_idx in range(22): # blank line at the start of each section _ = next(coeff_str) # section header nx, nchx, dmean = [x.strip() for x in next(coeff_str).split(" ") if x] nx = int(nx) all_nchx[chan_idx] = nchx = int(nchx) all_dmean[chan_idx] = float(dmean) # coeff locations (indexes to put the future coefficients in) locations = [int(x.strip()) for x in next(coeff_str).split(" ") if x] assert (len(locations) == nchx) for x in range(nchx): all_nchanx[chan_idx, x] = locations[x] - 1 # Read 'nchx' coefficients for each of 96 FOV for fov_idx in range(96): # chan_num, fov_num, *coefficients, error coeff_line_parts = [ x.strip() for x in next(coeff_str).split(" ") if x ][2:] coeffs = [float(x) for x in coeff_line_parts[:nchx]] ameans = [float(x) for x in coeff_line_parts[nchx:-1]] error_val = float(coeff_line_parts[-1]) for x in range(nchx): all_coeffs[chan_idx, fov_idx, all_nchanx[chan_idx, x]] = coeffs[x] all_amean[all_nchanx[chan_idx, x], fov_idx, chan_idx] = ameans[x] return all_dmean, all_coeffs, all_amean, all_nchx, all_nchanx
def read_atms_limb_correction_coefficients(fn): if os.path.isfile(fn): coeff_str = open(fn, "r").readlines() else: parts = fn.split(":") mod_part, file_part = parts if len(parts) == 2 else ("", parts[0]) mod_part = mod_part or __package__ # self.__module__ coeff_str = get_resource_string(mod_part, file_part).decode().split("\n") # make it a generator coeff_str = (line.strip() for line in coeff_str) all_coeffs = np.zeros((22, 96, 22), dtype=np.float32) all_amean = np.zeros((22, 96, 22), dtype=np.float32) all_dmean = np.zeros(22, dtype=np.float32) all_nchx = np.zeros(22, dtype=np.int32) all_nchanx = np.zeros((22, 22), dtype=np.int32) all_nchanx[:] = 9999 # There should be 22 sections for chan_idx in range(22): # blank line at the start of each section _ = next(coeff_str) # section header nx, nchx, dmean = [x.strip() for x in next(coeff_str).split(" ") if x] nx = int(nx) all_nchx[chan_idx] = nchx = int(nchx) all_dmean[chan_idx] = float(dmean) # coeff locations (indexes to put the future coefficients in) locations = [int(x.strip()) for x in next(coeff_str).split(" ") if x] assert(len(locations) == nchx) for x in range(nchx): all_nchanx[chan_idx, x] = locations[x] - 1 # Read 'nchx' coefficients for each of 96 FOV for fov_idx in range(96): # chan_num, fov_num, *coefficients, error coeff_line_parts = [x.strip() for x in next(coeff_str).split(" ") if x][2:] coeffs = [float(x) for x in coeff_line_parts[:nchx]] ameans = [float(x) for x in coeff_line_parts[nchx:-1]] error_val = float(coeff_line_parts[-1]) for x in range(nchx): all_coeffs[chan_idx, fov_idx, all_nchanx[chan_idx, x]] = coeffs[x] all_amean[all_nchanx[chan_idx, x], fov_idx, chan_idx] = ameans[x] return all_dmean, all_coeffs, all_amean, all_nchx, all_nchanx
def load_config_file(self, config_file): """Load one configuration file into internal storage. If the config_file is a relative path string and can't be found it will be loaded from a package relative location. If it can't be found in the package an exception is raised. """ # If we were provided a string filepath then open the file if isinstance(config_file, str): if not os.path.isabs(config_file): # Its not an absolute path, lets see if its relative path cwd_config = os.path.join(os.path.curdir, config_file) if os.path.exists(cwd_config): config_file = cwd_config config_file = open(config_file, 'r') else: # they have specified a package provided file LOG.debug("Loading package provided rescale config: '%s'" % (config_file, )) try: config_str = get_resource_string( self.__module__, config_file) except StandardError: LOG.error("Rescale config '%s' was not found" % (config_file, )) raise config_file = StringIO(config_str) else: config_file = open(config_file, 'r') # Read in each line for line in config_file: # Clean the line line = line.strip() # Ignore comments and blank lines if line.startswith(self.COMMENT_CHARACTER) or line == "": continue # Get each element parts = tuple(x.strip() for x in line.split(",")) # Parse the line self.parse_config_parts(parts)
def load_config_file(self, config_file): """Load one configuration file into internal storage. If the config_file is a relative path string and can't be found it will be loaded from a package relative location. If it can't be found in the package an exception is raised. """ # If we were provided a string filepath then open the file if isinstance(config_file, str): if not os.path.isabs(config_file): # Its not an absolute path, lets see if its relative path cwd_config = os.path.join(os.path.curdir, config_file) if os.path.exists(cwd_config): config_file = cwd_config config_file = open(config_file, 'r') else: # they have specified a package provided file LOG.debug("Loading package provided rescale config: '%s'" % (config_file,)) try: config_str = get_resource_string(self.__module__, config_file).decode() except ValueError: LOG.error("Rescale config '%s' was not found" % (config_file,)) raise config_file = StringIO(config_str) else: config_file = open(config_file, 'r') # Read in each line for line in config_file: # Clean the line line = line.strip() # Ignore comments and blank lines if line.startswith(self.COMMENT_CHARACTER) or line == "": continue # Get each element parts = tuple( x.strip() for x in line.split(",") ) # Parse the line self.parse_config_parts(parts)