1 |
58d05081
|
Adam M. Wilson
|
## Example script that downloads data from Google Earth Engine using the python API
|
2 |
|
|
## MODIS MOD09GA data is processed to extract the MOD09 cloud flag and calculate monthly cloud frequency
|
3 |
|
|
|
4 |
|
|
|
5 |
|
|
## import some libraries
|
6 |
|
|
import ee
|
7 |
|
|
from ee import mapclient
|
8 |
|
|
import ee.mapclient
|
9 |
|
|
import datetime
|
10 |
|
|
import wget
|
11 |
|
|
import os
|
12 |
15936a3e
|
Adam M. Wilson
|
from subprocess import call
|
13 |
58d05081
|
Adam M. Wilson
|
|
14 |
|
|
#import logging
|
15 |
|
|
#logging.basicConfig()
|
16 |
|
|
|
17 |
|
|
## set working directory (where files will be downloaded)
|
18 |
|
|
os.chdir('/home/adamw/acrobates/adamw/projects/cloud/data/mod09')
|
19 |
|
|
|
20 |
15936a3e
|
Adam M. Wilson
|
MY_SERVICE_ACCOUNT = '511722844190@developer.gserviceaccount.com' # replace with your service account
|
21 |
|
|
MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine-privatekey.p12' # replace with you private key file path
|
22 |
58d05081
|
Adam M. Wilson
|
|
23 |
15936a3e
|
Adam M. Wilson
|
#MY_SERVICE_ACCOUNT = '205878743334-4mrtqgu0n5rnsv1vanrvv6atqk6vu8am@developer.gserviceaccount.com'
|
24 |
|
|
#MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine_Jeremy-privatekey.p12'
|
25 |
58d05081
|
Adam M. Wilson
|
|
26 |
|
|
ee.Initialize(ee.ServiceAccountCredentials(MY_SERVICE_ACCOUNT, MY_PRIVATE_KEY_FILE))
|
27 |
|
|
|
28 |
|
|
## set map center to speed up viewing
|
29 |
15936a3e
|
Adam M. Wilson
|
#ee.mapclient.centerMap(-121.767, 46.852, 11)
|
30 |
58d05081
|
Adam M. Wilson
|
|
31 |
|
|
#///////////////////////////////////
|
32 |
|
|
#// Function to extract cloud flags
|
33 |
15936a3e
|
Adam M. Wilson
|
def getmod09(img): return(img.select(['state_1km']).expression("((b(0)/1024)%2)"));
|
34 |
|
|
|
35 |
58d05081
|
Adam M. Wilson
|
#// Date ranges
|
36 |
15936a3e
|
Adam M. Wilson
|
yearstart=2001
|
37 |
|
|
yearstop=2001
|
38 |
58d05081
|
Adam M. Wilson
|
monthstart=1
|
39 |
15936a3e
|
Adam M. Wilson
|
monthstop=2
|
40 |
58d05081
|
Adam M. Wilson
|
|
41 |
|
|
#////////////////////////////////////////////////////
|
42 |
|
|
# Loop through months and get monthly % missing data
|
43 |
|
|
|
44 |
|
|
## set a year-month if you don't want to run the loop (for testing)
|
45 |
|
|
year=2001
|
46 |
15936a3e
|
Adam M. Wilson
|
month=2
|
47 |
|
|
|
48 |
|
|
|
49 |
|
|
## define the regions to be processed
|
50 |
|
|
regions=['[[-180, -60], [-180, 0], [0, 0], [0, -60]]', # SW
|
51 |
|
|
'[[-180, 0], [-180, 90], [0, 90], [0, 0]]', # NW
|
52 |
|
|
'[[0, 0], [0, 90], [180, 90], [180, 0]]', # NE
|
53 |
|
|
'[[0, 0], [0, -60], [180, -60], [180, 0]]'] # SE
|
54 |
|
|
## name the regions (these names will be used in the file names
|
55 |
|
|
## must be same length as regions list above
|
56 |
|
|
rnames=['SW','NW','NE','SE']
|
57 |
|
|
|
58 |
|
|
## Loop over regions, years, months to generate monthly timeseries
|
59 |
|
|
for r in range(0,len(regions)): # loop over regions
|
60 |
|
|
for year in range(yearstart,yearstop+1): # loop over years
|
61 |
|
|
for month in range(monthstart,monthstop+1): # loop over months
|
62 |
|
|
print('Processing '+rnames[r]+"_"+str(year)+'_'+str(month))
|
63 |
|
|
|
64 |
|
|
# output filename
|
65 |
|
|
filename='mod09_'+rnames[r]+"_"+str(year)+"_"+str(month)
|
66 |
|
|
|
67 |
|
|
# Check if file already exists and continue if so...
|
68 |
|
|
if(os.path.exists(filename+".zip")):
|
69 |
|
|
print("File exists:"+filename)
|
70 |
|
|
continue
|
71 |
|
|
|
72 |
|
|
# MOD09 internal cloud flag for this year-month
|
73 |
|
|
# to filter by a date range: filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31))
|
74 |
|
|
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09);
|
75 |
|
|
# calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer
|
76 |
|
|
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte();
|
77 |
|
|
|
78 |
|
|
# Next few lines for testing only
|
79 |
|
|
# print info to confirm there is data
|
80 |
|
|
# mod09a.getInfo()
|
81 |
|
|
# add to plot to confirm it's working
|
82 |
|
|
# ee.mapclient.addToMap(mod09a, {'range': '0,100'}, 'MOD09')
|
83 |
|
|
|
84 |
|
|
# build the URL and name the object (so that when it's unzipped we know what it is!)
|
85 |
|
|
path =mod09a.getDownloadUrl({
|
86 |
|
|
'name': filename, # name the file (otherwise it will be a uninterpretable hash)
|
87 |
|
|
'scale': 1000, # resolution in meters
|
88 |
|
|
'crs': 'EPSG:4326', # MODIS sinusoidal
|
89 |
|
|
'region': regions[r] # region defined above
|
90 |
|
|
});
|
91 |
|
|
|
92 |
|
|
# Sometimes EE will serve a corrupt zipped file with no error
|
93 |
|
|
# to check this, use a while loop that keeps going till there is an unzippable file.
|
94 |
|
|
# This has the potential for an infinite loop...
|
95 |
|
|
|
96 |
|
|
while(not(os.path.exists(filename+".zip"))):
|
97 |
|
|
# download with wget
|
98 |
|
|
print("Downloading "+filename)
|
99 |
|
|
wget.download(path)
|
100 |
|
|
# try to unzip it
|
101 |
|
|
print("Unzipping "+filename)
|
102 |
|
|
zipstatus=call("unzip "+filename+".zip",shell=True)
|
103 |
|
|
# if file doesn't exists or it didn't unzip, remove it and try again
|
104 |
|
|
if(zipstatus==9):
|
105 |
|
|
print("ERROR: "+filename+" unzip-able")
|
106 |
|
|
os.remove(filename)
|
107 |
|
|
|
108 |
|
|
print 'Finished'
|
109 |
58d05081
|
Adam M. Wilson
|
|