Esempio n. 1
0
def LCC_to_LUWA(WaPOR_LCC,
                Output_dir,
                ProtectedArea_tif,
                Reservoir_tif,
                LCC_LUWA_dict=None):
    #    WaPOR_LCC,Output_dir,ProtectedArea_tif,Reservoir_tif=_escape_str_paths(WaPOR_LCC,Output_dir,ProtectedArea_tif,Reservoir_tif)
    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)
Esempio n. 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)
    latlim = [GeoT[3] + ysize * GeoT[5], GeoT[3]]
    lonlim = [GeoT[0], GeoT[0] + xsize * GeoT[1]]
    xRes = GeoT[1]
    yRes = -GeoT[5]
    #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, Basin_reservoir, latlim, lonlim, xRes,
                        yRes)
    Rasterize_shapefile(Resrv_to_Lake, Resrv_to_Lake.replace('.shp', '.tif'),
                        latlim, lonlim, xRes, yRes)

    Rasterize_shapefile(Lake_to_Reserv, Lake_to_Reserv.replace('.shp', '.tif'),
                        latlim, lonlim, xRes, yRes)

    #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
Esempio n. 3
0
def Rasterize_shape_basin(shapefile, raster_template, output_raster):

    driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
        raster_template)
    latlim = [GeoT[3] + ysize * GeoT[5], GeoT[3]]
    lonlim = [GeoT[0], GeoT[0] + xsize * GeoT[1]]
    xRes = GeoT[1]
    yRes = -GeoT[5]
    Rasterize_shapefile(shapefile, output_raster, latlim, lonlim, xRes, yRes)
Esempio n. 4
0
def main(Dir,
         data='RET',
         Startdate='2009-01-01',
         Enddate='2018-12-31',
         latlim=[-40.05, 40.05],
         lonlim=[-30.5, 65.05],
         level=1,
         version=2,
         Waitbar=1):
    """
    This function downloads WaPOR daily 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(
        f'\nDownload WaPOR Level {level} dekadal {data} data for the period {Startdate} till {Enddate}'
    )

    # Download data
    WaPOR.API.version = version
    catalog = WaPOR.API.catalog
    bbox = [lonlim[0], latlim[0], lonlim[1], latlim[1]]

    if level == 1:
        cube_code = f"L1_{data}_E"
    else:
        print('Invalid Level')

    try:
        cube_info = WaPOR.API.getCubeInfo(cube_code)
        multiplier = cube_info['measure']['multiplier']
    except:
        print(
            'ERROR: Cannot get cube info. Check if WaPOR version has cube %s' %
            (cube_code))
        return None
    time_range = '{0},{1}'.format(Startdate, Enddate)
    try:
        df_avail = WaPOR.API.getAvailData(cube_code, time_range=time_range)
    except:
        print('ERROR: cannot get list of available data')
        return None
    if Waitbar == 1:
        import WaPOR.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, 'WAPOR.v%s_daily_%s' % (version, cube_code))
    if not os.path.exists(Dir):
        os.makedirs(Dir)

    for index, row in df_avail.iterrows():
        ### get download url
        download_url = WaPOR.API.getCropRasterURL(bbox,
                                                  cube_code,
                                                  row['time_code'],
                                                  row['raster_id'],
                                                  WaPOR.API.Token,
                                                  print_job=False)

        filename = '{0}.tif'.format(row['raster_id'])
        outfilename = os.path.join(Dir, filename)
        download_file = os.path.join(Dir,
                                     'raw_{0}.tif'.format(row['raster_id']))
        ### Download raster file
        resp = requests.get(download_url)
        open(download_file, 'wb').write(resp.content)

        ### correct raster with multiplier
        driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
            download_file)
        Array = gis.OpenAsArray(download_file, nan_values=True)
        Array = np.where(Array < 0, 0, Array)  #mask out flagged value -9998
        CorrectedArray = Array * multiplier
        gis.CreateGeoTiff(outfilename, CorrectedArray, driver, NDV, xsize,
                          ysize, GeoT, Projection)
        os.remove(download_file)

        if Waitbar == 1:
            amount += 1
            WaitbarConsole.printWaitBar(amount,
                                        total_amount,
                                        prefix='Progress:',
                                        suffix='Complete',
                                        length=50)
    return Dir
Esempio n. 5
0
def main(Dir, data='AETI', Startdate='2009-01-01', Enddate='2018-12-31', 
         latlim=[-40.05, 40.05], lonlim=[-30.5, 65.05],level=1, 
         version = 2, Waitbar = 1,cached_catalog=True):
    """
    This function downloads WaPOR dekadal 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)
    cached_catalog -- True  Use a cached catalog. False Load a new catalog from the database
    """
    print(f'\nDownload WaPOR Level {level} dekadal {data} data for the period {Startdate} till {Enddate}')

    # Download data
    WaPOR.API.version=version
    catalog=WaPOR.API.getCatalog(cached=cached_catalog)
    bbox=[lonlim[0],latlim[0],lonlim[1],latlim[1]]
    
    if level==1:
        cube_code=f"L1_{data}_D"
    elif level==2:
        cube_code=f'L2_{data}_D'
    elif level==3:
        print('Level 3 data only available in some areas with specific data cube code below: ')        
        for i,row in catalog.iterrows():            
            if (f'L3' in row['code'])&(f'{data}' in row['code'])&(row['code'][-1]=='D'):
                print('%s: %s'%(row['caption'],row['code']))
        cube_code=input('Insert Level 3 cube code for the selected area: ')
    else:
        print('Invalid Level')
    
    try:
        cube_info=WaPOR.API.getCubeInfo(cube_code)
        multiplier=cube_info['measure']['multiplier']
    except:
        print('ERROR: Cannot get cube info. Check if WaPOR version has cube %s'%(cube_code))
        return None
    time_range='{0},{1}'.format(Startdate,Enddate)
    try:
        df_avail=WaPOR.API.getAvailData(cube_code,time_range=time_range)
    except:
        print('ERROR: cannot get list of available data')
        return None
    if Waitbar == 1:
        import WaPOR.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,'WAPOR.v%s_dekadal_%s' %(version,cube_code))
    if not os.path.exists(Dir):
        os.makedirs(Dir)
        
    for index,row in df_avail.iterrows():  
        ### get download url
        download_url=WaPOR.API.getCropRasterURL(bbox,cube_code,
                                               row['time_code'],
                                               row['raster_id'],
                                               WaPOR.API.Token,
                                               print_job=False)      
              
        filename='{0}.tif'.format(row['raster_id'])
        outfilename=os.path.join(Dir,filename)       
        download_file=os.path.join(Dir,'raw_{0}.tif'.format(row['raster_id']))
        ### Download raster file
        resp=requests.get(download_url)         
        open(download_file,'wb').write(resp.content) 
        
        ### number of days
        timestr=row['time_code']
        startdate=datetime.datetime.strptime(timestr[1:11],'%Y-%m-%d')
        enddate=datetime.datetime.strptime(timestr[12:22],'%Y-%m-%d')
        ndays=(enddate.timestamp()-startdate.timestamp())/86400
        
        ### correct raster with multiplier and number of days in dekad
        driver, NDV, xsize, ysize, GeoT, Projection= gis.GetGeoInfo(download_file)
        Array = gis.OpenAsArray(download_file,nan_values=True)
        Array=np.where(Array<0,0,Array) #mask out flagged value -9998
        if data not in ['LCC','PHE']:
            CorrectedArray=Array*multiplier*ndays
        else:
            CorrectedArray=Array*multiplier
        gis.CreateGeoTiff(outfilename,CorrectedArray,
                          driver, NDV, xsize, ysize, GeoT, Projection)
        os.remove(download_file)        

        if Waitbar == 1:                 
            amount += 1
            WaitbarConsole.printWaitBar(amount, total_amount, 
                                        prefix = 'Progress:', 
                                        suffix = 'Complete', 
                                        length = 50)
    return Dir
Esempio n. 6
0
def main(Dir,
         Startdate='2009-01-01',
         Enddate='2018-12-31',
         latlim=[-40.05, 40.05],
         lonlim=[-30.5, 65.05],
         level=1,
         version=2,
         Waitbar=1):
    """
    This function downloads monthly WaPOR AETI 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(
        '\nDownload monthly WaPOR Actual Evapotranspiration data for the period %s till %s'
        % (Startdate, Enddate))

    # 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:
        print(
            'This module only support level 1 and level 2 data. For higher level, use WaPORAPI module'
        )

    try:
        cube_info = WaPOR.API.getCubeInfo(cube_code)
        multiplier = cube_info['measure']['multiplier']
    except:
        print(
            'ERROR: Cannot get cube info. Check if WaPOR version has cube L1_PCP_M'
        )
        return None
    time_range = '{0},{1}'.format(Startdate, Enddate)
    try:
        df_avail = WaPOR.API.getAvailData(cube_code, time_range=time_range)
    except:
        print('ERROR: cannot get list of available data')
        return None
    if Waitbar == 1:
        import WaPOR.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():
        download_url = WaPOR.API.getCropRasterURL(bbox,
                                                  cube_code,
                                                  row['time_code'],
                                                  row['raster_id'],
                                                  WaPOR.API.Token,
                                                  print_job=False)

        Date = datetime.strptime(row['MONTH'], '%Y-%m')
        filename = 'AET_WAPOR.v2.0_level%s_mm-month-1_monthly_%s.%02s.tif' % (
            level, Date.strftime('%Y'), Date.strftime('%m'))
        outfilename = os.path.join(Dir, filename)
        download_file = os.path.join(Dir, '{0}.tif'.format(row['raster_id']))
        #Download raster file
        resp = requests.get(download_url)
        open(download_file, 'wb').write(resp.content)
        driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
            download_file)
        Array = gis.OpenAsArray(download_file, nan_values=True)
        CorrectedArray = Array * multiplier
        gis.CreateGeoTiff(outfilename, CorrectedArray, driver, NDV, xsize,
                          ysize, GeoT, Projection)
        os.remove(download_file)

        if Waitbar == 1:
            amount += 1
            WaitbarConsole.printWaitBar(amount,
                                        total_amount,
                                        prefix='Progress:',
                                        suffix='Complete',
                                        length=50)
Esempio n. 7
0
def main(Dir,
         data='AETI',
         Startdate='2009-01-01',
         Enddate='2018-12-31',
         latlim=[-40.05, 40.05],
         lonlim=[-30.5, 65.05],
         level=1,
         version=2,
         Waitbar=1):
    """
    This function downloads seasonal WAPOR LCC 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(
        f'\nDownload WaPOR Level {level} yearly {data} data for the period {Startdate} till {Enddate}'
    )

    # Download data
    WaPOR.API.version = version
    bbox = [lonlim[0], latlim[0], lonlim[1], latlim[1]]
    catalog = WaPOR.API.catalog

    if level == 1:
        cube_code = f"L1_{data}_S"
    elif level == 2:
        cube_code = f'L2_{data}_S'
    elif level == 3:
        print(
            'Level 3 data only available in some areas with specific data cube code below: '
        )
        for i, row in catalog.iterrows():
            if (f'L3' in row['code']) & (f'{data}' in row['code']) & (
                    row['code'][-1] == 'S'):
                print('%s: %s' % (row['caption'], row['code']))
        cube_code = input('Insert Level 3 cube code for the selected area: ')
    else:
        print('Invalid Level')

    try:
        cube_info = WaPOR.API.getCubeInfo(cube_code)
        multiplier = cube_info['measure']['multiplier']
    except:
        print(
            'ERROR: Cannot get cube info. Check if WaPOR version has cube %s' %
            (cube_code))
        return None
    time_range = '{0},{1}'.format(Startdate, Enddate)
    try:
        df_avail = WaPOR.API.getAvailData(cube_code, time_range=time_range)
    except:
        print('ERROR: cannot get list of available data')
        return None
    if Waitbar == 1:
        import WaPOR.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, 'WAPOR.v%s_seasonal_%s' % (version, cube_code))
    if not os.path.exists(Dir):
        os.makedirs(Dir)

    for index, row in df_avail.iterrows():
        season_val = {'Season 1': 'S1', 'Season 2': 'S2'}
        if data == 'PHE':
            stage_val = {'End': 'EOS', 'Maximum': 'MOS', 'Start': 'SOS'}
            raster_stage = stage_val[row['STAGE']]
        else:
            raster_stage = None

        download_url = WaPOR.API.getCropRasterURL(
            bbox,
            cube_code,
            row['time_code'],
            row['raster_id'],
            WaPOR.API.Token,
            season=season_val[row['SEASON']],
            stage=raster_stage,
            print_job=True)
        filename = '{0}.tif'.format(row['raster_id'])
        outfilename = os.path.join(Dir, filename)
        download_file = os.path.join(Dir,
                                     'raw_{0}.tif'.format(row['raster_id']))
        print(download_url)
        #Download raster file
        resp = requests.get(download_url)
        open(download_file, 'wb').write(resp.content)
        driver, NDV, xsize, ysize, GeoT, Projection = gis.GetGeoInfo(
            download_file)
        Array = gis.OpenAsArray(download_file, nan_values=True)
        CorrectedArray = Array * multiplier
        gis.CreateGeoTiff(outfilename, CorrectedArray, driver, NDV, xsize,
                          ysize, GeoT, Projection)
        os.remove(download_file)

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