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 |
|
|
#////////////////////////////////////////////////////
|
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 |
|
|
month=1
|
47 |
|
|
|
48 |
|
|
for year in range(yearstart,yearstop+1): {
|
49 |
|
|
for month in range(monthstart,monthstop); {
|
50 |
|
|
|
51 |
|
|
print('Processing '+str(year)+'_'+str(month))
|
52 |
|
|
|
53 |
|
|
## MOD09 internal cloud flag for this year-month
|
54 |
|
|
## to filter by a date range: filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31))
|
55 |
|
|
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09);
|
56 |
|
|
|
57 |
|
|
## calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer
|
58 |
|
|
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte();
|
59 |
|
|
|
60 |
|
|
## print info to confirm there is data
|
61 |
|
|
#mod09a.getInfo()
|
62 |
|
|
|
63 |
|
|
## add to plot to confirm it's working
|
64 |
|
|
#ee.mapclient.addToMap(mod09a, {'range': '0,100'}, 'MOD09')
|
65 |
|
|
|
66 |
|
|
## define region for download
|
67 |
|
|
region='[[-72, -1], [-72, 11], [-59, 11], [-59, -1]]' #h11v08
|
68 |
|
|
# '[[-90, -1], [-90, 20], [-49, 20], [-49, -1]]' // Northern S. America
|
69 |
|
|
# '[[-180, -90], [-180, 90], [180, 90], [180, -90]]' //global
|
70 |
|
|
# '[[-180, -60], [-180, 90], [180, 90], [180, -60]]' // Western Hemisphere
|
71 |
|
|
|
72 |
|
|
## build the URL and name the object (so that when it's unzipped we know what it is!)
|
73 |
|
|
path =mod09a.getDownloadUrl({
|
74 |
|
|
'name': 'mod09_'+str(year)+"_"+str(month), # name the file (otherwise it will be a uninterpretable hash)
|
75 |
|
|
'scale': 926, # resolution in meters
|
76 |
|
|
'crs': 'EPSG:4326', # MODIS sinusoidal
|
77 |
|
|
'region': region
|
78 |
|
|
});
|
79 |
|
|
|
80 |
|
|
## download with wget
|
81 |
|
|
wget.download(path)
|
82 |
|
|
|
83 |
|
|
}}
|
84 |
|
|
|