-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_data_files.py
119 lines (93 loc) · 4.14 KB
/
generate_data_files.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
112
113
114
115
116
117
118
119
import matplotlib.pyplot as plt
import geopandas
import georasters as gr
import os
import numpy as np
import scipy.signal
from geo_scripts.process_height import gen_ranges, get_slices, aggregate_slices, filter_masked, get_global_raster, \
rasterize_shapefile, aggregate_grid
def main():
precip_data = gr.from_file(os.path.expanduser("~/Downloads/datasets/chelsea/CHELSA_bio10_12.tif"))
print("ocean map")
# get the chelsea data mask to be used as the main ocean mask
x_ranges = list(gen_ranges(-180, 180, 1))
y_ranges = list(gen_ranges(90, -90, 1))
slices = get_slices(precip_data, x_ranges, y_ranges)
precip_ag = aggregate_slices(slices, filter_masked)
chelsea_mask = precip_ag.mask
# load shapefiles
water_shape = geopandas.read_file(
os.path.expanduser("~/Downloads/datasets/natural_earth/ne_50m_ocean/ne_50m_ocean.shp"))
df_river = geopandas.read_file(os.path.expanduser(
"~/Downloads/datasets/natural_earth/ne_50m_rivers_lake_centerlines_scale_rank/ne_50m_rivers_lake_centerlines_scale_rank.shp"))
df_lake = geopandas.read_file(
os.path.expanduser("~/Downloads/datasets/natural_earth/ne_50m_lakes/ne_50m_lakes.shp"))
df_lake_historic = geopandas.read_file(
os.path.expanduser("~/Downloads/datasets/natural_earth/ne_50m_lakes_historic/ne_50m_lakes_historic.shp"))
# rasterize the shapefiles
raster_1 = get_global_raster(1)
ocean = rasterize_shapefile(water_shape, raster_1)
lake_r = rasterize_shapefile(df_lake, raster_1)
lake_h_r = rasterize_shapefile(df_lake_historic, raster_1)
# clip out the caspian sea
n = 41
s = 54
e = 234
w = 226
area_mask = np.zeros(lake_r.shape, dtype=np.bool)
area_mask[n:s, w:e] = True
sea_mask = ocean & area_mask
# merge the rasters
underwater = chelsea_mask | lake_r | lake_h_r | sea_mask
# save the underwater mask
np.save("./data/underwater_mask.npy", underwater)
np.save("./data/ocean_mask.npy", chelsea_mask)
# littoral cells
conv = np.asarray([
[0, 1, 0],
[1, 0, 1],
[0, 1, 0],
])
lit = scipy.signal.convolve2d(chelsea_mask, conv, mode="same", boundary="wrap")
lit = lit >= 1
np.save("./data/littoral.npy", lit)
# generate the desert map
print("desert map")
desert = precip_ag < 250
np.save("./data/desert.npy", desert.data)
# generate river map
print("river map")
river_rasterized = rasterize_shapefile(df_river, raster_1)
np.save("./data/river.npy", river_rasterized)
# generate elevation map
print("elevation map")
height = gr.from_file(os.path.expanduser("~/Downloads/datasets/elevation/one_deg_height.tif")).raster
np.save("./data/height.npy", height.data)
stddev = gr.from_file(os.path.expanduser("~/Downloads/datasets/elevation/one_deg_stddev.tif")).raster
np.save("./data/stddev.npy", stddev.data)
# generate steppes map
print("Steppes map")
ecos_shape = geopandas.read_file(
os.path.expanduser("~/Downloads/datasets/official_teow/official/wwf_terr_ecos.shp"))
steppes = ecos_shape[ecos_shape["BIOME"] == 8]
raster_1 = get_global_raster(1)
steppes_rasterized = rasterize_shapefile(steppes, raster_1)
np.save("./data/steppes.npy", steppes_rasterized)
# Efective temperatures?
print("effective temperature")
effective_temperature = np.load("./data/effective_temperature_large.npy")
effective_mask = np.load("./data/effective_temperature_large_mask.npy")
effective_temperature = np.ma.array(effective_temperature, mask=effective_mask)
chelsea_geot = (-180.00013888885002, 0.0083333333, 0.0, 83.99986041515001, 0.0, -0.0083333333)
raster = gr.GeoRaster(effective_temperature, chelsea_geot)
et_ag = aggregate_grid(raster, x_ranges, y_ranges)
np.save("./data/effective_temperature.npy", et_ag.data)
# terrestrial plant threshold
tpt = et_ag > 12.75
np.save("./data/terrestrial_plant_threshold.npy", tpt.data)
# agriculture thresholds
print("agriculture")
agriculture = tpt & (desert == False) | (river_rasterized & tpt)
np.save("./data/agriculture.npy", agriculture.data)
if __name__ == '__main__':
main()