示例#1
0
def main():
    paths.build('F:')

    startday = datetime(2013, 12, 17, 0)
    endday = datetime(2013, 12, 31, 0)
    finalyear = 2013

    base_dir = paths.ndvi_individ
    output = paths.ndvi_spline

    year = '2000'
    ref_map = os.path.join(base_dir, year, 'NDVI2000_01_01.tif')
    _, _, _, _, lon, lat, linke, prj, fill_val = read_map(ref_map, 'Gtiff')

    srs = osr.SpatialReference(prj)
    sr_wkt = srs.ExportToWkt()

    for day in rrule.rrule(rrule.DAILY, dtstart=startday, until=endday):
        nr = day.strftime('%j')
        year = day.strftime('%Y')

        # ndvi_daily = time_interpolation(day, lat, lon, finalyear)
        ndvi_daily = time_interpolation(base_dir, day, finalyear)

        # Write daily values to new daily rasters
        daily_doy = 'ndvi{}_{}.tif'.format(year, nr)
        outpath = os.path.join(output, year)

        if not os.path.exists(outpath):
            os.makedirs(outpath)

        outname = os.path.join(outpath, daily_doy)
        write_map(outname, 'Gtiff', lon, lat, ndvi_daily, sr_wkt, fill_val)
def main():
    paths.build('F:')

    startday = datetime(2013, 12, 17, 0)
    endday = datetime(2013, 12, 31, 0)
    finalyear = 2013

    base_dir = paths.ndvi_individ
    output = paths.ndvi_spline

    year = '2000'
    ref_map = os.path.join(base_dir, year, 'NDVI2000_01_01.tif')
    _, _, _, _, lon, lat, linke, prj, fill_val = read_map(ref_map, 'Gtiff')

    srs = osr.SpatialReference(prj)
    sr_wkt = srs.ExportToWkt()

    for day in rrule.rrule(rrule.DAILY, dtstart=startday, until=endday):
        nr = day.strftime('%j')
        year = day.strftime('%Y')

        # ndvi_daily = time_interpolation(day, lat, lon, finalyear)
        ndvi_daily = time_interpolation(base_dir, day, finalyear)

        # Write daily values to new daily rasters
        daily_doy = 'ndvi{}_{}.tif'.format(year, nr)
        outpath = os.path.join(output, year)

        if not os.path.exists(outpath):
            os.makedirs(outpath)

        outname = os.path.join(outpath, daily_doy)
        write_map(outname, 'Gtiff', lon, lat, ndvi_daily, sr_wkt, fill_val)
示例#3
0
def run():
    paths.build('F:')

    penman_example = 'PM_NM_2000_001.tif'
    map_for_reference = os.path.join(paths.penman, '2000', penman_example)
    _, _, _, _, x, y, _, prj, fill_val = read_map(map_for_reference, 'Gtiff')

    start = datetime.datetime(2000, 1, 1, 0)
    end = datetime.datetime(2001, 12, 31, 0)

    fill_val = -999.

    for day in rrule.rrule(rrule.DAILY, dtstart=start, until=end):
        year = day.strftime('%Y')
        month = day.strftime('%m')
        day_mth = day.strftime('%d')

        # yearstr = str(year)
        # output = os.path.join(output_dir, new_dir, year)
        # print(output_dir)

        ndvi = get_kcb(paths.ndvi_std_all, day)

        # kcb = remake_array(mask_path, kcb)
        ndvi[ndvi == fill_val] = 0
        # print(ndvi, ndvi.shape)

        new_ndvi = 'NDVI{}_{}_{}.tif'.format(year, month, day_mth)

        output_dir = os.path.join(paths.etrm_input_root, 'NDVI_individ',
                                  str(year))
        ndvi_out = os.path.join(output_dir, new_ndvi)

        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)

        print(('Saving New NDVI file as {}'.format(ndvi_out)))
        write_map(ndvi_out, 'Gtiff', x, y, ndvi, prj, fill_val)
示例#4
0
def run():
    paths.build('F:')

    penman_example = 'PM_NM_2000_001.tif'
    map_for_reference = os.path.join(paths.penman, '2000', penman_example)
    _, _, _, _, x, y, _, prj, fill_val = read_map(map_for_reference, 'Gtiff')

    start = datetime.datetime(2000, 1, 1, 0)
    end = datetime.datetime(2001, 12, 31, 0)

    fill_val = -999.

    for day in rrule.rrule(rrule.DAILY, dtstart=start, until=end):
        year = day.strftime('%Y')
        month = day.strftime('%m')
        day_mth = day.strftime('%d')

        # yearstr = str(year)
        # output = os.path.join(output_dir, new_dir, year)
        # print(output_dir)

        ndvi = get_kcb(paths.ndvi_std_all, day)

        # kcb = remake_array(mask_path, kcb)
        ndvi[ndvi == fill_val] = 0
        # print(ndvi, ndvi.shape)

        new_ndvi = 'NDVI{}_{}_{}.tif'.format(year, month, day_mth)

        output_dir = os.path.join(paths.etrm_input_root, 'NDVI_individ', str(year))
        ndvi_out = os.path.join(output_dir, new_ndvi)

        if not os.path.isdir(output_dir):
            os.makedirs(output_dir)

        print('Saving New NDVI file as {}'.format(ndvi_out))
        write_map(ndvi_out, 'Gtiff', x, y, ndvi, prj, fill_val)
示例#5
0
def time_interpolation(base_dir, day, finalyear):

    year = day.year

    #base_dir = 'G:\\Walnut\\Modis\\'
    # output  'F:\\ETRM_Inputs\\NDVI_spline\\'

    cnt = day.timetuple().tm_yday
    i = 0
    first_date = 0
    while first_date == 0:
        # print(i)
        if cnt == NUMS[i]:
            first_date = NUMS[i]
            next_date = NUMS[i + 1]
        elif (cnt < NUMS[i]) and (cnt > NUMS[i - 1]):
            # print('two')
            first_date = NUMS[i - 1]
            next_date = NUMS[i]
        elif (cnt > NUMS[i]) and (cnt < NUMS[i + 1]):
            # print('three')
            # print(NUMS[i + 1])
            first_date = NUMS[i]
            next_date = NUMS[i + 1]
        elif (cnt >= 353) and (year == finalyear):
            first_date = NUMS[NUMSIZE - 1]
            # print('first_date: ', first_date)
            next_date = first_date
            # print('next_date: ', next_date)
        elif cnt >= 353:
            first_date = NUMS[NUMSIZE - 1]
            # print('first_date: ', first_date)
            i = 0
            next_date = NUMS[i]
            # print('next_date: ', next_date)

        i += 1

    print(
        '-----------------------------------------------------------------------------'
    )
    print(('DOY:', cnt))
    # print(year)
    raster_first_date = datetime(year, 1, 1) + timedelta(first_date - 1)
    print(('raster first date: ', raster_first_date))

    td = timedelta(next_date - 1)
    if (cnt >= 353) and (year == finalyear):
        newyear = year
        raster_next_date = datetime(newyear, 1, 1) + td
    elif cnt >= 353:
        newyear = year + 1
        # print(year)
        raster_next_date = datetime(newyear, 1, 1) + td
    elif cnt < 353:
        newyear = year
        raster_next_date = datetime(newyear, 1, 1) + td

    rfd = raster_first_date.timetuple()
    tail = '{}_{:02n}_{:02n}.tif'.format(year, rfd.tm_mon, rfd.tm_mday)

    raster_now = os.path.join(base_dir, '{}'.format(year),
                              'NDVI{}'.format(tail))
    print(('First raster to interpolate: ', raster_now))

    # resX, resY, cols, rows, Lon, Lat, ndvi, prj, FillVal = read_map(os.path.join(base_dir, raster_now), 'Gtiff')
    ndvi = read_map(os.path.join(base_dir, raster_now), 'Gtiff')[6]

    rnd = raster_next_date.timetuple()
    tail2 = '{}_{:02n}_{:02n}.tif'.format(newyear, rnd.tm_mon, rnd.tm_mday)

    raster_next = os.path.join(base_dir, '{}'.format(newyear),
                               'NDVI{}'.format(tail2))
    print(('Future raster to interpolate with: ', raster_next))

    # resX, resY, cols, rows, Lon, Lat, ndvinext, prj, FillVal = read_map(os.path.join(base_dir, raster_next), 'Gtiff')
    ndvinext = read_map(os.path.join(base_dir, raster_next), 'Gtiff')[6]

    # arr1 = ndvi
    # arr2 = ndvinext

    # rejoin Linke, LinkeNext into a single array of shape (2, 2160, 4320)
    arr = r_['0,3', ndvi, ndvinext]
    # print('arr.shape',arr.shape)

    # define the grid coordinates where you want to interpolate
    latitude_index = arange(R)
    longitude_index = arange(C)
    y, x = meshgrid(longitude_index, latitude_index)
    # print('X',X)
    # print(X.shape)
    # print('Y',Y)
    # print(Y.shape)

    # Setup time variables for interpolation
    days_dif = raster_next_date - day
    days_dif = float(days_dif.days)
    max_days_diff = raster_next_date - raster_first_date
    max_days_diff = float(max_days_diff.days)
    # proportion = float(days_dif / max_days_diff)
    # print('proportion',proportion)
    print(('day', day))
    print(('days difference from next ndvi raster', days_dif))
    print(('out of max days difference', max_days_diff))

    # Start of the interpolation part....
    if (cnt >= 353) and (year == finalyear):
        interp = 0.0  # Set to 0 otherwise will divide by zero and give error
    else:
        interp = 1 - (
            days_dif / max_days_diff
        )  # 1 = weight completely next month values, 0 = previous month
    print(('interp ratio between monthly images', interp))

    # 0.5 corresponds to half way between arr1 and arr2
    coordinates = ones((R, C)) * interp, x, y
    # coordones = np.ones((2525, 2272)) * interp
    # print('coordinates',coordinates)
    # print(coordones.shape)

    # given arrays, interpolate at coordinates (could be any subset but in this case using full arrays)
    newarr = ndimage.map_coordinates(arr, coordinates, order=2)

    return newarr
def time_interpolation(base_dir, day, finalyear):

    year = day.year

    #base_dir = 'G:\\Walnut\\Modis\\'
    # output  'F:\\ETRM_Inputs\\NDVI_spline\\'

    cnt = day.timetuple().tm_yday
    i = 0
    first_date = 0
    while first_date == 0:
        # print(i)
        if cnt == NUMS[i]:
            first_date = NUMS[i]
            next_date = NUMS[i + 1]
        elif (cnt < NUMS[i]) and (cnt > NUMS[i - 1]):
            # print('two')
            first_date = NUMS[i - 1]
            next_date = NUMS[i]
        elif (cnt > NUMS[i]) and (cnt < NUMS[i + 1]):
            # print('three')
            # print(NUMS[i + 1])
            first_date = NUMS[i]
            next_date = NUMS[i + 1]
        elif (cnt >= 353) and (year == finalyear):
            first_date = NUMS[NUMSIZE - 1]
            # print('first_date: ', first_date)
            next_date = first_date
            # print('next_date: ', next_date)
        elif cnt >= 353:
            first_date = NUMS[NUMSIZE - 1]
            # print('first_date: ', first_date)
            i = 0
            next_date = NUMS[i]
            # print('next_date: ', next_date)

        i += 1

    print('-----------------------------------------------------------------------------')
    print('DOY:', cnt)
    # print(year)
    raster_first_date = datetime(year, 1, 1) + timedelta(first_date - 1)
    print('raster first date: ', raster_first_date)

    td = timedelta(next_date - 1)
    if (cnt >= 353) and (year == finalyear):
        newyear = year
        raster_next_date = datetime(newyear, 1, 1) + td
    elif cnt >= 353:
        newyear = year + 1
        # print(year)
        raster_next_date = datetime(newyear, 1, 1) + td
    elif cnt < 353:
        newyear = year
        raster_next_date = datetime(newyear, 1, 1) + td

    rfd = raster_first_date.timetuple()
    tail = '{}_{:02n}_{:02n}.tif'.format(year, rfd.tm_mon, rfd.tm_mday)

    raster_now = os.path.join(base_dir, '{}'.format(year), 'NDVI{}'.format(tail))
    print('First raster to interpolate: ', raster_now)

    # resX, resY, cols, rows, Lon, Lat, ndvi, prj, FillVal = read_map(os.path.join(base_dir, raster_now), 'Gtiff')
    ndvi = read_map(os.path.join(base_dir, raster_now), 'Gtiff')[6]

    rnd = raster_next_date.timetuple()
    tail2 = '{}_{:02n}_{:02n}.tif'.format(newyear, rnd.tm_mon, rnd.tm_mday)

    raster_next = os.path.join(base_dir, '{}'.format(newyear), 'NDVI{}'.format(tail2))
    print('Future raster to interpolate with: ', raster_next)

    # resX, resY, cols, rows, Lon, Lat, ndvinext, prj, FillVal = read_map(os.path.join(base_dir, raster_next), 'Gtiff')
    ndvinext = read_map(os.path.join(base_dir, raster_next), 'Gtiff')[6]

    # arr1 = ndvi
    # arr2 = ndvinext

    # rejoin Linke, LinkeNext into a single array of shape (2, 2160, 4320)
    arr = r_['0,3', ndvi, ndvinext]
    # print('arr.shape',arr.shape)

    # define the grid coordinates where you want to interpolate
    latitude_index = arange(R)
    longitude_index = arange(C)
    y, x = meshgrid(longitude_index, latitude_index)
    # print('X',X)
    # print(X.shape)
    # print('Y',Y)
    # print(Y.shape)

    # Setup time variables for interpolation
    days_dif = raster_next_date - day
    days_dif = float(days_dif.days)
    max_days_diff = raster_next_date - raster_first_date
    max_days_diff = float(max_days_diff.days)
    # proportion = float(days_dif / max_days_diff)
    # print('proportion',proportion)
    print('day', day)
    print('days difference from next ndvi raster', days_dif)
    print('out of max days difference', max_days_diff)

    # Start of the interpolation part....
    if (cnt >= 353) and (year == finalyear):
        interp = 0.0  # Set to 0 otherwise will divide by zero and give error
    else:
        interp = 1 - (days_dif / max_days_diff)  # 1 = weight completely next month values, 0 = previous month
    print('interp ratio between monthly images', interp)

    # 0.5 corresponds to half way between arr1 and arr2
    coordinates = ones((R, C)) * interp, x, y
    # coordones = np.ones((2525, 2272)) * interp
    # print('coordinates',coordinates)
    # print(coordones.shape)

    # given arrays, interpolate at coordinates (could be any subset but in this case using full arrays)
    newarr = ndimage.map_coordinates(arr, coordinates, order=2)

    return newarr