-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmgbbhods_0_prepro.py
111 lines (76 loc) · 3.47 KB
/
mgbbhods_0_prepro.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""
Zero Pre-processing step of the MGB-BHO Downscaling
Gets Domain Intersection
Save domain related files:
- bho_midpts.gpkg :: BHO midpoints from intersection
- table_t0.xlsx :: inpolygon BHO points with MGB polygons
- dict_bho_domain.pickle :: dictioary of {cotrecho:mini}
@author: Mino Sorribas
"""
# standard python
import time
import pickle
from datetime import datetime,timedelta
import warnings
# plotting, numpy, dataframes and spatial
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import geopandas as gpd
# downscaling functions
import funcs_io
import funcs_op
# ignore warnings
warnings.filterwarnings('ignore')
print("--------------------------------------------------------------------")
print(" Initial Step of the MGB-BHO Downscaling - Get Domain Intersection ")
print("--------------------------------------------------------------------")
#-----------------------------------------------------------------------------
# INPUT PATHS AND FILES
#-----------------------------------------------------------------------------
print(" Initializing filepaths... ")
PATH_MAIN = '../'
PATH_INPUT = PATH_MAIN + 'input/'
# table mgb topology
FILE_MINI = PATH_INPUT + 'mini.xlsx'
# geopackage BHO drainage
FILE_GDF_BHO = PATH_INPUT + 'geoft_bho_2017_5k_trecho_drenagem.gpkg'
# shapefile MGB
FILE_MGB_CATCHMENTS_SHP = PATH_INPUT + 'mgb_sa_unit_catchments_sirgas2000.shp'
#-----------------------------------------------------------------------------
# OUTPUT FILES
#-----------------------------------------------------------------------------
# geopackage BHO points
FILE_BHO_INTER = 'bho_midpts.gpkg'
#-----------------------------------------------------------------------------
# LOAD TABLES
#-----------------------------------------------------------------------------
print(" Loading datatables and spatial data... ")
# table mgb topology
df_tble_mini = funcs_io.read_tble_mini(FILE_MINI)
# bho trechos (geodataframe)
gdf_tble_bho = funcs_io.read_gdf_bho(FILE_GDF_BHO)
# mgb catchments (shapefile)
gdf_mgb_catchments = gpd.read_file(FILE_MGB_CATCHMENTS_SHP)
#-----------------------------------------------------------------------------
# PRE-PROCESSING - IDENTIFY MGB DOMAIN INSIDE BHO (STEP ZERO)
#-----------------------------------------------------------------------------
print(" Pre-processing domain (MGB inside BHO)... ")
# obtain raw domain (bho inside mgb catchments) -> drop bho_midpts.gpkg and pickle.
dict_bho_domain = funcs_op.associate_bho_mini_domain(gdf_tble_bho,
gdf_mgb_catchments,
pts_to_gpkg = FILE_BHO_INTER,
)
gdf_tble_bho = gdf_tble_bho.drop('geometry',axis=1)
#... from now on, works with the table
#... and adopt 'df_tble_bho' as the variable name
#... thus, the next statement keeps the old object (same memory)
# id(df_tble_bho)==id(gdf_tble_bho)
df_tble_bho = gdf_tble_bho
#-----------------------------------------------------------------------------
# MAKE INTERSECTION TABLE
#-----------------------------------------------------------------------------
print(" Make intersection table (adjusted for table 1 processing)... ")
df_bho_inter = funcs_op.make_tble_t0(df_tble_mini, df_tble_bho, FILE_BHO_INTER)
print(" Done... ")