-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
172 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,30 @@ | ||
e9bc4b5da0a989a2dee26bb5f77cab64dd72103c33328d0e6b6497ffc162ba4d harvest/earthstat_global_alfalfa_harvest_ha.tif | ||
984c66e6326b9b66594d04314485adcc622255a13bd71512847b4f1b1e06f61d harvest/earthstat_global_clover_harvest_ha.tif | ||
705abfab0294c2bfca4d2a5e333bf0b7237339b52b52880f7cfb78a21c7c2aa3 harvest/earthstat_global_grassnes_harvest_ha.tif | ||
1b360b651c7f376daa750266ccec0df67306f0692727b3738c35f959bd055b9f harvest/earthstat_global_maizefor_harvest_ha.tif | ||
5c4c5bd48c14f51030ad5cd19aafbe0dddb154d8dc612b85cae5b75e0c69af06 harvest/earthstat_global_triticale_harvest_ha.tif | ||
a73ae9e7af6af3ba06176f65b7af96e3d98f71d4c7f3d04f15ba0c8641d5bf5e production/earthstat_global_alfalfa_production_t.tif | ||
74b2c08865d430eef071beae49c00e5a4f4d6f4a63b0fd8e228dfe09873fd2b3 production/earthstat_global_clover_production_t.tif | ||
1bee00b30181661f8c8a5de5dec9a28983fff3ee973ff8465a46376ba7195f38 production/earthstat_global_grassnes_production_t.tif | ||
7b794071b81f59a939ed156042817ca83863f16fda434577048a075d997489e1 production/earthstat_global_maizefor_production_t.tif | ||
bca39c9f4ef90f65a8416e663181dcaa488ca5007b9b3a3e3587c15f80fc4793 production/earthstat_global_triticale_production_t.tif | ||
cbe1a7805e0497ee844247872940af14e17d75ce6fe2121cd1ea9a8eb21fd18a harvest/earthstat_global_harvest_GrassnessLegumenesMixedgrassRyeforSorghumforSwedeforTurnipforVegforOilseedforFornes_ha.tif | ||
3215dcabdbd9890d5af9421ef8f70602fd0e410cf71680e14faa676a6e53bc3a harvest/earthstat_global_harvest_MaizeforSoybOilseedforCitrusnesRapeseedBeetforWhea_ha.tif | ||
e9bc4b5da0a989a2dee26bb5f77cab64dd72103c33328d0e6b6497ffc162ba4d harvest/earthstat_global_harvest_alfalfa_ha.tif | ||
984c66e6326b9b66594d04314485adcc622255a13bd71512847b4f1b1e06f61d harvest/earthstat_global_harvest_clover_ha.tif | ||
725c8af97d543e7d3a8c0a44c03fc0ab6b61e59d214a2272552ce9f69ea96ea8 harvest/earthstat_global_harvest_fornes_ha.tif | ||
705abfab0294c2bfca4d2a5e333bf0b7237339b52b52880f7cfb78a21c7c2aa3 harvest/earthstat_global_harvest_grassnes_ha.tif | ||
80a4097873281b7a5465b9676879e8676c85ce7118af70bca1e63315458bb055 harvest/earthstat_global_harvest_legumenes_ha.tif | ||
1b360b651c7f376daa750266ccec0df67306f0692727b3738c35f959bd055b9f harvest/earthstat_global_harvest_maizefor_ha.tif | ||
5634cda2060f6e2b221917fc1b306cd5a35ec658729fc8d276a7dcb032b4b2aa harvest/earthstat_global_harvest_mixedgrass_ha.tif | ||
a936cc4a7892f0a99350a6f727d71e548e462ded2843251b66a4552f7597f354 harvest/earthstat_global_harvest_oilseedfor_ha.tif | ||
b00509436d9583f7cf2a608774c2ac6058edb2e9267c279ae9889ab9ffc3fdd7 harvest/earthstat_global_harvest_ryefor_ha.tif | ||
1ff3a7422c074f673b5911b14ced5acb657ea74bf9101290bc19563d9c345776 harvest/earthstat_global_harvest_swedefor_ha.tif | ||
5c4c5bd48c14f51030ad5cd19aafbe0dddb154d8dc612b85cae5b75e0c69af06 harvest/earthstat_global_harvest_triticale_ha.tif | ||
ebb1243f4dee27af2accf3d1cb4dd65080e3bd56d60a742865cbe0474b053555 harvest/earthstat_global_harvest_turnipfor_ha.tif | ||
ca0883a8552533f062bc67f5a5363b7b19b71c92f7961945037360922c0e0407 harvest/earthstat_global_harvest_vegfor_ha.tif | ||
e619e877dff9c1a594ff2877a120461fdb2cf3a54cba930ece38d0f292da177c production/earthstat_global_production_GrassnessLegumenesMixedgrassRyeforSorghumforSwedeforTurnipforVegforOilseedforFornes_t.tif | ||
b7ed03d27e76fce9410fb00a422791a7a0113e2b9ae5c026a7c8ac405dbd7a64 production/earthstat_global_production_MaizeforSoybOilseedforCitrusnesRapeseedBeetforWhea_t.tif | ||
a73ae9e7af6af3ba06176f65b7af96e3d98f71d4c7f3d04f15ba0c8641d5bf5e production/earthstat_global_production_alfalfa_t.tif | ||
74b2c08865d430eef071beae49c00e5a4f4d6f4a63b0fd8e228dfe09873fd2b3 production/earthstat_global_production_clover_t.tif | ||
333fad59cc36eeea714a9b2315d2f422c8247b9389abe0506f51bebfc6c38268 production/earthstat_global_production_fornes_t.tif | ||
1bee00b30181661f8c8a5de5dec9a28983fff3ee973ff8465a46376ba7195f38 production/earthstat_global_production_grassnes_t.tif | ||
5ef4a91ecebea50eea22284995ec4ac1aa0ec4ff32e6d45f1390d6a124030c5c production/earthstat_global_production_legumenes_t.tif | ||
7b794071b81f59a939ed156042817ca83863f16fda434577048a075d997489e1 production/earthstat_global_production_maizefor_t.tif | ||
84ae5ed01a1119f7a3ba4e0529345710e553f2d6529cfd082814624ca32f71f9 production/earthstat_global_production_mixedgrass_t.tif | ||
6ac0d6424b35c905536f7f02d8ac658ec03d7d548ccd5c60800033594c453e04 production/earthstat_global_production_oilseedfor_t.tif | ||
9949e83069e582e543a95e1e6660be1393eefcccaaebc91ea0c422a856626cb9 production/earthstat_global_production_ryefor_t.tif | ||
acfb1a92ca203a3f97dcb002960c3b3cf688c99d126dbee06cbb5e2378330560 production/earthstat_global_production_swedefor_t.tif | ||
bca39c9f4ef90f65a8416e663181dcaa488ca5007b9b3a3e3587c15f80fc4793 production/earthstat_global_production_triticale_t.tif | ||
53ae5bde5a9ed3d6500eea1ea0aef64df9ee0dcbe63a5e69dc5d72860d1be662 production/earthstat_global_production_turnipfor_t.tif | ||
1814503f3433f6fafdcf61b790dd2594cf3c0ab4d13bc1bcf45076c6a4107b45 production/earthstat_global_production_vegfor_t.tif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import logging | ||
from enum import Enum | ||
from pathlib import Path | ||
from time import sleep | ||
from typing import Annotated | ||
|
||
import rasterio as rio | ||
import typer | ||
|
||
logging.basicConfig(level=logging.INFO) | ||
log = logging.getLogger("aggregate") | ||
|
||
GRAS_SILAGE_COMPONENTS = { | ||
"grassness": 1, | ||
"legumenes": 1, | ||
"mixedgrass": 1, | ||
"ryefor": 1, | ||
"sorghumfor": 1, | ||
"swedefor": 1, | ||
"turnipfor": 1, | ||
"vegfor": 1, | ||
"oilseedfor": 1, | ||
"fornes": 1, | ||
} | ||
|
||
# earhstat_25%maizefor_15%soyb_10%oilseedfor_10%citrusnes_10%rapeseed_5%beetfor_5%whea | ||
OTHER_CONCENTRATES_COMPONENTS = { | ||
"maizefor": 0.25, | ||
"soyb": 0.15, | ||
"oilseedfor": 0.1, | ||
"citrusnes": 0.1, | ||
"rapeseed": 0.1, | ||
"beetfor": 0.05, | ||
"whea": 0.05, | ||
} | ||
|
||
|
||
class HarvestOrProd(str, Enum): | ||
harvest = "harvest" | ||
production = "production" | ||
|
||
|
||
def crop(filename: str) -> str: | ||
"""Extract crop name from filename like: | ||
earthstat_global_harvest_oilseedfor_ha.tif -> oilseedfor | ||
""" | ||
return filename.split("_")[3] | ||
|
||
|
||
def aggregate(data_dir: Path, proportions: dict[str, float], harvest_or_prod: HarvestOrProd) -> str: | ||
"""Aggregate rasters that in files that are present in proportions map with the corresponding proportion | ||
into one raster. | ||
""" | ||
files = list(data_dir.glob("*.tif")) | ||
components = [(f, proportions[crop(f.stem)]) for f in files if crop(f.stem) in proportions.keys()] | ||
for i, (file, proportion) in enumerate(components): | ||
if i == 0: | ||
with rio.open(file) as ref: | ||
ref_meta = ref.meta.copy() | ||
data = ref.read(1, masked=True) * proportion | ||
else: | ||
with rio.open(file) as src: | ||
data += src.read(1, masked=True) * proportion | ||
|
||
unit = "ha" if harvest_or_prod == "harvest" else "t" | ||
outfile = ( | ||
data_dir | ||
/ f"earthstat_global_{harvest_or_prod.name}_{''.join(f.title() for f in proportions.keys())}_{unit}.tif" | ||
) | ||
|
||
with rio.open(outfile, "w", **ref_meta) as dest: | ||
dest.write(data, 1) | ||
|
||
return outfile.as_posix() | ||
|
||
|
||
def main( | ||
data_dir: Annotated[Path, typer.Argument], | ||
harvest_or_prod: Annotated[HarvestOrProd, typer.Argument(case_sensitive=False)], | ||
) -> None: | ||
if len(list(data_dir.glob("*.tif"))) == 0: | ||
raise typer.BadParameter(f"Directory {data_dir} does not contain any tif files.") | ||
sleep(10) | ||
gras_silage_filename = aggregate(data_dir, GRAS_SILAGE_COMPONENTS, harvest_or_prod) | ||
others_filename = aggregate(data_dir, OTHER_CONCENTRATES_COMPONENTS, harvest_or_prod) | ||
log.info(f"Created {gras_silage_filename}") | ||
log.info(f"Created {others_filename}") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
for file in data/*.zip; do | ||
filename=`basename $file .zip`; | ||
crop_type="${filename%%_*}" | ||
|
||
unzip -oj $file \ | ||
"${filename}/${crop_type}_Production.tif" \ | ||
"${filename}/${crop_type}_HarvestedAreaHectares.tif" \ | ||
-d data/; | ||
|
||
rm $file; | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.