def ndfi_value_for_cells(cell_key):

    cell = Cell.get(Key(cell_key))

    ndfi = NDFI('MOD09GA', cell.report.comparation_range(),
                cell.report.range())

    bounds = cell.bounds(amazon_bounds)
    logging.info(bounds)
    ne = bounds[0]
    sw = bounds[1]
    polygons = [[(sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]),
                 (ne[1], sw[0])]]
    data = ndfi.ndfi_change_value(cell.report.base_map(), [polygons])
    logging.info(data)
    if 'data' not in data:
        logging.error("can't get ndfi change value")
        return
    ndfi = data['data']['properties']['ndfiSum']['values']
    for row in xrange(10):
        for col in xrange(10):
            idx = row * 10 + col
            count = float(ndfi['count'][idx])
            s = float(ndfi['sum'][idx])
            if count > 0.0:
                ratio = s / count
            else:
                ratio = 0.0
            ratio = ratio / 10.0  #10 value is experimental
            # asign to cell
            logging.info('cell ndfi (%d, %d): %f' % (row, col, ratio))
            c = cell.child(row, col)
            c.ndfi_change_value = ratio
            c.put()
def ndfi_map():
    """ return map_id for specified ndfi range date
        if date range is not specified only ndfi from 1st of the month
        are computed

        returns a json with mapid and token

        an example request look like this
        /api/v0/ndfi&starttime=1254305000000&endtime=1256900200000

        starttime and endtime are timestamp in milliseconds
    """
    # parse params
    now = datetime.now()
    starttime = request.args.get('starttime', first_of_current_month())
    endtime = request.args.get('endtime', timestamp(now))
    try:
        starttime = float(starttime)
        endtime = float(endtime)
    except ValueError:
        # try to parse as normal format (not timestamp)
        starttime = timestamp(parse(request.args.get('starttime')))
        endtime = timestamp(parse(request.args.get('endtime')))

    ee_resource = 'MOD09GA'
    ndfi = NDFI(ee_resource,
        past_month_range(datetime.fromtimestamp(starttime/1000)), (starttime, endtime))

    ne = amazon_bounds[0]
    sw = amazon_bounds[1]
    polygons = [[ (sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0]) ]]
    #return jsonify(ndfi.mapid())
    return jsonify(ndfi.ndfi_change_value(polygons))
def ndfi_value_for_cells(cell_key):

    cell = Cell.get(Key(cell_key))

    ndfi = NDFI('MOD09GA',
            cell.report.comparation_range(),
            cell.report.range())

    bounds = cell.bounds(amazon_bounds)
    logging.info(bounds)
    ne = bounds[0]
    sw = bounds[1]
    polygons = [[ (sw[1], sw[0]), (sw[1], ne[0]), (ne[1], ne[0]), (ne[1], sw[0]) ]]
    data = ndfi.ndfi_change_value(cell.report.base_map(), [polygons])
    logging.info(data)
    if 'data' not in data:
        logging.error("can't get ndfi change value")
        return
    ndfi = data['data']['properties']['ndfiSum']['values']
    for row in xrange(10):
        for col in xrange(10):
            idx = row*10 + col
            count = float(ndfi['count'][idx])
            s = float(ndfi['sum'][idx])
            if count > 0.0:
                ratio = s/count
            else:
                ratio = 0.0
            ratio = ratio/10.0 #10 value is experimental
            # asign to cell
            logging.info('cell ndfi (%d, %d): %f' % (row, col, ratio))
            c = cell.child(row, col)
            c.ndfi_change_value = ratio
            c.put()
def testing():
    r = Report.current()
    r = Report.get(Key('ahBpbWF6b24tcHJvdG90eXBlcg4LEgZSZXBvcnQYiaECDA'))
    logging.info("report " + unicode(r))
    ee_resource = 'MOD09GA'
    ndfi = NDFI(ee_resource,
            r.comparation_range(),
            r.range())
    #return str(ndfi.mapid2())
    return str(ndfi.freeze_map(1089491, r.key().id()))