|
45 | 45 | } |
46 | 46 | }, |
47 | 47 | "source": [ |
48 | | - "## Gridded Elevations ([atl06p](https://slideruleearth.io/rtd/api_reference/icesat2.html#atl06p))" |
| 48 | + "## Segment Elevations ([atl06p](https://slideruleearth.io/rtd/api_reference/icesat2.html#atl06p))" |
49 | 49 | ] |
50 | 50 | }, |
51 | 51 | { |
|
66 | 66 | "outputs": [], |
67 | 67 | "source": [ |
68 | 68 | "# load the necessary packages\n", |
| 69 | + "from io import BytesIO\n", |
69 | 70 | "from sliderule import icesat2, ipysliderule, io, sliderule\n", |
70 | 71 | "import ipywidgets as widgets\n", |
71 | 72 | "import geopandas\n", |
72 | 73 | "import logging\n", |
73 | 74 | "import warnings\n", |
| 75 | + "import base64\n", |
74 | 76 | "import time\n", |
| 77 | + "import copy\n", |
75 | 78 | "import json\n", |
76 | 79 | "import re\n", |
77 | 80 | "from IPython import display\n", |
78 | 81 | "# atl03 plotting imports\n", |
79 | 82 | "import numpy as np\n", |
80 | | - "import matplotlib.lines\n", |
81 | 83 | "import matplotlib.pyplot as plt\n", |
82 | 84 | "# autoreload\n", |
83 | 85 | "%load_ext autoreload\n", |
|
134 | 136 | "run_output = widgets.Output()\n", |
135 | 137 | "refresh_button = widgets.Button(description=\"Refresh Plot\")\n", |
136 | 138 | "refresh_output = widgets.Output()\n", |
| 139 | + "download_output = widgets.Output()\n", |
| 140 | + "download_atl06_button = widgets.Button(description=\"Download File\")\n", |
| 141 | + "download_atl03_button = widgets.Button(description=\"Download File\")\n", |
| 142 | + "SRwidgets.file_format.options = [\"GeoJSON\",\"csv\",\"geoparquet\"]\n", |
| 143 | + "SRwidgets.file_format.value = 'geoparquet'\n", |
137 | 144 | "show_code06_button = widgets.Button(description=\"Show Code\")\n", |
138 | | - "show_code06_output = widgets.Output()" |
| 145 | + "show_code06_output = widgets.Output()\n" |
139 | 146 | ] |
140 | 147 | }, |
141 | 148 | { |
|
309 | 316 | " if max_plot_points > atl06_rsps.shape[0]:\n", |
310 | 317 | " max_plot_points = atl06_rsps.shape[0]\n", |
311 | 318 | " print(f'Plotting {max_plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n", |
312 | | - " fields = m.default_atl06_fields()\n", |
313 | | - " atl06_rsps.leaflet.GeoData(m.map, column_name=SRwidgets.variable.value,\n", |
314 | | - " cmap=SRwidgets.colormap, max_plot_points=max_plot_points, tooltip=True,\n", |
315 | | - " colorbar=True, fields=fields)\n", |
| 319 | + " fields = atl06_rsps.leaflet.default_atl06_fields()\n", |
| 320 | + " atl06_rsps.leaflet.GeoData(m.map,\n", |
| 321 | + " column_name=SRwidgets.variable.value,\n", |
| 322 | + " cmap=SRwidgets.colormap,\n", |
| 323 | + " max_plot_points=max_plot_points,\n", |
| 324 | + " tooltip=True,\n", |
| 325 | + " colorbar=True,\n", |
| 326 | + " fields=fields\n", |
| 327 | + " )\n", |
316 | 328 | " # install handlers and callbacks\n", |
| 329 | + " atl06_rsps.leaflet.set_observables(SRwidgets)\n", |
317 | 330 | " atl06_rsps.leaflet.add_selected_callback(SRwidgets.atl06_click_handler)\n", |
318 | 331 | " m.add_region_callback(atl06_rsps.leaflet.handle_region)\n", |
319 | 332 | "\n", |
|
330 | 343 | " if max_plot_points > atl06_rsps.shape[0]:\n", |
331 | 344 | " max_plot_points = atl06_rsps.shape[0]\n", |
332 | 345 | " print(f'Plotting {max_plot_points} of {atl06_rsps.shape[0]} elevations. This may take 10-60+ seconds for larger point datasets.')\n", |
333 | | - " fields = m.default_atl06_fields()\n", |
334 | | - " atl06_rsps.leaflet.GeoData(m.map, column_name=SRwidgets.variable.value,\n", |
335 | | - " cmap=SRwidgets.colormap, max_plot_points=max_plot_points, tooltip=True,\n", |
336 | | - " colorbar=True, fields=fields)\n", |
| 346 | + " fields = atl06_rsps.leaflet.default_atl06_fields()\n", |
| 347 | + " atl06_rsps.leaflet.GeoData(m.map,\n", |
| 348 | + " column_name=SRwidgets.variable.value,\n", |
| 349 | + " cmap=SRwidgets.colormap,\n", |
| 350 | + " max_plot_points=max_plot_points,\n", |
| 351 | + " tooltip=True,\n", |
| 352 | + " colorbar=True,\n", |
| 353 | + " fields=fields\n", |
| 354 | + " )\n", |
337 | 355 | " # install handlers and callbacks\n", |
| 356 | + " atl06_rsps.leaflet.set_observables(SRwidgets)\n", |
338 | 357 | " atl06_rsps.leaflet.add_selected_callback(SRwidgets.atl06_click_handler)\n", |
339 | 358 | " m.add_region_callback(atl06_rsps.leaflet.handle_region)\n", |
340 | 359 | "\n", |
|
350 | 369 | " print('parms = ', atl06_json, sep='')\n", |
351 | 370 | " print('gdf = icesat2.atl06p(parms)')\n", |
352 | 371 | "\n", |
| 372 | + "# Download ATL06-SR data as geojson\n", |
| 373 | + "display.display(download_output)\n", |
| 374 | + "#SRwidgets.file_format.value = 'geoparquet'\n", |
| 375 | + "def download_file(gdf, filename, mime_type='text/json'):\n", |
| 376 | + " if (mime_type == 'text/json'):\n", |
| 377 | + " content = base64.b64encode(gdf.to_json().encode()).decode()\n", |
| 378 | + " elif (mime_type == 'text/csv'):\n", |
| 379 | + " content = base64.b64encode(gdf.to_csv().encode()).decode()\n", |
| 380 | + " elif (mime_type == 'application/vnd.apache.parquet'):\n", |
| 381 | + " fid = BytesIO()\n", |
| 382 | + " parms = copy.copy(atl06_parms)\n", |
| 383 | + " version = sliderule.get_version()\n", |
| 384 | + " parms['version'] = version['icesat2']['version']\n", |
| 385 | + " parms['commit'] = version['icesat2']['commit']\n", |
| 386 | + " io.to_parquet(gdf, fid, parameters=parms, regions=m.regions)\n", |
| 387 | + " content = base64.b64encode(fid.getbuffer()).decode()\n", |
| 388 | + " # create download link\n", |
| 389 | + " url = f'data:{mime_type};charset=utf-8;base64,{content}'\n", |
| 390 | + " js = f\"\"\"\n", |
| 391 | + " var a = document.createElement('a');\n", |
| 392 | + " a.setAttribute('download', '{filename}');\n", |
| 393 | + " a.setAttribute('href', '{url}');\n", |
| 394 | + " a.click();\n", |
| 395 | + " \"\"\"\n", |
| 396 | + " with download_output:\n", |
| 397 | + " display.clear_output()\n", |
| 398 | + " display.display(display.HTML(f'<script>{js}</script>'))\n", |
| 399 | + "\n", |
| 400 | + "def on_atl06_download_clicked(e=None):\n", |
| 401 | + " download_file(atl06_rsps, SRwidgets.atl06_filename,\n", |
| 402 | + " mime_type=SRwidgets.mime_type)\n", |
| 403 | + "\n", |
353 | 404 | "# link buttons\n", |
354 | 405 | "run_button.on_click(on_run_clicked)\n", |
355 | 406 | "refresh_button.on_click(on_refresh_clicked)\n", |
356 | | - "show_code06_button.on_click(on_show_code06_clicked)" |
| 407 | + "show_code06_button.on_click(on_show_code06_clicked)\n", |
| 408 | + "download_atl06_button.on_click(on_atl06_download_clicked)" |
357 | 409 | ] |
358 | 410 | }, |
359 | 411 | { |
|
412 | 464 | "]))\n", |
413 | 465 | "\n", |
414 | 466 | "# display buttons\n", |
415 | | - "display.display(run_button)\n", |
416 | | - "display.display(refresh_button, refresh_output)\n", |
417 | | - "display.display(show_code06_button, show_code06_output)" |
| 467 | + "display.display(SRwidgets.HBox([run_button, refresh_button, refresh_output]))\n", |
| 468 | + "display.display(SRwidgets.HBox([download_atl06_button, SRwidgets.file_format]))\n", |
| 469 | + "display.display(SRwidgets.HBox([show_code06_button, show_code06_output]))\n" |
418 | 470 | ] |
419 | 471 | }, |
420 | 472 | { |
|
500 | 552 | " url = url_textbox.value\n", |
501 | 553 | " icesat2.init(url, loglevel=logging.ERROR)\n", |
502 | 554 | "\n", |
503 | | - " # sliderule asset and data release\n", |
504 | | - " asset = SRwidgets.asset.value\n", |
505 | | - "\n", |
506 | 555 | " # build sliderule parameters using latest values from widget\n", |
507 | 556 | " atl03_parms = {\n", |
508 | 557 | " # processing parameters\n", |
|
596 | 645 | " print('parms = ', atl03_json, sep='')\n", |
597 | 646 | " print('gdf = icesat2.atl03sp(parms)')\n", |
598 | 647 | "\n", |
| 648 | + "\n", |
| 649 | + "def on_atl03_download_clicked(e=None):\n", |
| 650 | + " download_file(atl03_rsps, SRwidgets.atl03_filename,\n", |
| 651 | + " mime_type=SRwidgets.mime_type)\n", |
| 652 | + "\n", |
599 | 653 | "# install click handler callback\n", |
600 | | - "show_code03_button.on_click(on_show_code03_clicked)" |
| 654 | + "show_code03_button.on_click(on_show_code03_clicked)\n", |
| 655 | + "download_atl03_button.on_click(on_atl03_download_clicked)" |
601 | 656 | ] |
602 | 657 | }, |
603 | 658 | { |
|
636 | 691 | "display.display(elev_dropdown)\n", |
637 | 692 | "display.display(pc_button)\n", |
638 | 693 | "display.display(pc_output)\n", |
| 694 | + "display.display(SRwidgets.HBox([download_atl03_button, SRwidgets.file_format]))\n", |
639 | 695 | "display.display(show_code03_button, show_code03_output)" |
640 | 696 | ] |
641 | 697 | } |
|
0 commit comments