Project

General

Profile

« Previous | Next » 

Revision a05f49eb

Added by Jim Regetz almost 13 years ago

  • ID a05f49eb9917be5c8d91e262609c631410e57c81

automated determination of which days to aggregate for any given month

View differences:

climate/extra/aggregate-daily-lst.py
23 23
# Created on 16-May-2012
24 24

  
25 25
import os, glob
26
import datetime
26
import datetime, calendar
27 27
import ftplib
28 28
import grass.script as gs
29 29

  
......
36 36
    date = datetime.datetime.strptime('%d%d' % (year, doy), '%Y%j')
37 37
    return date.strftime('%Y.%m.%d')
38 38

  
39
def get_doy_range(year, month):
40
    last_day_of_month = calendar.monthrange(year, month)[1]
41
    start_doy = int(datetime.datetime.strptime('%d.%02d.%02d' % (year,
42
        month, 1), '%Y.%m.%d').strftime('%j'))
43
    end_doy = int(datetime.datetime.strptime('%d.%02d.%02d' % (year,
44
        month, last_day_of_month), '%Y.%m.%d').strftime('%j'))
45
    return (int(start_doy), int(end_doy))
46

  
39 47
# quick function to return list of dirs in wd
40 48
def list_contents(ftp):
41 49
    listing = []
......
178 186
    # return name of qc-adjusted LST raster in GRASS
179 187
    return lstname
180 188

  
181
#---------------------------------------------
182
# test: download then aggregate for one month
183
#---------------------------------------------
189

  
190
#--------------------------------------------
191
# test procedures mostly for timing purposes
192
#--------------------------------------------
184 193

  
185 194
# TODO: set up a (temporary?) GRASS database to use for processing? code
186 195
# currently assumes it's being run within an existing GRASS session
......
192 201
#     proj4='+proj=sinu +a=6371007.181 +b=6371007.181 +ellps=sphere')
193 202
##    proj4='+proj=sinu +R=6371007.181 +nadgrids=@null +wktext')
194 203

  
204
# (1) download then aggregate for one month
205

  
195 206
tile = 'h09v04'
196 207
year = 2005
197
start_doy = 1
198
end_doy = 31
208
month = 1
199 209
hdfdir = '.'
200 210

  
211
# determine range of doys for the specified month
212
start_doy, end_doy = get_doy_range(year, month)
201 213
# download data
202 214
### [atlas 17-May-2012] Wall time: 111.62 s
203 215
hdfs = download_mod11a1(hdfdir, tile, start_doy, end_doy, year)
204

  
205
# note: now that we've downloaded the files into local directory
206
# 'hdfdir', this should yield the same list...
207
#hdfs = get_hdf_paths(hdfdir, tile, start_doy, end_doy, year)
208

  
209 216
# generate monthly pixelwise mean & count of high-quality daytime LST
210
# values
211 217
### [atlas 17-May-2012] Wall time: 53.79 s
212 218
gs.os.environ['GRASS_OVERWRITE'] = '1'
213 219
LST = [load_qc_adjusted_lst(hdf) for hdf in hdfs]
214
calc_clim(LST, 'LST_%s_%d_%03d_%03d' % (tile, year, start_doy, end_doy))
220
clims = calc_clim(LST, 'LST_%s_%d_%02d' % (tile, year, month))
215 221
# clean up
216 222
gs.run_command('g.remove', rast=','.join(LST))
217 223
gs.os.environ['GRASS_OVERWRITE'] = '0'
224

  
225

  
226
# (2) aggregate all 12 months in one year, using local data
227

  
228
tile = 'h09v04'
229
year = 2005
230
hdfdir = '/home/layers/data/climate/MOD11A1.004-OR-orig'
231

  
232
### [atlas 17-May-2012] Wall time: 802.86 s
233
gs.os.environ['GRASS_OVERWRITE'] = '1'
234
for month in range(1, 12+1):
235
    start_doy, end_doy = get_doy_range(year, month)
236
    hdfs = get_hdf_paths(hdfdir, tile, start_doy, end_doy, year)
237
    LST = [load_qc_adjusted_lst(hdf) for hdf in hdfs]
238
    clims = calc_clim(LST, 'LST_%s_%d_%02d' % (tile, year, month))
239
    gs.run_command('g.remove', rast=','.join(LST))
240
gs.os.environ['GRASS_OVERWRITE'] = '0'

Also available in: Unified diff