-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarize_biomes.py
160 lines (126 loc) · 4.25 KB
/
summarize_biomes.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
120
121
122
123
124
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
import sys
import os
# import Nio
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import numba
from numba import jit
import dask
from dask import delayed
import psutil
from rasterio.plot import show
def main():
inpfname = "/home/gabriel/transicao/doutorado/rochedo/OTIMIZAGRO_Marcos_Costa/WEG/weg_comp_2012-2050.nc"
inpvname = "landuse"
codfname = "/home/gabriel/transicao/doutorado/rochedo/OTIMIZAGRO_Marcos_Costa/biomas.nc"
codvname = "Band1"
cods = [1,2,3,4,5,6]
clas = (list(range(0,40)))
maxcod = 6
maxcla = 40
inpvarname = "PRECT"
outvarname = "prec"
finp = xr.open_dataset(inpfname, decode_times=False)
inparr = finp[inpvname]
times = np.round(inparr['time'])
fcod = xr.open_dataset(codfname)
codarr = fcod[codvname]
year = times[0]
yinparr = inparr.sel(time = year, method = 'nearest')
yinparr = yinparr.sel(lat = slice(-10.0,0.0), lon = slice(-70.,-60.))
codarr = codarr.sel(lat = slice(-10.0,0.0), lon = slice(-70.,-60.))
# yinparr = yinparr.sel(lat = slice(-2.0,0.0), lon = slice(-62.,-60.))
# codarr = codarr.sel(lat = slice(-2.0,0.0), lon = slice(-62.,-60.))
# show(yinparr)
# show(codarr)
cod = 5
# vec = yinparr.values[10,:]
# # print(sum1d(vec)
# mat = yinparr.values[10:20,10:20]
# codmat = codarr.values[10:20,10:20]
# print(mat)
# print(yinparr.dtype)
# print(sum2d(mat))
# print(sum2d(yinparr.values))
# outmat = np.zeros((len(cods),len(clas)), dtype = inparr.dtype)
outmat = np.zeros((maxcod+1,maxcla+1), dtype = np.float64)
chunksize = 100
# print(countclasses(yinparr.values.astype(int),outmat))
array = dask.array.from_array(yinparr.values.astype(int), chunks = chunksize)
codarray = dask.array.from_array(codarr.values.astype(int), chunks = chunksize)
# array = yinparr.values.astype(int)
# codarray = codarr.values.astype(int)
print(sys.getsizeof(array))
print(sys.getsizeof(codarray))
# exit()
# filloutmat = countclasses_cod(array, codarray, outmat)
# filloutmat = countclassesc_cod(mat.astype(int),codmat.astype(int), outmat)
# print(np.isfinite(codarr.values[0,0]))
# lala = filloutmat.compute()
# icods = {cods[i]:i for i in range(len(cods))}
# iclas = {clas[i]:i for i in range(len(clas))}
# print(iclas)
@delayed
# @jit(nopython = True, parallel = True)
@jit(nopython = True)
def countclasses_cod(array,codarray,outmat):
# for i in numba.prange(array.shape[0]):
# for j in numba.prange(array.shape[1]):
for i in range(array.shape[0]):
for j in range(array.shape[1]):
if np.isfinite(codarray[i,j]) & np.isfinite(array[i,j]):
# print(outmat[codarray[i,j],array[i,j]])
outmat[codarray[i,j],array[i,j]] += 1
# print(np.max(outmat))
return outmat
@jit(nopython = True)
def countclasses(array,outmat):
for i in range(array.shape[0]):
for j in range(array.shape[1]):
# sum += array[i,j]
outmat[0,array[i,j]] += 1.0
return outmat
@jit(nopython = True)
def countclasses(array,outmat):
for i in range(array.shape[0]):
for j in range(array.shape[1]):
# sum += array[i,j]
outmat[0,array[i,j]] += 1.0
return outmat
@jit(nopython = True)
def sum2d(array):
sum = 0.0
for i in range(array.shape[0]):
for j in range(array.shape[1]):
sum += array[i,j]
return sum
@jit
def sum1d(array):
sum = 0.0
for i in range(array.shape[0]):
sum += array[i]
return sum
# print(yinparr.lat)
# print(codarr.lat)
#
# mskyinparr = yinparr
# mskyinparr = mskyinparr.where(codarr == cod)
# show(mskyinparr)
#
# print(np.sum(mskyinparr))
# # cod = 5
#
# # MemoryError
# indcodes = {cod:np.where(codarr == cod) for cod in cods}
#
# print(np.sum(yinparr.values[indcodes[cod]]))
# print(indcodes)
# mskinparr = inparr.
#
# inparr.sel(time = year, method = 'nearest').where(codarr == cod)
#inparr.sel(time = 2012, method = 'nearest')
# finp.to_netcdf(outfname, mode = "w", format = "NETCDF3_64BIT")
# finp[inpvarname].to_netcdf(outfname, mode = "w", format = "NETCDF3_64BIT")
if __name__ == "__main__":
main()