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)
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)
def Get_PROBAV_Para_Veg(workbook, number, Example_fileName, year, month, day, 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'] # Define the bands that will be used sensor1 = 'PROBAV' sensor2 = 'VIIRS' res1 = '375m' res2 = '100m' res3 = '30m' # 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'] # read PROBAV name input Name_PROBAV_Image = '%s' % str( ws['D%d' % number].value) # Must be a tiff or hdf file ############################################################################################################################################## ############################################# maak hier een functie van zodat deze ook in preSEBAL_1.py gebruikt kan worden ################## ############################################################################################################################################## spectral_reflectance_PROBAV, cloud_mask_temp = Open_PROBAV_Reflectance( Name_PROBAV_Image, input_folder, output_folder, Example_fileName) ############################################################################################################################################## ############################ RETURN spectral_reflectance_PROBAV, cloud_mask_temp ##################################################### ############################################################################################################################################## # 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 None: 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 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.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) 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") # 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: # 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: 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, 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_MODIS_Para_Veg(workbook, number, Example_fileName, year, month, day, path_radiance, Apparent_atmosf_transm, cos_zn, dr, DEM_resh, epsg_to): 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] # General sensor resolution and names res2 = '250m' # Extract the name of the thermal and quality MODIS image from the excel file ws = workbook['MODIS_Input'] Name_MODIS_Image_Ref = str(ws['B%d' % number].value) #reflectance Name_MODIS_Image_NDVI = str(ws['D%d' % number].value) #ndvi # Create complete path to data src_FileName_NDVI = os.path.join(input_folder, '%s.hdf' % Name_MODIS_Image_NDVI) src_FileName_Ref = os.path.join(input_folder, '%s.hdf' % Name_MODIS_Image_Ref) ws = workbook['Additional_Input'] # Calculate NDVI try: if (ws['B%d' % number].value) is not None: # Output folder NDVI defined by the user ndvi_fileName = os.path.join( output_folder, 'Output_vegetation', 'User_NDVI_%s_%s%02d%02d.tif' % (res2, year, month, day)) # Reproject and reshape users NDVI NDVI = SEBAL.Reshape_Reproject_Input_data( r'%s' % str(ws['B%d' % number].value), ndvi_fileName, Example_fileName) NDVI_MAX = np.nanmax(NDVI) NDVI_SD = np.nanstd(NDVI) print('NDVI User max ', NDVI_MAX) print('NDVI User sd', NDVI_SD) # Create Water mask based on PROBA-V water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0])) water_mask_temp[NDVI < 0.0] = 1 # Create Quality map QC_Map = np.zeros((shape_lsc[1], shape_lsc[0])) else: # Calculate the NDVI based on MODIS NDVI = Open_reprojected_hdf(src_FileName_NDVI, 0, epsg_to, 0.0001, Example_fileName) # NDVI stats NDVI_MODIS_MAX = np.nanmax(NDVI) NDVI_MODIS_SD = np.nanstd(NDVI) print('NDVI MODIS max ', NDVI_MODIS_MAX) print('NDVI MODIS sd', NDVI_MODIS_SD) # Create Water mask based on MODIS water_mask_temp = np.zeros((shape_lsc[1], shape_lsc[0])) water_mask_temp[NDVI < 0] = 1 # Create Quality map QC_Map = np.zeros((shape_lsc[1], shape_lsc[0])) QC_Map[np.logical_and(NDVI < -0.2, NDVI > 1.0)] = 1 except: assert "Please check the MODIS path, was not able to create NDVI" # Check Water Mask and replace the temporary water mask if needed try: if (ws['E%d' % number].value) is not None: # Overwrite the Water mask 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)) # Reproject and reshape users surface albedo Surf_albedo = SEBAL.Reshape_Reproject_Input_data( r'%s' % str(ws['C%d' % number].value), surface_albedo_fileName, Example_fileName) # if the users surface albedo data cannot be reprojected than use the original MODIS data as imported into SEBAL else: # Calculate the MOD9 based on MODIS by opening and reproject bands B1_modis = Open_reprojected_hdf(src_FileName_Ref, 11, epsg_to, 0.0001, Example_fileName) B2_modis = Open_reprojected_hdf(src_FileName_Ref, 12, epsg_to, 0.0001, Example_fileName) B3_modis = Open_reprojected_hdf(src_FileName_Ref, 13, epsg_to, 0.0001, Example_fileName) B4_modis = Open_reprojected_hdf(src_FileName_Ref, 14, epsg_to, 0.0001, Example_fileName) B5_modis = Open_reprojected_hdf(src_FileName_Ref, 15, epsg_to, 0.0001, Example_fileName) B6_modis = Open_reprojected_hdf(src_FileName_Ref, 16, epsg_to, 0.0001, Example_fileName) B7_modis = Open_reprojected_hdf(src_FileName_Ref, 17, epsg_to, 0.0001, Example_fileName) # Calc surface albedo within shortwave domain using a weighting function (Tasumi et al 2008) Surf_albedo = 0.215 * B1_modis + 0.215 * B2_modis + 0.242 * B3_modis + 0.129 * B4_modis + 0.101 * B5_modis + 0.062 * B6_modis + 0.036 * B7_modis # Define bad pixels QC_Map[np.logical_and(Surf_albedo < 0.0, Surf_albedo > 1.0)] = 1 except: assert "Please check the PROBA-V path, was not able to create Albedo" # Calculate the Fpar, TIR, Nitrogen, Vegetation Cover, LAI and b10_emissivity based on PROBA-V 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)) return (Surf_albedo, NDVI, LAI, vegt_cover, FPAR, Nitrogen, tir_emis, b10_emissivity, water_mask_temp, QC_Map)
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)