Project

General

Profile

Download (2.43 KB) Statistics
| Branch: | Revision:
1
#!/usr/bin/env python
2

    
3
## Example script that downloads data from Google Earth Engine using the python API
4
## MODIS MOD09GA data is processed to extract the MOD09 cloud flag and calculate monthly cloud frequency
5

    
6
## import some libraries
7
import ee
8
from ee import mapclient
9
import ee.mapclient 
10
import datetime
11
import wget
12
import os
13
import sys
14

    
15
def Usage():
16
    print('Usage: ee.MOD9.py -projwin  ulx uly lrx lry -o output   ') 
17
    sys.exit( 1 )
18

    
19
ulx = float(sys.argv[2])
20
uly = float(sys.argv[3])
21
lrx = float(sys.argv[4])
22
lry = float(sys.argv[5])
23
output = sys.argv[7]
24

    
25
print ( output , ulx ,uly ,lrx , lry )
26

    
27
#import logging
28

    
29
MY_SERVICE_ACCOUNT = '364044830827-ubb6ja607b8j7t8m9uooi4c01vgah4ms@developer.gserviceaccount.com'  # replace with your service account
30
MY_PRIVATE_KEY_FILE = '/home/selv/GEE/fe3f13d90031e3eedaa9974baa6994e467b828f7-privatekey.p12'      # replace with you private key file path
31
ee.Initialize(ee.ServiceAccountCredentials(MY_SERVICE_ACCOUNT, MY_PRIVATE_KEY_FILE))
32

    
33
#///////////////////////////////////
34
#// Function to extract cloud flags
35
def getmod09(img): return(img.select(['state_1km']).expression("((b(0)/1024)%2)"));
36

    
37
## set a year-month if you don't want to run the loop (for testing)
38
year=2001
39
month=1
40

    
41
print('Processing '+str(year)+'_'+str(month))
42

    
43
## MOD09 internal cloud flag for this year-month
44
## to filter by a date range:  filterDate(datetime.datetime(yearstart,monthstart,1),datetime.datetime(yearstop,monthstop,31))
45
mod09 = ee.ImageCollection("MOD09GA").filter(ee.Filter.calendarRange(year,year,"year")).filter(ee.Filter.calendarRange(month,month,"month")).map(getmod09);
46

    
47
## calculate mean cloudiness (%), rename band, multiply by 100, and convert to integer
48
mod09a=mod09.mean().select([0], ['MOD09_'+str(year)+'_'+str(month)]).multiply(ee.Image(100)).byte();
49

    
50
## print info to confirm there is data
51
# mod09a.getInfo()
52

    
53
## define region for download
54
region=[ulx,lry ], [ulx, uly], [lrx, uly], [lrx, lry]  #h11v08
55
strregion=str(list(region))
56

    
57
## Define tiles
58
region='[[-72, -1], [-72, 11], [-59, 11], [-59, -1]]'
59
## build the URL and name the object (so that when it's unzipped we know what it is!)
60
path =mod09a.getDownloadUrl({
61
'name': output,  # name the file (otherwise it will be a uninterpretable hash)
62
'scale': 926,                               # resolution in meters
63
'crs': 'EPSG:4326',                         # MODIS sinusoidal
64
'region': strregion                  # region defined above
65
});
66

    
67
## download with wget
68
wget.download(path)
69

    
70

    
71

    
(46-46/50)