Skip to content

Commit 27fcdda

Browse files
authored
Fix/wpop cmd (#509)
* wpop stac search + dep variables * wpop vars from stac * comment pop vars print
1 parent 79d8a08 commit 27fcdda

10 files changed

Lines changed: 520 additions & 45 deletions

File tree

rapida/cli/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from rapida.cli.aclick import RapidaCommandGroup
22
from rapida.cli.population import population
3-
from rapida.util.setup_logger import setup_logger
3+
44
from rapida.cli.admin import admin
55
from rapida.cli.auth import auth
66
from rapida.cli.init import init
@@ -16,10 +16,11 @@
1616
from rapida.cli.connectivity import connectivity
1717
from rich.progress import Progress
1818
import click
19-
import nest_asyncio
20-
nest_asyncio.apply()
19+
# import nest_asyncio
20+
# nest_asyncio.apply()
2121
import uvloop
2222
import asyncio
23+
2324
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
2425

2526

rapida/cli/assess.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def build_variable_help():
147147
if vars_list:
148148
vars_str = ", ".join(vars_list)
149149
parts.append(f"{comp} ({vars_str})")
150+
if comp == 'population': print(len(vars_list))
150151
return "The variable/s to be assessed. Will be filtered by selected components. Available variables per component:\n\n" + "\n\n".join(parts)
151152

152153

@@ -262,7 +263,8 @@ def assess(ctx, all=False, components=None, variables=None, year=None, datetime
262263

263264
component(progress=progress,
264265
variables=variables,
265-
target_year=year,
266+
#target_year=year,
267+
year=year,
266268
datetime_range=datetime_range,
267269
outage_date=outage_date,
268270
cloud_cover=cloud_cover,

rapida/cli/population.py

Lines changed: 91 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import asyncio
21

32
import click
3+
from aenum import no_arg
44

5+
from rapida.cli import RapidaCommandGroup
56
from rapida.components.population import run_download, process_aggregates, population_sync
7+
from rapida.components.population.wpopstac import stac_search
8+
from rapida.components.population import constants as wpopconst
9+
from rapida.util.bbox_param_type import BboxParamType
10+
import tempfile
611

7-
@click.group()
12+
@click.group(cls=RapidaCommandGroup, no_args_is_help=True)
813
def population():
914
"""
1015
Population data management commands.
@@ -15,12 +20,13 @@ def population():
1520
pass
1621

1722

18-
@population.command(no_args_is_help=True, short_help="Synchronize population data from world pop with data stored in our Azure Storage")
23+
@population.command(short_help="Synchronize population data from world pop with data stored in our Azure Storage")
1924
@click.option('--force-reprocessing', help='Force reprocessing of data even if the data specified already exists', is_flag=True)
2025
@click.option('--country', help='The ISO3 code of the country to process the data for')
2126
@click.option('--download-path', help='The local path to save the data to. If not provided, it will automatically save to the provided azure container that was set when initializing with `rapida init`', required=False)
27+
@click.option('--year', help='target year', type=int, default=2020)
2228
@click.option('--all-data', help='Sync all datasets for all countries. It should not be used together with --country flag', is_flag=True, default=False)
23-
def sync(force_reprocessing, country, download_path, all_data):
29+
async def sync(force_reprocessing, country, download_path, year, all_data):
2430
"""
2531
Download population data (sex and age structures) from worldpop,
2632
then do additional processing to convert them to Cloud Optimised GeoTiff and aggregate them for our needs.
@@ -47,10 +53,10 @@ def sync(force_reprocessing, country, download_path, all_data):
4753
"""
4854
if all_data and country:
4955
raise click.UsageError("The --country flag should not be used together with --all-data flag")
50-
asyncio.run(population_sync(force_reprocessing=force_reprocessing, country_code=country, download_path=download_path, all_data=all_data))
56+
await population_sync(force_reprocessing=force_reprocessing, country_code=country, download_path=download_path, all_data=all_data, year=year)
5157

5258

53-
@population.command(no_args_is_help=True, short_help="Download population data (Cloud Optimised GeoTiff format) from UNDP Azure Blob Storage.")
59+
@population.command(short_help="Download population data (Cloud Optimised GeoTiff format) from UNDP Azure Blob Storage.")
5460
@click.option('--country',
5561
help='The ISO3 code of the country to process the data for')
5662
@click.option('--download-path',
@@ -65,7 +71,7 @@ def sync(force_reprocessing, country, download_path, all_data):
6571
help='Download only non aggregated files (original worldpop data). Default is to download only aggregated data.',
6672
is_flag=True,
6773
default=False)
68-
def download(country, download_path, age_group, sex, non_aggregates):
74+
async def download(country, download_path, age_group, sex, non_aggregates):
6975
"""
7076
Download population data (Cloud Optimised GeoTiff format) from UNDP Azure Blob Storage.
7177
@@ -96,15 +102,15 @@ def download(country, download_path, age_group, sex, non_aggregates):
96102
97103
If you would like to download original data for specific conditions, use --sex or --age-group together. `--age-group=total` is not supported for non-aggregate data.
98104
"""
99-
asyncio.run(run_download(country_code=country, download_path=download_path, age_group=age_group, sex=sex, non_aggregates=non_aggregates))
105+
await run_download(country_code=country, download_path=download_path, age_group=age_group, sex=sex, non_aggregates=non_aggregates)
100106

101-
@population.command(no_args_is_help=True, short_help="Download original worldpop data and aggregate them")
107+
@population.command(short_help="Download original worldpop data and aggregate them")
102108
@click.option('--country', help='The ISO3 code of the country to process the data for')
103109
@click.option('--age-group', help='The age group (child, active or elderly) to process the data for', type=click.Choice(['child', 'active', 'elderly']))
104110
@click.option('--sex', help='The sex (male or female) to process the data for', type=click.Choice(['male', 'female']))
105111
@click.option('--download-path', help='The local path to save the data to. If not provided, it will automatically save to the provided azure container that was set when initializing with `rapida init`', required=False)
106112
@click.option('--force-reprocessing', help='Force reprocessing of data even if the data specified already exists', is_flag=True)
107-
def aggregate(country, age_group, sex, download_path, force_reprocessing):
113+
async def aggregate(country, age_group, sex, download_path, force_reprocessing):
108114
"""
109115
Download original worldpop data and aggregate them
110116
@@ -114,4 +120,78 @@ def aggregate(country, age_group, sex, download_path, force_reprocessing):
114120
115121
You can only aggregate for a specific age group or sex by using `--age-group` or `--sex` options.
116122
"""
117-
asyncio.run(process_aggregates(country_code=country, age_group=age_group, sex=sex, download_path=download_path, force_reprocessing=force_reprocessing))
123+
await process_aggregates(country_code=country, age_group=age_group, sex=sex, download_path=download_path, force_reprocessing=force_reprocessing)
124+
125+
126+
127+
128+
129+
@population.command( short_help=f'Search world population data from STAC server')
130+
131+
@click.option('-b', '--bbox',
132+
required=True,
133+
type=BboxParamType(),
134+
help='Bounding box xmin/west, ymin/south, xmax/east, ymax/north'
135+
)
136+
137+
138+
@click.option("--year", "year",
139+
type=int,
140+
required=False,
141+
help='Year for which to search population'
142+
)
143+
@click.option("--sex-category", "sex_category",
144+
type=click.Choice(list(wpopconst.SEX_MAPPING.values()) + ['total'], case_sensitive=False),
145+
required=True,
146+
default='total',
147+
help=f'Aggregate population based on sex category'
148+
)
149+
150+
@click.option( "--age-group", "age_group",
151+
type=click.Choice(list(wpopconst.WORLDPOP_AGE_MAPPING) + ['total'], case_sensitive=False),
152+
required=True,
153+
default='total',
154+
help=f'Sex of the population'
155+
)
156+
157+
# @click.option(
158+
# "--dst-dir",
159+
# "dst_dir", # Function argument name
160+
# type=click.Path(
161+
# exists=False, # Set to True if you want Click to fail if the dir doesn't exist yet
162+
# file_okay=False, # Strictly enforce that this is a directory, not a file
163+
# dir_okay=True,
164+
# resolve_path=True # Resolves relative paths (like '.') to absolute paths automatically
165+
# ),
166+
# default=tempfile.gettempdir(), # Defaults to the current working directory
167+
# show_default=True, # Tells the user what the default is in the --help menu
168+
# help="Destination directory to save the downloaded the images."
169+
# )
170+
171+
172+
173+
174+
# @click.option(
175+
# '--cmask', '-cm', "mask_clouds",
176+
# is_flag=True,
177+
# help=(
178+
# "Enable strict Cloud Masking (ignores pixels with NASA quality flags of 3). "
179+
# "Disable this flag during major storm events to prevent atmospheric noise "
180+
# "from erroneously masking out legitimate blackout signals."
181+
# ),
182+
# default=False
183+
# )
184+
185+
186+
187+
@click.pass_context
188+
def search(ctx, bbox:tuple[float, float, float, float]=None, year:int=None, sex_category:str=None, age_group:str=None):
189+
progress = ctx.obj.get('progress')
190+
with progress:
191+
urls = stac_search(bbox=bbox, year=year, sex_category=sex_category,age_group=age_group)
192+
193+
194+
195+
196+
197+

0 commit comments

Comments
 (0)