{ "cells": [ { "cell_type": "markdown", "id": "seasonal-faith", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "# Load Landsat Granule/Scene" ] }, { "cell_type": "code", "execution_count": 1, "outputs": [], "source": [ "import rasterio.warp\n", "import affine\n", "import numpy\n", "import pyproj\n", "import starepandas\n", "import pystare" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "code", "execution_count": 2, "id": "opponent-corpus", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:34.626942Z", "iopub.status.busy": "2022-09-15T05:07:34.626717Z", "iopub.status.idle": "2022-09-15T05:07:35.619297Z", "shell.execute_reply": "2022-09-15T05:07:35.618507Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "fname = '../tests/data/LC08_L2SP_042036_20210729_20210804_02_T1_SR_B1.TIF'\n", "with rasterio.open(fname) as dataset:\n", " data = dataset.read()\n", " mask = dataset.dataset_mask()\n", " crs = dataset.crs\n", "\n", " # Pixel center instead of pixel corner\n", " transform = dataset.transform * affine.Affine.translation(0.5, 0.5)" ] }, { "cell_type": "code", "execution_count": 3, "id": "3612620f-ffa1-4953-8db0-6efc69e6d054", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:35.622867Z", "iopub.status.busy": "2022-09-15T05:07:35.622224Z", "iopub.status.idle": "2022-09-15T05:07:35.625746Z", "shell.execute_reply": "2022-09-15T05:07:35.625292Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "data = data[0, 0:500, 0:500]" ] }, { "cell_type": "code", "execution_count": 4, "id": "usual-grain", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:35.627901Z", "iopub.status.busy": "2022-09-15T05:07:35.627648Z", "iopub.status.idle": "2022-09-15T05:07:35.634892Z", "shell.execute_reply": "2022-09-15T05:07:35.634312Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "indices = numpy.indices(data.shape, dtype='int16')\n", "xy = numpy.array(transform*indices, dtype='int32')" ] }, { "cell_type": "code", "execution_count": 5, "id": "technical-absorption", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:35.639007Z", "iopub.status.busy": "2022-09-15T05:07:35.637597Z", "iopub.status.idle": "2022-09-15T05:07:35.726564Z", "shell.execute_reply": "2022-09-15T05:07:35.725791Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "transformer = pyproj.Transformer.from_crs(crs, \"epsg:4326\")\n", "lat, lon = transformer.transform(xy[0], xy[1])" ] }, { "cell_type": "code", "execution_count": 6, "id": "bibliographic-daily", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:35.730062Z", "iopub.status.busy": "2022-09-15T05:07:35.729598Z", "iopub.status.idle": "2022-09-15T05:07:38.173746Z", "shell.execute_reply": "2022-09-15T05:07:38.172296Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "sids = pystare.from_latlon_2d(lat, lon, adapt_level=True)" ] }, { "cell_type": "code", "execution_count": 7, "id": "beginning-coverage", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:38.178573Z", "iopub.status.busy": "2022-09-15T05:07:38.178257Z", "iopub.status.idle": "2022-09-15T05:07:38.185989Z", "shell.execute_reply": "2022-09-15T05:07:38.185317Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "landsat = starepandas.STAREDataFrame({'lon': lon.flatten(),\n", " 'lat': lat.flatten(),\n", " 'band_1': data.flatten()})" ] }, { "cell_type": "code", "execution_count": 8, "id": "closed-retailer", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:38.188798Z", "iopub.status.busy": "2022-09-15T05:07:38.188472Z", "iopub.status.idle": "2022-09-15T05:07:38.194281Z", "shell.execute_reply": "2022-09-15T05:07:38.193213Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "landsat = starepandas.STAREDataFrame(landsat, sids=sids.flatten())" ] }, { "cell_type": "markdown", "id": "2320bffe-2cfd-469f-bd7f-32193f75a49d", "metadata": { "pycharm": { "name": "#%% md\n" } }, "source": [ "## High-Level" ] }, { "cell_type": "code", "execution_count": 9, "id": "d2d6437a-4cfa-4022-a4f5-e37e51238f9d", "metadata": { "execution": { "iopub.execute_input": "2022-09-15T05:07:38.197425Z", "iopub.status.busy": "2022-09-15T05:07:38.197047Z", "iopub.status.idle": "2022-09-15T05:07:38.200680Z", "shell.execute_reply": "2022-09-15T05:07:38.200071Z" }, "pycharm": { "name": "#%%\n" } }, "outputs": [], "source": [ "# starepandas.read_geotiff(fname, add_xy=True, add_coordinates=True, add_latlon=True, add_sids=False)" ] } ], "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }