Skip to content

Instantly share code, notes, and snippets.

@g4brielvs
Last active July 23, 2024 18:07
Show Gist options
  • Save g4brielvs/80859c21ca0017e4b2e417b4665c58c0 to your computer and use it in GitHub Desktop.
Save g4brielvs/80859c21ca0017e4b2e417b4665c58c0 to your computer and use it in GitHub Desktop.
BlackMarblePy Example
import pandas as pd
from blackmarble.extract import bm_extract
from blackmarble.raster import bm_raster
# Set NASA EarthData Token (envvar or alternative recommended)
bearer = "<NASA-EARTHDATA-TOKEN>"
# Retrieve GADM polygon of Lebanon
gdf = geopandas.read_file(
"https://geodata.ucdavis.edu/gadm/gadm4.1/json/gadm41_GHA_1.json.zip"
)
# Create NTL raster
ntl_r = bm_raster(
gdf,
product_id="VNP46A4",
date_range=pd.date_range("2019-01-01", "2022-01-01", freq="YS"),
bearer=bearer
)
# Create NTL dataset from 2012 to 2022
ntl_df = bm_extract(
gdf,
product_id="VNP46A4",
date_range=pd.date_range("2012-01-01", "2022-01-01", freq="YS"),
bearer=bearer,
aggfunc = ["sum"],
)
@Abdulazizbek
Copy link

Abdulazizbek commented Feb 8, 2024

How can I save a downloaded file (geotif) as an image (ex: in tif or some other format).
For example:
I want to save this range of data as an image file: date_range = pd.date_range("2022-01-01", "2022-03-31", freq="D")

Thank you beforehand

@g4brielvs
Copy link
Author

g4brielvs commented Feb 8, 2024

@Abdulazizbek Thank you for reaching out! When invoking bm_raster, the result will be a xrray.Dataset, so you may use leverage its plotting features.

You may try:

r["Gap_Filled_DNB_BRDF-Corrected_NTL"].sel(time="2022-01-01").plot.pcolormesh(
    robust=True,
)

Alternatively, you may also use rioxarray to export a raster.

r["Gap_Filled_DNB_BRDF-Corrected_NTL"].sel(time="2021-02-05").rio.to_raster('output.tif')

Finally, please see also this example on the BlackMarblePy's documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment