Skip to content

Instantly share code, notes, and snippets.

@kissmygritts
Created February 7, 2023 18:34
Show Gist options
  • Save kissmygritts/2275d8eeca65d577752b23d78643a4b5 to your computer and use it in GitHub Desktop.
Save kissmygritts/2275d8eeca65d577752b23d78643a4b5 to your computer and use it in GitHub Desktop.
NDVI data wrangling
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "497bee76-a5d2-4a5b-91c5-e59169359c9e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/Users/mitchellgritts/Documents/projects/vp-airflow/dags\n"
]
}
],
"source": [
"cd ../.."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c8fb1937-5ef5-4702-b205-17b2b84870d4",
"metadata": {},
"outputs": [],
"source": [
"from typing import Dict\n",
"\n",
"import numpy as np\n",
"import rasterio\n",
"from numpy.typing import DTypeLike\n",
"from odc.geo.geobox import GeoBox\n",
"from rasterio import transform\n",
"from rasterio.enums import Compression\n",
"from rasterio.profiles import Profile"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2452cb90-78e9-4bb2-add0-998b9ff57474",
"metadata": {},
"outputs": [],
"source": [
"class DefaultRasterProfile(Profile):\n",
" \"\"\"Default profile to use when writing rasters.\n",
"\n",
" `defaults` variable sets the default parameters for the Profile, see here: https://github.com/rasterio/rasterio/blob/main/rasterio/profiles.py\n",
"\n",
" Example:\n",
" # this will add width, height, and count to the raster profile\n",
" raster_profile = DefaultRasterProfile(widht=100, height=500, count=1)\n",
" \"\"\"\n",
"\n",
" defaults = {\n",
" \"driver\": \"GTiff\",\n",
" \"interleave\": \"pixel\",\n",
" \"tiled\": True,\n",
" \"blockxsize\": 512,\n",
" \"blockysize\": 512,\n",
" \"compress\": Compression.deflate,\n",
" \"zlevel\": 9,\n",
" }\n",
"\n",
"\n",
"DEFAULT_RASTER_PROFILE = DefaultRasterProfile()\n",
"\n",
"\n",
"def apply_default_raster_profile(profile: Profile) -> Profile:\n",
" \"\"\"Apply default raster profile settings to the given profile\n",
"\n",
" `DEFAULT_RASTER_PROFILE` only contains these properties, driver, interleave, tiled, compress,\n",
" blockxsize, blockysize. These properties are merged with the properties of the given profile.\n",
" If the given profile contains these properties they will be overwritten.\n",
"\n",
" Use this instead of `DefaultRasterProfile(widht=100, height=100, ...)` to ensure the default\n",
" setting aren't overwritten.\n",
"\n",
" Args:\n",
" profile (Profile): Profile that will be updated with `DEFAULT_RASTER_PROFILE` settings.\n",
"\n",
" Returns:\n",
" Profile: A new Profile with the `DEFAULT_RASTER_PROFILE` settings applied.\n",
" \"\"\"\n",
" return Profile({**profile, **DEFAULT_RASTER_PROFILE})"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "62ce910c-aa75-4ff8-a215-86e6abbe3c99",
"metadata": {},
"outputs": [],
"source": [
"src_raster = \"/Users/mitchellgritts/Downloads/ndvi-2018-03.tiff\"\n",
"output_raster = \"/Users/mitchellgritts/Downloads/ndvi-2018-03.deflate.tif\"\n",
"nodata = 32767\n",
"\n",
"with rasterio.open(src_raster) as src:\n",
" profile = src.profile.copy()\n",
" src_nodata = src.nodata\n",
" data = src.read(masked=False)\n",
" \n",
" # set nodata value\n",
" data[np.isnan(data)] = nodata\n",
" # make integer (I don't know how you plan to do this with NDVI\n",
" new_data = data * 100\n",
" new_data = new_data.astype(np.int16)\n",
" \n",
" # make output profile\n",
" profile.update({\n",
" \"dtype\": np.int16,\n",
" \"nodata\": nodata\n",
" })\n",
" output_profile = apply_default_raster_profile(profile)\n",
" \n",
" # write output\n",
" with rasterio.open(output_raster, \"w\", **output_profile) as dst:\n",
" dst.write(new_data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa9165e1-28b6-443a-a50a-f1a61e8314b0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment