Created
August 25, 2023 18:23
-
-
Save kissmygritts/574686eba99b6babb0185eccd5ef1f17 to your computer and use it in GitHub Desktop.
example of masked and unmasked arrays and how they work with nodata values
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"/Users/mitchellgritts/Documents/projects/vp-airflow/dags\n" | |
] | |
} | |
], | |
"source": [ | |
"cd ../.." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import rasterio\n", | |
"from rasterio import transform, CRS\n", | |
"from rasterio.plot import show\n", | |
"import numpy as np\n", | |
"import operator\n", | |
"import random\n", | |
"from rasterio import CRS\n", | |
"\n", | |
"def make_raster(filename, profile, data):\n", | |
" default_profile = {\n", | |
" \"count\": 1,\n", | |
" \"compress\": \"lzw\",\n", | |
" \"crs\": CRS.from_epsg(3310),\n", | |
" \"driver\": \"GTiff\",\n", | |
" \"dtype\": data.dtype,\n", | |
" \"height\": data.shape[0],\n", | |
" \"interleave\": \"band\",\n", | |
" \"nodata\": 0.0,\n", | |
" \"tiled\": False,\n", | |
" \"transform\": transform.Affine.translation(0, 5)\n", | |
" * transform.Affine.scale(-5, 5),\n", | |
" \"width\": data.shape[1],\n", | |
" } | profile\n", | |
"\n", | |
" with rasterio.open(filename, 'w', **default_profile) as dataset:\n", | |
" dataset.write(data)\n", | |
"\n", | |
" return filename" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"zero_nodata_filename = make_raster(\n", | |
" filename=\"/Users/mitchellgritts/Downloads/multiband_raster.tif\",\n", | |
" profile={\n", | |
" \"count\": 3,\n", | |
" \"nodata\": 999,\n", | |
" },\n", | |
" data=np.array(\n", | |
" [\n", | |
" [\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" ],\n", | |
" [\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" ],\n", | |
" [\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" ],\n", | |
" ],\n", | |
" dtype=np.int16,\n", | |
" ),\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"with rasterio.open(zero_nodata_filename) as src:\n", | |
" array_unmasked = src.read()\n", | |
" array_masked = src.read(masked=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"masked_array(\n", | |
" data=[[[--, --, 1, 1],\n", | |
" [--, --, 1, 1],\n", | |
" [--, --, 1, 1]],\n", | |
"\n", | |
" [[--, --, 1, 1],\n", | |
" [--, --, 1, 1],\n", | |
" [--, --, 1, 1]],\n", | |
"\n", | |
" [[--, --, 1, 1],\n", | |
" [--, --, 1, 1],\n", | |
" [--, --, 1, 1]]],\n", | |
" mask=[[[ True, True, False, False],\n", | |
" [ True, True, False, False],\n", | |
" [ True, True, False, False]],\n", | |
"\n", | |
" [[ True, True, False, False],\n", | |
" [ True, True, False, False],\n", | |
" [ True, True, False, False]],\n", | |
"\n", | |
" [[ True, True, False, False],\n", | |
" [ True, True, False, False],\n", | |
" [ True, True, False, False]]],\n", | |
" fill_value=999,\n", | |
" dtype=int16)" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array_masked" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[[999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1]],\n", | |
"\n", | |
" [[999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1]],\n", | |
"\n", | |
" [[999, 999, 1, 1],\n", | |
" [999, 999, 1, 1],\n", | |
" [999, 999, 1, 1]]], dtype=int16)" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"array_unmasked" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[2997, 2997, 3, 3],\n", | |
" [2997, 2997, 3, 3],\n", | |
" [2997, 2997, 3, 3]])" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum_unmasked = np.sum(array_unmasked, axis=0)\n", | |
"sum_unmasked" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"masked_array(\n", | |
" data=[[--, --, 3, 3],\n", | |
" [--, --, 3, 3],\n", | |
" [--, --, 3, 3]],\n", | |
" mask=[[ True, True, False, False],\n", | |
" [ True, True, False, False],\n", | |
" [ True, True, False, False]],\n", | |
" fill_value=999999)" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sum_masked = np.sum(array_masked, axis=0)\n", | |
"sum_masked" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([[3., 3., 3., 3.],\n", | |
" [3., 3., 3., 3.],\n", | |
" [3., 3., 3., 3.]])" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"compare_array = np.ones(sum_unmasked.shape) * 3\n", | |
"compare_array" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[False False True True]\n", | |
" [False False True True]\n", | |
" [False False True True]]\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.isclose(compare_array, sum_unmasked))\n", | |
"print(np.isclose(compare_array, sum_unmasked).all())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[-- -- True True]\n", | |
" [-- -- True True]\n", | |
" [-- -- True True]]\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"print(np.isclose(compare_array, sum_masked))\n", | |
"print(np.isclose(compare_array, sum_masked).all())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "vp-notebooks", | |
"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.10" | |
}, | |
"orig_nbformat": 4 | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment