Watershed.py 6.45 KB
Newer Older
Marcais Jean's avatar
Marcais Jean committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import geopandas as gpd


class Watershed(object):
    """ This Watershed class enables to create a watershed object with all these properties (intrinsic et extrinsic)"""

    def __init__(self, code, name=-1, contour=-1, hydrological_signatures=-1, geologic_properties=-1, geomorphologic_properties=-1,
                 topographic_properties=-1, hydro_climatic_fluxes=-1):
        self.code = code
        self.name = name
        self.contour = contour
        self.hydrological_signatures = hydrological_signatures
        self.geologic_properties = geologic_properties
        self.geomorphologic_properties = geomorphologic_properties
        self.topographic_properties = topographic_properties
        self.hydro_climatic_fluxes = hydro_climatic_fluxes

    def to_dict(self):
        watershed_dict = {'name': self.name, 'code': self.code}
20
        watershed_dict.update(self.hydrological_signatures.__dict__)
21
        # watershed_dict.update(self.geologic_properties.__dict__)
22
23
        # watershed_dict.update(self.geomorphologic_properties.__dict__)
        # watershed_dict.update(self.topographic_properties.__dict__)
Marcais Jean's avatar
Marcais Jean committed
24
25
        return watershed_dict

26
    def extract_watershed_contour_from_filename(self, filepath, code_column_name='Code'):
Marcais Jean's avatar
Marcais Jean committed
27
        shpfile_contour = gpd.read_file(filepath)
28
29
30
        self.extract_watershed_contour(shpfile_contour, code_column_name)

    def extract_watershed_contour(self, shpfile_contour, code_column_name='Code'):
Marcais Jean's avatar
Marcais Jean committed
31
32
33
34
35
36
37
        shpfile_contour = shpfile_contour[shpfile_contour.loc[:, code_column_name] == self.code]
        self.contour = shpfile_contour

    def extract_banquehydro_discharge_timeseries(self, foldername):
        from HydroClimaticFluxes import HydroClimaticFluxes
        self.hydro_climatic_fluxes = HydroClimaticFluxes(self.code)
        self.hydro_climatic_fluxes.extract_discharge_timeseries(foldername, type_data='BanqueHydro')
38
        self.hydro_climatic_fluxes.keep_clean_HydroYear()
39
        # self.hydro_climatic_fluxes.extract_discharge_timeseries(foldername, type_data='csv_file')
Marcais Jean's avatar
Marcais Jean committed
40

41
    def extract_SAFRAN_forcings(self, foldername):
Marcais Jean's avatar
Marcais Jean committed
42
        from HydroClimaticFluxes import HydroClimaticFluxes
Marcais Jean's avatar
Marcais Jean committed
43
44
        if(self.hydro_climatic_fluxes==-1):
            self.hydro_climatic_fluxes = HydroClimaticFluxes(self.code)
45
        self.hydro_climatic_fluxes.extract_safran_timeseries_from_contour(foldername, self.contour)
Marcais Jean's avatar
Marcais Jean committed
46

Marcais Jean's avatar
Marcais Jean committed
47
    def extract_hydrological_signatures(self, option='all', adimensioned='no'):
Marcais Jean's avatar
Marcais Jean committed
48
49
        from HydrologicalSignatures import HydrologicalSignatures
        self.hydrological_signatures = HydrologicalSignatures()
Marcais Jean's avatar
Marcais Jean committed
50
51
52
53
        df_discharge = self.hydro_climatic_fluxes.discharge_timeseries
        if(adimensioned=='yes'):
            from numpy import nanmedian
            df_discharge.Q = df_discharge.Q/nanmedian(df_discharge.Q)
54
        if (option == 'all'):
Marcais Jean's avatar
Marcais Jean committed
55
            self.hydrological_signatures.from_discharge_time_series(df_discharge,'Q')
56
            df_obs_mod = self.hydro_climatic_fluxes.merge_time_series(merging_option='inner')
57
            self.hydrological_signatures.from_discharge_and_climate_time_series(df_obs_mod, 'Q', 'Ptot')
58
            self.hydrological_signatures.from_climate_time_series(self.hydro_climatic_fluxes.safran_timeseries,
59
60
                                                                  'Ptot', 'ET0')
        elif (option=='discharge_based'):
Marcais Jean's avatar
Marcais Jean committed
61
            self.hydrological_signatures.from_discharge_time_series(df_discharge,'Q')
Marcais Jean's avatar
Marcais Jean committed
62

63
64
65
    def extract_geologic_properties_from_filename(self, geol_foldername):
        geol_shp = gpd.read_file(geol_foldername)
        self.extract_geologic_properties(geol_shp)
66

67
    def extract_geologic_properties(self, geol_shp):
Marcais Jean's avatar
Marcais Jean committed
68
69
        from GeologicProperties import GeologicProperties
        self.geologic_properties = GeologicProperties()
70
71
        self.geologic_properties.extract_main_geology(geol_shp)
        # self.geologic_properties.extract_main_geology(geol_shp, self.contour)
Marcais Jean's avatar
Marcais Jean committed
72
73
74
75
76
77
78
79
80

    @staticmethod
    def test():
        watershed_name = 'Le Meaudret à Méaudre'
        code = 'W3315010'
        # shp_contour_filepath = '/home/jean.marcais/Donnees/DonneesLaura/BanqueHydro/Shapes/BassinsVersantsMetropole' \
        #                        '/BV_4207_stations.shp'
        shp_contour_filepath = './TestData/BH/W3315010_contour.shp'
        watershed_test = Watershed(code, watershed_name)
81
        watershed_test.extract_watershed_contour_from_filename(shp_contour_filepath, 'Code')
Marcais Jean's avatar
Marcais Jean committed
82
83
84
        # banquehydro_foldername = '/home/jean.marcais/Donnees/BanqueHydro/'
        banquehydro_foldername = './TestData/BH/'
        watershed_test.extract_banquehydro_discharge_timeseries(banquehydro_foldername)
Marcais Jean's avatar
Marcais Jean committed
85
86
        # safran_foldername = '/home/jean.marcais/Donnees/SAFRAN/SAFRAN_Vidal/'
        safran_foldername = 'C:/Users/laura.lindeperg/Documents/DonneesLaura/SAFRAN/daily/'
87
        watershed_test.extract_SAFRAN_forcings(safran_foldername)
Marcais Jean's avatar
Marcais Jean committed
88
89
90
91
        watershed_test.extract_hydrological_signatures()

        # BDLisa_shp = '/home/jean.marcais/Donnees/DonneesLaura/BD_Lisa/BD_Lisa_regionalhydrogeology.shp'
        BDLisa_shp = './TestData/GEOL/BDLisa_regional_hydrology_sample.shp'
92
        watershed_test.extract_geologic_properties_from_filename(BDLisa_shp)
Marcais Jean's avatar
Marcais Jean committed
93
94
95
96

        # BRGM_geol_map = '/home/jean.marcais/Donnees/BRGM/1M/GEO001M_CART_FR_S_FGEOL_2154.shp'
        BRGM_geol_map = './TestData/GEOL/GEO001M_CART_FR_S_FGEOL_2154_sample.shp'
        watershed_test.geologic_properties.extract_average_age_geology(BRGM_geol_map, watershed_test.contour)
97
        watershed_dict = watershed_test.to_dict()
Marcais Jean's avatar
Marcais Jean committed
98
99
        return watershed_test

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    @staticmethod
    def test_hydrosignatures_Jean():
        watershed_name = 'Blabla'
        code_list = ['A1072010', 'A1080330', 'A3472010', 'I0102010', 'J3413030']
        shp_contour_filepath = '/home/jean.marcais/Donnees/DonneesLaura/BanqueHydro/Shapes/BassinsVersantsMetropole' \
                                '/BV_4207_stations.shp'
        banquehydro_foldername = '/home/jean.marcais/Donnees/BanqueHydro/'
        safran_foldername = '/home/jean.marcais/Donnees/SAFRAN/SAFRAN_Vidal/'
        watersheds=[]
        for code in code_list:
            print(code)
            watershed_test = Watershed(code, watershed_name)
            watershed_test.extract_watershed_contour_from_filename(shp_contour_filepath, 'Code')
            watershed_test.extract_banquehydro_discharge_timeseries(banquehydro_foldername)
            watershed_test.extract_SAFRAN_forcings(safran_foldername)
            watershed_test.extract_hydrological_signatures()
            watersheds.append(watershed_test)

        return watersheds
Marcais Jean's avatar
Marcais Jean committed
119