|
17 | 17 | "from glob import glob\n",
|
18 | 18 | "import os\n",
|
19 | 19 | "from matplotlib.colors import ListedColormap, BoundaryNorm\n",
|
20 |
| - "from utils import draw_legend" |
21 |
| - ] |
22 |
| - }, |
23 |
| - { |
24 |
| - "cell_type": "code", |
25 |
| - "execution_count": 2, |
26 |
| - "id": "bb4b746e", |
27 |
| - "metadata": {}, |
28 |
| - "outputs": [], |
29 |
| - "source": [ |
| 20 | + "from utils import draw_legend\n", |
| 21 | + "from config import Config\n", |
30 | 22 | "from rasterio.enums import Resampling"
|
31 | 23 | ]
|
32 | 24 | },
|
|
38 | 30 | "outputs": [],
|
39 | 31 | "source": [
|
40 | 32 | "import warnings\n",
|
41 |
| - "warnings.filterwarnings('ignore')" |
| 33 | + "warnings.filterwarnings('ignore')\n", |
| 34 | + "config = Config()\n", |
| 35 | + "dir_separator = config.dir_sep" |
42 | 36 | ]
|
43 | 37 | },
|
44 | 38 | {
|
|
51 | 45 | "fire_id = \"202\""
|
52 | 46 | ]
|
53 | 47 | },
|
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 13, |
| 51 | + "id": "66357e9e", |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "image_dir = f\"E:/Projects/UCB/FiredPy/data/Fire_events/{fire_id}/Landsat/\"\n", |
| 56 | + "file_address = glob(os.path.join(image_dir, '*_T1'))" |
| 57 | + ] |
| 58 | + }, |
54 | 59 | {
|
55 | 60 | "cell_type": "code",
|
56 | 61 | "execution_count": 5,
|
|
145 | 150 | " xarray.DataArray: The multi-band image with the NIR and SWIR bands.\n",
|
146 | 151 | " \"\"\"\n",
|
147 | 152 | " bands = [] \n",
|
148 |
| - " image_path = image_dir.split(\"\\\\\")[-1]\n", |
| 153 | + " image_path = image_dir.split(dir_separator)[-1]\n", |
149 | 154 | " \n",
|
150 | 155 | " if MASK_IMAGES:\n",
|
151 | 156 | " nir = rioxarray.open_rasterio(f\"{image_dir}/{image_path}_B5.{ext}\").rio.clip(mask_geom, fire_events.crs)\n",
|
|
205 | 210 | " Returns:\n",
|
206 | 211 | " xarray.DataArray: The RGB image with the red, green, and blue bands.\n",
|
207 | 212 | " \"\"\"\n",
|
208 |
| - " image_path = image_dir.split(\"\\\\\")[-1]\n", |
| 213 | + " image_path = image_dir.split(dir_separator)[-1]\n", |
209 | 214 | " \n",
|
210 | 215 | " if MASK_IMAGES:\n",
|
211 | 216 | " rgb = [rioxarray.open_rasterio(f\"{image_dir}/{image_path}_B{x}.{ext}\").rio.clip(mask_geom, fire_events.crs) for x in [4,3,2]]\n",
|
|
249 | 254 | " Returns:\n",
|
250 | 255 | " xarray.DataArray: The false-color image with the shortwave infrared, near-infrared, and red bands.\n",
|
251 | 256 | " \"\"\" \n",
|
252 |
| - " image_path = image_dir.split(\"\\\\\")[-1]\n", |
| 257 | + " image_path = image_dir.split(dir_separator)[-1]\n", |
253 | 258 | " \n",
|
254 | 259 | " if MASK_IMAGES:\n",
|
255 | 260 | " image = [rioxarray.open_rasterio(f\"{image_dir}/{image_path}_B{x}.{ext}\").rio.clip(mask_geom, fire_events.crs) for x in [7,5,4]]\n",
|
|
297 | 302 | " return NBR"
|
298 | 303 | ]
|
299 | 304 | },
|
300 |
| - { |
301 |
| - "cell_type": "code", |
302 |
| - "execution_count": 13, |
303 |
| - "id": "66357e9e", |
304 |
| - "metadata": {}, |
305 |
| - "outputs": [], |
306 |
| - "source": [ |
307 |
| - "image_dir = f\"E:/Projects/UCB/FiredPy/data/Fire_events/{fire_id}/Landsat/\"\n", |
308 |
| - "file_address = glob(os.path.join(image_dir, '*_T1'))" |
309 |
| - ] |
310 |
| - }, |
311 | 305 | {
|
312 | 306 | "cell_type": "code",
|
313 | 307 | "execution_count": 14,
|
|
340 | 334 | ]
|
341 | 335 | },
|
342 | 336 | {
|
| 337 | + "attachments": {}, |
343 | 338 | "cell_type": "markdown",
|
344 | 339 | "id": "b5684035",
|
345 | 340 | "metadata": {},
|
|
542 | 537 | "fig.savefig(f'images/masked/Landsat/classified_dNBR_event_{fire_id}.png', dpi=300, bbox_inches='tight')"
|
543 | 538 | ]
|
544 | 539 | },
|
| 540 | + { |
| 541 | + "attachments": {}, |
| 542 | + "cell_type": "markdown", |
| 543 | + "id": "faa6b451", |
| 544 | + "metadata": {}, |
| 545 | + "source": [ |
| 546 | + "### Save Tiff files" |
| 547 | + ] |
| 548 | + }, |
545 | 549 | {
|
546 | 550 | "cell_type": "code",
|
547 | 551 | "execution_count": 20,
|
548 | 552 | "id": "74677997",
|
549 | 553 | "metadata": {},
|
550 | 554 | "outputs": [],
|
551 | 555 | "source": [
|
| 556 | + "# save pre and postfire NBRs as tiff files\n", |
552 | 557 | "for image_dir, label in zip(file_address, labels):\n",
|
553 |
| - " f_name = os.path.join(image_dir.split(\"\\\\\")[0], f'NBR_{label}.tif')\n", |
| 558 | + " f_name = os.path.join(image_dir.split(dir_separator)[0], f'NBR_{label}.tif')\n", |
554 | 559 | " nbr_raws[label].rio.to_raster(f_name)"
|
555 | 560 | ]
|
556 | 561 | },
|
|
561 | 566 | "metadata": {},
|
562 | 567 | "outputs": [],
|
563 | 568 | "source": [
|
564 |
| - "f_name_diff = os.path.join(image_dir.split(\"\\\\\")[0], f'dNBR pre-post fire.tif')\n", |
| 569 | + "# save dNBR as tiff file\n", |
| 570 | + "f_name_diff = os.path.join(image_dir.split(dir_separator)[0], f'dNBR pre-post fire.tif')\n", |
565 | 571 | "dNBR_post_raw.rio.to_raster(f_name_diff)"
|
566 | 572 | ]
|
567 | 573 | },
|
|
582 | 588 | "metadata": {},
|
583 | 589 | "outputs": [],
|
584 | 590 | "source": [
|
| 591 | + "# Save pre and postfire RGB images as tiff files\n", |
585 | 592 | "rgb_images = dict.fromkeys(labels, None)\n",
|
586 | 593 | "for image_dir, label in zip(file_address, labels):\n",
|
587 | 594 | " rgb = open_RGB(image_dir, \"TIF\", False, False)\n",
|
588 | 595 | " rgb_images[label] = rgb\n",
|
589 |
| - " f_name = os.path.join(image_dir.split(\"\\\\\")[0], f'RGB_{label}.tif')\n", |
| 596 | + " f_name = os.path.join(image_dir.split(dir_separator)[0], f'RGB_{label}.tif')\n", |
590 | 597 | " rgb_images[label].rio.to_raster(f_name)"
|
591 | 598 | ]
|
592 | 599 | },
|
|
597 | 604 | "metadata": {},
|
598 | 605 | "outputs": [],
|
599 | 606 | "source": [
|
| 607 | + "# Save pre and postfire false color composite image as tiff\n", |
600 | 608 | "fcc_images = dict.fromkeys(labels, None)\n",
|
601 | 609 | "\n",
|
602 | 610 | "for image_dir, label in zip(file_address, labels):\n",
|
603 | 611 | " rgb = open_false_color(image_dir, \"TIF\", False, False)\n",
|
604 | 612 | " fcc_images[label] = rgb\n",
|
605 |
| - " f_name = os.path.join(image_dir.split(\"\\\\\")[0], f'false_color_{label}.tif')\n", |
| 613 | + " f_name = os.path.join(image_dir.split(dir_separator)[0], f'false_color_{label}.tif')\n", |
606 | 614 | " fcc_images[label].rio.to_raster(f_name)"
|
607 | 615 | ]
|
608 | 616 | },
|
609 |
| - { |
610 |
| - "cell_type": "code", |
611 |
| - "execution_count": 17, |
612 |
| - "id": "89b08c77", |
613 |
| - "metadata": {}, |
614 |
| - "outputs": [ |
615 |
| - { |
616 |
| - "data": { |
617 |
| - "text/plain": [ |
618 |
| - "((23223, 16350), (23193, 16590))" |
619 |
| - ] |
620 |
| - }, |
621 |
| - "execution_count": 17, |
622 |
| - "metadata": {}, |
623 |
| - "output_type": "execute_result" |
624 |
| - } |
625 |
| - ], |
626 |
| - "source": [ |
627 |
| - "nir_pre.shape, nir_post.shape" |
628 |
| - ] |
629 |
| - }, |
630 |
| - { |
631 |
| - "cell_type": "code", |
632 |
| - "execution_count": 18, |
633 |
| - "id": "2fa3701c", |
634 |
| - "metadata": {}, |
635 |
| - "outputs": [ |
636 |
| - { |
637 |
| - "data": { |
638 |
| - "text/plain": [ |
639 |
| - "((23223, 16350), (23193, 16590))" |
640 |
| - ] |
641 |
| - }, |
642 |
| - "execution_count": 18, |
643 |
| - "metadata": {}, |
644 |
| - "output_type": "execute_result" |
645 |
| - } |
646 |
| - ], |
647 |
| - "source": [ |
648 |
| - "swir_pre.shape, swir_post.shape" |
649 |
| - ] |
650 |
| - }, |
651 | 617 | {
|
652 | 618 | "cell_type": "code",
|
653 | 619 | "execution_count": 15,
|
|
716 | 682 | "# fig.savefig(f'images/masked/Landsat/NIR_and_SWIR_difference_event_{fire_id}.png', dpi=300)"
|
717 | 683 | ]
|
718 | 684 | },
|
719 |
| - { |
720 |
| - "cell_type": "code", |
721 |
| - "execution_count": null, |
722 |
| - "id": "769be370", |
723 |
| - "metadata": {}, |
724 |
| - "outputs": [], |
725 |
| - "source": [] |
726 |
| - }, |
727 |
| - { |
728 |
| - "cell_type": "code", |
729 |
| - "execution_count": null, |
730 |
| - "id": "230c2f51", |
731 |
| - "metadata": {}, |
732 |
| - "outputs": [], |
733 |
| - "source": [] |
734 |
| - }, |
735 | 685 | {
|
736 | 686 | "cell_type": "code",
|
737 | 687 | "execution_count": 20,
|
|
0 commit comments