def Open_landsat(src_FileName, proyDEM_fileName):
    """
    This function opens a landsat image and returns the data array of a specific landsat band.
    """
    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # crop band to the DEM extent
    ls, ulx, uly, lrx, lry, epsg_to = SEBAL.reproject_dataset_example(src_FileName, proyDEM_fileName)

    # Open the cropped Landsat image for the band number
    ls_data = ls.GetRasterBand(1).ReadAsArray()
    return(ls_data)
def Open_landsat(src_FileName, proyDEM_fileName):
    """
    This function opens a landsat image and returns the data array of a specific landsat band.
    """
    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # crop band to the DEM extent
    ls, ulx, uly, lrx, lry, epsg_to = SEBAL.reproject_dataset_example(src_FileName, proyDEM_fileName)

    # Open the cropped Landsat image for the band number
    ls_data = ls.GetRasterBand(1).ReadAsArray()
    return(ls_data)
Example #3
0
def Open_reprojected_hdf(input_name, Band, epsg_to, scale_factor, Example_fileName):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    g=gdal.Open(input_name, gdal.GA_ReadOnly)

    folder_out = os.path.dirname(input_name)
    output_name_temp = os.path.join(folder_out, "temporary.tif")

    # Open and reproject
    name_in = g.GetSubDatasets()[Band][0]

    reproject_MODIS(name_in, output_name_temp, epsg_to)

    dest, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(output_name_temp, Example_fileName)
    Array = dest.GetRasterBand(1).ReadAsArray() * scale_factor

    os.remove(output_name_temp)

    return(Array)
Example #4
0
def Get_LS_Para_Veg(workbook, number, Example_fileName, year, month, day,
                    path_radiance, Apparent_atmosf_transm, cos_zn, dr):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    input_folder = r"%s" % str(ws['B%d' % number].value)
    output_folder = r"%s" % str(ws['C%d' % number].value)

    ws = workbook['Additional_Input']

    # If all additional fields are filled in than do not open the datasets
    if ws['B%d' % number].value is None or ws['C%d' % number].value is None:

        print('-------------------- Open Landsat VIS -----------------------')

        # Open the Landsat_Input sheet
        ws = workbook['Landsat_Input']

        # Extract Landsat name, number and amount of thermal bands from excel file
        Name_Landsat_Image = str(ws['B%d' % number].value)
        Landsat_nr = int(
            ws['C%d' % number].value
        )  # Type of Landsat (LS) image used (LS5, LS7, or LS8)
        # temperature: 1 = Band 6 for LS_5 & 7, Band 10 for LS_8 (optional)
        # Define bands used for each Landsat number
        if Landsat_nr == 5 or Landsat_nr == 7:
            Bands = np.array([1, 2, 3, 4, 5, 7, 6])
        elif Landsat_nr == 8:
            Bands = np.array([2, 3, 4, 5, 6, 7, 10, 11])
        else:
            print('Landsat image not supported, use Landsat 7 or 8')

        # Open MTL landsat and get the correction parameters
        Landsat_meta_fileName = os.path.join(input_folder,
                                             '%s_MTL.txt' % Name_Landsat_Image)
        Lmin, Lmax, k1_c, k2_c = info_band_metadata(Landsat_meta_fileName,
                                                    Bands)
        print('Lmin= ', Lmin)
        print('Lmax= ', Lmax)
        print('k1= ', k1_c)
        print('k2= ', k2_c)

        sensor1 = 'LS%d' % Landsat_nr
        res1 = '30m'
        res2 = '30m'
        res3 = '30m'

        # Mean solar exo-atmospheric irradiance for each band (W/m2/microm)
        # for the different Landsat images (L5, L7, or L8)
        ESUN_L5 = np.array([1983, 1796, 1536, 1031, 220, 83.44])
        ESUN_L7 = np.array([1997, 1812, 1533, 1039, 230.8, 84.9])
        ESUN_L8 = np.array([1973.28, 1842.68, 1565.17, 963.69, 245, 82.106])

        # Open one band - To get the metadata of the landsat images only once (to get the extend)
        src_FileName = os.path.join(input_folder, '%s_B2.TIF' %
                                    Name_Landsat_Image)  # before 10!
        ls, band_data, ulx, uly, lrx, lry, x_size_ls, y_size_ls = Get_Extend_Landsat(
            src_FileName)
        print('Original LANDSAT Image - ')
        print('  Size :', x_size_ls, y_size_ls)
        print('  Upper Left corner x, y: ', ulx, ', ', uly)
        print('  Lower right corner x, y: ', lrx, ', ', lry)

        lsc, ulx, uly, lrx, lry, epsg_to = SEBAL.reproject_dataset_example(
            src_FileName, Example_fileName)

        #	Get the extend of the remaining landsat file	after clipping based on the DEM file
        y_size_lsc = lsc.RasterYSize
        x_size_lsc = lsc.RasterXSize
        shape_lsc = [x_size_lsc, y_size_lsc]

        print('--- ')
        print('Cropped LANDSAT Image - ')
        print('  Size :', x_size_lsc, y_size_lsc)
        print('  Upper Left corner x, y: ', ulx, ', ', uly)
        print('  Lower right corner x, y: ', lrx, ', ', lry)

        # if landsat 5 or 7 is used then first create a mask for removing the no data stripes
        if Landsat_nr == 5 or Landsat_nr == 7:
            src_FileName = os.path.join(
                input_folder,
                '%s_B6.TIF' % (Name_Landsat_Image))  #open smallest band
            if not os.path.exists(src_FileName):
                src_FileName = os.path.join(
                    input_folder, '%s_B6_VCID_2.TIF' % (Name_Landsat_Image))
            src_FileName_2 = os.path.join(
                input_folder,
                '%s_B1.TIF' % (Name_Landsat_Image))  #open smallest band
            src_FileName_3 = os.path.join(
                input_folder,
                '%s_B3.TIF' % (Name_Landsat_Image))  #open smallest band
            src_FileName_4 = os.path.join(
                input_folder,
                '%s_B4.TIF' % (Name_Landsat_Image))  #open smallest band
            src_FileName_5 = os.path.join(
                input_folder,
                '%s_B7.TIF' % (Name_Landsat_Image))  #open smallest band
            src_FileName_6 = os.path.join(
                input_folder,
                '%s_B2.TIF' % (Name_Landsat_Image))  #open smallest band
            src_FileName_7 = os.path.join(
                input_folder,
                '%s_B5.TIF' % (Name_Landsat_Image))  #open smallest band
            ls_data = Open_landsat(src_FileName, Example_fileName)
            ls_data_2 = Open_landsat(src_FileName_2, Example_fileName)
            ls_data_3 = Open_landsat(src_FileName_3, Example_fileName)
            ls_data_4 = Open_landsat(src_FileName_4, Example_fileName)
            ls_data_5 = Open_landsat(src_FileName_5, Example_fileName)
            ls_data_6 = Open_landsat(src_FileName_6, Example_fileName)
            ls_data_7 = Open_landsat(src_FileName_7, Example_fileName)

            # create and save the landsat mask for all images based on band 11 (smallest map)
            QC_Map = np.zeros((shape_lsc[1], shape_lsc[0]))
            QC_Map = np.where(
                np.logical_or.reduce(
                    (ls_data == 0, ls_data_2 == 0, ls_data_3 == 0,
                     ls_data_4 == 0, ls_data_5 == 0, ls_data_6 == 0,
                     ls_data_7 == 0)), 1, 0)

        # If landsat 8 then use landsat band 10 and 11
        elif Landsat_nr == 8:
            src_FileName_11 = os.path.join(
                input_folder,
                '%s_B11.TIF' % (Name_Landsat_Image))  #open smallest band
            ls_data_11 = Open_landsat(src_FileName_11, Example_fileName)

            src_FileName_10 = os.path.join(
                input_folder,
                '%s_B10.TIF' % (Name_Landsat_Image))  #open smallest band
            ls_data_10 = Open_landsat(src_FileName_10, Example_fileName)

            # create and save the landsat mask for all images based on band 10 and 11
            QC_Map = np.zeros((shape_lsc[1], shape_lsc[0]))
            QC_Map = np.where(np.logical_or(ls_data_11 == 0, ls_data_10 == 0),
                              1, 0)

        else:
            print('Landsat image not supported, use Landsat 7 or 8')

        # Open data of the landsat mask
        ls_data = Open_landsat(src_FileName, Example_fileName)

        # Create 3D array to store Spectral radiance and Reflectivity for each band
        Reflect, Spec_Rad = Landsat_Reflect(Bands, input_folder,
                                            Name_Landsat_Image, output_folder,
                                            shape_lsc, QC_Map, Lmax, Lmin,
                                            ESUN_L5, ESUN_L7, ESUN_L8, cos_zn,
                                            dr, Landsat_nr, Example_fileName)

        # save spectral data
        for i in range(0, 6):
            spec_ref_fileName = os.path.join(
                output_folder, 'Output_radiation_balance',
                '%s_spectral_reflectance_B%s_%s_%s%02d%02d.tif' %
                (sensor1, Bands[i], res3, year, month, day))
            SEBAL.save_GeoTiff_proy(lsc,
                                    Reflect[:, :, i],
                                    spec_ref_fileName,
                                    shape_lsc,
                                    nband=1)

    else:
        # Get General information example file
        lsc = gdal.Open(Example_fileName)
        nrow = lsc.RasterYSize
        ncol = lsc.RasterXSize
        shape_lsc = [ncol, nrow]

    ######################### Calculate Vegetation Parameters Based on VIS data #####################################

    # Open the Additional input excel sheet
    ws = workbook['Additional_Input']

    # Check NDVI and Calculate NDVI
    try:
        if (ws['B%d' % number].value) is not None:

            # Output folder NDVI
            ndvi_fileName_user = os.path.join(
                output_folder, 'Output_vegetation',
                'User_NDVI_%s_%s%02d%02d.tif' % (res3, year, month, day))
            NDVI = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['B%d' % number].value), ndvi_fileName_user,
                Example_fileName)

            water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
            water_mask_temp[NDVI < 0.0] = 1.0
            SEBAL.save_GeoTiff_proy(lsc,
                                    NDVI,
                                    ndvi_fileName_user,
                                    shape_lsc,
                                    nband=1)

        else:
            # use the Landsat reflectance to calculate the surface albede, NDVI
            NDVI = SEBAL.Calc_NDVI(Reflect)

            # Calculate temporal water mask
            water_mask_temp = SEBAL.Water_Mask(shape_lsc, Reflect)

    except:
        assert "Please check the NDVI input path"

    # Check Water Mask and replace if it is filled in the additianal data sheet
    try:
        if (ws['E%d' % number].value) is not None:

            # Overwrite the Water mask and change the output name
            water_mask_temp_fileName = os.path.join(
                output_folder, 'Output_soil_moisture',
                'User_Water_mask_temporary_%s_%s%02d%02d.tif' %
                (res2, year, month, day))
            water_mask_temp = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['E%d' % number].value),
                water_mask_temp_fileName, Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc,
                                    water_mask_temp,
                                    water_mask_temp_fileName,
                                    shape_lsc,
                                    nband=1)

    except:
        assert "Please check the Water Mask input path"

    # Check Surface albedo
    try:
        if (ws['C%d' % number].value) is not None:

            # Output folder surface albedo
            surface_albedo_fileName = os.path.join(
                output_folder, 'Output_vegetation',
                'User_surface_albedo_%s_%s%02d%02d.tif' %
                (res2, year, month, day))
            Surf_albedo = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['C%d' % number].value), surface_albedo_fileName,
                Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc,
                                    Surf_albedo,
                                    surface_albedo_fileName,
                                    shape_lsc,
                                    nband=1)

        else:

            # use the Landsat reflectance to calculate the surface albede, NDVI
            Surf_albedo = SEBAL.Calc_albedo(Reflect, path_radiance,
                                            Apparent_atmosf_transm, Landsat_nr)

    except:
        assert "Please check the Albedo input path"

    # calculate vegetation properties
    FPAR, tir_emis, Nitrogen, vegt_cover, LAI, b10_emissivity = SEBAL.Calc_vegt_para(
        NDVI, water_mask_temp)

    print('Average NDVI = %s' % np.nanmean(NDVI))
    print('Average Surface Albedo = %s' % np.nanmean(Surf_albedo))
    print('Average LAI = %s' % np.nanmean(LAI))
    print('Average Vegetation Cover = %s' % np.nanmean(vegt_cover))
    print('Average FPAR = %s' % np.nanmean(FPAR))

    if not "QC_Map" in locals():
        QC_Map = np.zeros((shape_lsc[1], shape_lsc[0]))

    return (Surf_albedo, NDVI, LAI, vegt_cover, FPAR, Nitrogen, tir_emis,
            b10_emissivity, water_mask_temp, QC_Map)
Example #5
0
def Get_USER_Para_Veg(workbook, number, Example_fileName, year, month, day):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    output_folder = r"%s" %str(ws['C%d' %number].value)

    ws = workbook['Additional_Input']
    
    # Define the bands that will be used
    res2 = '10m'
    res3 = '10m'    
 
    # Get General information example file
    lsc = gdal.Open(Example_fileName)
    nrow = lsc.RasterYSize
    ncol = lsc.RasterXSize
    shape_lsc = [ncol, nrow]

    ######################### Calculate Vegetation Parameters Based on VIS data #####################################

    # Extract the name of the NDVI USER image from the excel file
    ws = workbook['USER_Input']
    src_FileName_NDVI = r"%s" %str(ws['B%d' %number].value)                #NDVI

    # Open the Additional input excel sheet
    ws = workbook['Additional_Input']

    # Check NDVI and Calculate NDVI
    try:
        if (ws['B%d' % number].value) is None:    
            
            # Open User Albedo
            dest_NDVI = SEBAL.reproject_dataset_example(src_FileName_NDVI, Example_fileName)[0]
            NDVI = dest_NDVI.GetRasterBand(1).ReadAsArray()     

            # Create Water mask based on PROBA-V
            water_mask_temp = np.where(NDVI < 0.0, 1.0, 0.0) 
            
        else:
            # Output folder NDVI
            if not os.path.exists(os.path.join(output_folder, 'Output_vegetation')):
                os.makedirs(os.path.join(output_folder, 'Output_vegetation'))
            ndvi_fileName_user = os.path.join(output_folder, 'Output_vegetation', 'User_NDVI_%s_%s%02d%02d.tif' %(res3, year, month, day))
            NDVI = SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['B%d' % number].value),ndvi_fileName_user,Example_fileName)
            water_mask_temp = np.where(NDVI < 0.0, 1.0, 0.0)           
            
    except:
        print("Please check the NDVI input path")

    # Check Water Mask and replace if it is filled in the additianal data sheet
    try:
        if (ws['E%d' % number].value) is not None:
            
            if not os.path.exists(os.path.join(output_folder, 'Output_soil_moisture')):
                os.makedirs(os.path.join(output_folder, 'Output_soil_moisture')) 
                
            # Overwrite the Water mask and change the output name
            water_mask_temp_fileName = os.path.join(output_folder, 'Output_soil_moisture', 'User_Water_mask_temporary_%s_%s%02d%02d.tif' %(res2, year, month, day))
            water_mask_temp = SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['E%d' % number].value), water_mask_temp_fileName, Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc, water_mask_temp, water_mask_temp_fileName, shape_lsc, nband=1)

    except:
        print("Please check the Water Mask input path")

    # Extract the name of the surface albedo USER image from the excel file
    ws = workbook['USER_Input']
    src_FileName_Surf_albedo = r"%s" %str(ws['C%d' %number].value)                #surface albedo
    
    ws = workbook['Additional_Input']
    # Check Surface albedo
    try:
        if (ws['C%d' % number].value) is not None:

            # Output folder surface albedo
            if not os.path.exists(os.path.join(output_folder, 'Output_vegetation')):
                os.makedirs(os.path.join(output_folder, 'Output_vegetation'))
            surface_albedo_fileName = os.path.join(output_folder, 'Output_vegetation','User_surface_albedo_%s_%s%02d%02d.tif' %(res2, year, month, day))
            Surf_albedo=SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['C%d' % number].value),surface_albedo_fileName,Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc, Surf_albedo, surface_albedo_fileName, shape_lsc, nband=1)

        else:

            # Open User Albedo
            dest_Surf_albedo = SEBAL.reproject_dataset_example(src_FileName_Surf_albedo, Example_fileName)[0]
            Surf_albedo = dest_Surf_albedo.GetRasterBand(1).ReadAsArray()     
            
            # Set limit surface albedo
            Surf_albedo = np.minimum(Surf_albedo, 0.6)

    except:
          print("Please check the Albedo input path")

    # calculate vegetation properties
    FPAR,tir_emis,Nitrogen,vegt_cover,LAI,b10_emissivity=SEBAL.Calc_vegt_para(NDVI, water_mask_temp)

    # create quality map
    QC_Map = np.zeros(NDVI.shape)
    QC_Map[np.isnan(NDVI)] = 1

    print('Average NDVI = %s' %np.nanmean(NDVI))
    print('Average Surface Albedo = %s' %np.nanmean(Surf_albedo))
    print('Average LAI = %s' %np.nanmean(LAI))
    print('Average Vegetation Cover = %s' %np.nanmean(vegt_cover))
    print('Average FPAR = %s' %np.nanmean(FPAR))

    return(Surf_albedo, NDVI, LAI, vegt_cover, FPAR, Nitrogen, tir_emis, water_mask_temp, QC_Map)
Example #6
0
def Get_USER_Para_Thermal(workbook, number, Example_fileName, year, month, day, water_mask_temp, surf_temp_offset, Thermal_Sharpening_not_needed):
    
    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    output_folder = r"%s" %str(ws['C%d' %number].value)

    # Open General information example file
    lsc = gdal.Open(Example_fileName)
    nrow = lsc.RasterYSize
    ncol = lsc.RasterXSize
    shape_lsc = [ncol, nrow]

    # General sensor resolution and names'
    res2 = '250m'

    # Extract the name of the thermal and quality MODIS image from the excel file
    ws = workbook['USER_Input']
    src_FileName_LST = r"%s" %str(ws['D%d' %number].value)                #land surface temperature

    ws = workbook['Additional_Input']

    try:
        # If all additional fields are filled in than do not open the datasets
        if ws['D%d' % number].value is None:

            print('...................... Open USER Thermal ........................')

            # Open User LST
            dest_Surface_temp = SEBAL.reproject_dataset_example(src_FileName_LST, Example_fileName)[0]
            Surface_temp = dest_Surface_temp.GetRasterBand(1).ReadAsArray()
            Thermal_Sharpening_not_needed = 1
            
        else:

            # Output folder surface temperature
            surf_temp_fileName = os.path.join(output_folder, 'Output_vegetation','User_surface_temp_%s_%s%02d%02d.tif' %(res2, year, month, day))
            Surface_temp = SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['D%d' % number].value), surf_temp_fileName, Example_fileName)
            Thermal_Sharpening_not_needed = 1

    except:
        assert "Please check the surface temperature input path"


    # Cloud mask:
    temp_water = np.zeros((shape_lsc[1], shape_lsc[0]))
    temp_water = np.copy(Surface_temp)
    temp_water[water_mask_temp == 0.0] = np.nan
    temp_water_sd = np.nanstd(temp_water)     # Standard deviation
    temp_water_mean = np.nanmean(temp_water)  # Mean
    print('Mean water temperature = ', '%0.3f (Kelvin)' % temp_water_mean)
    print('SD water temperature = ', '%0.3f (Kelvin)' % temp_water_sd)
    cloud_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
    cloud_mask_temp[Surface_temp < np.minimum((temp_water_mean - 1.0 * temp_water_sd -
               surf_temp_offset),290)] = 1.0
    cloud_mask_temp[np.isnan(Surface_temp)] = 1

    # remove wrong values VIIRS defined by user
    Surface_temp[cloud_mask_temp == 1] = np.nan
    print('Mean Surface Temperature = %s Kelvin' %np.nanmean(Surface_temp))
    
    return(Surface_temp, cloud_mask_temp, Thermal_Sharpening_not_needed)
Example #7
0
def Open_PROBAV_Reflectance(Name_PROBAV_Image, input_folder, output_folder,
                            Example_fileName):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Define bands probav
    bands = ['SM', 'B1', 'B2', 'B3', 'B4']  #'SM', 'BLUE', 'RED', 'NIR', 'SWIR'

    # Get extension of PROBAV dataset
    Name_PROBAV, Name_PROBAV_exe = os.path.splitext(Name_PROBAV_Image)

    # Set the index number at 0
    index = 0

    # constants
    n188_float = 248

    if (Name_PROBAV_exe == '.hdf5' or Name_PROBAV_exe == '.HDF5'):

        # write the data one by one to the spectral_reflectance_PROBAV
        for bandnmr in bands:

            # Translate the PROBA-V names to the Landsat band names
            Band_number = {'SM': 7, 'B1': 8, 'B2': 10, 'B3': 9, 'B4': 11}

            # Open the hdf file
            Band_PROBAVhdf_fileName = os.path.join(input_folder,
                                                   Name_PROBAV_Image)
            g = gdal.Open(Band_PROBAVhdf_fileName, gdal.GA_ReadOnly)

            #  Define temporary file out and band name in
            name_out = os.path.join(input_folder,
                                    '%s_test.tif' % (Name_PROBAV_Image))
            name_in = g.GetSubDatasets()[Band_number[bandnmr]][0]

            # Get environmental variable
            SEBAL_env_paths = os.environ["SEBAL"].split(';')
            GDAL_env_path = SEBAL_env_paths[0]
            GDAL_TRANSLATE = os.path.join(GDAL_env_path, 'gdal_translate.exe')

            # run gdal translate command
            FullCmd = '%s -of GTiff %s %s' % (GDAL_TRANSLATE, name_in,
                                              name_out)
            SEBAL.Run_command_window(FullCmd)

            # Get the data array
            dest_PV = gdal.Open(name_out)
            Data = dest_PV.GetRasterBand(1).ReadAsArray()
            dest_PV = None

            # Define the x and y spacing
            Meta_data = g.GetMetadata()
            Lat_Top = float(Meta_data['LEVEL3_GEOMETRY_TOP_RIGHT_LATITUDE'])
            Lon_Left = float(
                Meta_data['LEVEL3_GEOMETRY_BOTTOM_LEFT_LONGITUDE'])
            Pixel_size = float(
                (Meta_data['LEVEL3_GEOMETRY_VNIR_VAA_MAPPING']).split(' ')[-3])
            x_size_pv = int(Data.shape[1])
            y_size_pv = int(Data.shape[0])
            ulx = Lon_Left - 0.5 * Pixel_size
            uly = Lat_Top + 0.5 * Pixel_size
            lrx = ulx + x_size_pv * Pixel_size
            lry = uly - y_size_pv * Pixel_size

            # Define the georeference of the PROBA-V data
            geo_PROBAV = [
                Lon_Left - 0.5 * Pixel_size, Pixel_size, 0,
                Lat_Top + 0.5 * Pixel_size, 0, -Pixel_size
            ]  #0.000992063492063

            ################################# Create a MEMORY file ##############################
            # create memory output with the PROBA-V band
            fmt = 'MEM'
            driver = gdal.GetDriverByName(fmt)
            dst_dataset = driver.Create('', int(Data.shape[1]),
                                        int(Data.shape[0]), 1,
                                        gdal.GDT_Float32)
            dst_dataset.SetGeoTransform(geo_PROBAV)

            # set the reference info
            srs = osr.SpatialReference()
            srs.SetWellKnownGeogCS("WGS84")
            dst_dataset.SetProjection(srs.ExportToWkt())

            # write the array in the geotiff band
            dst_dataset.GetRasterBand(1).WriteArray(Data)

            ################################# reproject PROBAV MEMORY file ##############################

            # Reproject the PROBA-V band  to match DEM's resolution
            PROBAV, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                dst_dataset, Example_fileName)

            dst_dataset = None

            #################################### Get example information ################################

            if not "shape_lsc" in locals():
                nrow = PROBAV.RasterYSize
                ncol = PROBAV.RasterXSize
                shape_lsc = [ncol, nrow]

            # Open the reprojected PROBA-V band data
            data_PROBAV_DN = PROBAV.GetRasterBand(1).ReadAsArray(
                0, 0, ncol, nrow)

            # Define the filename to store the cropped Landsat image
            dst_FileName = os.path.join(output_folder,
                                        'Output_radiation_balance',
                                        'proy_PROBAV_%s.tif' % bandnmr)

            # close the PROBA-V
            g = None

            if not "spectral_reflectance_PROBAV" in locals():
                spectral_reflectance_PROBAV = np.zeros(
                    [shape_lsc[1], shape_lsc[0], 5])

            # If the band data is the SM band than write the data into the spectral_reflectance_PROBAV and create cloud mask
            if bandnmr is 'SM':
                cloud_mask_temp = np.zeros(data_PROBAV_DN.shape)
                cloud_mask_temp[data_PROBAV_DN[:, :] != n188_float] = 1
                spectral_reflectance_PROBAV[:, :, index] = cloud_mask_temp

            # If the band data is not SM change the DN values into PROBA-V values and write into the spectral_reflectance_PROBAV
            else:
                data_PROBAV = data_PROBAV_DN / 2000
                spectral_reflectance_PROBAV[:, :, index] = data_PROBAV[:, :]

            # Change the spectral reflectance to meet certain limits
            spectral_reflectance_PROBAV[:, :, index] = np.where(
                spectral_reflectance_PROBAV[:, :, index] <= 0, np.nan,
                spectral_reflectance_PROBAV[:, :, index])
            spectral_reflectance_PROBAV[:, :, index] = np.where(
                spectral_reflectance_PROBAV[:, :, index] >= 150, np.nan,
                spectral_reflectance_PROBAV[:, :, index])

            # Save the PROBA-V as a tif file
            SEBAL.save_GeoTiff_proy(PROBAV,
                                    spectral_reflectance_PROBAV[:, :, index],
                                    dst_FileName,
                                    shape_lsc,
                                    nband=1)

            # Go to the next index
            index = index + 1

            # Remove temporary file
            os.remove(name_out)

    else:
        for bandnmr in bands:

            # Open information
            Band_PROBAVhdf_fileName = os.path.join(
                input_folder, "%s_%s.tif" % (Name_PROBAV_Image, bandnmr))
            g = gdal.Open(Band_PROBAVhdf_fileName, gdal.GA_ReadOnly)

            # Open original raster band
            x_size_pv = g.RasterXSize
            y_size_pv = g.RasterYSize
            geo_out = g.GetGeoTransform()
            ulx = geo_out[0]
            uly = geo_out[3]
            xDist = geo_out[1]
            yDist = geo_out[5]
            lrx = ulx + x_size_pv * xDist
            lry = uly - y_size_pv * yDist

            # Reproject the PROBA-V band  to match DEM's resolution
            PROBAV, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                g, Example_fileName)

            data_PROBAV = PROBAV.GetRasterBand(1).ReadAsArray()

            # Define example array properties
            if not "shape_lsc" in locals():
                nrow = PROBAV.RasterYSize
                ncol = PROBAV.RasterXSize
                shape_lsc = [ncol, nrow]

            # Define spectal reflactance empty array
            if not "spectral_reflectance_PROBAV" in locals():
                spectral_reflectance_PROBAV = np.zeros(
                    [shape_lsc[1], shape_lsc[0], 5])

            # If the band data is the SM band than write the data into the spectral_reflectance_PROBAV and create cloud mask
            if bandnmr is 'SM':
                cloud_mask_temp = np.zeros(data_PROBAV.shape)
                cloud_mask_temp[data_PROBAV[:, :] != n188_float] = 1
                spectral_reflectance_PROBAV[:, :, index] = cloud_mask_temp

            # If the band data is not SM change the DN values into PROBA-V values and write into the spectral_reflectance_PROBAV
            else:
                spectral_reflectance_PROBAV[:, :,
                                            index] = data_PROBAV[:, :] / 2000

            # Define the filename to store the cropped Landsat image
            dst_FileName = os.path.join(output_folder,
                                        'Output_radiation_balance',
                                        'proy_PROBAV_%s.tif' % bandnmr)

            # Define shape
            if not "shape_lsc" in locals():
                nrow = PROBAV.RasterYSize
                ncol = PROBAV.RasterXSize
                shape_lsc = [ncol, nrow]

            SEBAL.save_GeoTiff_proy(PROBAV,
                                    spectral_reflectance_PROBAV[:, :, index],
                                    dst_FileName,
                                    shape_lsc,
                                    nband=1)

            # Go to the next index
            index = index + 1

    # Original size PROBAV dataset
    print('Original PROBA-V Image - ')
    print('  Size :', x_size_pv, y_size_pv)
    print('  Upper Left corner x, y: ', ulx, ', ', uly)
    print('  Lower right corner x, y: ', lrx, ', ', lry)

    print('Reprojected PROBA-V Image - ')
    print('  Size :', shape_lsc[1], shape_lsc[0])
    print('  Upper Left corner x, y: ', ulx_dem, ', ', uly_dem)
    print('  Lower right corner x, y: ', lrx_dem, ', ', lry_dem)

    return (spectral_reflectance_PROBAV, cloud_mask_temp)
Example #8
0
def Get_VIIRS_Para_Thermal(workbook, number, Example_fileName, year, month,
                           day, water_mask_temp, b10_emissivity, Temp_inst, Rp,
                           tau_sky, surf_temp_offset,
                           Thermal_Sharpening_not_needed):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    input_folder = r"%s" % str(ws['B%d' % number].value)
    output_folder = r"%s" % str(ws['C%d' % number].value)

    # Open General information example file
    lsc = gdal.Open(Example_fileName)
    nrow = lsc.RasterYSize
    ncol = lsc.RasterXSize
    shape_lsc = [ncol, nrow]

    # Open the VIIRS_PROBAV_Input sheet
    ws = workbook['VIIRS_PROBAV_Input']

    # Extract VIIRS name from input excel
    Name_VIIRS_Image = str(ws['B%d' % number].value)
    Name_VIIRS_Image_QC = str(ws['C%d' % number].value)
    k1 = 606.399172
    k2 = 1258.78
    sensor1 = 'PROBAV'
    sensor2 = 'VIIRS'
    res1 = '375m'
    res2 = '100m'
    res3 = '30m'

    # Open the Landsat_Input sheet
    ws = workbook['Additional_Input']

    try:
        # If all additional fields are filled in than do not open the datasets
        if ws['D%d' % number].value is None:

            print(
                '...................... Open VIIRS Thermal ........................'
            )

            # Define the VIIRS thermal data name
            VIIRS_data_name = os.path.join(input_folder,
                                           '%s' % (Name_VIIRS_Image))

            # Reproject VIIRS thermal data
            VIIRS, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                VIIRS_data_name, Example_fileName)

            # Open VIIRS thermal data
            data_VIIRS = VIIRS.GetRasterBand(1).ReadAsArray()

            # Set the conditions for the brightness temperature
            brightness_temp = np.where(data_VIIRS >= 250, data_VIIRS, np.nan)

            # Constants
            L_lambda_b10 = ((2 * 6.63e-34 * (3.0e8)**2) /
                            ((11.45e-6)**5 * (np.exp(
                                (6.63e-34 * 3e8) /
                                (1.38e-23 *
                                 (11.45e-6) * brightness_temp)) - 1))) * 1e-6

            # Get Surface Temperature
            Surface_temp = SEBAL.Get_Thermal(L_lambda_b10, Rp, Temp_inst,
                                             tau_sky, b10_emissivity, k1, k2)
            Surface_temp = Surface_temp.clip(230.0, 360.0)

        else:

            # Output folder surface temperature
            surf_temp_fileName = os.path.join(
                output_folder, 'Output_vegetation',
                'User_surface_temp_%s_%s%02d%02d.tif' %
                (res2, year, month, day))
            Surface_temp = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['D%d' % number].value), surf_temp_fileName,
                Example_fileName)
            Thermal_Sharpening_not_needed = 1

    except:
        assert "Please check the surface temperature input path"

    try:
        # Check Quality
        if Name_VIIRS_Image_QC != 'None':

            # Define the VIIRS Quality data name
            VIIRS_data_name = os.path.join(input_folder,
                                           '%s' % (Name_VIIRS_Image_QC))

            # Reproject VIIRS Quality data
            dest_VIIRS_QC, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                VIIRS_data_name, Example_fileName)

            # Open VIIRS Quality data
            cloud_mask_temp = dest_VIIRS_QC.GetRasterBand(1).ReadAsArray()

        else:

            # Cloud mask:
            temp_water = np.zeros((shape_lsc[1], shape_lsc[0]))
            temp_water = np.copy(Surface_temp)
            temp_water[water_mask_temp == 0.0] = np.nan
            temp_water_sd = np.nanstd(temp_water)  # Standard deviation
            temp_water_mean = np.nanmean(temp_water)  # Mean
            print('Mean water temperature = ',
                  '%0.3f (Kelvin)' % temp_water_mean)
            print('SD water temperature = ', '%0.3f (Kelvin)' % temp_water_sd)
            cloud_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
            cloud_mask_temp[Surface_temp < np.minimum(
                (temp_water_mean - 1.0 * temp_water_sd -
                 surf_temp_offset), 290)] = 1.0
    except:
        assert "Please check the QC temperature input path"

    # remove wrong values VIIRS defined by user
    Surface_temp[cloud_mask_temp == 1] = np.nan
    print('Mean Surface Temperature = %s Kelvin' % np.nanmean(Surface_temp))

    return (Surface_temp, cloud_mask_temp, Thermal_Sharpening_not_needed)
def Get_VIIRS_Para_Thermal(workbook, number, Example_fileName, year, DOY, water_mask_temp, b10_emissivity, Temp_inst,  Rp, tau_sky, surf_temp_offset, Thermal_Sharpening_not_needed):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    input_folder = r"%s" %str(ws['B%d' %number].value)
    output_folder = r"%s" %str(ws['C%d' %number].value)

    # Open General information example file
    lsc = gdal.Open(Example_fileName)
    nrow = lsc.RasterYSize
    ncol = lsc.RasterXSize
    shape_lsc = [ncol, nrow]

    # Open the VIIRS_PROBAV_Input sheet
    ws = workbook['VIIRS_PROBAV_Input']

    # Extract VIIRS name from input excel
    Name_VIIRS_Image = str(ws['B%d' %number].value)
    Name_VIIRS_Image_QC = str(ws['C%d' %number].value)
    k1=606.399172
    k2=1258.78
    sensor1 = 'PROBAV'
    sensor2 = 'VIIRS'
    res1 = '375m'
    res2 = '100m'
    res3 = '30m'

    # Open the Landsat_Input sheet
    ws = workbook['Additional_Input']

    try:
        # If all additional fields are filled in than do not open the datasets
        if ws['D%d' % number].value is None:

            print('...................... Open VIIRS Thermal ........................')

             # Define the VIIRS thermal data name
            VIIRS_data_name=os.path.join(input_folder, '%s' % (Name_VIIRS_Image))

            # Reproject VIIRS thermal data
            VIIRS, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
       	                        VIIRS_data_name, Example_fileName)

            # Open VIIRS thermal data
            data_VIIRS = VIIRS.GetRasterBand(1).ReadAsArray()

            # Set the conditions for the brightness temperature
            brightness_temp=np.where(data_VIIRS>=250, data_VIIRS, np.nan)

            # Constants
            L_lambda_b10 = ((2*6.63e-34*(3.0e8)**2)/((11.45e-6)**5*(np.exp((6.63e-34*3e8)/(1.38e-23*(11.45e-6)*brightness_temp))-1)))*1e-6

            # Get Surface Temperature
            Surface_temp = SEBAL.Get_Thermal(L_lambda_b10, Rp, Temp_inst, tau_sky, b10_emissivity, k1, k2)
            Surface_temp = Surface_temp.clip(230.0, 360.0)

        else:
            surf_temp_fileName = os.path.join(output_folder, 'Output_vegetation','User_surface_temp_%s_%s_%s.tif' %(res2, year, DOY))
            Surface_temp = SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['D%d' % number].value),surf_temp_fileName, Example_fileName)
            Thermal_Sharpening_not_needed = 1

    except:
        assert "Please check the surface temperature input path"

    try:
        # Check Quality
        if Name_VIIRS_Image_QC != 'None':

            # Define the VIIRS Quality data name
            VIIRS_data_name=os.path.join(input_folder, '%s' % (Name_VIIRS_Image_QC))

            # Reproject VIIRS Quality data
            dest_VIIRS_QC, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                                           VIIRS_data_name, Example_fileName)

            # Open VIIRS Quality data
            cloud_mask_temp = dest_VIIRS_QC.GetRasterBand(1).ReadAsArray()

        else:

            # Cloud mask:
            temp_water = np.zeros((shape_lsc[1], shape_lsc[0]))
            temp_water = np.copy(Surface_temp)
            temp_water[water_mask_temp == 0.0] = np.nan
            temp_water_sd = np.nanstd(temp_water)     # Standard deviation
            temp_water_mean = np.nanmean(temp_water)  # Mean
            print('Mean water temperature = ', '%0.3f (Kelvin)' % temp_water_mean)
            print('SD water temperature = ', '%0.3f (Kelvin)' % temp_water_sd)
def Get_PROBAV_Para_Veg(workbook, number, Example_fileName, year, DOY,
                        path_radiance, Apparent_atmosf_transm, cos_zn, dr,
                        DEM_resh):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    input_folder = r"%s" % str(ws['B%d' % number].value)
    output_folder = r"%s" % str(ws['C%d' % number].value)

    ws = workbook['Additional_Input']

    # If all additional fields are filled in than do not open the datasets
    if ws['B%d' % number].value is None or ws['C%d' % number].value is None:

        print(
            '--------------------- Open PROBA-V VIS ------------------------')

        # Open the Landsat_Input sheet
        ws = workbook['VIIRS_PROBAV_Input']

        Name_PROBAV_Image = '%s' % str(
            ws['D%d' % number].value)  # Must be a tiff file

        # Define the bands that will be used
        bands = ['SM', 'B1', 'B2', 'B3',
                 'B4']  #'SM', 'BLUE', 'RED', 'NIR', 'SWIR'
        sensor1 = 'PROBAV'
        sensor2 = 'VIIRS'
        res1 = '375m'
        res2 = '100m'
        res3 = '30m'

        # Set the index number at 0
        index = 0

        # constants
        n188_float = 248  # Now it is 248, but we do not exactly know what this really means and if this is for constant for all images.

        # write the data one by one to the spectral_reflectance_PROBAV
        for bandnmr in bands:

            # Translate the PROBA-V names to the Landsat band names
            Band_number = {'SM': 7, 'B1': 8, 'B2': 10, 'B3': 9, 'B4': 11}

            # Open the hdf file
            Band_PROBAVhdf_fileName = os.path.join(
                input_folder, '%s.HDF5' % (Name_PROBAV_Image))
            g = gdal.Open(Band_PROBAVhdf_fileName, gdal.GA_ReadOnly)

            #  Define temporary file out and band name in
            name_out = os.path.join(input_folder,
                                    '%s_test.tif' % (Name_PROBAV_Image))
            name_in = g.GetSubDatasets()[Band_number[bandnmr]][0]

            # Get environmental variable
            SEBAL_env_paths = os.environ["SEBAL"].split(';')
            GDAL_env_path = SEBAL_env_paths[0]
            GDAL_TRANSLATE = os.path.join(GDAL_env_path, 'gdal_translate.exe')

            # run gdal translate command
            FullCmd = '%s -of GTiff %s %s' % (GDAL_TRANSLATE, name_in,
                                              name_out)
            SEBAL.Run_command_window(FullCmd)

            # Get the data array
            dest_PV = gdal.Open(name_out)
            Data = dest_PV.GetRasterBand(1).ReadAsArray()
            dest_PV = None

            # Remove temporary file
            os.remove(name_out)

            # Define the x and y spacing
            Meta_data = g.GetMetadata()
            Lat_Top = float(Meta_data['LEVEL3_GEOMETRY_TOP_RIGHT_LATITUDE'])
            Lon_Left = float(
                Meta_data['LEVEL3_GEOMETRY_BOTTOM_LEFT_LONGITUDE'])
            Pixel_size = float(
                (Meta_data['LEVEL3_GEOMETRY_VNIR_VAA_MAPPING']).split(' ')[-3])

            # Define the georeference of the PROBA-V data
            geo_PROBAV = [
                Lon_Left - 0.5 * Pixel_size, Pixel_size, 0,
                Lat_Top + 0.5 * Pixel_size, 0, -Pixel_size
            ]  #0.000992063492063

            ################################# Create a MEMORY file ##############################
            # create memory output with the PROBA-V band
            fmt = 'MEM'
            driver = gdal.GetDriverByName(fmt)
            dst_dataset = driver.Create('', int(Data.shape[1]),
                                        int(Data.shape[0]), 1,
                                        gdal.GDT_Float32)
            dst_dataset.SetGeoTransform(geo_PROBAV)

            # set the reference info
            srs = osr.SpatialReference()
            srs.SetWellKnownGeogCS("WGS84")
            dst_dataset.SetProjection(srs.ExportToWkt())

            # write the array in the geotiff band
            dst_dataset.GetRasterBand(1).WriteArray(Data)

            ################################# reproject PROBAV MEMORY file ##############################

            # Reproject the PROBA-V band  to match DEM's resolution
            PROBAV, ulx_dem, lry_dem, lrx_dem, uly_dem, epsg_to = SEBAL.reproject_dataset_example(
                dst_dataset, Example_fileName)

            dst_dataset = None

            #################################### Get example information ################################

            if not "shape_lsc" in locals():
                nrow = PROBAV.RasterYSize
                ncol = PROBAV.RasterXSize
                shape_lsc = [ncol, nrow]

            # Open the reprojected PROBA-V band data
            data_PROBAV_DN = PROBAV.GetRasterBand(1).ReadAsArray(
                0, 0, ncol, nrow)

            # Define the filename to store the cropped Landsat image
            dst_FileName = os.path.join(output_folder,
                                        'Output_radiation_balance',
                                        'proy_PROBAV_%s.tif' % bandnmr)

            # close the PROBA-V
            g = None

            if not "spectral_reflectance_PROBAV" in locals():
                spectral_reflectance_PROBAV = np.zeros(
                    [shape_lsc[1], shape_lsc[0], 5])

            # If the band data is not SM change the DN values into PROBA-V values and write into the spectral_reflectance_PROBAV
            if bandnmr is not 'SM':
                data_PROBAV = data_PROBAV_DN / 2000
                spectral_reflectance_PROBAV[:, :, index] = data_PROBAV[:, :]

            # If the band data is the SM band than write the data into the spectral_reflectance_PROBAV and create cloud mask
            else:
                cloud_mask_temp = np.zeros(data_PROBAV_DN.shape)
                cloud_mask_temp[data_PROBAV_DN[:, :] != n188_float] = 1
                spectral_reflectance_PROBAV[:, :, index] = cloud_mask_temp

            # Change the spectral reflectance to meet certain limits
            spectral_reflectance_PROBAV[:, :, index] = np.where(
                spectral_reflectance_PROBAV[:, :, index] <= 0, np.nan,
                spectral_reflectance_PROBAV[:, :, index])
            spectral_reflectance_PROBAV[:, :, index] = np.where(
                spectral_reflectance_PROBAV[:, :, index] >= 150, np.nan,
                spectral_reflectance_PROBAV[:, :, index])

            # Save the PROBA-V as a tif file
            SEBAL.save_GeoTiff_proy(PROBAV,
                                    spectral_reflectance_PROBAV[:, :, index],
                                    dst_FileName,
                                    shape_lsc,
                                    nband=1)

            # Go to the next index
            index = index + 1

        # Original size PROBAV dataset
        x_size_pv = int(Data.shape[1])
        y_size_pv = int(Data.shape[0])
        ulx = Lon_Left - 0.5 * Pixel_size
        uly = Lat_Top + 0.5 * Pixel_size
        lrx = ulx + x_size_pv * Pixel_size
        lry = uly - y_size_pv * Pixel_size

        print('Original PROBA-V Image - ')
        print('  Size :', x_size_pv, y_size_pv)
        print('  Upper Left corner x, y: ', ulx, ', ', uly)
        print('  Lower right corner x, y: ', lrx, ', ', lry)

        print('Reprojected PROBA-V Image - ')
        print('  Size :', shape_lsc[1], shape_lsc[0])
        print('  Upper Left corner x, y: ', ulx_dem, ', ', uly_dem)
        print('  Lower right corner x, y: ', lrx_dem, ', ', lry_dem)

    else:
        # Get General information example file
        lsc = gdal.Open(Example_fileName)
        nrow = lsc.RasterYSize
        ncol = lsc.RasterXSize
        shape_lsc = [ncol, nrow]

    ######################### Calculate Vegetation Parameters Based on VIS data #####################################

    # Open the Additional input excel sheet
    ws = workbook['Additional_Input']

    # Check NDVI and Calculate NDVI
    try:
        if (ws['B%d' % number].value) is not None:

            # Output folder NDVI
            ndvi_fileName_user = os.path.join(
                output_folder, 'Output_vegetation',
                'User_NDVI_%s_%s_%s.tif' % (res3, year, DOY))
            NDVI = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['B%d' % number].value), ndvi_fileName_user,
                Example_fileName)

            water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
            water_mask_temp[NDVI < 0.0] = 1.0
            SEBAL.save_GeoTiff_proy(lsc,
                                    NDVI,
                                    ndvi_fileName_user,
                                    shape_lsc,
                                    nband=1)

        else:
            n218_memory = spectral_reflectance_PROBAV[:, :,
                                                      2] + spectral_reflectance_PROBAV[:, :,
                                                                                       3]
            NDVI = np.zeros((shape_lsc[1], shape_lsc[0]))
            NDVI[n218_memory != 0] = (
                spectral_reflectance_PROBAV[:, :, 3][n218_memory != 0] -
                spectral_reflectance_PROBAV[:, :, 2][n218_memory != 0]) / (
                    spectral_reflectance_PROBAV[:, :, 2][n218_memory != 0] +
                    spectral_reflectance_PROBAV[:, :, 3][n218_memory != 0])

            # Create Water mask based on PROBA-V
            water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
            water_mask_temp[np.logical_and(
                spectral_reflectance_PROBAV[:, :, 2] >=
                spectral_reflectance_PROBAV[:, :, 3], DEM_resh > 0)] = 1

    except:
        assert "Please check the NDVI input path"

    # Check Water Mask and replace if it is filled in the additianal data sheet
    try:
        if (ws['E%d' % number].value) is not None:

            # Overwrite the Water mask and change the output name
            water_mask_temp_fileName = os.path.join(
                output_folder, 'Output_soil_moisture',
                'User_Water_mask_temporary_%s_%s_%s.tif' % (res2, year, DOY))
            water_mask_temp = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['E%d' % number].value),
                water_mask_temp_fileName, Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc,
                                    water_mask_temp,
                                    water_mask_temp_fileName,
                                    shape_lsc,
                                    nband=1)

    except:
        assert "Please check the Water Mask input path"

    # Check Surface albedo
    try:
        if (ws['C%d' % number].value) is not None:

            # Output folder surface albedo
            surface_albedo_fileName = os.path.join(
                output_folder, 'Output_vegetation',
                'User_surface_albedo_%s_%s_%s.tif' % (res2, year, DOY))
            Surf_albedo = SEBAL.Reshape_Reproject_Input_data(
                r'%s' % str(ws['C%d' % number].value), surface_albedo_fileName,
                Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc,
                                    Surf_albedo,
                                    surface_albedo_fileName,
                                    shape_lsc,
                                    nband=1)

        else:

            # Calculate surface albedo based on PROBA-V
            Surf_albedo = 0.219 * spectral_reflectance_PROBAV[:, :,
                                                              1] + 0.361 * spectral_reflectance_PROBAV[:, :,
                                                                                                       2] + 0.379 * spectral_reflectance_PROBAV[:, :,
                                                                                                                                                3] + 0.041 * spectral_reflectance_PROBAV[:, :,
                                                                                                                                                                                         4]

            # Set limit surface albedo
            Surf_albedo = np.minimum(Surf_albedo, 0.6)

    except:
        assert "Please check the Albedo input path"

    # calculate vegetation properties
    FPAR, tir_emis, Nitrogen, vegt_cover, LAI, b10_emissivity = SEBAL.Calc_vegt_para(
        NDVI, water_mask_temp, shape_lsc)

    # create quality map
    QC_Map = np.zeros(NDVI.shape)
    QC_Map[np.isnan(NDVI)] = 1

    print('Average NDVI = %s' % np.nanmean(NDVI))
    print('Average Surface Albedo = %s' % np.nanmean(Surf_albedo))
    print('Average LAI = %s' % np.nanmean(LAI))
    print('Average Vegetation Cover = %s' % np.nanmean(vegt_cover))
    print('Average FPAR = %s' % np.nanmean(FPAR))

    return (Surf_albedo, NDVI, LAI, vegt_cover, FPAR, Nitrogen, tir_emis,
            b10_emissivity, water_mask_temp, QC_Map)
def Get_LS_Para_Veg(workbook, number, Example_fileName, year, DOY, path_radiance, Apparent_atmosf_transm, cos_zn, dr):

    import SEBAL.pySEBAL.pySEBAL_code as SEBAL

    # Open the General input sheet
    ws = workbook['General_Input']

    # Extract the input and output folder, and Image type from the excel file
    input_folder = r"%s" %str(ws['B%d' %number].value)
    output_folder = r"%s" %str(ws['C%d' %number].value)

    ws = workbook['Additional_Input']

    # If all additional fields are filled in than do not open the datasets
    if ws['B%d' % number].value is None or ws['C%d' % number].value is None:

        print('-------------------- Open Landsat VIS -----------------------')

        # Open the Landsat_Input sheet
        ws = workbook['Landsat_Input']

        # Extract Landsat name, number and amount of thermal bands from excel file
        Name_Landsat_Image = str(ws['B%d' %number].value)
        Landsat_nr = int(ws['C%d' %number].value)            # Type of Landsat (LS) image used (LS5, LS7, or LS8)
                                                             # temperature: 1 = Band 6 for LS_5 & 7, Band 10 for LS_8 (optional)
        # Define bands used for each Landsat number
        if Landsat_nr == 5 or Landsat_nr == 7:
            Bands = np.array([1, 2, 3, 4, 5, 7, 6])
        elif Landsat_nr == 8:
           Bands = np.array([2, 3, 4, 5, 6, 7, 10, 11])
        else:
            print('Landsat image not supported, use Landsat 7 or 8')

        # Open MTL landsat and get the correction parameters
        Landsat_meta_fileName = os.path.join(input_folder, '%s_MTL.txt' %Name_Landsat_Image)
        Lmin, Lmax, k1_c, k2_c = info_band_metadata(Landsat_meta_fileName, Bands)
        print('Lmin= ', Lmin)
        print('Lmax= ', Lmax)
        print('k1= ', k1_c)
        print('k2= ', k2_c)

        sensor1 = 'LS%d' %Landsat_nr
        res1 = '30m'
        res2 = '30m'
        res3 = '30m'

        # Mean solar exo-atmospheric irradiance for each band (W/m2/microm)
        # for the different Landsat images (L5, L7, or L8)
        ESUN_L5 = np.array([1983, 1796, 1536, 1031, 220, 83.44])
        ESUN_L7 = np.array([1997, 1812, 1533, 1039, 230.8, 84.9])
        ESUN_L8 = np.array([1973.28, 1842.68, 1565.17, 963.69, 245, 82.106])

        # Open one band - To get the metadata of the landsat images only once (to get the extend)
        src_FileName = os.path.join(input_folder, '%s_B2.TIF' %Name_Landsat_Image)  # before 10!
        ls, band_data, ulx, uly, lrx, lry, x_size_ls, y_size_ls = Get_Extend_Landsat(src_FileName)
        print('Original LANDSAT Image - ')
        print('  Size :', x_size_ls, y_size_ls)
        print('  Upper Left corner x, y: ', ulx, ', ', uly)
        print('  Lower right corner x, y: ', lrx, ', ', lry)

        lsc, ulx, uly, lrx, lry, epsg_to = SEBAL.reproject_dataset_example(src_FileName, Example_fileName)

        #	Get the extend of the remaining landsat file	after clipping based on the DEM file
        y_size_lsc = lsc.RasterYSize
        x_size_lsc = lsc.RasterXSize
        shape_lsc = [x_size_lsc, y_size_lsc]

        print('--- ')
        print('Cropped LANDSAT Image - ')
        print('  Size :', x_size_lsc, y_size_lsc)
        print('  Upper Left corner x, y: ', ulx, ', ',  uly)
        print('  Lower right corner x, y: ', lrx, ', ', lry)

        # if landsat 5 or 7 is used then first create a mask for removing the no data stripes
        if Landsat_nr == 5 or Landsat_nr == 7:
            src_FileName = os.path.join(input_folder, '%s_B6.TIF' % (Name_Landsat_Image)) #open smallest band
            if not os.path.exists(src_FileName):
                src_FileName = os.path.join(input_folder, '%s_B6_VCID_2.TIF' % (Name_Landsat_Image))
            src_FileName_2 = os.path.join(input_folder, '%s_B1.TIF' % (Name_Landsat_Image)) #open smallest band
            src_FileName_3 = os.path.join(input_folder, '%s_B3.TIF' % (Name_Landsat_Image)) #open smallest band
            src_FileName_4 = os.path.join(input_folder, '%s_B4.TIF' % (Name_Landsat_Image)) #open smallest band
            src_FileName_5 = os.path.join(input_folder, '%s_B7.TIF' % (Name_Landsat_Image)) #open smallest band
            src_FileName_6 = os.path.join(input_folder, '%s_B2.TIF' % (Name_Landsat_Image)) #open smallest band
            src_FileName_7 = os.path.join(input_folder, '%s_B5.TIF' % (Name_Landsat_Image)) #open smallest band
            ls_data=Open_landsat(src_FileName,Example_fileName)
            ls_data_2=Open_landsat(src_FileName_2,Example_fileName)
            ls_data_3=Open_landsat(src_FileName_3,Example_fileName)
            ls_data_4=Open_landsat(src_FileName_4,Example_fileName)
            ls_data_5=Open_landsat(src_FileName_5,Example_fileName)
            ls_data_6=Open_landsat(src_FileName_6,Example_fileName)
            ls_data_7=Open_landsat(src_FileName_7,Example_fileName)

            # create and save the landsat mask for all images based on band 11 (smallest map)
            QC_Map=np.zeros((shape_lsc[1], shape_lsc[0]))
            QC_Map=np.where(np.logical_or.reduce((ls_data==0,ls_data_2==0,ls_data_3==0,ls_data_4==0,ls_data_5==0,ls_data_6==0,ls_data_7==0)),1,0)

        # If landsat 8 then use landsat band 10 and 11
        elif Landsat_nr == 8:
             src_FileName_11 = os.path.join(input_folder, '%s_B11.TIF' % (Name_Landsat_Image)) #open smallest band
             ls_data_11=Open_landsat(src_FileName_11,Example_fileName)

             src_FileName_10 = os.path.join(input_folder, '%s_B10.TIF' % (Name_Landsat_Image)) #open smallest band
             ls_data_10=Open_landsat(src_FileName_10, Example_fileName)

             # create and save the landsat mask for all images based on band 10 and 11
             QC_Map=np.zeros((shape_lsc[1], shape_lsc[0]))
             QC_Map=np.where(np.logical_or(ls_data_11==0, ls_data_10==0),1,0)

        else:
            print('Landsat image not supported, use Landsat 7 or 8')

        # Open data of the landsat mask
        ls_data=Open_landsat(src_FileName, Example_fileName)

        # Create 3D array to store Spectral radiance and Reflectivity for each band
        Reflect, Spec_Rad = Landsat_Reflect(Bands, input_folder, Name_Landsat_Image, output_folder, shape_lsc, QC_Map, Lmax, Lmin, ESUN_L5, ESUN_L7, ESUN_L8, cos_zn, dr, Landsat_nr, Example_fileName)

        # save spectral data
        for i in range(0,6):
            spec_ref_fileName = os.path.join(output_folder, 'Output_radiation_balance','%s_spectral_reflectance_B%s_%s_%s_%s.tif' %(sensor1, Bands[i], res3, year, DOY))
            SEBAL.save_GeoTiff_proy(lsc, Reflect[:, :, i], spec_ref_fileName, shape_lsc, nband=1)

    else:
        # Get General information example file
        lsc = gdal.Open(Example_fileName)
        nrow = lsc.RasterYSize
        ncol = lsc.RasterXSize
        shape_lsc = [ncol, nrow]

    ######################### Calculate Vegetation Parameters Based on VIS data #####################################

    # Open the Additional input excel sheet
    ws = workbook['Additional_Input']

    # Check NDVI and Calculate NDVI
    try:
        if (ws['B%d' % number].value) is not None:

            # Output folder NDVI
            ndvi_fileName_user = os.path.join(output_folder, 'Output_vegetation', 'User_NDVI_%s_%s_%s.tif' %(res3, year, DOY))
            NDVI=SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['B%d' % number].value),ndvi_fileName_user,Example_fileName)

            water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0]))
            water_mask_temp[NDVI < 0.0] = 1.0
            SEBAL.save_GeoTiff_proy(lsc, NDVI, ndvi_fileName_user, shape_lsc, nband=1)

        else:
            # use the Landsat reflectance to calculate the surface albede, NDVI
            NDVI = SEBAL.Calc_NDVI(Reflect)

            # Calculate temporal water mask
            water_mask_temp=SEBAL.Water_Mask(shape_lsc,Reflect)

    except:
        assert "Please check the NDVI input path"

    # Check Water Mask and replace if it is filled in the additianal data sheet
    try:
        if (ws['E%d' % number].value) is not None:

            # Overwrite the Water mask and change the output name
            water_mask_temp_fileName = os.path.join(output_folder, 'Output_soil_moisture', 'User_Water_mask_temporary_%s_%s_%s.tif' %(res2, year, DOY))
            water_mask_temp = SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['E%d' % number].value), water_mask_temp_fileName, Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc, water_mask_temp, water_mask_temp_fileName, shape_lsc, nband=1)

    except:
        assert "Please check the Water Mask input path"

    # Check Surface albedo
    try:
        if (ws['C%d' % number].value) is not None:

            # Output folder surface albedo
            surface_albedo_fileName = os.path.join(output_folder, 'Output_vegetation','User_surface_albedo_%s_%s_%s.tif' %(res2, year, DOY))
            Surf_albedo=SEBAL.Reshape_Reproject_Input_data(r'%s' %str(ws['C%d' % number].value),surface_albedo_fileName,Example_fileName)
            SEBAL.save_GeoTiff_proy(lsc, Surf_albedo, surface_albedo_fileName, shape_lsc, nband=1)

        else:

            # use the Landsat reflectance to calculate the surface albede, NDVI
            Surf_albedo = SEBAL.Calc_albedo(Reflect, path_radiance, Apparent_atmosf_transm)

    except:
          assert "Please check the Albedo input path"

    # calculate vegetation properties
    FPAR,tir_emis,Nitrogen,vegt_cover,LAI,b10_emissivity=SEBAL.Calc_vegt_para(NDVI, water_mask_temp,shape_lsc)

    print('Average NDVI = %s' %np.nanmean(NDVI))
    print('Average Surface Albedo = %s' %np.nanmean(Surf_albedo))
    print('Average LAI = %s' %np.nanmean(LAI))
    print('Average Vegetation Cover = %s' %np.nanmean(vegt_cover))
    print('Average FPAR = %s' %np.nanmean(FPAR))

    return(Surf_albedo, NDVI, LAI, vegt_cover, FPAR, Nitrogen, tir_emis, b10_emissivity, water_mask_temp, QC_Map)