-
Notifications
You must be signed in to change notification settings - Fork 5
/
pdsi.py
56 lines (40 loc) · 1.6 KB
/
pdsi.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
import ee, datetime
import pandas as pd
import geopandas as gpd
import matplotlib.dates as mdates
from IPython.display import Image
from matplotlib import dates
from shapely.geometry import shape
import skimage
def fc2df(fc):
# Convert a FeatureCollection into a pandas DataFrame
# Features is a list of dict with the output
features = fc.getInfo()['features']
dictarr = []
for f in features:
# Store all attributes in a dict
attr = f['properties']
# and treat geometry separately
attr['geometry'] = f['geometry'] # GeoJSON Feature!
# attr['geometrytype'] = f['geometry']['type']
dictarr.append(attr)
df = gpd.GeoDataFrame(dictarr)
# Convert GeoJSON features to shape
df['geometry'] = map(lambda s: shape(s), df.geometry)
return df
ee.Initialize()
imageCollection = ee.ImageCollection('IDAHO_EPSCOR/TERRACLIMATE').filterDate('1992-09-01', '2018-09-01').select('pdsi');
def function(im):
return im.rename([im.get("system:index")])
arr = imageCollection.map(function)
imageCollection = arr
def stackCollection(collection):
first = ee.Image(collection.first()).select([])
def appendBands(image, previous):
return ee.Image(previous).addBands(image)
return ee.Image(collection.iterate(appendBands, first))
stacked = stackCollection(imageCollection)
Countries = ee.FeatureCollection('users/giacomofalchetta/shirebasin')
pdsi = stacked.reduceRegions(collection=Countries, reducer=ee.Reducer.mean())
pdsi = fc2df(pdsi)
pdsi.to_csv("D:\OneDrive - FONDAZIONE ENI ENRICO MATTEI\Visiting IIASA\hydropower_remotesensing/pdsi.csv")