Repository navigation
Expand file tree
/
Copy path2_split_geoscales.py
More file actions
89 lines (78 loc) · 3 KB
/
Copy path2_split_geoscales.py
File metadata and controls
89 lines (78 loc) · 3 KB
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
# ---------------------------------------------------------------------------
# 2_split_geoscales.py
# Authors: Mariel Sorlien
# Python 3.7
#
# Description:
# Combines annual impervious cover CSV files and saves a separate CSV for each geoscale. Also calculates gross and
# percent change to impervious cover between years.
#
# REQUIRES GIS/ARCPY
# ---------------------------------------------------------------------------
# Import modules
from pathlib import Path
import pandas as pd
from functions import prep_csv
# Set working directory, projection --------------------------------------------
base_folder = Path.cwd().parents[2] / "Data" / "int_tabulardata" / "impervious_int"
# Define variables
in_csv = [
"IMPERVIOUS_1985_NBEP2026.csv", "IMPERVIOUS_1990_NBEP2026.csv", "IMPERVIOUS_1995_NBEP2026.csv",
"IMPERVIOUS_2000_NBEP2026.csv", "IMPERVIOUS_2005_NBEP2026.csv", "IMPERVIOUS_2010_NBEP2026.csv",
"IMPERVIOUS_2015_NBEP2026.csv", "IMPERVIOUS_2020_NBEP2026.csv", "IMPERVIOUS_2025_NBEP2026.csv"
]
source_year = 2026
nbep_year = 2026
# RUN SCRIPT ----------------------------------------------------------------------------------------------------------
print("\nIMPORTING DATA")
df = pd.DataFrame()
for csv in in_csv:
print("Reading in", csv)
temp_csv = base_folder / csv
df_temp = pd.read_csv(temp_csv)
df = pd.concat([df, df_temp])
print("Sorting data by location, year")
sort_col = ["Geoscale", "Geoscale_Name", "Year"]
df.sort_values(by=sort_col, inplace=True)
df.reset_index(drop=True, inplace=True)
print("\nSPLITTING DATA BY GEOSCALE")
geoscale_list = df["Geoscale"].unique()
for geoscale in geoscale_list:
print("By", geoscale)
prep_csv.split_geoscale(
in_df=df,
geoscale=geoscale,
nbep_year=nbep_year,
source_year=source_year,
out_path=base_folder
)
print("\nCALCULATING CHANGE")
print("Calculating gross, percent change")
df['Prev_Year'] = df.Year.shift(1)
df['Prev_Site'] = df["Geoscale_Name"].shift(1)
df["Gross_Change_Acres"] = df["Acres_Impervious"] - df["Acres_Impervious"].shift(1)
df["Percent_Change"] = df["Gross_Change_Acres"] / df["Acres_Impervious"].shift(1) * 100
print("Dropping extra rows")
df.drop(df[df.Year == df.Prev_Year].index, inplace=True)
df.drop(df[df.Geoscale_Name != df.Prev_Site].index, inplace=True)
print("Updating columns")
df["Year"] = df.Prev_Year.astype(str) + "-" + df.Year.astype(str)
df["Year"] = df["Year"].str.replace(".0", "", regex=False)
keep_cols = [
"Geoscale", "Geoscale_Name", "Town", "State", "HUC12", "HUC12_Name", "HUC10", "HUC10_Name", "Basin", "Study_Area",
"Year", "Percent_Change", "Gross_Change_Acres"
]
df = df.reindex(columns=keep_cols)
print("\nSPLITTING DATA BY GEOSCALE")
geoscale_list = df["Geoscale"].unique()
for geoscale in geoscale_list:
print("By", geoscale)
prep_csv.split_geoscale(
in_df=df,
geoscale=geoscale,
nbep_year=nbep_year,
source_year=source_year,
out_path=base_folder,
csv_prefix="IMPERVIOUS_change_"
)
print("\nDONE")