extract_HydroSig.py 10 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
# -*- 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
12
import pickle
13
14


Laura LINDEPERG's avatar
Laura LINDEPERG committed
15
computer = 'laura'
16
# **************************** Data *****************************
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
if(computer == 'laura'):
    # Banque Hydro
    banquehydro_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/BanqueHydro/Export2020/'
    # SAFRAN
    safran_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/SAFRAN/'
    # GEOL
    BDLisa_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BDLisa/'
    BRGM_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BRGM/'
    # Watersheds
    shp_watersheds_path = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/complete_df_wrong_geometries.shp'
    shp_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/'
elif(computer== 'jean'):
    # Banque Hydro
    banquehydro_foldername = '/home/jean.marcais/Donnees/BanqueHydro/'  #'/media/jean.marcais/My Passport/DonneesLaura/BanqueHydro/Export2020/'
    # SAFRAN
    safran_foldername = '/home/jean.marcais/Donnees/DonneesLaura/Watersheds/SAFRAN/' #'/media/jean.marcais/My Passport/DonneesLaura/SAFRAN/daily/'
    # GEOL
    BDLisa_filepath = '/home/jean.marcais/Donnees/DonneesLaura/Watersheds/GEOL/BDLisa/'
    BRGM_filepath = '/home/jean.marcais/Donnees/DonneesLaura/Watersheds/GEOL/BRGM/'
    # Watersheds
    shp_watersheds_path = '/home/jean.marcais/Donnees/DonneesLaura/Watersheds/complete_df_wrong_geometries.shp'
    shp_foldername = '/home/jean.marcais/Donnees/DonneesLaura/Watersheds/GEOMETRY/'
39

40
41

shp_watersheds = gpd.read_file(shp_watersheds_path)
42
43

# List of the stations'codes
44
watershed_code = shp_watersheds.loc[:,'Code']
45
# Get a sample of them for test
Laura LINDEPERG's avatar
Laura LINDEPERG committed
46
code_for_test = watershed_code.loc[402:]
Laura LINDEPERG's avatar
Laura LINDEPERG committed
47
# code_for_test = ['V1015810']
Laura LINDEPERG's avatar
Laura LINDEPERG committed
48
# code_for_test = ['V2814020', 'V5045020', 'H4033010', 'S4214010']
49
# code_for_test = ['E6456510', 'A7642010']
50
51
52

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

Laura LINDEPERG's avatar
Laura LINDEPERG committed
53
# problematic_watersheds = pd.DataFrame()
54
studied_watersheds = pd.DataFrame()
55

Laura LINDEPERG's avatar
Laura LINDEPERG committed
56
57
for i in code_for_test:
# for i in watershed_code:
58
    # Get the station's name
59
    watershed_stations_i = shp_watersheds[shp_watersheds.loc[:, 'Code'] == i]
60
61
62
63
    watershed_name_i =  watershed_stations_i.loc[:, 'Nom'].values[0]
    
    # Create waterhed object and extract its properties
    watershed_i = Watershed(i, watershed_name_i)
64
65
    shpfile_contour_i = gpd.read_file(shp_foldername+i+'.shp')
    watershed_i.contour = shpfile_contour_i
Laura LINDEPERG's avatar
Laura LINDEPERG committed
66
    
Laura LINDEPERG's avatar
Laura LINDEPERG committed
67
    watershed_i.extract_banquehydro_discharge_timeseries(banquehydro_foldername) 
68
69
    safran_timeseries_i = pd.read_csv(safran_foldername+i+'_safran_timeseries.csv')
    watershed_i.hydro_climatic_fluxes.safran_timeseries = safran_timeseries_i
70
    watershed_i.extract_hydrological_signatures(adimensioned='yes')
Laura LINDEPERG's avatar
Laura LINDEPERG committed
71

Laura LINDEPERG's avatar
Laura LINDEPERG committed
72
73
    # watershed_i.extract_geologic_properties_from_filename(BDLisa_filepath+i+'_BDLisa.shp')
    # watershed_i.geologic_properties.extract_average_age_geology(BRGM_filepath+i+'_BRGM.shp')
74
75
76
    
    # Create a dictionnary and fill the dataframe
    watershed_dict = watershed_i.to_dict()
Laura LINDEPERG's avatar
Laura LINDEPERG committed
77
    # problematic_watersheds = problematic_watersheds.append(watershed_dict, ignore_index=True)
78
    studied_watersheds = studied_watersheds.append(watershed_dict, ignore_index=True)
79
80
    
    # save object using pickle
Laura LINDEPERG's avatar
Laura LINDEPERG committed
81
82
    # filename_i = 'C:/Users/laura.lindeperg/Documents/INRAE_2021/CODE/fhysa/Object_watershed/watershed_'+i
    filename_i = 'C:/Users/laura.lindeperg/Documents/INRAE_2021/CODE/fhysa/Object_watershed/Adim/watershed_adim_'+i
83
84
85
86
    outfile_i = open(filename_i, 'wb')
    pickle.dump(watershed_i, outfile_i)
    outfile_i.close()
    
87
88

# And save it
Laura LINDEPERG's avatar
Laura LINDEPERG committed
89
# studied_watersheds.to_csv('616_stations_hydrosig_df.csv', index=False)
Laura LINDEPERG's avatar
Laura LINDEPERG committed
90

91
92
93
94
95
96
## Open stored watershed objects
# i = code_for_test[0]
# filename_i = 'watershed_'+i
# infile_i = open(filename_i, 'rb')
# my_watershed = pickle.load(infile_i)
# infile_i.close()
Laura LINDEPERG's avatar
Laura LINDEPERG committed
97

98

Laura LINDEPERG's avatar
Laura LINDEPERG committed
99
100
101
102
# df_v0 = pd.read_csv('4_stations_df_v0.csv')
# df_shp = pd.read_csv('4_stations_df_shp_read_from_files.csv')
# df_brgm = pd.read_csv('4_stations_df_shpANDbrgm_read_from_files.csv')
# df_bdlisa = pd.read_csv('4_stations_df_shpANDbrgmANDbdlisa_read_from_files.csv')
103

Laura LINDEPERG's avatar
Laura LINDEPERG committed
104
105

# ## Few plots for test
Laura LINDEPERG's avatar
Laura LINDEPERG committed
106
# df_some_stations_path = '510_first_stations_df.csv'
Laura LINDEPERG's avatar
Laura LINDEPERG committed
107
# df_some_stations = pd.read_csv(df_some_stations_path)
108

109
110
111
# # 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)
112

Laura LINDEPERG's avatar
Laura LINDEPERG committed
113
#   # recession indices
114
115
116
# 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)
117
118
119
120
121
122
123
124



# 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');


Laura LINDEPERG's avatar
Laura LINDEPERG committed
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215



# ## PREVIOUS VERSION

# # **************************** 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'
# BDLisa_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BDLisa/'

# # BRGM_geol_map = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/CarteGeolBRGM/FR_vecteur/FR_vecteur/GEO001M_CART_FR_S_FGEOL_2154.shp'
# BRGM_filepath = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOL/BRGM/'

# # 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)

# shp_watersheds_path = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/complete_df_wrong_geometries.shp'
# shp_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/Watersheds/GEOMETRY/'

# shp_watersheds = gpd.read_file(shp_watersheds_path)

# # List of the stations'codes
# watershed_code = shp_watersheds.loc[:,'Code']
# # Get a sample of them for test
# code_for_test = watershed_code.loc[0:3]
# # 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:
#     # Get the station's name
#     watershed_stations_i = shp_watersheds[shp_watersheds.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)
#     shpfile_contour_i = gpd.read_file(shp_foldername+i+'.shp')
#     watershed_i.contour = shpfile_contour_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.extract_geologic_properties_from_filename(BDLisa_filepath+i+'_BDLisa.shp')
#     # watershed_i.geologic_properties.extract_average_age_geology(BRGM_geol_map, watershed_i.contour)
#     watershed_i.geologic_properties.extract_average_age_geology(BRGM_filepath+i+'_BRGM.shp')
    
#     # Create a dictionnary and fill the dataframe
#     watershed_dict = watershed_i.to_dict()
#     studied_watersheds = studied_watersheds.append(watershed_dict, ignore_index=True)