Example #1
0
def import_file(src, filename, dst, usr_id):
    usr = User.objects.get(pk=usr_id)
    logger.debug("[x] Importing %r to DB" % filename)
    timestamp = get_timestamp_by_filename(filename)

    # TODO: Shaoqing: remoteid is not guaranteed to be unique.
    # It was chosen by the supplier of the data. IMHO you
    # should use the uuid.
    remoteid = get_remoteid_by_filename(filename)
    ts = get_auth(usr, remoteid)

    if ts is not False:
        str_year = str(timestamp.year[0])
        str_month = str(timestamp.month[0])
        str_day = str(timestamp.day[0])
        store_dst = dst + ts.uuid + '/' + str_year +\
            '-' + str_month + '-' + str_day + '/'
        store_dstf = store_dst + filename
        values = {"value": store_dstf}

        ts.set_event(timestamp[0], values)
        ts.save()
        data_move(src + filename, store_dst)
        logger.info(
            '[x] File:--%r-- has been successfully imported',
            src + filename)
    else:
        logger.error('[x] File:--%r-- has been rejected' % (src + filename))
        data_move(src + filename, ERROR_file)
        raise Exception("[x] %r _FAILED to be imported" % (src + filename))
Example #2
0
def import_geotiff(dirname, filename, dst, usr_id):

    usr = User.objects.get(pk=usr_id)
    ts = get_timeseries_by_filename(filename, usr)
    src = os.path.join(dirname, filename)

    # Check permissions.

    if not can_change(usr, ts):
        logger.error("[x] File:--%r-- has been rejected", src)
        data_move(src, ERROR_file)
        raise Exception("[x] %r _FAILED to be imported" % src)

    # Move file to its permanent location.

    timestamp = get_timestamp_by_filename(filename)  # pandas DatetimeIndex

    str_date = "{:4d}-{:02d}-{:02d}".format(
        timestamp.year[0].item(),
        timestamp.month[0].item(),
        timestamp.day[0].item()
    )

    store_dst = os.path.join(dst, ts.uuid, str_date)
    data_move(src, store_dst)

    # Save into database.

    try:
        src = os.path.join(store_dst, filename)
        logger.debug("[x] Importing %r into DB", src)
        values = {"value": src}
        ts.set_event(timestamp[0], values)
        ts.save()
        logger.info("[x] File:--%r-- has been successfully imported", src)
    except Exception, e:
        logger.error("[x] File:--%r-- failed to be imported", src)
        data_move(src, ERROR_file)
        raise Exception(
            "[x] %r _FAILED to be imported" % src + ' reason--%r' % e)