Last active
August 11, 2023 11:56
-
-
Save jgomezdans/4c8b172ee2122fdd0296acfb01fe7fe3 to your computer and use it in GitHub Desktop.
grib_convert
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
# -*- coding: utf-8 -*- | |
import pandas as pd | |
import rioxarray as rio | |
from cfgrib.xarray_to_grib import to_grib | |
def geotiff_to_grib2(input_geotiff, output_grib2, var_name="signal_mask"): | |
ds = rio.open_rasterio(input_geotiff) | |
# Set up a time coordinate. | |
ds.coords["time"] = pd.Timestamp("2023-08-08T14:53:00") | |
# GRIB expects coordinates to be named "longitude" and "latitude | |
ds = ds.rename({"x": "longitude", "y": "latitude"}) | |
ds2 = ds.to_dataset(name=var_name) | |
ds2[var_name].attrs["GRIB_shortName"] = "sgn" | |
ds2[var_name].attrs["gridType"] = "regular_ll" | |
to_grib(ds2, output_grib2) | |
if __name__ == "__main__": | |
input_f = "/home/jose/Downloads/MCD14ML_VNP14ML_2021.tif" | |
output_f = "/tmp/testme.grb2" | |
geotiff_to_grib2(input_f, output_f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment