Project

General

Profile

« Previous | Next » 

Revision 01b3830e

Added by Jim Regetz over 12 years ago

  • ID 01b3830e935fd53889c078715af701506dc1bc97
  • Child e84c3d48

added function documentation and removed two obsolete functions

View differences:

climate/extra/aggregate-daily-lst.py
31 31
# helper functions
32 32
#------------------
33 33

  
34
# quick function to reformat e.g. 200065 to 2000.03.05
35 34
def yj_to_ymd(year, doy):
36
    date = datetime.datetime.strptime('%d%d' % (year, doy), '%Y%j')
35
    """Return date as e.g. '2000.03.05' based on specified year and
36
    numeric day-of-year (doy) """
37
    date = datetime.datetime.strptime('%d%03d' % (year, doy), '%Y%j')
37 38
    return date.strftime('%Y.%m.%d')
38 39

  
39 40
def get_doy_range(year, month):
41
    """Determine starting and ending numeric day-of-year (doy)
42
    asscociated with the specified month and year.
43

  
44
    Arguments:
45
    year -- four-digit integer year
46
    month -- integer month (1-12)
47

  
48
    Returns tuple of start and end doy for that month/year.
49
    """
40 50
    last_day_of_month = calendar.monthrange(year, month)[1]
41 51
    start_doy = int(datetime.datetime.strptime('%d.%02d.%02d' % (year,
42 52
        month, 1), '%Y.%m.%d').strftime('%j'))
......
46 56

  
47 57
# quick function to return list of dirs in wd
48 58
def list_contents(ftp):
59
    """Parse ftp directory listing into list of names of the files
60
    and/or directories contained therein. May or may not be robust in
61
    general, but seems to work fine for LP DAAP ftp server."""
49 62
    listing = []
50 63
    ftp.dir(listing.append)
51 64
    contents = [item.split()[-1] for item in listing[1:]]
......
132 145
        hdfs.append(os.path.abspath(files[0]))
133 146
    return hdfs
134 147

  
135
# create raster of pixelwise means for a list of input rasters
136
def calc_mean(maplist, name, overwrite=False):
137
    numerator = '(%s)' % '+'.join(['if(isnull(%s), 0, %s)' % (m, m)
138
        for m in maplist])
139
    denominator = '(%s)' % '+'.join(['if(!isnull(%s))' % m
140
        for m in maplist])
141
    gs.mapcalc('mean_%s = %s/%s' % (name, numerator, denominator),
142
        overwrite=overwrite)
148
def calc_clim(maplist, name, overwrite=False):
149
    """Generate some climatalogies in GRASS based on the input list of
150
    maps. As usual, current GRASS region settings apply. Produces the
151
    following output rasters:
152
      * nobs: count of number of (non-null) values over the input maps
153
      * mean: arithmetic mean of (non-null) values over the input maps
143 154

  
144
# create raster of pixelwise N for a list of input rasters
145
def calc_nobs(maplist, name, overwrite=False):
146
    denominator = '(%s)' % '+'.join(['if(!isnull(%s))' % m
147
        for m in maplist])
148
    gs.mapcalc('nobs_%s = %s' % (name, denominator), overwrite=overwrite)
155
    Arguments:
156
    maplist -- list of names of GRASS maps to be aggregated
157
    name -- template (suffix) for GRASS output map names
149 158

  
150
# ...combined version of the above two functions...
151
def calc_clim(maplist, name, overwrite=False):
159
    Returns list of names of the output maps created in GRASS.
160
    """
152 161
    denominator = '(%s)' % '+'.join(['if(!isnull(%s))' % m
153 162
        for m in maplist])
154 163
    gs.mapcalc('nobs_%s = %s' % (name, denominator), overwrite=overwrite)

Also available in: Unified diff