Пример #1
0
    def _read_metadata(self, prologue, image_files, epilogue=None):
        if epilogue:
            mda = self._metadata_reader(prologue,
                                        image_files,
                                        epilogue=epilogue)
        else:
            mda = self._metadata_reader(prologue, image_files)
        if "%.2f" % mda.sublon != "%.2f" % self.sublon:
            if CHECK_CONFIG_SUBLON:
                raise mipp.ReaderError(
                    "Sub satellite point in config file (%.2f) don't match data (%.2f)"
                    % (self.sublon, mda.sublon))
            else:
                logger.warning(
                    "Modifying sub satellite point from %.2f to %.2f" %
                    (self.sublon, mda.sublon))
                self.sublon = mda.sublon

        chn = self._config_reader.get_channel(mda.channel)
        if mda.image_size[0] != chn.size[0]:
            raise mipp.ReaderError(
                "unknown image width for %s, %s: %d" %
                (self.satname, mda.channel, mda.image_size[0]))

        mda.pixel_size = numpy.array([chn.resolution, chn.resolution],
                                     dtype=numpy.float64)
        for k, v in self.__dict__.items():
            if k[0] != '_' and type(v) != types.FunctionType:
                setattr(mda, k, v)

        img = _xrit.read_imagedata(image_files[0])

        return mda
Пример #2
0
    def __init__(self, config_reader):
        #
        # Read configuration file based on satellite name
        #
        sat = config_reader('satellite')

        #
        # Load format decoder based on level1 format
        #
        lv1_format = config_reader('level1')['format']
        logger.info("Loading %s" % lv1_format)
        try:
            _args = imp.find_module(lv1_format)
        except ImportError:
            raise mipp.ReaderError("unknown level-1 format: '%s'" % lv1_format)
        try:
            mdl = imp.load_module(lv1_format, *_args)
        finally:
            if _args[0]:
                _args[0].close()

        self._metadata_reader = mdl.read_metadata
        self._image_reader = mdl.read_image
        self._tar_file = None

        #
        # Attributing
        #
        self.__dict__.update(sat)

        self._config_reader = config_reader
        self.satname = self.satname + self.number
        self.satnumber = self.number
        del self.number
Пример #3
0
class SatelliteLoader(object):
    # Currently this one only works for geos satellites
    #
    # We will return an ImageLoader object where access to data is like:
    # image[:], image[] or image() will return full disk
    # image[2:56, 1020:1070]
    # image.area_extent(area_extent)
    #
    def __init__(self, config_reader):
        #
        # Read configuration file based on satellite name
        #
        sat = config_reader('satellite')
        projname = sat['projection'].lower()
        if not projname.startswith('geos'):
            raise mipp.ReaderError(
                "currently we only support projections of type: 'GEOS'")

        #
        # Load format decoder based on level1 format
        #
        format = config_reader('level1')['format']
        try:
            pth = None
            for mod in format.split("/")[:-1]:
                pth = [imp.find_module(mod, pth)[1]]
            args = imp.find_module(format.split("/")[-1], pth)

        except ImportError, err:
            raise mipp.ReaderError(("unknown level-1 format: '%s'" % format) +
                                   "in " + str(pth) + "\n" + str(err))
        try:
            m = imp.load_module(format, *args)
        finally:
            if args[0]:
                args[0].close()

        self._metadata_reader = m.read_metadata

        #
        # Attributing
        #
        self.__dict__.update(sat)

        self._config_reader = config_reader
        self.satname = self.satname + self.number
        self.satnumber = self.number
        delattr(self, 'number')

        # backwards compatible
        if not hasattr(self, 'proj4_params'):
            try:
                sublon = float(projname.split('(')[1].split(')')[0])
            except (IndexError, ValueError):
                raise mipp.ReaderError("Could not determine sub satellite " +
                                       "point from projection name '%s'" %
                                       projname)
            self.proj4_params = "proj=geos lon_0=%.2f lat_0=0.00 a=6378169.00 b=6356583.80 h=35785831.00" % sublon
Пример #4
0
    def load(self, time_stamp, channel, **kwarg):
        if channel not in self._config_reader.channel_names:
            raise mipp.ReaderError("unknown channel name '%s'" % channel)
        opt = self._config_reader('level1')
        val = {}
        val["channel"] = channel + '*'

        # Prologue

        val["segment"] = "PRO".ljust(9, '_')

        filename_pro = opt.get('filename_pro', opt['filename'])
        prologue = glob.glob(opt['dir'] + '/' + \
                             (time_stamp.strftime(filename_pro)%val))
        if not prologue:
            raise mipp.NoFiles("missing prologue file: '%s'" %
                               (time_stamp.strftime(filename_pro) % val))
        prologue = prologue[0]

        # Regular channels

        val["segment"] = "0????????"
        image_files = glob.glob(opt['dir'] + '/' + \
                                time_stamp.strftime(opt['filename'])%val)

        if not image_files:
            raise mipp.NoFiles("no data files: '%s'" %
                               (time_stamp.strftime(opt['filename']) % val))
        image_files.sort()

        # Check if the files are xrit-compressed, and decompress them
        # accordingly:
        decomp_files = decompress(image_files)

        logger.info("Read %s" % prologue)
        prologue = _xrit.read_prologue(prologue)

        # Epilogue

        val["segment"] = "EPI".ljust(9, '_')

        filename_epi = opt.get('filename_epi', opt['filename'])
        epilogue = glob.glob(opt['dir'] + '/' + \
                             (time_stamp.strftime(filename_epi)%val))

        if not epilogue:
            logger.info("No epilogue file to read.")
        else:
            epilogue = epilogue[0]
            logger.info("Read %s" % epilogue)
            epilogue = _xrit.read_epilogue(epilogue)
            return self.load_files(prologue,
                                   decomp_files,
                                   epilogue=epilogue,
                                   **kwarg)

        return self.load_files(prologue, decomp_files, **kwarg)
Пример #5
0
    def __init__(self, config_reader):
        #
        # Read configuration file based on satellite name
        #
        sat = config_reader('satellite')
        projname = sat['projection'].lower()
        if not projname.startswith('geos'):
            raise mipp.ReaderError(
                "currently we only support projections of type: 'GEOS'")

        #
        # Load format decoder based on level1 format
        #
        format = config_reader('level1')['format']
        try:
            pth = None
            for mod in format.split("/")[:-1]:
                pth = [imp.find_module(mod, pth)[1]]
            args = imp.find_module(format.split("/")[-1], pth)

        except ImportError, err:
            raise mipp.ReaderError(("unknown level-1 format: '%s'" % format) +
                                   "in " + str(pth) + "\n" + str(err))
Пример #6
0
 def load_file(self,
               filename,
               channel,
               only_metadata=False,
               mask=True,
               calibrate=1):
     if channel not in self._config_reader.channel_names:
         raise mipp.ReaderError("unknown channel name '%s'" % channel)
     self._tar_file = filename
     mda = self._load_metadata(channel)
     if only_metadata:
         return mda
     mda, img = self._load_image(mda, mask=mask, calibrate=calibrate)
     return mipp.mda.mslice(mda), img
Пример #7
0
def read_metadata(xmlbuffer):

    # Speciel decoders
    def dec_isoformat(rts):
        return datetime.strptime(rts, "%Y-%m-%dT%H:%M:%S.%fZ")

    def dec_orbit_number(rts):
        return int(rts[:5])

    def dec_satellite_name(rts):
        return rts.replace('-', '')

    def dec_calibration_unit(rts):
        _trans = {'radar brightness': 'nrcs'}
        rts = rts.replace(' ', '-').lower()
        return rts

    attributes = {
        'product_level': ('generalHeader/itemName', str),
        'satellite_name':
        ('productInfo/missionInfo/mission', dec_satellite_name),
        'orbit_number': ('productInfo/missionInfo/absOrbit', dec_orbit_number),
        'sensor_type': ('productInfo/acquisitionInfo/sensor', str),
        'beam_mode': ('productInfo/acquisitionInfo/imagingMode', str),
        'polarisation':
        ('productInfo/acquisitionInfo/polarisationList/polLayer', str),
        'beamid':
        ('productInfo/acquisitionInfo/elevationBeamConfiguration', str),
        'calibrated': ('productInfo/productVariantInfo/radiometricCorrection',
                       str),
        'calibration_factor': ('calibration/calibrationConstant/calFactor',
                               float),
        'calibration_beamid': ('calibration/calibrationConstant/beamID', str),
        'calibration_unit': ('productInfo/imageDataInfo/pixelValueID',
                             dec_calibration_unit),
        'image_data_path': ('productComponents/imageData/file/location/path',
                            str),
        'image_data_filename':
        ('productComponents/imageData/file/location/filename', str),
        'time_start': ('productInfo/sceneInfo/start/timeUTC', dec_isoformat),
        'center_coor_lat': ('productInfo/sceneInfo/sceneCenterCoord/lat',
                            float),
        'center_coor_lon': ('productInfo/sceneInfo/sceneCenterCoord/lon',
                            float)
    }

    check_attributes = {
        'product_level': 'level 1b product',
        'satellite_name': 'tsx',
        'sensor_type': 'sar'
    }

    tree = etree.fromstring(xmlbuffer)

    # Check satellite, sensor and product level
    for key, val in check_attributes.items():
        try:
            path = attributes[key][0]
            attr = tree.xpath(path)[0].text.lower()
            if not attr.startswith(val):
                raise mipp.ReaderError(
                    "This does not look like a TSX SAR " +
                    "Level 1B Product, %s is '%s' expected '%s'" %
                    (key, attr, val))
        except IndexError:
            raise mipp.ReaderError(
                "This does not look like a TSX SAR " +
                "Level 1B Product, could not find attribute '%s' (%s)" %
                (key, path))

    mda = Metadata()
    for key, val in attributes.items():
        setattr(mda, key, val[1](tree.xpath(val[0])[0].text))
    mda.image_filename = (mda.image_data_path + '/' + mda.image_data_filename)
    delattr(mda, 'image_data_path')
    delattr(mda, 'image_data_filename')
    return mda
Пример #8
0
def read_metadata(xmlbuffer):
    mda = Metadata()

    # Speciel decoders
    def dec_timeformat(strn):
        strn = strn.split('.')
        return (datetime.strptime(strn[0], "%Y-%m-%d %H:%M:%S") +
                timedelta(seconds=float('.' + strn[1])))

    def dec_orbit_number(strn):
        return int(strn[:5])

    attributes = (
        ('_ROOT_/Attribute', {
            'Satellite ID': ('satellite_name', str),
            'Product Filename': ('image_filename', str),
            'Product Type': ('product_type', str),
            'Acquisition Station ID': ('facility_id', str),
            'Scene Sensing Start UTC': ('time_start', dec_timeformat),
            'Scene Sensing Stop UTC': ('time_stop', dec_timeformat),
            'Orbit Number': ('orbit_number', dec_orbit_number),
            'Sample Format': ('product_format', str),
            'Image Scale': ('image_scale', str),
            'Image Layers': ('layers', int),
            'Bits per Sample': ('bits_per_sample', int),
            'Samples per Pixel': ('samples_per_pixel', int),
        }),
        ('MBI/Attribute', {
            'Column Spacing': ('sample_spacing', float),
            'Line Spacing': ('line_spacing', float)
        }),
        ('S01/Attribute', {
            'Polarisation': ('polarisation', str),
        }),
    )

    tree = etree.fromstring(xmlbuffer)

    #
    # Get Atrributes
    #
    for path, attr in attributes:
        names = attr.keys()
        path = tree.xpath(path)
        for i in path:
            name = i.attrib['Name']
            if name in names:
                names.remove(name)
                val = i.text
                setattr(mda, attr[name][0], attr[name][1](val))

    satid = 'CSK'
    if not mda.satellite_name.upper().startswith(satid):
        raise mipp.ReaderError(
            "This does not look like a CosmoSkymed product, " +
            "satellite ID does now start with '%s'" % satid)

    mda.image_filename = os.path.splitext(mda.image_filename)[0] + '.MBI.tif'

    mda.no_data_value = 0
    mda.calibrated = 'NOTCALIBRATED'

    return mda
Пример #9
0
    def load(self, time_stamp, channel, **kwarg):
        if channel not in self._config_reader.channel_names:
            raise mipp.ReaderError("unknown channel name '%s'" % channel)
        opt = self._config_reader('level1')
        val = {}
        val["channel"] = channel + '*'

        if isinstance(time_stamp, (tuple, list)):
            start_time, end_time = time_stamp
        else:
            start_time = time_stamp
            end_time = time_stamp

        # Prologue

        val["segment"] = "PRO".ljust(9, '_')

        filename_pro = opt.get('filename_pro', opt['filename'])
        prologue = glob.glob(
            start_time.strftime(os.path.join(opt['dir'], filename_pro)) % val)

        if not prologue:
            logger.info("No prologue file to read.")
            prologue = None
        else:
            prologue = prologue[0]
            logger.info("Read %s" % prologue)
            prologue = _xrit.read_prologue(prologue)

        # Regular channels

        val["segment"] = "0??*"
        dt = timedelta(minutes=1)
        image_files = []
        while start_time <= end_time:
            image_files.extend(
                glob.glob(
                    start_time.strftime(
                        os.path.join(opt['dir'], opt['filename'])) % val))
            start_time += dt

        if not image_files:
            raise mipp.NoFiles("no data files: '%s'" %
                               (start_time.strftime(opt['filename']) % val))
        image_files.sort()

        # Check if the files are xrit-compressed, and decompress them
        # accordingly:
        decomp_files = decompress(image_files)

        # Epilogue

        val["segment"] = "EPI".ljust(9, '_')

        filename_epi = opt.get('filename_epi', opt['filename'])
        epilogue = glob.glob(
            end_time.strftime(os.path.join(opt['dir'], filename_epi)) % val)

        if not epilogue:
            logger.info("No epilogue file to read.")
        else:
            epilogue = epilogue[0]
            logger.info("Read %s" % epilogue)
            epilogue = _xrit.read_epilogue(epilogue)
            return self.load_files(prologue,
                                   decomp_files,
                                   epilogue=epilogue,
                                   **kwarg)

        return self.load_files(prologue, decomp_files, **kwarg)