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 |
|
|
|
13 |
|
|
#import logging
|
14 |
|
|
#logging.basicConfig()
|
15 |
|
|
|
16 |
|
|
## set working directory (where files will be downloaded)
|
17 |
|
|
os.chdir('/home/adamw/acrobates/adamw/projects/cloud/data/mod09')
|
18 |
|
|
|
19 |
|
|
#MY_SERVICE_ACCOUNT = '511722844190@developer.gserviceaccount.com' # replace with your service account
|
20 |
|
|
#MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine-privatekey.p12' # replace with you private key file path
|
21 |
|
|
|
22 |
|
|
MY_SERVICE_ACCOUNT = '205878743334-4mrtqgu0n5rnsv1vanrvv6atqk6vu8am@developer.gserviceaccount.com'
|
23 |
|
|
MY_PRIVATE_KEY_FILE = '/home/adamw/EarthEngine_Jeremy-privatekey.p12'
|
24 |
|
|
|
25 |
|
|
ee.Initialize(ee.ServiceAccountCredentials(MY_SERVICE_ACCOUNT, MY_PRIVATE_KEY_FILE))
|
26 |
|
|
|
27 |
|
|
## set map center to speed up viewing
|
28 |
|
|
ee.mapclient.centerMap(-121.767, 46.852, 11)
|
29 |
|
|
|
30 |
|
|
#///////////////////////////////////
|
31 |
|
|
#// Function to extract cloud flags
|
32 |
|
|
def getmod09(img): return(img.select(['state_1km']).expression("((b(0)/1024)%2)"));
|
33 |
|
|
|
34 |
|
|
#// Date ranges
|
35 |
|
|
yearstart=2000
|
36 |
|
|
yearstop=2010
|
37 |
|
|
monthstart=1
|
38 |
|
|
monthstop=12
|
39 |
|
|
|
40 |
|
|
#////////////////////////////////////////////////////
|
41 |
|
|
# Loop through months and get monthly % missing data
|
42 |
|
|
|
43 |
|
|
## set a year-month if you don't want to run the loop (for testing)
|
44 |
|
|
year=2001
|
45 |
|
|
month=1
|
46 |
|
|
|
47 |
|
|
for year in range(yearstart,yearstop+1): {
|
48 |
|
|
for month in range(monthstart,monthstop); {
|
49 |
|
|
|
50 |
|
|
print('Processing '+str(year)+'_'+str(month))
|
51 |
|
|
|
52 |
|
|
## MOD09 internal cloud flag for this year-month
|
53 |
|
|
## to filter by a date range: filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31))
|
54 |
|
|
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09);
|
55 |
|
|
|
56 |
|
|
## calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer
|
57 |
|
|
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte();
|
58 |
|
|
|
59 |
|
|
## print info to confirm there is data
|
60 |
|
|
#mod09a.getInfo()
|
61 |
|
|
|
62 |
|
|
## add to plot to confirm it's working
|
63 |
|
|
#ee.mapclient.addToMap(mod09a, {'range': '0,100'}, 'MOD09')
|
64 |
|
|
|
65 |
|
|
## define region for download
|
66 |
|
|
region='[[-72, -1], [-72, 11], [-59, 11], [-59, -1]]' #h11v08
|
67 |
|
|
# '[[-90, -1], [-90, 20], [-49, 20], [-49, -1]]' // Northern S. America
|
68 |
|
|
# '[[-180, -90], [-180, 90], [180, 90], [180, -90]]' //global
|
69 |
|
|
# '[[-180, -60], [-180, 90], [180, 90], [180, -60]]' // Western Hemisphere
|
70 |
|
|
|
71 |
6fc314e2
|
Adam M. Wilson
|
## Define tiles
|
72 |
|
|
|
73 |
|
|
|
74 |
58d05081
|
Adam M. Wilson
|
## build the URL and name the object (so that when it's unzipped we know what it is!)
|
75 |
|
|
path =mod09a.getDownloadUrl({
|
76 |
|
|
'name': 'mod09_'+str(year)+"_"+str(month), # name the file (otherwise it will be a uninterpretable hash)
|
77 |
|
|
'scale': 926, # resolution in meters
|
78 |
|
|
'crs': 'EPSG:4326', # MODIS sinusoidal
|
79 |
6fc314e2
|
Adam M. Wilson
|
'region': region # region defined above
|
80 |
58d05081
|
Adam M. Wilson
|
});
|
81 |
|
|
|
82 |
|
|
## download with wget
|
83 |
|
|
wget.download(path)
|
84 |
|
|
|
85 |
|
|
}}
|
86 |
|
|
|