# -*- coding: utf-8 -*- """The table of the GRDC gauges (Burek and Smilovic, 2023) on FlowNest. The crosswalk, plus for each gauge the number of channel pixels upstream, its own area and the pixel centre; next_down is 0 for a gauge with no gauge downstream. Used by: Figures 8, 9 and 12. Input: the global release (paths.GLOBAL) and the GRDC crosswalk (paths.GRDC_XW). Output: ../Data/Data06c_grdc_gauges.csv Usage: python Data06c_grdc_gauge_table.py """ import os, sys, json, hashlib import numpy as np, pandas as pd from affine import Affine HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import paths as PTH from flownest import Release # noqa: E402 RELEASE = PTH.GLOBAL XDIR = PTH.GRDC_XW DATD = os.path.join(os.path.dirname(HERE), 'Data') rel = Release(RELEASE) GT = Affine.from_gdal(*rel.geotransform) meta = json.load(open(os.path.join(RELEASE, 'metadata.json'))) xmeta = json.load(open(os.path.join(XDIR, 'metadata.json'))) assert meta['code_version'] == '10.61' and xmeta['code_version'] == '10.66' assert xmeta['release']['metadata_sha256'] == hashlib.sha256(open(os.path.join(RELEASE, 'metadata.json'), 'rb').read()).hexdigest(), \ 'the crosswalk is made on this FlowNest release' # ---- the gauge table of step 10 X = pd.read_parquet(os.path.join(XDIR, 'flownest_merit90m_crosswalk_grdc_burek2023.parquet')) assert len(X) == 10241 and (X.pixel_class == 'channel_pixel').all() and X.moved_to_neighbouring_pixel.sum() == 9 X = X.sort_values('station_seq_id').reset_index(drop=True) assert (X.station_seq_id.values == np.arange(1, 10242)).all() and (np.diff(X.pixel_seq_id.values) >= 0).all() pix, b = X.pixel_seq_id.values.astype(np.int64), X.basin_id.values.astype(np.int64) P = rel.rows_at('pixel_1km2.parquet', pix, ['row', 'col', 'basin_id', 'upstream_area_km2', 'upstream_channel_pixel_count']) assert (P['basin_id'] == b).all() and (P['row'] == X.row.values).all() and (P['col'] == X['col'].values).all() assert (np.abs(P['upstream_area_km2'] / X.upstream_area_km2.values - 1) < 1e-6).all(), 'step 10 writes the area as float32' cnt = P['upstream_channel_pixel_count'].astype(np.int64) area = P['upstream_area_km2'].astype(np.float64) nd = X.next_down_station_seq_id.values.astype(np.int64) own_number = nd == X.station_seq_id.values nd = np.where(own_number, 0, nd) # no gauge downstream: 0 here, as the figure scripts read it inner = pd.Series(area).groupby(nd).sum().drop(0, errors='ignore') own = area.copy(); own[inner.index.values - 1] -= inner.values lon, lat = GT * (P['col'] + 0.5, P['row'] + 0.5) BT = rel.table('basin.parquet', ['basin_id', 'outlet_pixel_seq_id', 'channel_pixel_count']) assert (BT['basin_id'] == np.arange(1, BT['basin_id'].size + 1)).all() lo, n = BT['outlet_pixel_seq_id'][b - 1], BT['channel_pixel_count'][b - 1] assert (lo <= pix).all() and (pix + cnt <= lo + n).all(), 'the range of a gauge stays in its basin' O = pd.DataFrame(dict(grdc_station_id=X.grdc_station_id, station_seq_id=X.station_seq_id, basin_id=b, basin_station_seq_id=X.basin_station_seq_id, upstream_station_count=X.upstream_station_count, next_down_station_seq_id=nd, pixel_seq_id=pix, upstream_channel_pixel_count=cnt, upstream_area_km2=area, own_area_km2=own, lon=lon, lat=lat)) assert (np.diff(O.pixel_seq_id.values) >= 0).all() and (O.own_area_km2 == 0).sum() == 56, 'the other gauge on a pixel owns nothing' O.to_csv(os.path.join(DATD, 'Data06c_grdc_gauges.csv'), index=False) print('%d gauges in %d basins, %d moved to the neighbouring pixel of their MERIT area, %d with no gauge downstream' % (len(O), O.basin_id.nunique(), X.moved_to_neighbouring_pixel.sum(), own_number.sum()))