extract_HydroSig.py 5.11 KiB
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 15 12:34:40 2021

@author: laura.lindeperg
"""

import geopandas as gpd
import pandas as pd
import seaborn as sns
from Watershed import Watershed




# **************************** Data *****************************

# Banque Hydro
banquehydro_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Export2020/'
# banquehydro_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Export2020/Stations/A1080330.csv'

# SAFRAN
# safran_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/SAFRAN/daily/'
# safran_grid_shpfilename = './TestData/SAFRAN/maille_meteo_fr_pr93.shp'
safran_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/SAFRAN/'

# GEOL
BDLisa_shp = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BD_Lisa/RegionalHydrogeologyAnalysisMe/BD_Lisa_regionalhydrogeology.shp'

BRGM_geol_map = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/CarteGeolBRGM/FR_vecteur/FR_vecteur/GEO001M_CART_FR_S_FGEOL_2154.shp'

# Watersheds
# shp_stations_filepath = 'E:/DonneesLaura/BanqueHydro/Shapes/StationBHYDRO_L93.shp'
df_stations_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/StationsNonInfluenceesExplore2/Synthèse analyses/Synthèse_meta_selection_624.csv'
shp_contour_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Shapes/BassinsVersantsMetropole/BV_4207_stations.shp' 

df_stations = pd.read_csv(df_stations_filepath, sep = ';', encoding='latin-1')
shp_contour = gpd.read_file(shp_contour_filepath)
# List of the stations'codes
watershed_code = df_stations.loc[:,'Code']
# Get a sample of them for test
code_for_test = watershed_code.loc[1:2]
# code_for_test = ['A1072010', 'A1080330']
# code_for_test = ['A1072010', 'A1080330', 'A3472010', 'I0102010', 'J3413030']

# # extract safran gpd
# from HydroClimaticFluxes import HydroClimaticFluxes
# HCF = HydroClimaticFluxes(code=-1)
# # Ptot_gpd = HCF.extract_safran_variable(safran_foldername, 'Ptot')
# ET0_gpd = HCF.extract_safran_variable(safran_foldername, 'ET0')
# Tair_gpd = HCF.extract_safran_variable(safran_foldername, 'Tair')
# Snow_gpd = HCF.extract_safran_variable(safran_foldername, 'Snow')
# Rain_gpd = HCF.extract_safran_variable(safran_foldername, 'Rain')

# *************************** Extract watersheds' properties ************************************


studied_watersheds = pd.DataFrame()
for i in code_for_test:
#for i in watershed_code:
    print(i)
    # Get the station's name
    watershed_stations_i = df_stations[df_stations.loc[:, 'Code'] == i]
    watershed_name_i =  watershed_stations_i.loc[:, 'Nom'].values[0]
    
    # Create waterhed object and extract its properties
    watershed_i = Watershed(i, watershed_name_i)
    watershed_i.extract_watershed_contour_from_filename(shp_contour_filepath, 'Code')
    watershed_i.extract_banquehydro_discharge_timeseries(banquehydro_foldername)
    
    # watershed_i.extract_SAFRAN_forcings(safran_foldername, safran_grid_shpfilename)
    # watershed_i.hydro_climatic_fluxes.intersect_safran_gpd_and_contour(Ptot_gpd, watershed_i.contour,'Ptot')
    # watershed_i.hydro_climatic_fluxes.intersect_safran_gpd_and_contour(ET0_gpd, watershed_i.contour, 'ET0')
    # watershed_i.hydro_climatic_fluxes.intersect_safran_gpd_and_contour(Tair_gpd, watershed_i.contour, 'Tair')
    # watershed_i.hydro_climatic_fluxes.intersect_safran_gpd_and_contour(Snow_gpd, watershed_i.contour, 'Snow')
    # watershed_i.hydro_climatic_fluxes.intersect_safran_gpd_and_contour(Rain_gpd, watershed_i.contour, 'Rain')
    # watershed_i.hydro_climatic_fluxes.safran_timeseries['Ptot'] = watershed_i.hydro_climatic_fluxes.safran_timeseries['Snow'] + watershed_i.hydro_climatic_fluxes.safran_timeseries['Rain']
    
    safran_timeseries_i = pd.read_csv(safran_foldername+i+'_safran_timeseries.csv')
    watershed_i.hydro_climatic_fluxes.safran_timeseries = safran_timeseries_i
    
    watershed_i.extract_hydrological_signatures()
    watershed_i.extract_geologic_properties_from_filename(BDLisa_shp)
    watershed_i.geologic_properties.extract_average_age_geology(BRGM_geol_map, watershed_i.contour)
    
    # Create a dictionnary and fill the dataframe
    watershed_dict = watershed_i.to_dict()
    studied_watersheds = studied_watersheds.append(watershed_dict, ignore_index=True)





# And save it
# studied_watersheds.to_csv('few_stations_df.csv', index=False)


# Few plots for test
df_some_stations_path = 'few_stations_df.csv'
df_some_stations = pd.read_csv(df_some_stations_path)

# bf indices
sns.relplot(x="q_mean", y="bfi", hue="maingeol_description", data=df_some_stations)
sns.relplot(x="bfi", y="bf_magni", hue="maingeol_description", data=df_some_stations)

 # recession indices
sns.relplot(x="q_mean", y="tau_1", hue="maingeol_description", data=df_some_stations)
sns.relplot(x="q_mean", y="tau_2", hue="maingeol_description", data=df_some_stations)
sns.relplot(x="tau_1", y="tau_2", hue="maingeol_description", data=df_some_stations)




# all_watershed = shp_contour.plot(color='white', edgecolor='#f4a460')
# problematic_watershed = shp_contour[shp_contour.Code=="A7821010"]
# problematic_watershed.plot(ax=all_watershed, color='blue');