def _get_file_format(self, filename): """ Get the file type and any errors :param filename: the filename to read the type of :return: filetype :raises: FileFormatError if there is an error """ if not os.path.isfile(filename): raise FileFormatError(["File does not exist"]) try: attributes = get_netcdf_file_attributes(filename) except (RuntimeError, IOError) as ex: raise FileFormatError(["File is unreadable", ex.args[0]]) attributes_lower = {attr.lower(): val for attr, val in list(attributes.items())} if self.GASSP_VERSION_ATTRIBUTE_NAME.lower() in attributes_lower: file_type = "NetCDF/GASSP/{}".format(attributes_lower[self.GASSP_VERSION_ATTRIBUTE_NAME.lower()]) elif self.NCAR_RAF_CONVENTIONS_ATTRIBUTE_NAME.lower() in attributes_lower: ncarraf_convention = attributes_lower[self.NCAR_RAF_CONVENTIONS_ATTRIBUTE_NAME.lower()] version = "" if self.NCAR_RAF_CONVENTION_VERSION_ATTRIBUTE_NAME.lower() in attributes_lower: version = "/{}".format(attributes_lower[self.NCAR_RAF_CONVENTION_VERSION_ATTRIBUTE_NAME.lower()]) if not ncarraf_convention == self.NCAR_RAF_KNOWN_CONVENTION: raise FileFormatError(["NCAR-RAF convention unknown, expecting '{}' was '{}'" .format(self.NCAR_RAF_KNOWN_CONVENTION, ncarraf_convention)]) file_type = "NetCDF/{}{}".format(ncarraf_convention, version) else: raise FileFormatError(["File does not appear to be NCAR RAF or GASSP. No attributes for either '{}' or '{}'" .format(self.GASSP_VERSION_ATTRIBUTE_NAME, self.NCAR_RAF_CONVENTIONS_ATTRIBUTE_NAME)]) return file_type
def _load_data_definition(self, filenames): """ Load the definition of the data :param filenames: filenames from which to load the data :return: variable selector containing the data definitions """ variables_list = [get_netcdf_file_variables(f) for f in filenames] attributes = [get_netcdf_file_attributes(f) for f in filenames] variable_selector = self.variableSelectorClass(attributes, variables_list) return variable_selector
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: source = atts['source'] except KeyError as ex: errors = ['No source attribute found in {}'.format(filename)] else: if not source.startswith('CIS'): errors = ['Source ({}) does not match CIS in {}'.format(source, filename)] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: title = atts['title'] except KeyError as ex: errors = ['No title attribute found in {}'.format(filename)] else: if 'ICON' not in title: errors = ['Title ({}) does not contain ICON in {}'.format(title, filename)] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: title = atts['title'] except KeyError as ex: errors = ['No title attribute found in {}'.format(filename)] else: if not title.startswith("CARIBIC"): errors = ['Title ({}) does not appear to match CARIBIC in {}'.format(title, filename)] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: history = atts['history'] except KeyError as ex: errors = ['No history attribute found in {}'.format(filename)] else: if "Assembled using assemble and _readict" not in history: errors = ['History ({}) does not appear to match ICARTT output in {}'.format(history, filename)] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: comment = atts['comment'] except KeyError as ex: errors = ['No comment attribute found in {}'.format(filename)] else: if "Converted by ukca_flight_to_netcdf.py" not in comment: errors = [ 'Comment ({}) does not match ukca_flight in {}'.format( comment, filename) ] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: title = atts['title'] except KeyError as ex: errors = ['No title attribute found in {}'.format(filename)] else: if 'ICON' not in title: errors = [ 'Title ({}) does not contain ICON in {}'.format( title, filename) ] return errors
def get_file_type_error(self, filename): """ Test that the file is of the correct signature :param filename: the file name for the file :return: list fo errors or None """ from cis.data_io.netcdf import get_netcdf_file_attributes atts = get_netcdf_file_attributes(filename) errors = None try: title = atts['title'] except KeyError as ex: errors = ['No title attribute found in {}'.format(filename)] else: if not title.startswith("CARIBIC"): errors = [ 'Title ({}) does not appear to match CARIBIC in {}'.format( title, filename) ] return errors