Project

General

Profile

« Previous | Next » 

Revision 58d05081

Added by Adam Wilson about 11 years ago

Adding ee.MOD09.py script to process and download data from Google Earth Engine

View differences:

climate/procedures/MOD35C5_Evaluation.r
11 11
library(rgeos)
12 12
library(splancs)
13 13

  
14

  
15
## add tags for distribution
16
## MOD35
17
tags=c("TIFFTAG_IMAGEDESCRIPTION='Collection 5 MOD35 Cloud Frequency for 2009 extracted from MOD09GA state_1km bits 0-1. The MOD35 bits encode four categories (with associated confidence that the pixel is actually clear): confidently clear (confidence > 0.99), probably clear (0.99 >= confidence > 0.95), probably cloudy (0.95 >= confidence > 0.66), and confidently cloudy (confidence <= 0.66).  Following the advice of the MODIS science team (Frey, 2010), we binned confidently clear and probably clear together as clear and the other two classes as cloudy.  The daily cloud mask time series were summarized to mean cloud frequency (CF) by calculating the proportion of cloudy days during 2009'",
18
  "TIFFTAG_DOCUMENTNAME='Collection 5 MOD35 Cloud Frequency'",
19
  "TIFFTAG_DATETIME='20090101'",
20
  "TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
21
system(paste("/usr/local/src/gdal-1.10.0/swig/python/scripts/gdal_edit.py data/MOD35_2009.tif ",paste("-mo ",tags,sep="",collapse=" "),sep=""))
22
## MOD09
23
tags=c("TIFFTAG_IMAGEDESCRIPTION='Collection 5 MOD09 Cloud Frequency for 2009 extracted from MOD09GA \'PGE11\' internal cloud mask algorithm (embedded in MOD09GA \'state_1km\' bit 10. The daily cloud mask time series were summarized to mean cloud frequency (CF) by calculating the proportion of cloudy days during 2009'",
24
  "TIFFTAG_DOCUMENTNAME='Collection 5 MOD09 Cloud Frequency'",
25
  "TIFFTAG_DATETIME='20090101'",
26
  "TIFFTAG_ARTIST='Adam M. Wilson (adam.wilson@yale.edu)'")
27
system(paste("/usr/local/src/gdal-1.10.0/swig/python/scripts/gdal_edit.py data/MOD09_2009.tif ",paste("-mo ",tags,sep="",collapse=" "),sep=""))
28

  
14 29
## get % cloudy
15 30
mod09=raster("data/MOD09_2009.tif")
16 31
names(mod09)="C5MOD09CF"
......
20 35
names(mod35c5)="C5MOD35CF"
21 36
NAvalue(mod35c5)=0
22 37

  
38

  
23 39
## mod35C6 annual
24 40
if(!file.exists("data/MOD35C6_2009.tif")){
25 41
  system("/usr/local/gdal-1.10.0/bin/gdalbuildvrt  -a_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -sd 1 -b 1 data/MOD35C6.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1-9]*_mean.nc'` ")
26
#  system("gdalbuildvrt data/MOD35C6.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1-9]*_mean.nc'` ")
27

  
28
#  system("/usr/local/gdal-1.10.0/bin/gdalbuildvrt -a_srs '+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs' -sd 4 -b 1 data/MOD35C6_CFday_pmiss.vrt `find /home/adamw/acrobates/adamw/projects/interp/data/modis/mod35/summary/ -name '*h[1]*.nc'` ")
29
#  system("gdalwarp data/MOD35C6_CFday_pmiss.vrt data/MOD35C6_CFday_pmiss.tif -r bilinear")
30

  
31 42
  system("align.sh data/MOD35C6.vrt data/MOD09_2009.tif data/MOD35C6_2009.tif")
32
#  system("align.sh data/MOD35C6_CFday_pmiss.vrt data/MOD09_2009.tif data/MOD35C6_CFday_pmiss.tif")
33 43
}
44

  
34 45
mod35c6=raster("data/MOD35C6_2009.tif")
35 46
names(mod35c6)="C6MOD35CF"
36 47
NAvalue(mod35c6)=255
......
44 55
               "data/MCD12Q1_IGBP_2009_051_wgs84_1km.tif -overwrite ",sep=""))}
45 56
lulc=raster("data/MCD12Q1_IGBP_2009_051_wgs84_1km.tif")
46 57

  
47
#  lulc=ratify(lulc)
48 58
  data(worldgrids_pal)  #load palette
49 59
  IGBP=data.frame(ID=0:16,col=worldgrids_pal$IGBP[-c(18,19)],
50 60
    lulc_levels2=c("Water","Forest","Forest","Forest","Forest","Forest","Shrublands","Shrublands","Savannas","Savannas","Grasslands","Permanent wetlands","Croplands","Urban and built-up","Cropland/Natural vegetation mosaic","Snow and ice","Barren or sparsely vegetated"),stringsAsFactors=F)
51 61
  IGBP$class=rownames(IGBP);rownames(IGBP)=1:nrow(IGBP)
52 62
  levels(lulc)=list(IGBP)
53
#lulc=crop(lulc,mod09)
54 63
names(lulc)="MCD12Q1"
55 64

  
56 65
## make land mask
......
58 67
  land=calc(lulc,function(x) ifelse(x==0,NA,1),file="data/land.tif",options=c("COMPRESS=LZW","ZLEVEL=9","PREDICTOR=2"),datatype="INT1U",overwrite=T)
59 68
land=raster("data/land.tif")
60 69

  
61
## mask cloud masks to land pixels
62
#mod09l=mask(mod09,land)
63
#mod35l=mask(mod35,land)
64

  
65 70
#####################################
66 71
### compare MOD43 and MOD17 products
67 72

  
68 73
## MOD17
69
#extent(mod17)=alignExtent(mod17,mod09)
70 74
if(!file.exists("data/MOD17.tif"))
71 75
system("align.sh ~/acrobates/adamw/projects/interp/data/modis/MOD17/MOD17A3_Science_NPP_mean_00_12.tif data/MOD09_2009.tif data/MOD17.tif")
72 76
mod17=raster("data/MOD17.tif",format="GTiff")
......
98 102
names(pp)="MOD35pp"
99 103

  
100 104

  
101
#hist(dif,maxsamp=1000000)
102
## draw lulc-stratified random sample of mod35-mod09 differences 
103
#samp=sampleStratified(lulc, 1000, exp=10)
104
#save(samp,file="LULC_StratifiedSample_10000.Rdata")
105
#mean(dif[samp],na.rm=T)
106
#Stats(dif,function(x) c(mean=mean(x),sd=sd(x)))
107

  
108

  
109 105
###
110

  
111 106
n=100
112 107
at=seq(0,100,len=n)
113 108
cols=grey(seq(0,1,len=n))
......
115 110
bgyr=colorRampPalette(c("blue","green","yellow","red"))
116 111
cols=bgyr(n)
117 112

  
118

  
119 113
### Transects
120 114
r1=Lines(list(
121 115
  Line(matrix(c(
......
209 203
  overlay(mod35c5,mod09,fun=function(x,y) {return(x-y)},file="data/dif_c5_09.tif",format="GTiff",options=c("COMPRESS=LZW","ZLEVEL=9"),overwrite=T)
210 204
dif_c5_09=raster("data/dif_c5_09.tif",format="GTiff")
211 205

  
212
#dif_c6_09=mod35c6-mod09
213
#dif_c5_c6=mod35c5-mod35c6
214 206

  
215 207
##################################################################################
216 208
## Identify problematic areas with hard edges in cloud frequency
......
310 302
### read them back in
311 303
pp_bias=raster("data/pp_bias.tif")
312 304
names(pp_bias)="Processing Path"
305
NAvalue(pp_bias)=255
313 306
lulc_bias=raster("data/lulc_bias.tif")
314 307
names(lulc_bias)="Land Use Land Cover"
308
NAvalue(lulc_bias)=255
309

  
310
## read in WWF biome data to summarize by biome
311
if(!file.exists("data/teow/wwf_terr_ecos.shp"){
312
  system("wget -O data/teow.zip http://assets.worldwildlife.org/publications/15/files/original/official_teow.zip?1349272619")
313
  system("unzip data/teow.zip -d data/teow/")
314
   biome=readOGR("data/teow/wwf_terr_ecos.shp","wwf_terr_ecos")
315
   biome=biome[biome$BIOME<50,]
316
   biome2=gUnaryUnion(biome,id=biome$BIOME)
317
  ## create biome.csv using names in html file   
318
  biomeid=read.csv("data/teow/biome.csv",stringsAsFactors=F)
319
  biome2=SpatialPolygonsDataFrame(biome2,data=biomeid)
320
  writeOGR(biome2,"data/teow","biomes",driver="ESRI Shapefile")
321
}
322
   biome=readOGR("data/teow/biomes.shp","biomes")
323

  
324
biome2=extract(pp_bias,biome[biome$Biome==12,],df=T,fun=function(x) data.frame(mean=mean(x,na.rm=T),sd=sd(x,na.rm=T),prop=(sum(!is.na(x))/length(x))))
315 325

  
316
pat=c(0,0.05,1)#seq(0,0.-5,len=2) #,seq(0.05,.1,len=50))
317
grayr2=colorRampPalette(c("red","transparent"))#grey(c(.75,.5,.25))))
326
pat=c(seq(0,100,len=100),254)#seq(0,0.-5,len=2) #,seq(0.05,.1,len=50))
327
grayr2=colorRampPalette(c("grey","green","red"))#
328
#grayr2=colorRampPalette(grey(c(.75,.5,.25)))
318 329
levelplot(stack(pp_bias,lulc_bias),col.regions=c(grayr2(2)),at=pat,
319 330
          colorkey=F,margin=F,maxpixels=1e6)+layer(sp.lines(coast,lwd=.5))
331
levelplot(lulc_bias,col.regions=c(grayr2(100),"black"),at=pat,
332
          colorkey=T,margin=F,maxpixels=1e4)+layer(sp.lines(coast,lwd=.5))
333

  
334
histogram(lulc_bias)
320 335

  
321 336
cor(td1$MOD17,td1$C6MOD35,use="complete",method="spearman")
322 337
cor(td1$MOD17[td1$edgeb==1],td1$C5MOD35[td1$edgeb==1],use="complete",method="spearman")
climate/procedures/MOD35_ExtractProcessPath.r
6 6
library(raster)
7 7
library(spgrass6)
8 8
library(rgeos)
9
##download 3 days of modis swath data:
10 9

  
11 10
#### Set up command for running swtif to grid and mosaic the swath data
12 11
stitch=paste("sudo MRTDATADIR=\"/usr/local/heg/2.12/data\" PGSHOME=/usr/local/heg/2.12/TOOLKIT_MTD PWD=/home/adamw /usr/local/heg/2.12b/bin/swtif")
......
15 14
url="ftp://ladsweb.nascom.nasa.gov/allData/51/MOD35_L2/2012/"
16 15
dir.create("swath")
17 16

  
17
##download 3 days of modis swath data:
18 18
getdata=F
19 19
if(getdata)
20 20
  system(paste("wget -S --recursive --no-parent --no-directories -N -P swath --accept \"hdf\" --accept \"002|003|004\" ",url))
climate/procedures/ee.MOD09.py
1
## 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

  
85

  

Also available in: Unified diff