コード例 #1
0
def Reclass_LCC_to_LUWA(WaPOR_LCC,
                        Output_dir,
                        ProtectedArea_tif,
                        Reservoir_tif,
                        LCC_LUWA_dict=None):
    if LCC_LUWA_dict is None:
        LCC_LUWA_dict = {
            'PLU': (1, []),
            'ULU': (2, []),
            'MLU': (3, [41, 42]),  #Rainfed crop
            'MWU': (4, [42, 50]),  #irrigated crop and built-up
        }
    driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(WaPOR_LCC)
    LCC = gis.OpenAsArray(WaPOR_LCC, nan_values=True)
    #ULU: The default is ULU
    LUWA = 2 * np.ones(np.shape(LCC), dtype=np.float32)
    #PLU: WDPA
    PLU = gis.OpenAsArray(ProtectedArea_tif, nan_values=True)
    LUWA = np.where(PLU == 1, 1, LUWA)
    #MLU: Rainfed crop => Modified Land Use
    for code in LCC_LUWA_dict['MLU'][1]:
        LUWA = np.where(LCC == code, 3, LUWA)
    #MWU: Irrigated crop, Reservoir, Urban => Managed Water Use
    for code in LCC_LUWA_dict['MWU'][1]:
        LUWA = np.where(LCC == code, 4, LUWA)
    MWU = gis.OpenAsArray(Reservoir_tif, nan_values=True)
    LUWA = np.where(MWU == 1, 4, LUWA)
    output_file = os.path.join(
        Output_dir,
        os.path.basename(WaPOR_LCC).replace('LCC', 'LUWA'))
    gis.CreateGeoTiff(output_file, LUWA, driver, NDV, xsize, ysize, GeoT,
                      Projection)
コード例 #2
0
def Adjust_GRaND_reservoir(output_raster, WaPOR_LCC, GRaND_Reservoir,
                           Resrv_to_Lake, Lake_to_Reserv):

    #Getting GeoTranformation from LCC map
    driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(WaPOR_LCC)
    #Rasterize selected area for reservoir and un-reservoir shapefile
    Basin_reservoir = os.path.join(
        os.path.split(Resrv_to_Lake)[0], 'Reservoir_GRanD.tif')
    Rasterize_shapefile(GRaND_Reservoir, WaPOR_LCC, Basin_reservoir)
    Rasterize_shapefile(Resrv_to_Lake, WaPOR_LCC,
                        Resrv_to_Lake.replace('.shp', '.tif'))
    Rasterize_shapefile(Lake_to_Reserv, WaPOR_LCC,
                        Lake_to_Reserv.replace('.shp', '.tif'))

    #Edit Resvr
    Resrv = gis.OpenAsArray(Basin_reservoir, nan_values=True)
    LCC = gis.OpenAsArray(WaPOR_LCC, nan_values=True)
    UnResrv = gis.OpenAsArray(Resrv_to_Lake.replace('.shp', '.tif'),
                              nan_values=True)
    MakeResrv = gis.OpenAsArray(Lake_to_Reserv.replace('.shp', '.tif'),
                                nan_values=True)

    Resrv = np.where(((LCC == 80) * (MakeResrv == 1)), 1, Resrv)
    Resrv = np.where(((Resrv == 1) * (UnResrv == 1)), np.nan, Resrv)

    #    output=os.path.join(os.path.split(Resrv_to_Lake)[0],'Reservoir_adjusted.tif')
    gis.CreateGeoTiff(output_raster, Resrv, driver, NDV, xsize, ysize, GeoT,
                      Projection)
    return output_raster
コード例 #3
0
ファイル: NPP_dekadal.py プロジェクト: IHEProjects/WaPOR
def main(APIToken='',
         Dir='',
         Startdate='2009-01-01',
         Enddate='2018-12-31',
         latlim=[-40.05, 40.05],
         lonlim=[-30.5, 65.05],
         version=2,
         level=1,
         Waitbar=1):
    """
    This function downloads dekadal WaPOR Net Primary Production data

    Keyword arguments:
    Dir -- 'C:/file/to/path/'
    Startdate -- 'yyyy-mm-dd'
    Enddate -- 'yyyy-mm-dd'
    latlim -- [ymin, ymax] (values must be between -40.05 and 40.05)
    lonlim -- [xmin, xmax] (values must be between -30.05 and 65.05)
    """
    print('WaPOR NPP: Download dekadal WaPOR Net Primary Production data'
          ' for the period %s till %s' % (Startdate, Enddate))
    WaPOR.API.setAPIToken(APIToken)
    checkMemory('Start')

    # Download data
    # WaPOR.API.version = version
    # catalog = WaPOR.API.getCatalog(version, level, True)

    bbox = [lonlim[0], latlim[0], lonlim[1], latlim[1]]

    if level == 1:
        cube_code = 'L1_NPP_D'
    elif level == 2:
        cube_code = 'L2_NPP_D'
    elif level == 3:
        print('WaPOR NPP: Level 3 data only available in some areas'
              ' with specific data cube code below: ')

        catalog = WaPOR.API.getCatalog(version, level, True)
        for i, row in catalog.iterrows():
            if ('L3_NPP' in row['code']) & ('_D' in row['code']):
                print('%s: %s' % (row['caption'], row['code']))
        cube_code = input('Insert Level 3 cube code for the selected area: ')
    else:
        raise Exception('Invalid Level')

    cube_info = WaPOR.API.getCubeInfo(cube_code, version=version, level=level)
    try:
        multiplier = cube_info['measure']['multiplier']
        # unit = cube_info['measure']['unit']
    except BaseException:
        raise Exception('WaPOR NPP ERROR: Cannot get cube info.'
                        ' Check if WaPOR version has cube %s' % (cube_code))
    finally:
        cube_info = None

    time_range = '{0},{1}'.format(Startdate, Enddate)

    df_avail = WaPOR.API.getAvailData(cube_code,
                                      time_range=time_range,
                                      version=version,
                                      level=level)
    # try:
    # except:
    #     print('WaPOR NPP ERROR: cannot get list of available data')
    #     return None

    # if Waitbar == 1:
    #     import watools.Functions.Start.WaitbarConsole as WaitbarConsole
    #     total_amount = len(df_avail)
    #     amount = 0
    #     WaitbarConsole.printWaitBar(
    #         amount, total_amount, prefix='Progress:', suffix='Complete', length=50)

    Dir = os.path.join(Dir, cube_code)
    if not os.path.exists(Dir):
        os.makedirs(Dir)

    for index, row in df_avail.iterrows():
        print('WaPOR NPP: ----- {} -----'.format(index))
        checkMemory('{} AvailData loop start'.format(index))

        # Download raster file name
        download_file = os.path.join(Dir, '{0}.tif'.format(row['raster_id']))
        print('WaPOR NPP: Downloaded file :', download_file)

        # Local raster file name
        filename = 'NPP_WAPOR.v%s_l%s-dekad-1_%s.tif' % (version, level,
                                                         row['raster_id'])
        outfilename = os.path.join(Dir, filename)
        print('WaPOR NPP: Local      file :', outfilename)

        # Downloading raster file
        checkMemory('{} Downloading start'.format(index))
        resp = requests.get(
            WaPOR.API.getCropRasterURL(bbox, cube_code, row['time_code'],
                                       row['raster_id']))
        with open(download_file, 'wb') as fp:
            fp.write(resp.content)
        resp = None
        checkMemory('{} Downloading end'.format(index))

        # GDAL download_file * multiplier => outfilename
        driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
            download_file)

        Array = gis.OpenAsArray(download_file, nan_values=False)
        print('WaPOR NPP: Array         : {t}'.format(t=Array.dtype.name))

        # checkMemory('{} Multiply start'.format(index))
        # print('WaPOR AET: NDV           : {v} {t}'.format(
        #     v=NDV, t=type(NDV)))
        # print('WaPOR AET: multiplier    : {v} {t}'.format(
        #     v=multiplier, t=type(multiplier)))

        NDV = np.float32(NDV)
        multiplier = np.float32(multiplier)
        print('WaPOR NPP: NDV           : {v} {t}'.format(v=NDV,
                                                          t=NDV.dtype.name))
        print('WaPOR NPP: multiplier    : {v} {t}'.format(
            v=multiplier, t=multiplier.dtype.name))

        # Array = np.where(Array < 0, 0, Array)  # mask out flagged value -9998

        NDV = NDV * multiplier
        Array = Array * multiplier
        print('WaPOR NPP: NDV           : {v} {t}'.format(v=NDV,
                                                          t=NDV.dtype.name))
        print('WaPOR NPP: Array         : {t}'.format(t=Array.dtype.name))
        checkMemory('{} Multiply end'.format(index))

        gis.CreateGeoTiff(outfilename, Array, driver, NDV, xsize, ysize, GeoT,
                          Projection)

        Array = None
        checkMemory('{} AvailData loop end'.format(index))

        # Remove downloaded raster file
        try:
            os.remove(download_file)
        except OSError as err:
            # if failed, report it back to the user
            print("WaPOR NPP ERROR: %s - %s." % (err.filename, err.strerror))
コード例 #4
0
ファイル: AET_monthly.py プロジェクト: IHEProjects/WaPOR
def main(APIToken='',
         Dir='',
         Startdate='2009-01-01', Enddate='2018-12-31',
         latlim=[-40.05, 40.05], lonlim=[-30.5, 65.05],
         version=2, level=1, Waitbar=1):
    """
    This function downloads dekadal WaPOR Actual Evapotranspiration data

    Keyword arguments:
    Dir -- 'C:/file/to/path/'
    Startdate -- 'yyyy-mm-dd'
    Enddate -- 'yyyy-mm-dd'
    latlim -- [ymin, ymax] (values must be between -40.05 and 40.05)
    lonlim -- [xmin, xmax] (values must be between -30.05 and 65.05)
    """
    print('WaPOR AET: Download dekadal WaPOR Actual Evapotranspiration data'
          ' for the period %s till %s' % (Startdate, Enddate))
    WaPOR.API.setAPIToken(APIToken)
    checkMemory('Start')

    # Download data
    # WaPOR.API.version = version
    # catalog = WaPOR.API.getCatalog()

    bbox = [lonlim[0], latlim[0], lonlim[1], latlim[1]]

    if level == 1:
        cube_code = 'L1_AETI_M'
    elif level == 2:
        cube_code = 'L2_AETI_M'
    else:
        raise Exception('WaPOR AET ERROR: This module'
                        ' only support level 1 and level 2 data.'
                        ' For higher level, use WaPORAPI module')

    cube_info = WaPOR.API.getCubeInfo(
        cube_code, version=version, level=level)
    try:
        multiplier = cube_info['measure']['multiplier']
        # unit = cube_info['measure']['unit']
    except BaseException:
        raise Exception('WaPOR AET ERROR: Cannot get cube info.'
                        ' Check if WaPOR version has cube %s' % (cube_code))
    finally:
        cube_info = None

    time_range = '{0},{1}'.format(Startdate, Enddate)

    df_avail = WaPOR.API.getAvailData(
        cube_code, time_range=time_range, version=version, level=level)
    # try:
    # except:
    #     print('ERROR: cannot get list of available data')
    #     return None

    # if Waitbar == 1:
    #     import watools.Functions.Start.WaitbarConsole as WaitbarConsole
    #     total_amount = len(df_avail)
    #     amount = 0
    #     WaitbarConsole.printWaitBar(amount, total_amount, prefix='Progress:',
    #                                 suffix='Complete', length=50)

    Dir = os.path.join(Dir, cube_code)
    if not os.path.exists(Dir):
        os.makedirs(Dir)

    for index, row in df_avail.iterrows():
        print('WaPOR AET: ----- {} -----'.format(index))
        checkMemory('{} AvailData loop start'.format(index))

        # Download raster file name
        download_file = os.path.join(Dir, '{0}.tif'.format(row['raster_id']))
        print('WaPOR AET: Downloaded file :', download_file)

        # Local raster file name
        filename = 'AET_WAPOR.v%s_l%s-month-1_%s.%02s.tif' % (
            version, level,
            datetime.strptime(row['MONTH'], '%Y-%m').strftime('%Y'),
            datetime.strptime(row['MONTH'], '%Y-%m').strftime('%m'))
        outfilename = os.path.join(Dir, filename)
        print('WaPOR AET: Local      file :', outfilename)

        # Downloading raster file
        checkMemory('{} Downloading start'.format(index))
        resp = requests.get(WaPOR.API.getCropRasterURL(bbox,
                                                       cube_code,
                                                       row['time_code'],
                                                       row['raster_id']))
        with open(download_file, 'wb') as fp:
            fp.write(resp.content)
        resp = None
        checkMemory('{} Downloading end'.format(index))

        # GDAL download_file * multiplier => outfilename
        driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
            download_file)

        Array = gis.OpenAsArray(download_file, nan_values=True)
        print('WaPOR AET: Array         : {t}'.format(
            t=Array.dtype.name))

        # checkMemory('{} Multiply start'.format(index))
        # print('WaPOR AET: NDV           : {v} {t}'.format(
        #     v=NDV, t=type(NDV)))
        # print('WaPOR AET: multiplier    : {v} {t}'.format(
        #     v=multiplier, t=type(multiplier)))

        NDV = np.float32(NDV)
        multiplier = np.float32(multiplier)
        print('WaPOR AET: NDV           : {v} {t}'.format(
            v=NDV, t=NDV.dtype.name))
        print('WaPOR AET: multiplier    : {v} {t}'.format(
            v=multiplier, t=multiplier.dtype.name))

        NDV = NDV * multiplier
        Array = Array * multiplier
        print('WaPOR AET: NDV           : {v} {t}'.format(
            v=NDV, t=NDV.dtype.name))
        print('WaPOR AET: Array         : {t}'.format(
            t=Array.dtype.name))
        checkMemory('{} Multiply end'.format(index))

        gis.CreateGeoTiff(outfilename, Array,
                          driver, NDV, xsize, ysize, GeoT, Projection)

        Array = None
        checkMemory('{} AvailData loop end'.format(index))

        # Remove downloaded raster file
        try:
            os.remove(download_file)
        except OSError as err:
            # if failed, report it back to the user
            print("WaPOR AET ERROR: %s - %s." % (err.filename, err.strerror))

        # if Waitbar == 1:
        #     amount += 1
        #     WaitbarConsole.printWaitBar(amount, total_amount,
        #                                 prefix='Progress:',
        #                                 suffix='Complete',
        #                                 length=50)
    checkMemory('End')