Skip to content

Commit

Permalink
Merge pull request #414 from iridl/xchourio/oromia-bad-years
Browse files Browse the repository at this point in the history
Xchourio/oromia bad years
  • Loading branch information
xchourio authored Apr 1, 2024
2 parents 7f6ac55 + 3da3515 commit 2349c9e
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import cftime
import datetime
import pandas as pd
import xarray as xr
import sys
import os
import numpy as np
import shutil


def convert_zarr_to_dataset(zarr_path):
# Zarr file exist
if os.path.exists(zarr_path) and os.path.isdir(zarr_path):
try:
ds = xr.open_zarr(zarr_path)
for var_name, variable in ds.data_vars.items():
print(var_name)
print(ds[var_name].values)
print("Zarr file successfully converted to Xarray Dataset.")
return ds
except Exception as e:
print(f"An error occurred while opening the Zarr file: {e}")
else:
print("Zarr directory does not exist.")
return None

dirout='/data/aaron/fbf-candidate/'
regionsdic = {
"ethiopia/southern-oromia": {"file": "/data/aaron/fbf-candidate/original-data/ethiopia/oromia_bad_years.csv",
"time-span":'2000-2023',
"version":'v3'
},
"djibouti": {"file": "/data/aaron/fbf-candidate/original-data/djibouti/bad_years.csv",
"time-span":None,
"version":'v2'
}

}

vars=['rank','hum_bady','nma_bady']

seasons = {"djf":1,
"jfm":2,
"fma":3,
"mam":4,
"amj":5,
"mjj":6,
"jja":7,
"jas":8,
"aso":9,
"son":10,
"ond":11,
"ndj":12}



if len(sys.argv) < 3:
print("\033[91m"+"Two arguments is expected. (season, region)"+"\033[0m")
print("\033[1m"+"Example: python bad-years-v3-3season.py ond djibouti"+"\033[0m")
sys.exit()
elif not sys.argv[1] in seasons:
print("\033[91m"+"The season is not allowed."+"\033[0m")
print("\033[1m"+"Example: "+", ".join(seasons.keys()) +"\033[0m")
sys.exit()
elif not sys.argv[2].lower() in regionsdic:
print("\033[91m"+"The region is not defined."+"\033[0m")
print("\033[1m"+"regions available: "+ (", ".join(regionsdic.keys())) +"\033[0m")
sys.exit()
elif len(sys.argv) ==4:
if not sys.argv[3] in vars:
print("\033[91m"+"The var is not allowed."+"\033[0m")
print("\033[1m"+"Example: "+", ".join(vars) +"\033[0m")
sys.exit()

season = f"{sys.argv[1].lower()}"
region = f"{sys.argv[2]}".lower()
var = f"{sys.argv[3]}"
column_file=season+'-'+var

else:
season = f"{sys.argv[1].lower()}"
region = f"{sys.argv[2]}".lower()
var = ''
column_file=season


bad=pd.read_csv(regionsdic[region]['file'], skiprows=1)
if not regionsdic[region]['time-span'] == None:
year_ini, year_end = map(int, regionsdic[region]['time-span'].split('-'))
for year in range(year_ini, year_end + 1):
if not year in bad['year'].values:
new_row = {bad.columns[0]: year, bad.columns[1]: 8}
new_row_df = pd.DataFrame(new_row, index=[0])
bad=pd.concat([bad, new_row_df], ignore_index=True)
bad = bad.sort_values(by='year').reset_index(drop=True)
del year, new_row, new_row_df,year_ini,year_end

years = [cftime.Datetime360Day(y, seasons[season], 16) for y in bad['year'].tolist()]

if column_file in bad.columns:
ranks = bad[column_file].to_list()
if var=='nma_bady':
ranks=[2 if rank == 'High Impact' else
1 if rank == 'Moderate Impact' else
0 if rank == 'Normal' else
rank for rank in ranks]
else:
print("\033[91m"+"The season "+column_file+" is not available in the file "+regionsdic[region]['file']+"."+"\033[0m")
sys.exit()

if not var =='':
ds = xr.Dataset(data_vars={var: xr.DataArray(ranks, coords={"T": years})})
else:
ds = xr.Dataset(data_vars={"rank": xr.DataArray(ranks, coords={"T": years})})

if "/" in region:
file=os.path.join(dirout,region+'-bad-years-'+regionsdic[region]['version']+'-'+season+'.zarr')
else:
file=os.path.join(dirout,region,'bad-years-'+regionsdic[region]['version']+'-'+season+'.zarr')

ds_aux = convert_zarr_to_dataset(file)
if ds_aux is not None:
# Zarr file exist
if np.array_equal(ds_aux['T'].values, ds['T'].values):
shutil.move(file, file.replace('.zarr','OLD.zarr'))
ds_aux[var] = (('T'), ranks)
ds_aux.to_zarr(file)

else:
print("\033[91m"+"The T coordinate has different sizes."+"\033[0m")
sys.exit()
print(ds_aux)
else:
ds.to_zarr(file)
sys.exit()

82 changes: 78 additions & 4 deletions fbfmaproom/fbfmaproom-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ countries:
predictors:
- pnep
- rain
predictand: bad-years
predictand: bad-years-v2
observations:
rain:
label: Rain
Expand Down Expand Up @@ -1292,6 +1292,17 @@ countries:
colormap: precip
lower_is_worse: no
format: bad
bad-years-v2:
label: Drought humanitarian ranking v2
description: Drought ranking of expert committee humanitarian data review (v2)
path: ethiopia/southern-oromia-bad-years-v2-ond.zarr
var_names:
value: rank
time: T
colormap: precip
lower_is_worse: yes
format: number0
units: rank
forecasts:
pnep:
label: Forecast prob non-exc
Expand Down Expand Up @@ -1877,7 +1888,7 @@ countries:
predictors:
- pnep-v2
- rain
predictand: bad-years
predictand: bad-years-rank-v2
observations:
rain:
label: Rain
Expand Down Expand Up @@ -1934,6 +1945,27 @@ countries:
colormap: precip
lower_is_worse: yes
format: number0
bad-years-rank-v2:
label: Reported bad years v2
description: Adjusted rank from NMA's Bad Year Classification (AA Workshop 2024)
path: djibouti/bad-years-v2-jas.zarr
var_names:
value: rank
time: T
colormap: precip
lower_is_worse: yes
format: number0
units: rank
bad-years-hum_bady-v2:
label: Bad Years for Humanitarian Sector 2024
description: Bad years identified by the Humanitarian Sector (AA Workshop 2024)
path: djibouti/bad-years-v2-jas.zarr
var_names:
value: hum_bady
time: T
colormap: precip
lower_is_worse: no
format: bad
forecasts:
pnep:
label: Forecast prob non-exc
Expand Down Expand Up @@ -2006,7 +2038,7 @@ countries:
predictors:
- pnep-v2
- rain
predictand: bad-years
predictand: bad-years-rank-v2
observations:
rain:
label: Rain
Expand Down Expand Up @@ -2051,6 +2083,27 @@ countries:
colormap: precip
lower_is_worse: yes
format: number0
bad-years-rank-v2:
label: Reported bad years v2
description: Adjusted rank from NMA's Bad Year Classification (AA Workshop 2024)
path: djibouti/bad-years-v2-mam.zarr
var_names:
value: rank
time: T
colormap: precip
lower_is_worse: yes
format: number0
units: rank
bad-years-hum_bady-v2:
label: Bad Years for Humanitarian Sector 2024
description: Bad years identified by the Humanitarian Sector (AA Workshop 2024)
path: djibouti/bad-years-v2-mam.zarr
var_names:
value: hum_bady
time: T
colormap: precip
lower_is_worse: no
format: bad
spi:
label: SPI
description: SPI calculated from CHIRPS
Expand Down Expand Up @@ -2136,7 +2189,7 @@ countries:
predictors:
- pnep-v2
- rain
predictand: bad-years
predictand: bad-years-rank-v2
observations:
rain:
label: Rain
Expand Down Expand Up @@ -2181,6 +2234,27 @@ countries:
colormap: precip
lower_is_worse: yes
format: number0
bad-years-rank-v2:
label: Reported bad years v2
description: Adjusted rank from NMA's Bad Year Classification (AA Workshop 2024)
path: djibouti/bad-years-v2-ond.zarr
var_names:
value: rank
time: T
colormap: precip
lower_is_worse: yes
format: number0
units: rank
bad-years-hum_bady-v2:
label: Bad Years for Humanitarian Sector 2024
description: Bad years identified by the Humanitarian Sector (AA Workshop 2024)
path: djibouti/bad-years-v2-ond.zarr
var_names:
value: hum_bady
time: T
colormap: precip
lower_is_worse: no
format: bad
spi:
label: SPI
description: SPI calculated from CHIRPS
Expand Down

0 comments on commit 2349c9e

Please sign in to comment.