Exemplo n.º 1
0
    def generate_data(self):
        '''
        Description:
            Provides the main processing algorithm for building the Surface 
            Temperature product.  It produces the final ST product.
        '''

        try:
            self.retrieve_metadata_information()
        except Exception:
            self.logger.exception('Failed reading input XML metadata file')
            raise

        # Register all the gdal drivers and choose the ENVI for our output
        gdal.AllRegister()
        envi_driver = gdal.GetDriverByName('ENVI')

        # Read the bands into memory

        # Landsat Radiance at sensor for thermal band
        self.logger.info('Loading intermediate thermal band data [{0}]'.format(
            self.thermal_name))
        dataset = gdal.Open(self.thermal_name)
        x_dim = dataset.RasterXSize  # They are all the same size
        y_dim = dataset.RasterYSize

        thermal_data = dataset.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Atmospheric transmittance
        self.logger.info(
            'Loading intermediate transmittance band data [{0}]'.format(
                self.transmittance_name))
        dataset = gdal.Open(self.transmittance_name)
        trans_data = dataset.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Atmospheric path radiance - upwelled radiance
        self.logger.info(
            'Loading intermediate upwelled band data [{0}]'.format(
                self.upwelled_name))
        dataset = gdal.Open(self.upwelled_name)
        upwelled_data = dataset.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)

        self.logger.info('Calculating surface radiance')
        # Surface radiance
        with np.errstate(invalid='ignore'):
            surface_radiance = (thermal_data - upwelled_data) / trans_data

        # Fix the no data locations
        no_data_locations = np.where(thermal_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        no_data_locations = np.where(trans_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        no_data_locations = np.where(upwelled_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        # Memory cleanup
        del thermal_data
        del trans_data
        del upwelled_data
        del no_data_locations

        # Downwelling sky irradiance
        self.logger.info(
            'Loading intermediate downwelled band data [{0}]'.format(
                self.downwelled_name))
        dataset = gdal.Open(self.downwelled_name)
        downwelled_data = dataset.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)

        # Landsat emissivity estimated from ASTER GED data
        self.logger.info(
            'Loading intermediate emissivity band data [{0}]'.format(
                self.emissivity_name))
        dataset = gdal.Open(self.emissivity_name)
        emissivity_data = dataset.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)

        # Save for the output product
        ds_srs = osr.SpatialReference()
        ds_srs.ImportFromWkt(dataset.GetProjection())
        ds_transform = dataset.GetGeoTransform()

        # Memory cleanup
        del dataset

        # Estimate Earth-emitted radiance by subtracting off the reflected
        # downwelling component
        radiance = (surface_radiance -
                    (1.0 - emissivity_data) * downwelled_data)

        # Account for surface emissivity to get Plank emitted radiance
        self.logger.info('Calculating Plank emitted radiance')
        with np.errstate(invalid='ignore'):
            radiance_emitted = radiance / emissivity_data

        # Fix the no data locations
        no_data_locations = np.where(surface_radiance == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        no_data_locations = np.where(downwelled_data == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        no_data_locations = np.where(emissivity_data == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        # Memory cleanup
        del downwelled_data
        del emissivity_data
        del surface_radiance
        del radiance
        del no_data_locations

        # Use Brightness Temperature LUT to get skin temperature
        # Read the correct one for what we are processing
        if self.satellite == 'LANDSAT_8':
            self.logger.info('Using Landsat 8 Brightness Temperature LUT')
            bt_name = 'L8_Brightness_Temperature_LUT.txt'

        elif self.satellite == 'LANDSAT_7':
            self.logger.info('Using Landsat 7 Brightness Temperature LUT')
            bt_name = 'L7_Brightness_Temperature_LUT.txt'

        elif self.satellite == 'LANDSAT_5':
            self.logger.info('Using Landsat 5 Brightness Temperature LUT')
            bt_name = 'L5_Brightness_Temperature_LUT.txt'

        elif self.satellite == 'LANDSAT_4':
            self.logger.info('Using Landsat 4 Brightness Temperature LUT')
            bt_name = 'L4_Brightness_Temperature_LUT.txt'

        bt_data = np.loadtxt(os.path.join(self.st_data_dir, bt_name),
                             dtype=float,
                             delimiter=' ')
        bt_radiance_lut = bt_data[:, 1]
        bt_temp_lut = bt_data[:, 0]

        self.logger.info('Generating ST results')
        st_data = np.interp(radiance_emitted, bt_radiance_lut, bt_temp_lut)

        # Scale the result
        st_data = st_data * MULT_FACTOR

        # Add the fill and scan gaps back into the results, since they may
        # have been lost
        self.logger.info('Adding fill and data gaps back into the Surface'
                         ' Temperature results')

        # Fix the no data locations
        no_data_locations = np.where(radiance_emitted == self.no_data_value)
        st_data[no_data_locations] = self.no_data_value

        # Memory cleanup
        del radiance_emitted
        del no_data_locations

        product_id = self.xml_filename.split('.xml')[0]
        st_img_filename = ''.join([product_id, '_st', '.img'])
        st_hdr_filename = ''.join([product_id, '_st', '.hdr'])
        st_aux_filename = ''.join([st_img_filename, '.aux', '.xml'])

        self.logger.info('Creating {0}'.format(st_img_filename))
        util.Geo.generate_raster_file(envi_driver, st_img_filename, st_data,
                                      x_dim, y_dim, ds_transform,
                                      ds_srs.ExportToWkt(), self.no_data_value,
                                      gdal.GDT_Int16)

        self.logger.info('Updating {0}'.format(st_hdr_filename))
        util.Geo.update_envi_header(st_hdr_filename, self.no_data_value)

        # Memory cleanup
        del ds_srs
        del ds_transform

        # Remove the *.aux.xml file generated by GDAL
        if os.path.exists(st_aux_filename):
            os.unlink(st_aux_filename)

        self.logger.info('Adding {0} to {1}'.format(st_img_filename,
                                                    self.xml_filename))
        # Add the estimated Surface Temperature product to the metadata
        espa_xml = metadata_api.parse(self.xml_filename, silence=True)
        bands = espa_xml.get_bands()
        sensor_code = product_id[0:4]

        # Find the TOA Band 1 to use for the specific band details
        base_band = None
        for band in bands.band:
            if band.product == 'toa_refl' and band.name == 'toa_band1':
                base_band = band

        if base_band is None:
            raise Exception('Failed to find the TOA band 1'
                            ' in the input data')

        st_band = metadata_api.band(product='st',
                                    source='toa_refl',
                                    name='surface_temperature',
                                    category='image',
                                    data_type='INT16',
                                    scale_factor=SCALE_FACTOR,
                                    add_offset=0,
                                    nlines=base_band.get_nlines(),
                                    nsamps=base_band.get_nsamps(),
                                    fill_value=str(self.no_data_value))

        st_band.set_short_name('{0}ST'.format(sensor_code))
        st_band.set_long_name('Surface Temperature')
        st_band.set_file_name(st_img_filename)
        st_band.set_data_units('temperature (kelvin)')

        pixel_size = metadata_api.pixel_size(base_band.pixel_size.x,
                                             base_band.pixel_size.x,
                                             base_band.pixel_size.units)
        st_band.set_pixel_size(pixel_size)

        st_band.set_resample_method('none')

        valid_range = metadata_api.valid_range(min=1500, max=3730)
        st_band.set_valid_range(valid_range)

        # Set the date, but first clean the microseconds off of it
        production_date = (datetime.datetime.strptime(
            datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
            '%Y-%m-%dT%H:%M:%S'))

        st_band.set_production_date(production_date)

        st_band.set_app_version(util.Version.app_version())

        bands.add_band(st_band)

        # Write the XML metadata file out
        with open(self.xml_filename, 'w') as metadata_fd:
            metadata_api.export(metadata_fd, espa_xml)

        # Memory cleanup
        del st_band
        del bands
        del espa_xml
        del st_data
    def generate_product(self):
        '''
        Description:
            Provides the main processing algorithm for generating the
            estimated Landsat emissivity product.  It produces the final
            emissivity product.
        '''

        self.logger = logging.getLogger(__name__)

        self.logger.info('Start - Estimate Landsat Emissivity')

        try:
            self.retrieve_metadata_information()
        except Exception:
            self.logger.exception('Failed reading input XML metadata file')
            raise

        try:
            self.determine_sensor_specific_coefficients()
        except Exception:
            self.logger.exception('Failed determining sensor coefficients')
            raise

        # Register all the gdal drivers and choose the GeoTiff for our temp
        # output
        gdal.AllRegister()
        geotiff_driver = gdal.GetDriverByName('GTiff')
        envi_driver = gdal.GetDriverByName('ENVI')

        # ====================================================================
        # Build NDVI in memory
        self.logger.info('Building TOA based NDVI band for Landsat data')

        # NIR ----------------------------------------------------------------
        data_set = gdal.Open(self.toa_nir_name)
        x_dim = data_set.RasterXSize  # They are all the same size
        y_dim = data_set.RasterYSize
        ls_nir_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                            x_dim, y_dim)
        nir_no_data_locations = np.where(ls_nir_data == self.no_data_value)
        ls_nir_data = ls_nir_data * self.toa_nir_scale_factor

        # RED ----------------------------------------------------------------
        data_set = gdal.Open(self.toa_red_name)
        ls_red_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                            x_dim, y_dim)
        red_no_data_locations = np.where(ls_red_data == self.no_data_value)
        ls_red_data = ls_red_data * self.toa_red_scale_factor

        # NDVI ---------------------------------------------------------------
        ls_ndvi_data = ((ls_nir_data - ls_red_data) /
                        (ls_nir_data + ls_red_data))

        # Cleanup no data locations
        ls_ndvi_data[nir_no_data_locations] = self.no_data_value
        ls_ndvi_data[red_no_data_locations] = self.no_data_value

        if self.keep_intermediate_data:
            geo_transform = data_set.GetGeoTransform()
            ds_srs = osr.SpatialReference()
            ds_srs.ImportFromWkt(data_set.GetProjection())

        # Memory cleanup
        del ls_red_data
        del ls_nir_data
        del nir_no_data_locations
        del red_no_data_locations

        # ====================================================================
        # Build NDSI in memory
        self.logger.info('Building TOA based NDSI band for Landsat data')

        # GREEN --------------------------------------------------------------
        data_set = gdal.Open(self.toa_green_name)
        ls_green_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                              x_dim, y_dim)
        green_no_data_locations = (
            np.where(ls_green_data == self.no_data_value))
        ls_green_data = ls_green_data * self.toa_green_scale_factor

        # SWIR1 --------------------------------------------------------------
        data_set = gdal.Open(self.toa_swir1_name)
        ls_swir1_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                              x_dim, y_dim)
        swir1_no_data_locations = (
            np.where(ls_swir1_data == self.no_data_value))
        ls_swir1_data = ls_swir1_data * self.toa_swir1_scale_factor

        # Build the Landsat TOA NDSI data
        self.logger.info('Building TOA based NDSI for Landsat data')
        ls_ndsi_data = ((ls_green_data - ls_swir1_data) /
                        (ls_green_data + ls_swir1_data))

        # Cleanup no data locations
        ls_ndsi_data[green_no_data_locations] = self.no_data_value
        # Cleanup no data locations
        ls_ndsi_data[swir1_no_data_locations] = self.no_data_value

        # Memory cleanup
        del ls_green_data
        del ls_swir1_data
        del green_no_data_locations
        del swir1_no_data_locations

        # Save for the output products
        ds_tmp_srs = osr.SpatialReference()
        ds_tmp_srs.ImportFromWkt(data_set.GetProjection())
        ds_tmp_transform = data_set.GetGeoTransform()

        # Memory cleanup
        del data_set

        # Save the locations for the specfied snow pixels
        self.logger.info('Determine snow pixel locations')
        selected_snow_locations = np.where(ls_ndsi_data > 0.4)

        # Save ndvi and ndsi no data locations
        ndvi_no_data_locations = np.where(ls_ndvi_data == self.no_data_value)
        ndsi_no_data_locations = np.where(ls_ndsi_data == self.no_data_value)

        # Memory cleanup
        del ls_ndsi_data

        # Turn all negative values to zero
        # Use a realy small value so that we don't have negative zero (-0.0)
        ls_ndvi_data[ls_ndvi_data < 0.0000001] = 0

        if self.keep_intermediate_data:
            self.logger.info('Writing Landsat NDVI raster')
            util.Geo.generate_raster_file(geotiff_driver,
                                          'internal_landsat_ndvi.tif',
                                          ls_ndvi_data,
                                          x_dim, y_dim,
                                          geo_transform,
                                          ds_srs.ExportToWkt(),
                                          self.no_data_value,
                                          gdal.GDT_Float32)

        # Build the estimated Landsat EMIS data from the ASTER GED data and
        # warp it to the Landsat scenes projection and image extents
        # For convenience the ASTER NDVI is also extracted and warped to the
        # Landsat scenes projection and image extents
        self.logger.info('Build thermal emissivity band and'
                         ' retrieve ASTER NDVI')
        (ls_emis_warped_name,
         aster_ndvi_warped_name) = self.build_ls_emis_data(geotiff_driver)

        # Load the warped estimated Landsat EMIS into memory
        data_set = gdal.Open(ls_emis_warped_name)
        ls_emis_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                             x_dim, y_dim)
        ls_emis_gap_locations = np.where(ls_emis_data == 0)
        ls_emis_no_data_locations = (
            np.where(ls_emis_data == self.no_data_value))

        # Load the warped ASTER NDVI into memory
        data_set = gdal.Open(aster_ndvi_warped_name)
        aster_ndvi_data = data_set.GetRasterBand(1).ReadAsArray(0, 0,
                                                                x_dim, y_dim)
        aster_ndvi_gap_locations = np.where(aster_ndvi_data == 0)
        aster_ndvi_no_data_locations = (
            np.where(aster_ndvi_data == self.no_data_value))

        # Turn all negative values to zero
        # Use a realy small value so that we don't have negative zero (-0.0)
        aster_ndvi_data[aster_ndvi_data < 0.0000001] = 0

        # Memory cleanup
        del data_set

        if not self.keep_intermediate_data:
            # Cleanup the temp files since we have them in memory
            if os.path.exists(ls_emis_warped_name):
                os.unlink(ls_emis_warped_name)
            if os.path.exists(aster_ndvi_warped_name):
                os.unlink(aster_ndvi_warped_name)

        self.logger.info('Normalizing Landsat and ASTER NDVI')
        # Normalize Landsat NDVI by max value
        max_ls_ndvi = ls_ndvi_data.max()
        self.logger.info('Max LS NDVI {0}'.format(max_ls_ndvi))
        ls_ndvi_data = ls_ndvi_data / float(max_ls_ndvi)

        if self.keep_intermediate_data:
            self.logger.info('Writing Landsat NDVI NORM MAX raster')
            util.Geo.generate_raster_file(geotiff_driver,
                                          'internal_landsat_ndvi_norm_max.tif',
                                          ls_ndvi_data,
                                          x_dim, y_dim,
                                          geo_transform,
                                          ds_srs.ExportToWkt(),
                                          self.no_data_value,
                                          gdal.GDT_Float32)

        # Normalize ASTER NDVI by max value
        max_aster_ndvi = aster_ndvi_data.max()
        self.logger.info('Max ASTER NDVI {0}'.format(max_aster_ndvi))
        aster_ndvi_data = aster_ndvi_data / float(max_aster_ndvi)

        if self.keep_intermediate_data:
            self.logger.info('Writing Aster NDVI NORM MAX raster')
            util.Geo.generate_raster_file(geotiff_driver,
                                          'internal_aster_ndvi_norm_max.tif',
                                          aster_ndvi_data,
                                          x_dim, y_dim,
                                          geo_transform,
                                          ds_srs.ExportToWkt(),
                                          self.no_data_value,
                                          gdal.GDT_Float32)


        # Soil - From prototype code variable name
        ls_emis_final = ((ls_emis_data - 0.975 * aster_ndvi_data) /
                         (1.0 - aster_ndvi_data))

        # Memory cleanup
        del aster_ndvi_data
        del ls_emis_data

        # Adjust estimated Landsat EMIS for vegetation and snow, to generate
        # the final Landsat EMIS data
        self.logger.info('Adjusting estimated EMIS for vegetation')
        ls_emis_final = (self.vegetation_coeff * ls_ndvi_data +
                         ls_emis_final * (1.0 - ls_ndvi_data))

        # Medium snow
        self.logger.info('Adjusting estimated EMIS for snow')
        ls_emis_final[selected_snow_locations] = self.snow_emis_value

        # Memory cleanup
        del ls_ndvi_data
        del selected_snow_locations

        # Add the fill and scan gaps and ASTER gaps back into the results,
        # since they may have been lost
        self.logger.info('Adding fill and data gaps back into the estimated'
                         ' Landsat emissivity results')
        ls_emis_final[ls_emis_no_data_locations] = self.no_data_value
        ls_emis_final[ls_emis_gap_locations] = self.no_data_value
        ls_emis_final[aster_ndvi_no_data_locations] = self.no_data_value
        ls_emis_final[aster_ndvi_gap_locations] = self.no_data_value
        ls_emis_final[ndvi_no_data_locations] = self.no_data_value
        ls_emis_final[ndsi_no_data_locations] = self.no_data_value

        # Memory cleanup
        del ls_emis_no_data_locations
        del ls_emis_gap_locations
        del aster_ndvi_no_data_locations
        del aster_ndvi_gap_locations

        product_id = self.xml_filename.split('.xml')[0]
        ls_emis_img_filename = ''.join([product_id, '_emis', '.img'])
        ls_emis_hdr_filename = ''.join([product_id, '_emis', '.hdr'])
        ls_emis_aux_filename = ''.join([ls_emis_img_filename, '.aux', '.xml'])

        self.logger.info('Creating {0}'.format(ls_emis_img_filename))
        util.Geo.generate_raster_file(envi_driver, ls_emis_img_filename,
                                      ls_emis_final, x_dim, y_dim,
                                      ds_tmp_transform,
                                      ds_tmp_srs.ExportToWkt(),
                                      self.no_data_value, gdal.GDT_Float32)

        self.logger.info('Updating {0}'.format(ls_emis_hdr_filename))
        util.Geo.update_envi_header(ls_emis_hdr_filename, self.no_data_value)

        # Remove the *.aux.xml file generated by GDAL
        if os.path.exists(ls_emis_aux_filename):
            os.unlink(ls_emis_aux_filename)

        self.logger.info('Adding {0} to {1}'.format(ls_emis_img_filename,
                                                    self.xml_filename))
        # Add the estimated Landsat emissivity to the metadata XML
        espa_xml = metadata_api.parse(self.xml_filename, silence=True)
        bands = espa_xml.get_bands()
        sensor_code = product_id[0:3]
        source_product = 'toa_refl'

        # Find the TOA Band 1 to use for the specific band details
        base_band = None
        for band in bands.band:
            if band.product == source_product and band.name == 'toa_band1':
                base_band = band

        if base_band is None:
            raise Exception('Failed to find the TOA BLUE band'
                            ' in the input data')

        emis_band = metadata_api.band(product='lst_temp',
                                      source=source_product,
                                      name='landsat_emis',
                                      category='image',
                                      data_type='FLOAT32',
                                      nlines=base_band.get_nlines(),
                                      nsamps=base_band.get_nsamps(),
                                      fill_value=str(self.no_data_value))

        emis_band.set_short_name('{0}EMIS'.format(sensor_code))
        emis_band.set_long_name('Landsat emissivity estimated from ASTER GED'
                                ' data')
        emis_band.set_file_name(ls_emis_img_filename)
        emis_band.set_data_units('Emissivity Coefficient')

        pixel_size = metadata_api.pixel_size(base_band.pixel_size.x,
                                             base_band.pixel_size.x,
                                             base_band.pixel_size.units)
        emis_band.set_pixel_size(pixel_size)

        valid_range = metadata_api.valid_range(min=0.0, max=1.0)
        emis_band.set_valid_range(valid_range)

        # Set the date, but first clean the microseconds off of it
        production_date = (
            datetime.datetime.strptime(datetime.datetime.now().
                                       strftime('%Y-%m-%dT%H:%M:%S'),
                                       '%Y-%m-%dT%H:%M:%S'))

        emis_band.set_production_date(production_date)

        emis_band.set_app_version(util.Version.app_version())

        bands.add_band(emis_band)

        # Write the XML metadata file out
        with open(self.xml_filename, 'w') as output_fd:
            metadata_api.export(output_fd, espa_xml)

        # Memory cleanup
        del ls_emis_final

        self.logger.info('Completed - Estimate Landsat Emissivity')
Exemplo n.º 3
0
    def createXML(self, scene_xml_file=None, output_xml_file=None,
        start_year=None, end_year=None, fill_value=None, imgfile=None,
        log_handler=None):
        """Creates an XML file for the products produced by
           runAnnualBurnSummaries.
        Description: routine to create the XML file for the burned area summary
            bands.  The sample scene-based XML file will be used as the basis
            for the projection information for the output XML file.  The image
            size, extents, etc. will need to be updated, as will the band
            information.
        
        History:
          Created on May 12, 2014 by Gail Schmidt, USGS/EROS LSRD Project

        Args:
          scene_xml_file - scene-based XML file to be used as the base XML
              information for the projection metadata.
          output_xml_file - name of the XML file to be written
          start_year - starting year of the scenes to process
          end_year - ending year of the scenes to process
          fill_value - fill or nodata value for this dataset
          imgfile - name of burned area image file with associated ENVI header
              which can be used to obtain the extents and geographic
              information for these products
          log_handler - handler for the logging information
   
        Returns:
            ERROR - error creating the XML file
            SUCCESS - successful creation of the XML file
        """

        # parse the scene-based XML file, just as a basis for the output XML
        # file.  the global attributes will be similar, but the extents and
        # size of the image will be different.  the bands will be based on the
        # bands that are output from this routine.
        xml = metadata_api.parse (scene_xml_file, silence=True)
        meta_bands = xml.get_bands()
        meta_global = xml.get_global_metadata()

        # update the global information
        meta_global.set_data_provider("USGS/EROS")
        meta_global.set_satellite("LANDSAT")
        meta_global.set_instrument("combination")
        del (meta_global.acquisition_date)
        meta_global.set_acquisition_date(None)

        # open the image file to obtain the geospatial and spatial reference
        # information
        ds = gdal.Open (imgfile)
        if ds is None:
            msg = "GDAL failed to open %s" % imgfile
            logIt (msg, log_handler)
            return ERROR

        ds_band = ds.GetRasterBand (1)
        if ds_band is None:
            msg = "GDAL failed to get the first band in %s" % imgfile
            logIt (msg, log_handler)
            return ERROR
        nlines = float(ds_band.YSize)
        nsamps = float(ds_band.XSize)
        nlines_int = ds_band.YSize 
        nsamps_int = ds_band.XSize 
        del (ds_band)

        ds_transform = ds.GetGeoTransform()
        if ds_transform is None:
            msg = "GDAL failed to get the geographic transform information " \
                "from %s" % imgfile
            logIt (msg, log_handler)
            return ERROR

        ds_srs = osr.SpatialReference()
        if ds_srs is None:
            msg = "GDAL failed to get the spatial reference information " \
                "from %s" % imgfile
            logIt (msg, log_handler)
            return ERROR
        ds_srs.ImportFromWkt (ds.GetProjection())
        del (ds)

        # get the UL and LR center of pixel map coordinates
        (map_ul_x, map_ul_y) = convert_imageXY_to_mapXY (0.5, 0.5,
            ds_transform)
        (map_lr_x, map_lr_y) = convert_imageXY_to_mapXY (
            nsamps - 0.5, nlines - 0.5, ds_transform)

        # update the UL and LR projection corners along with the origin of the
        # corners, for the center of the pixel (global projection information)
        for mycorner in meta_global.projection_information.corner_point:
            if mycorner.location == 'UL':
                mycorner.set_x (map_ul_x)
                mycorner.set_y (map_ul_y)
            if mycorner.location == 'LR':
                mycorner.set_x (map_lr_x)
                mycorner.set_y (map_lr_y)
        meta_global.projection_information.set_grid_origin("CENTER")

        # update the UL and LR latitude and longitude coordinates, using the
        # center of the pixel
        srs_lat_lon = ds_srs.CloneGeogCS()
        coord_tf = osr.CoordinateTransformation (ds_srs, srs_lat_lon)
        for mycorner in meta_global.corner:
            if mycorner.location == 'UL':
                (lon, lat, height) = \
                    coord_tf.TransformPoint (map_ul_x, map_ul_y)
                mycorner.set_longitude (lon)
                mycorner.set_latitude (lat)
            if mycorner.location == 'LR':
                (lon, lat, height) = \
                    coord_tf.TransformPoint (map_lr_x, map_lr_y)
                mycorner.set_longitude (lon)
                mycorner.set_latitude (lat)

        # determine the bounding coordinates; initialize using the UL and LR
        # then work around the scene edges
        # UL
        (map_x, map_y) = convert_imageXY_to_mapXY (0.0, 0.0, ds_transform)
        (ul_lon, ul_lat, height) = coord_tf.TransformPoint (map_x, map_y)
        # LR
        (map_x, map_y) = convert_imageXY_to_mapXY (nsamps, nlines, ds_transform)
        (lr_lon, lr_lat, height) = coord_tf.TransformPoint (map_x, map_y)

        # find the min and max values accordingly, for initialization
        west_lon = min (ul_lon, lr_lon)
        east_lon = max (ul_lon, lr_lon)
        north_lat = max (ul_lat, lr_lat)
        south_lat = min (ul_lat, lr_lat)

        # traverse the boundaries of the image to determine the bounding
        # coords; traverse one extra line and sample to get the the outer
        # extents of the image vs. just the UL of the outer edge.
        # top and bottom edges
        for samp in range (0, nsamps_int+1):
            # top edge
            (map_x, map_y) = convert_imageXY_to_mapXY (samp, 0.0, ds_transform)
            (top_lon, top_lat, height) = coord_tf.TransformPoint (map_x, map_y)

            # lower edge
            (map_x, map_y) = convert_imageXY_to_mapXY (samp, nlines,
                ds_transform)
            (low_lon, low_lat, height) = coord_tf.TransformPoint (map_x, map_y)

            # update the min and max values
            west_lon = min (top_lon, low_lon, west_lon)
            east_lon = max (top_lon, low_lon, east_lon)
            north_lat = max (top_lat, low_lat, north_lat)
            south_lat = min (top_lat, low_lat, south_lat)

        # left and right edges
        for line in range (0, nlines_int+1):
            # left edge
            (map_x, map_y) = convert_imageXY_to_mapXY (0.0, line, ds_transform)
            (left_lon, left_lat, height) = coord_tf.TransformPoint (map_x,
                map_y)

            # right edge
            (map_x, map_y) = convert_imageXY_to_mapXY (nsamps, line,
                ds_transform)
            (right_lon, right_lat, height) = coord_tf.TransformPoint (map_x,
                map_y)

            # update the min and max values
            west_lon = min (left_lon, right_lon, west_lon)
            east_lon = max (left_lon, right_lon, east_lon)
            north_lat = max (left_lat, right_lat, north_lat)
            south_lat = min (left_lat, right_lat, south_lat)

        # update the XML
        bounding_coords = meta_global.get_bounding_coordinates()
        bounding_coords.set_west (west_lon)
        bounding_coords.set_east (east_lon)
        bounding_coords.set_north (north_lat)
        bounding_coords.set_south (south_lat)

        del (ds_transform)
        del (ds_srs)

        # clear some of the global information that doesn't apply for these
        # products
        del (meta_global.scene_center_time)
        meta_global.set_scene_center_time(None)
        del (meta_global.lpgs_metadata_file)
        meta_global.set_lpgs_metadata_file(None)
        del (meta_global.orientation_angle)
        meta_global.set_orientation_angle(None)
        del (meta_global.level1_production_date)
        meta_global.set_level1_production_date(None)

        # clear the solar angles
        del (meta_global.solar_angles)
        meta_global.set_solar_angles(None)

        # save the first band and then wipe the bands out so that new bands
        # can be added for the burned area bands
        myband_save = meta_bands.band[0]
        del (meta_bands.band)
        meta_bands.band = []

        # create the band information; there are 4 output products per year
        # for the burned area dataset; add enough bands to cover the products
        # and years
        #    1. first date a burned area was observed (burned_area)
        #    2. number of times burn was observed (burn_count)
        #    3. number of good looks (good_looks_count)
        #    4. maximum probability for burned area (max_burn_prob)
        nproducts = 4
        nyears = end_year - start_year + 1
        nbands = nproducts * nyears
        for i in range (0, nbands):
            # add the new band
            myband = metadata_api.band()
            meta_bands.band.append(myband)

        # how many bands are there in the new XML file
        num_scene_bands =  len (meta_bands.band)
        print "New XML file has %d bands" % num_scene_bands

        # loop through the products and years to create the band metadata
        band_count = 0
        for product in range (1, nproducts+1):
            for year in range (start_year, end_year+1):
                myband = meta_bands.band[band_count]
                myband.set_product("burned_area")
                myband.set_short_name("LNDBA")
                myband.set_data_type("INT16")
                myband.set_pixel_size(myband_save.get_pixel_size())
                myband.set_fill_value(fill_value)
                myband.set_nlines(nlines)
                myband.set_nsamps(nsamps)
                myband.set_app_version(self.burned_area_version)
                production_date = time.strftime("%Y-%m-%dT%H:%M:%S",
                    time.gmtime())
                myband.set_production_date (  \
                    datetime_.datetime.strptime(production_date,
                    '%Y-%m-%dT%H:%M:%S'))

                # clear some of the band-specific fields that don't apply for
                # this product
                del (myband.source)
                myband.set_source(None)
                del (myband.saturate_value)
                myband.set_saturate_value(None)
                del (myband.scale_factor)
                myband.set_scale_factor(None)
                del (myband.add_offset)
                myband.set_add_offset(None)
                del (myband.toa_reflectance)
                myband.set_toa_reflectance(None)
                del (myband.bitmap_description)
                myband.set_bitmap_description(None)
                del (myband.class_values)
                myband.set_class_values(None)
                del (myband.qa_description)
                myband.set_qa_description(None)
                del (myband.calibrated_nt)
                myband.set_calibrated_nt(None)

                # handle the band-specific differences
                valid_range = metadata_api.valid_range()
                if product == 1:
                    name = "burned_area_%d" % year
                    long_name = "first DOY a burn was observed"
                    file_name = "burned_area_%d.img" % year
                    category = "image"
                    data_units = "day of year"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no burn observed" 

                elif product == 2:
                    name = "burn_count_%d" % year
                    long_name = "number of times a burn was observed"
                    file_name = "burn_count_%d.img" % year
                    category = "image"
                    data_units = "count"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no burn observed" 

                elif product == 3:
                    name = "good_looks_count_%d" % year
                    long_name = "number of good looks (pixels with good QA)"
                    file_name = "good_looks_count_%d.img" % year
                    category = "qa"
                    data_units = "count"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no valid pixels (water, cloud, " \
                        "snow, etc.)"

                elif product == 4:
                    name = "max_burn_prob_%d" % year
                    long_name = "maximum probability for burned area"
                    file_name = "max_burn_prob_%d.img" % year
                    category = "image"
                    data_units = "probability"
                    valid_range.min = 0
                    valid_range.max = 100
                    qa_description = "-9998: bad QA (water, cloud, snow, etc.)"

                myband.set_name(name)
                myband.set_long_name(long_name)
                myband.set_file_name(file_name)
                myband.set_category(category)
                myband.set_data_units(data_units)
                myband.set_valid_range(valid_range)
                myband.set_qa_description(qa_description)

                # increment the band counter
                band_count += 1

            # end for year
        # end for nproducts

        # write out a the XML file after validation
        # call the export with validation
        fd = open (output_xml_file, 'w')
        if fd == None:
            msg = "Unable to open the output XML file (%s) for writing." % \
                output_xml_file
            logIt (msg, log_handler)
            return ERROR

        metadata_api.export (fd, xml)
        fd.flush()
        fd.close()

        return SUCCESS
    def generate_product(self):
        '''
        Description:
            Provides the main processing algorithm for generating the
            estimated Landsat emissivity product.  It produces the final
            emissivity product.
        '''

        self.logger = logging.getLogger(__name__)

        self.logger.info('Start - Estimate Landsat Emissivity')

        try:
            self.retrieve_metadata_information()
        except Exception:
            self.logger.exception('Failed reading input XML metadata file')
            raise

        try:
            self.determine_sensor_specific_coefficients()
        except Exception:
            self.logger.exception('Failed determining sensor coefficients')
            raise

        # Register all the gdal drivers and choose the GeoTiff for our temp
        # output
        gdal.AllRegister()
        geotiff_driver = gdal.GetDriverByName('GTiff')
        envi_driver = gdal.GetDriverByName('ENVI')

        # ====================================================================
        # Build NDVI in memory
        self.logger.info('Building TOA based NDVI band for Landsat data')

        # NIR ----------------------------------------------------------------
        data_set = gdal.Open(self.toa_nir_name)
        x_dim = data_set.RasterXSize  # They are all the same size
        y_dim = data_set.RasterYSize
        ls_nir_data = data_set.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)
        nir_no_data_locations = np.where(ls_nir_data == self.no_data_value)
        ls_nir_data = ls_nir_data * self.toa_nir_scale_factor

        # RED ----------------------------------------------------------------
        data_set = gdal.Open(self.toa_red_name)
        ls_red_data = data_set.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)
        red_no_data_locations = np.where(ls_red_data == self.no_data_value)
        ls_red_data = ls_red_data * self.toa_red_scale_factor

        # NDVI ---------------------------------------------------------------
        ls_ndvi_data = ((ls_nir_data - ls_red_data) /
                        (ls_nir_data + ls_red_data))

        # Cleanup no data locations
        ls_ndvi_data[nir_no_data_locations] = self.no_data_value
        ls_ndvi_data[red_no_data_locations] = self.no_data_value

        if self.keep_intermediate_data:
            geo_transform = data_set.GetGeoTransform()
            ds_srs = osr.SpatialReference()
            ds_srs.ImportFromWkt(data_set.GetProjection())

        # Memory cleanup
        del ls_red_data
        del ls_nir_data
        del nir_no_data_locations
        del red_no_data_locations

        # ====================================================================
        # Build NDSI in memory
        self.logger.info('Building TOA based NDSI band for Landsat data')

        # GREEN --------------------------------------------------------------
        data_set = gdal.Open(self.toa_green_name)
        ls_green_data = data_set.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)
        green_no_data_locations = (np.where(
            ls_green_data == self.no_data_value))
        ls_green_data = ls_green_data * self.toa_green_scale_factor

        # SWIR1 --------------------------------------------------------------
        data_set = gdal.Open(self.toa_swir1_name)
        ls_swir1_data = data_set.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)
        swir1_no_data_locations = (np.where(
            ls_swir1_data == self.no_data_value))
        ls_swir1_data = ls_swir1_data * self.toa_swir1_scale_factor

        # Build the Landsat TOA NDSI data
        self.logger.info('Building TOA based NDSI for Landsat data')
        ls_ndsi_data = ((ls_green_data - ls_swir1_data) /
                        (ls_green_data + ls_swir1_data))

        # Cleanup no data locations
        ls_ndsi_data[green_no_data_locations] = self.no_data_value
        # Cleanup no data locations
        ls_ndsi_data[swir1_no_data_locations] = self.no_data_value

        # Memory cleanup
        del ls_green_data
        del ls_swir1_data
        del green_no_data_locations
        del swir1_no_data_locations

        # Save for the output products
        ds_tmp_srs = osr.SpatialReference()
        ds_tmp_srs.ImportFromWkt(data_set.GetProjection())
        ds_tmp_transform = data_set.GetGeoTransform()

        # Memory cleanup
        del data_set

        # Save the locations for the specfied snow pixels
        self.logger.info('Determine snow pixel locations')
        selected_snow_locations = np.where(ls_ndsi_data > 0.4)

        # Save ndvi and ndsi no data locations
        ndvi_no_data_locations = np.where(ls_ndvi_data == self.no_data_value)
        ndsi_no_data_locations = np.where(ls_ndsi_data == self.no_data_value)

        # Memory cleanup
        del ls_ndsi_data

        # Turn all negative values to zero
        # Use a realy small value so that we don't have negative zero (-0.0)
        ls_ndvi_data[ls_ndvi_data < 0.0000001] = 0

        if self.keep_intermediate_data:
            self.logger.info('Writing Landsat NDVI raster')
            util.Geo.generate_raster_file(geotiff_driver,
                                          'internal_landsat_ndvi.tif',
                                          ls_ndvi_data, x_dim, y_dim,
                                          geo_transform, ds_srs.ExportToWkt(),
                                          self.no_data_value, gdal.GDT_Float32)

        # Build the estimated Landsat EMIS data from the ASTER GED data and
        # warp it to the Landsat scenes projection and image extents
        # For convenience the ASTER NDVI is also extracted and warped to the
        # Landsat scenes projection and image extents
        self.logger.info('Build thermal emissivity band and'
                         ' retrieve ASTER NDVI')
        (ls_emis_warped_name,
         aster_ndvi_warped_name) = self.build_ls_emis_data(geotiff_driver)

        # Load the warped estimated Landsat EMIS into memory
        data_set = gdal.Open(ls_emis_warped_name)
        ls_emis_data = data_set.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)
        ls_emis_gap_locations = np.where(ls_emis_data == 0)
        ls_emis_no_data_locations = (np.where(
            ls_emis_data == self.no_data_value))

        # Load the warped ASTER NDVI into memory
        data_set = gdal.Open(aster_ndvi_warped_name)
        aster_ndvi_data = data_set.GetRasterBand(1).ReadAsArray(
            0, 0, x_dim, y_dim)
        aster_ndvi_gap_locations = np.where(aster_ndvi_data == 0)
        aster_ndvi_no_data_locations = (np.where(
            aster_ndvi_data == self.no_data_value))

        # Turn all negative values to zero
        # Use a realy small value so that we don't have negative zero (-0.0)
        aster_ndvi_data[aster_ndvi_data < 0.0000001] = 0

        # Memory cleanup
        del data_set

        if not self.keep_intermediate_data:
            # Cleanup the temp files since we have them in memory
            if os.path.exists(ls_emis_warped_name):
                os.unlink(ls_emis_warped_name)
            if os.path.exists(aster_ndvi_warped_name):
                os.unlink(aster_ndvi_warped_name)

        self.logger.info('Normalizing Landsat and ASTER NDVI')
        # Normalize Landsat NDVI by max value
        max_ls_ndvi = ls_ndvi_data.max()
        self.logger.info('Max LS NDVI {0}'.format(max_ls_ndvi))
        ls_ndvi_data = ls_ndvi_data / float(max_ls_ndvi)

        if self.keep_intermediate_data:
            self.logger.info('Writing Landsat NDVI NORM MAX raster')
            util.Geo.generate_raster_file(
                geotiff_driver, 'internal_landsat_ndvi_norm_max.tif',
                ls_ndvi_data, x_dim, y_dim, geo_transform,
                ds_srs.ExportToWkt(), self.no_data_value, gdal.GDT_Float32)

        # Normalize ASTER NDVI by max value
        max_aster_ndvi = aster_ndvi_data.max()
        self.logger.info('Max ASTER NDVI {0}'.format(max_aster_ndvi))
        aster_ndvi_data = aster_ndvi_data / float(max_aster_ndvi)

        if self.keep_intermediate_data:
            self.logger.info('Writing Aster NDVI NORM MAX raster')
            util.Geo.generate_raster_file(geotiff_driver,
                                          'internal_aster_ndvi_norm_max.tif',
                                          aster_ndvi_data, x_dim, y_dim,
                                          geo_transform, ds_srs.ExportToWkt(),
                                          self.no_data_value, gdal.GDT_Float32)

        # Soil - From prototype code variable name
        self.logger.info('Calculating EMIS Final')
        with np.errstate(divide='ignore'):
            ls_emis_final = ((ls_emis_data - 0.975 * aster_ndvi_data) /
                             (1.0 - aster_ndvi_data))

        # Memory cleanup
        del aster_ndvi_data
        del ls_emis_data

        # Adjust estimated Landsat EMIS for vegetation and snow, to generate
        # the final Landsat EMIS data
        self.logger.info('Adjusting estimated EMIS for vegetation')
        ls_emis_final = (self.vegetation_coeff * ls_ndvi_data + ls_emis_final *
                         (1.0 - ls_ndvi_data))

        # Medium snow
        self.logger.info('Adjusting estimated EMIS for snow')
        ls_emis_final[selected_snow_locations] = self.snow_emis_value

        # Memory cleanup
        del ls_ndvi_data
        del selected_snow_locations

        # Add the fill and scan gaps and ASTER gaps back into the results,
        # since they may have been lost
        self.logger.info('Adding fill and data gaps back into the estimated'
                         ' Landsat emissivity results')
        ls_emis_final[ls_emis_no_data_locations] = self.no_data_value
        ls_emis_final[ls_emis_gap_locations] = self.no_data_value
        ls_emis_final[aster_ndvi_no_data_locations] = self.no_data_value
        ls_emis_final[aster_ndvi_gap_locations] = self.no_data_value
        ls_emis_final[ndvi_no_data_locations] = self.no_data_value
        ls_emis_final[ndsi_no_data_locations] = self.no_data_value

        # Memory cleanup
        del ls_emis_no_data_locations
        del ls_emis_gap_locations
        del aster_ndvi_no_data_locations
        del aster_ndvi_gap_locations

        product_id = self.xml_filename.split('.xml')[0]
        ls_emis_img_filename = ''.join([product_id, '_emis', '.img'])
        ls_emis_hdr_filename = ''.join([product_id, '_emis', '.hdr'])
        ls_emis_aux_filename = ''.join([ls_emis_img_filename, '.aux', '.xml'])

        self.logger.info('Creating {0}'.format(ls_emis_img_filename))
        util.Geo.generate_raster_file(envi_driver, ls_emis_img_filename,
                                      ls_emis_final, x_dim,
                                      y_dim, ds_tmp_transform,
                                      ds_tmp_srs.ExportToWkt(),
                                      self.no_data_value, gdal.GDT_Float32)

        self.logger.info('Updating {0}'.format(ls_emis_hdr_filename))
        util.Geo.update_envi_header(ls_emis_hdr_filename, self.no_data_value)

        # Remove the *.aux.xml file generated by GDAL
        if os.path.exists(ls_emis_aux_filename):
            os.unlink(ls_emis_aux_filename)

        self.logger.info('Adding {0} to {1}'.format(ls_emis_img_filename,
                                                    self.xml_filename))
        # Add the estimated Landsat emissivity to the metadata XML
        espa_xml = metadata_api.parse(self.xml_filename, silence=True)
        bands = espa_xml.get_bands()
        sensor_code = product_id[0:3]
        source_product = 'toa_refl'

        # Find the TOA Band 1 to use for the specific band details
        base_band = None
        for band in bands.band:
            if band.product == source_product and band.name == 'toa_band1':
                base_band = band

        if base_band is None:
            raise Exception('Failed to find the TOA BLUE band'
                            ' in the input data')

        emis_band = metadata_api.band(product='lst_temp',
                                      source=source_product,
                                      name='landsat_emis',
                                      category='image',
                                      data_type='FLOAT32',
                                      nlines=base_band.get_nlines(),
                                      nsamps=base_band.get_nsamps(),
                                      fill_value=str(self.no_data_value))

        emis_band.set_short_name('{0}EMIS'.format(sensor_code))
        emis_band.set_long_name('Landsat emissivity estimated from ASTER GED'
                                ' data')
        emis_band.set_file_name(ls_emis_img_filename)
        emis_band.set_data_units('Emissivity Coefficient')

        pixel_size = metadata_api.pixel_size(base_band.pixel_size.x,
                                             base_band.pixel_size.x,
                                             base_band.pixel_size.units)
        emis_band.set_pixel_size(pixel_size)

        emis_band.set_resample_method('none')

        valid_range = metadata_api.valid_range(min=0.0, max=1.0)
        emis_band.set_valid_range(valid_range)

        # Set the date, but first clean the microseconds off of it
        production_date = (datetime.datetime.strptime(
            datetime.datetime.now().strftime('%Y-%m-%dT%H:%M:%S'),
            '%Y-%m-%dT%H:%M:%S'))

        emis_band.set_production_date(production_date)

        emis_band.set_app_version(util.Version.app_version())

        bands.add_band(emis_band)

        # Write the XML metadata file out
        with open(self.xml_filename, 'w') as output_fd:
            metadata_api.export(output_fd, espa_xml)

        # Memory cleanup
        del ls_emis_final

        self.logger.info('Completed - Estimate Landsat Emissivity')
Exemplo n.º 5
0
global_metadata.set_projection_information(projection_information)

# ---
global_metadata.set_orientation_angle(0)

# Add the global metadata to the top-level object
xml.set_global_metadata(global_metadata)

# Create bands object
bands = metadata_api.bandsType()

# Create a band
band = metadata_api.band(product="RDD",
                         name="band1",
                         category="image",
                         data_type="UINT8",
                         nlines="7321",
                         nsamps="7951",
                         fill_value="0")

band.set_short_name("LT5DN")
band.set_long_name("band 1 digital numbers")
band.set_file_name("LT50460282002042EDC01_B1.img")

pixel_size = metadata_api.pixel_size("30.000000", 30, "meters")
band.set_pixel_size(pixel_size)

band.set_data_units("digital numbers")

valid_range = metadata_api.valid_range(min="1", max=255)
band.set_valid_range(valid_range)
    def generate_data(self):
        '''
        Description:
            Provides the main processing algorithm for building the Land
            Surface Temperature product.  It produces the final LST product.
        '''

        try:
            self.retrieve_metadata_information()
        except Exception:
            self.logger.exception('Failed reading input XML metadata file')
            raise

        # Register all the gdal drivers and choose the ENVI for our output
        gdal.AllRegister()
        envi_driver = gdal.GetDriverByName('ENVI')

        # Read the bands into memory

        # Landsat Radiance at sensor for thermal band
        self.logger.info('Loading intermediate thermal band data')
        ds = gdal.Open(self.thermal_name)
        x_dim = ds.RasterXSize  # They are all the same size
        y_dim = ds.RasterYSize
        thermal_data = ds.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Atmospheric transmittance
        self.logger.info('Loading intermediate transmittance band data')
        ds = gdal.Open(self.transmittance_name)
        trans_data = ds.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Atmospheric path radiance - upwelled radiance
        self.logger.info('Loading intermediate upwelled band data')
        ds = gdal.Open(self.upwelled_name)
        upwelled_data = ds.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        self.logger.info('Calculating surface radiance')
        # Surface radiance
        surface_radiance = (thermal_data - upwelled_data) / trans_data

        # Fix the no data locations
        no_data_locations = np.where(thermal_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        no_data_locations = np.where(trans_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        no_data_locations = np.where(upwelled_data == self.no_data_value)
        surface_radiance[no_data_locations] = self.no_data_value

        # Memory cleanup
        del (thermal_data)
        del (trans_data)
        del (upwelled_data)
        del (no_data_locations)

        # Downwelling sky irradiance
        self.logger.info('Loading intermediate downwelled band data')
        ds = gdal.Open(self.downwelled_name)
        downwelled_data = ds.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Landsat emissivity estimated from ASTER GED data
        self.logger.info('Loading intermediate emissivity band data')
        ds = gdal.Open(self.emissivity_name)
        emissivity_data = ds.GetRasterBand(1).ReadAsArray(0, 0, x_dim, y_dim)

        # Save for the output product
        ds_srs = osr.SpatialReference()
        ds_srs.ImportFromWkt(ds.GetProjection())
        ds_transform = ds.GetGeoTransform()

        # Memory cleanup
        del (ds)

        # Estimate Earth-emitted radiance by subtracting off the reflected
        # downwelling component
        radiance = (surface_radiance -
                    (1.0 - emissivity_data) * downwelled_data)

        # Account for surface emissivity to get Plank emitted radiance
        self.logger.info('Calculating Plank emitted radiance')
        radiance_emitted = radiance / emissivity_data

        # Fix the no data locations
        no_data_locations = np.where(surface_radiance == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        no_data_locations = np.where(downwelled_data == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        no_data_locations = np.where(emissivity_data == self.no_data_value)
        radiance_emitted[no_data_locations] = self.no_data_value

        # Memory cleanup
        del (downwelled_data)
        del (emissivity_data)
        del (surface_radiance)
        del (radiance)
        del (no_data_locations)

        # Use Brightness Temperature LUT to get skin temperature
        # Read the correct one for what we are processing
        if self.satellite == 'LANDSAT_7':
            self.logger.info('Using Landsat 7 Brightness Temperature LUT')
            bt_name = 'L7_Brightness_Temperature_LUT.txt'

        elif self.satellite == 'LANDSAT_5':
            self.logger.info('Using Landsat 5 Brightness Temperature LUT')
            bt_name = 'L5_Brightness_Temperature_LUT.txt'

        bt_data = np.loadtxt(os.path.join(self.lst_data_dir, bt_name),
                             dtype=float, delimiter=' ')
        bt_radiance_LUT = bt_data[:, 1]
        bt_temp_LUT = bt_data[:, 0]

        self.logger.info('Generating LST results')
        lst_data = np.interp(radiance_emitted, bt_radiance_LUT, bt_temp_LUT)

        # Scale the result and convert it to an int16
        lst_data = lst_data * MULT_FACTOR
        lst_fata = lst_data.astype(np.int16)

        # Add the fill and scan gaps back into the results, since they may
        # have been lost
        self.logger.info('Adding fill and data gaps back into the Land'
                         ' Surface Temperature results')

        # Fix the no data locations
        no_data_locations = np.where(radiance_emitted == self.no_data_value)
        lst_data[no_data_locations] = self.no_data_value

        # Memory cleanup
        del (radiance_emitted)
        del (no_data_locations)

        product_id = self.xml_filename.split('.xml')[0]
        lst_img_filename = ''.join([product_id, '_lst', '.img'])
        lst_hdr_filename = ''.join([product_id, '_lst', '.hdr'])
        lst_aux_filename = ''.join([lst_img_filename, '.aux', '.xml'])

        self.logger.info('Creating {0}'.format(lst_img_filename))
        util.Geo.generate_raster_file(envi_driver, lst_img_filename,
                                      lst_data, x_dim, y_dim, ds_transform,
                                      ds_srs.ExportToWkt(), self.no_data_value,
                                      gdal.GDT_Int16)

        self.logger.info('Updating {0}'.format(lst_hdr_filename))
        util.Geo.update_envi_header(lst_hdr_filename, self.no_data_value)

        # Memory cleanup
        del (ds_srs)
        del (ds_transform)

        # Remove the *.aux.xml file generated by GDAL
        if os.path.exists(lst_aux_filename):
            os.unlink(lst_aux_filename)

        self.logger.info('Adding {0} to {1}'.format(lst_img_filename,
                                                    self.xml_filename))
        # Add the estimated Land Surface Temperature product to the metadata
        espa_xml = metadata_api.parse(self.xml_filename, silence=True)
        bands = espa_xml.get_bands()
        sensor_code = product_id[0:3]

        # Find the TOA Band 1 to use for the specific band details
        base_band = None
        for band in bands.band:
            if band.product == 'toa_refl' and band.name == 'toa_band1':
                base_band = band

        if base_band is None:
            raise Exception('Failed to find the TOA BLUE band'
                            ' in the input data')

        lst_band = metadata_api.band(product='lst',
                                     source='toa_refl',
                                     name='land_surface_temperature',
                                     category='image',
                                     data_type='INT16',
                                     scale_factor=SCALE_FACTOR,
                                     add_offset=0,
                                     nlines=base_band.get_nlines(),
                                     nsamps=base_band.get_nsamps(),
                                     fill_value=str(self.no_data_value))

        lst_band.set_short_name('{0}LST'.format(sensor_code))
        lst_band.set_long_name('Land Surface Temperature')
        lst_band.set_file_name(lst_img_filename)
        lst_band.set_data_units('temperature (kelvin)')

        pixel_size = metadata_api.pixel_size(base_band.pixel_size.x,
                                             base_band.pixel_size.x,
                                             base_band.pixel_size.units)
        lst_band.set_pixel_size(pixel_size)

        valid_range = metadata_api.valid_range(min=1500, max=3730)
        lst_band.set_valid_range(valid_range)

        # Set the date, but first clean the microseconds off of it
        production_date = (
            datetime.datetime.strptime(datetime.datetime.now().
                                       strftime('%Y-%m-%dT%H:%M:%S'),
                                       '%Y-%m-%dT%H:%M:%S'))

        lst_band.set_production_date(production_date)

        lst_band.set_app_version(util.Version.app_version())

        bands.add_band(lst_band)

        # Write the XML metadata file out
        with open(self.xml_filename, 'w') as fd:
            metadata_api.export(fd, espa_xml)

        # Memory cleanup
        del (lst_band)
        del (bands)
        del (espa_xml)
        del (lst_data)
Exemplo n.º 7
0
import datetime
from lxml import etree

import metadata_api


bands_element_path = '{http://espa.cr.usgs.gov/v1.0}bands'

xml = metadata_api.parse('LT50460282002042EDC01.xml', silence=True)

bands = xml.get_bands()

# Remove the L1T bands by creating a new list of all the others
bands.band[:] = [band for band in bands.band if band.product != 'L1T']

band = metadata_api.band(product="RDD", name="band1", category="image",
    data_type="UINT8", nlines="7321", nsamps="7951", fill_value="0")

band.set_short_name ("LT5DN")
band.set_long_name ("band 1 digital numbers")
band.set_file_name ("LT50460282002042EDC01_B1.img")

pixel_size = metadata_api.pixel_size ("30.000000", 30, "meters")
band.set_pixel_size (pixel_size)

band.set_data_units ("digital numbers")

valid_range = metadata_api.valid_range (min="1", max=255)
band.set_valid_range (valid_range)

toa_reflectance = metadata_api.toa_reflectance (gain=1.448, bias="-4.28819")
band.set_toa_reflectance (toa_reflectance)
    def createXML(self,
                  scene_xml_file=None,
                  output_xml_file=None,
                  start_year=None,
                  end_year=None,
                  fill_value=None,
                  imgfile=None,
                  log_handler=None):
        """Creates an XML file for the products produced by
           runAnnualBurnSummaries.
        Description: routine to create the XML file for the burned area summary
            bands.  The sample scene-based XML file will be used as the basis
            for the projection information for the output XML file.  The image
            size, extents, etc. will need to be updated, as will the band
            information.
        
        History:
          Created on May 12, 2014 by Gail Schmidt, USGS/EROS LSRD Project

        Args:
          scene_xml_file - scene-based XML file to be used as the base XML
              information for the projection metadata.
          output_xml_file - name of the XML file to be written
          start_year - starting year of the scenes to process
          end_year - ending year of the scenes to process
          fill_value - fill or nodata value for this dataset
          imgfile - name of burned area image file with associated ENVI header
              which can be used to obtain the extents and geographic
              information for these products
          log_handler - handler for the logging information
   
        Returns:
            ERROR - error creating the XML file
            SUCCESS - successful creation of the XML file
        """

        # parse the scene-based XML file, just as a basis for the output XML
        # file.  the global attributes will be similar, but the extents and
        # size of the image will be different.  the bands will be based on the
        # bands that are output from this routine.
        xml = metadata_api.parse(scene_xml_file, silence=True)
        meta_bands = xml.get_bands()
        meta_global = xml.get_global_metadata()

        # update the global information
        meta_global.set_data_provider("USGS/EROS")
        meta_global.set_satellite("LANDSAT")
        meta_global.set_instrument("combination")
        del (meta_global.acquisition_date)
        meta_global.set_acquisition_date(None)

        # open the image file to obtain the geospatial and spatial reference
        # information
        ds = gdal.Open(imgfile)
        if ds is None:
            msg = "GDAL failed to open %s" % imgfile
            logIt(msg, log_handler)
            return ERROR

        ds_band = ds.GetRasterBand(1)
        if ds_band is None:
            msg = "GDAL failed to get the first band in %s" % imgfile
            logIt(msg, log_handler)
            return ERROR
        nlines = float(ds_band.YSize)
        nsamps = float(ds_band.XSize)
        nlines_int = ds_band.YSize
        nsamps_int = ds_band.XSize
        del (ds_band)

        ds_transform = ds.GetGeoTransform()
        if ds_transform is None:
            msg = "GDAL failed to get the geographic transform information " \
                "from %s" % imgfile
            logIt(msg, log_handler)
            return ERROR

        ds_srs = osr.SpatialReference()
        if ds_srs is None:
            msg = "GDAL failed to get the spatial reference information " \
                "from %s" % imgfile
            logIt(msg, log_handler)
            return ERROR
        ds_srs.ImportFromWkt(ds.GetProjection())
        del (ds)

        # get the UL and LR center of pixel map coordinates
        (map_ul_x, map_ul_y) = convert_imageXY_to_mapXY(0.5, 0.5, ds_transform)
        (map_lr_x,
         map_lr_y) = convert_imageXY_to_mapXY(nsamps - 0.5, nlines - 0.5,
                                              ds_transform)

        # update the UL and LR projection corners along with the origin of the
        # corners, for the center of the pixel (global projection information)
        for mycorner in meta_global.projection_information.corner_point:
            if mycorner.location == 'UL':
                mycorner.set_x(map_ul_x)
                mycorner.set_y(map_ul_y)
            if mycorner.location == 'LR':
                mycorner.set_x(map_lr_x)
                mycorner.set_y(map_lr_y)
        meta_global.projection_information.set_grid_origin("CENTER")

        # update the UL and LR latitude and longitude coordinates, using the
        # center of the pixel
        srs_lat_lon = ds_srs.CloneGeogCS()
        coord_tf = osr.CoordinateTransformation(ds_srs, srs_lat_lon)
        for mycorner in meta_global.corner:
            if mycorner.location == 'UL':
                (lon, lat, height) = \
                    coord_tf.TransformPoint (map_ul_x, map_ul_y)
                mycorner.set_longitude(lon)
                mycorner.set_latitude(lat)
            if mycorner.location == 'LR':
                (lon, lat, height) = \
                    coord_tf.TransformPoint (map_lr_x, map_lr_y)
                mycorner.set_longitude(lon)
                mycorner.set_latitude(lat)

        # determine the bounding coordinates; initialize using the UL and LR
        # then work around the scene edges
        # UL
        (map_x, map_y) = convert_imageXY_to_mapXY(0.0, 0.0, ds_transform)
        (ul_lon, ul_lat, height) = coord_tf.TransformPoint(map_x, map_y)
        # LR
        (map_x, map_y) = convert_imageXY_to_mapXY(nsamps, nlines, ds_transform)
        (lr_lon, lr_lat, height) = coord_tf.TransformPoint(map_x, map_y)

        # find the min and max values accordingly, for initialization
        west_lon = min(ul_lon, lr_lon)
        east_lon = max(ul_lon, lr_lon)
        north_lat = max(ul_lat, lr_lat)
        south_lat = min(ul_lat, lr_lat)

        # traverse the boundaries of the image to determine the bounding
        # coords; traverse one extra line and sample to get the the outer
        # extents of the image vs. just the UL of the outer edge.
        # top and bottom edges
        for samp in range(0, nsamps_int + 1):
            # top edge
            (map_x, map_y) = convert_imageXY_to_mapXY(samp, 0.0, ds_transform)
            (top_lon, top_lat, height) = coord_tf.TransformPoint(map_x, map_y)

            # lower edge
            (map_x, map_y) = convert_imageXY_to_mapXY(samp, nlines,
                                                      ds_transform)
            (low_lon, low_lat, height) = coord_tf.TransformPoint(map_x, map_y)

            # update the min and max values
            west_lon = min(top_lon, low_lon, west_lon)
            east_lon = max(top_lon, low_lon, east_lon)
            north_lat = max(top_lat, low_lat, north_lat)
            south_lat = min(top_lat, low_lat, south_lat)

        # left and right edges
        for line in range(0, nlines_int + 1):
            # left edge
            (map_x, map_y) = convert_imageXY_to_mapXY(0.0, line, ds_transform)
            (left_lon, left_lat,
             height) = coord_tf.TransformPoint(map_x, map_y)

            # right edge
            (map_x, map_y) = convert_imageXY_to_mapXY(nsamps, line,
                                                      ds_transform)
            (right_lon, right_lat,
             height) = coord_tf.TransformPoint(map_x, map_y)

            # update the min and max values
            west_lon = min(left_lon, right_lon, west_lon)
            east_lon = max(left_lon, right_lon, east_lon)
            north_lat = max(left_lat, right_lat, north_lat)
            south_lat = min(left_lat, right_lat, south_lat)

        # update the XML
        bounding_coords = meta_global.get_bounding_coordinates()
        bounding_coords.set_west(west_lon)
        bounding_coords.set_east(east_lon)
        bounding_coords.set_north(north_lat)
        bounding_coords.set_south(south_lat)

        del (ds_transform)
        del (ds_srs)

        # clear some of the global information that doesn't apply for these
        # products
        del (meta_global.scene_center_time)
        meta_global.set_scene_center_time(None)
        del (meta_global.lpgs_metadata_file)
        meta_global.set_lpgs_metadata_file(None)
        del (meta_global.orientation_angle)
        meta_global.set_orientation_angle(None)
        del (meta_global.level1_production_date)
        meta_global.set_level1_production_date(None)

        # clear the solar angles
        del (meta_global.solar_angles)
        meta_global.set_solar_angles(None)

        # save the first band and then wipe the bands out so that new bands
        # can be added for the burned area bands
        myband_save = meta_bands.band[0]
        del (meta_bands.band)
        meta_bands.band = []

        # create the band information; there are 4 output products per year
        # for the burned area dataset; add enough bands to cover the products
        # and years
        #    1. first date a burned area was observed (burned_area)
        #    2. number of times burn was observed (burn_count)
        #    3. number of good looks (good_looks_count)
        #    4. maximum probability for burned area (max_burn_prob)
        nproducts = 4
        nyears = end_year - start_year + 1
        nbands = nproducts * nyears
        for i in range(0, nbands):
            # add the new band
            myband = metadata_api.band()
            meta_bands.band.append(myband)

        # how many bands are there in the new XML file
        num_scene_bands = len(meta_bands.band)
        print "New XML file has %d bands" % num_scene_bands

        # loop through the products and years to create the band metadata
        band_count = 0
        for product in range(1, nproducts + 1):
            for year in range(start_year, end_year + 1):
                myband = meta_bands.band[band_count]
                myband.set_product("burned_area")
                myband.set_short_name("LNDBA")
                myband.set_data_type("INT16")
                myband.set_pixel_size(myband_save.get_pixel_size())
                myband.set_fill_value(fill_value)
                myband.set_nlines(nlines)
                myband.set_nsamps(nsamps)
                myband.set_app_version(self.burned_area_version)
                production_date = time.strftime("%Y-%m-%dT%H:%M:%S",
                                                time.gmtime())
                myband.set_production_date (  \
                    datetime_.datetime.strptime(production_date,
                    '%Y-%m-%dT%H:%M:%S'))

                # clear some of the band-specific fields that don't apply for
                # this product (see metadata_api.py for the full list of
                # band-related values under class band)
                del (myband.source)
                myband.set_source(None)
                del (myband.saturate_value)
                myband.set_saturate_value(None)
                del (myband.scale_factor)
                myband.set_scale_factor(None)
                del (myband.add_offset)
                myband.set_add_offset(None)
                del (myband.radiance)
                myband.set_radiance(None)
                del (myband.reflectance)
                myband.set_reflectance(None)
                del (myband.thermal_const)
                myband.set_thermal_const(None)
                del (myband.bitmap_description)
                myband.set_bitmap_description(None)
                del (myband.class_values)
                myband.set_class_values(None)
                del (myband.qa_description)
                myband.set_qa_description(None)
                del (myband.calibrated_nt)
                myband.set_calibrated_nt(None)

                # handle the band-specific differences
                valid_range = metadata_api.valid_range()
                if product == 1:
                    name = "burned_area_%d" % year
                    long_name = "first DOY a burn was observed"
                    file_name = "burned_area_%d.img" % year
                    category = "image"
                    data_units = "day of year"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no burn observed"

                elif product == 2:
                    name = "burn_count_%d" % year
                    long_name = "number of times a burn was observed"
                    file_name = "burn_count_%d.img" % year
                    category = "image"
                    data_units = "count"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no burn observed"

                elif product == 3:
                    name = "good_looks_count_%d" % year
                    long_name = "number of good looks (pixels with good QA)"
                    file_name = "good_looks_count_%d.img" % year
                    category = "qa"
                    data_units = "count"
                    valid_range.min = 0
                    valid_range.max = 366
                    qa_description = "0: no valid pixels (water, cloud, " \
                        "snow, etc.)"

                elif product == 4:
                    name = "max_burn_prob_%d" % year
                    long_name = "maximum probability for burned area"
                    file_name = "max_burn_prob_%d.img" % year
                    category = "image"
                    data_units = "probability"
                    valid_range.min = 0
                    valid_range.max = 100
                    qa_description = "-9998: bad QA (water, cloud, snow, etc.)"

                myband.set_name(name)
                myband.set_long_name(long_name)
                myband.set_file_name(file_name)
                myband.set_category(category)
                myband.set_data_units(data_units)
                myband.set_valid_range(valid_range)
                myband.set_qa_description(qa_description)

                # increment the band counter
                band_count += 1

            # end for year
        # end for nproducts

        # write out a the XML file after validation
        # call the export with validation
        fd = open(output_xml_file, 'w')
        if fd == None:
            msg = "Unable to open the output XML file (%s) for writing." % \
                output_xml_file
            logIt(msg, log_handler)
            return ERROR

        metadata_api.export(fd, xml)
        fd.flush()
        fd.close()

        return SUCCESS