Skip to content

Commit

Permalink
Create a new version of the jupyter notebook
Browse files Browse the repository at this point in the history
Create a new version of the jupyter notebook based on an example code found in coclico repository.
  • Loading branch information
Mares2022 committed Feb 7, 2024
1 parent 9e54967 commit df53bf7
Showing 1 changed file with 39 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,17 @@
"id": "c91b40e6",
"metadata": {},
"source": [
"# bodemdalingvoorspellingskaarten\n",
"Convert tiff files into cloud optimised geotiff\n",
"# Sea Level Rise AR6\n",
"Notebook environment to migrate netcdf files to CF compliant zarr & CoG. \n",
"Note, this is still quite a mess, TODO: clean up.."
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"id": "f1aef40e",
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 1;\n var nbb_unformatted_code = \"# Optional; code formatter, installed as jupyter lab extension\\n#%load_ext lab_black\\n# Optional; code formatter, installed as jupyter notebook extension\\n%load_ext nb_black\";\n var nbb_formatted_code = \"# Optional; code formatter, installed as jupyter lab extension\\n# %load_ext lab_black\\n# Optional; code formatter, installed as jupyter notebook extension\\n%load_ext nb_black\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"# Optional; code formatter, installed as jupyter lab extension\n",
"#%load_ext lab_black\n",
Expand All @@ -47,21 +35,10 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "77b089ea",
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": "\n setTimeout(function() {\n var nbb_cell_id = 4;\n var nbb_unformatted_code = \"# Import standard packages\\nimport os\\nimport pathlib\\nimport sys\\nimport numpy as np\\nimport geopandas as gpd\\nimport pandas as pd\\nimport matplotlib.pyplot as plt\\nimport xarray as xr\\nimport netCDF4 as nc\\nimport numpy.ma as ma\\nimport rasterio\\nimport rioxarray as rio\\nfrom datacube.utils.cog import write_cog\\n\\n# Import custom functionality\\nfrom coclicodata.drive_config import p_drive\\nfrom coclicodata.etl.cf_compliancy_checker import check_compliancy, save_compliancy\\n\\n# Define (local and) remote drives\\ngca_data_dir = p_drive.joinpath(\\\"11208003-latedeo2022\\\",\\\"020_InternationalDeltaPortfolio\\\",\\\"datasets\\\")\\n\\n# Workaround to the Windows OS (10) udunits error after installation of cfchecker: https://github.com/SciTools/iris/issues/404\\nos.environ[\\\"UDUNITS2_XML_PATH\\\"] = str(\\n home.joinpath( # change to the udunits2.xml file dir in your Python installation\\n r\\\"Anaconda3\\\\pkgs\\\\udunits2-2.2.28-h892ecd3_0\\\\Library\\\\share\\\\udunits\\\\udunits2.xml\\\"\\n )\\n)\";\n var nbb_formatted_code = \"# Import standard packages\\nimport os\\nimport pathlib\\nimport sys\\nimport numpy as np\\nimport geopandas as gpd\\nimport pandas as pd\\nimport matplotlib.pyplot as plt\\nimport xarray as xr\\nimport netCDF4 as nc\\nimport numpy.ma as ma\\nimport rasterio\\nimport rioxarray as rio\\nfrom datacube.utils.cog import write_cog\\n\\n# Import custom functionality\\nfrom coclicodata.drive_config import p_drive\\nfrom coclicodata.etl.cf_compliancy_checker import check_compliancy, save_compliancy\\n\\n# Define (local and) remote drives\\ngca_data_dir = p_drive.joinpath(\\n \\\"11208003-latedeo2022\\\", \\\"020_InternationalDeltaPortfolio\\\", \\\"datasets\\\"\\n)\\n\\n# Workaround to the Windows OS (10) udunits error after installation of cfchecker: https://github.com/SciTools/iris/issues/404\\nos.environ[\\\"UDUNITS2_XML_PATH\\\"] = str(\\n home.joinpath( # change to the udunits2.xml file dir in your Python installation\\n r\\\"Anaconda3\\\\pkgs\\\\udunits2-2.2.28-h892ecd3_0\\\\Library\\\\share\\\\udunits\\\\udunits2.xml\\\"\\n )\\n)\";\n var nbb_cells = Jupyter.notebook.get_cells();\n for (var i = 0; i < nbb_cells.length; ++i) {\n if (nbb_cells[i].input_prompt_number == nbb_cell_id) {\n if (nbb_cells[i].get_text() == nbb_unformatted_code) {\n nbb_cells[i].set_text(nbb_formatted_code);\n }\n break;\n }\n }\n }, 500);\n ",
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"outputs": [],
"source": [
"# Import standard packages\n",
"import os\n",
Expand All @@ -78,39 +55,30 @@
"import rioxarray as rio\n",
"from datacube.utils.cog import write_cog\n",
"\n",
"# Make root directories importable by appending root to path\n",
"cwd = pathlib.Path().resolve()\n",
"sys.path.append(os.path.dirname(cwd))\n",
"\n",
"# Get root paths\n",
"home = pathlib.Path().home()\n",
"root = home.root\n",
"tmp_dir = home.joinpath(\"data\", \"tmp\")\n",
"\n",
"# Import custom functionality\n",
"from coclicodata.drive_config import p_drive\n",
"from coclicodata.etl.cf_compliancy_checker import check_compliancy, save_compliancy\n",
"from etl import p_drive\n",
"from etl.CF_compliancy_checker import check_compliancy, save_compliancy\n",
"\n",
"# Define (local and) remote drives\n",
"gca_data_dir = p_drive.joinpath(\"11208003-latedeo2022\",\"020_InternationalDeltaPortfolio\",\"datasets\")\n",
"coclico_data_dir = p_drive.joinpath(\"11205479-coclico\", \"FASTTRACK_DATA\")\n",
"\n",
"# Workaround to the Windows OS (10) udunits error after installation of cfchecker: https://github.com/SciTools/iris/issues/404\n",
"os.environ[\"UDUNITS2_XML_PATH\"] = str(\n",
" home.joinpath( # change to the udunits2.xml file dir in your Python installation\n",
" r\"Anaconda3\\pkgs\\udunits2-2.2.28-h892ecd3_0\\Library\\share\\udunits\\udunits2.xml\"\n",
" )\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0d2c960",
"metadata": {},
"outputs": [],
"source": [
"# use local or remote data dir\n",
"# Make root directories importable by appending root to path\n",
"#took out of the original code!\n",
"cwd = pathlib.Path().resolve()\n",
"sys.path.append(os.path.dirname(cwd))\n",
"\n",
"# Get root paths\n",
"home = pathlib.Path().home()\n",
"root = home.root\n",
"tmp_dir = home.joinpath(\"data\", \"tmp\")\n",
")\n",
"\n",
"# use local or remote data dir\n",
"use_local_data = False\n",
"ds_dirname = \"17_AR6_SLP_IPCC\"\n",
"\n",
Expand Down Expand Up @@ -693,6 +661,14 @@
"save_compliancy(cap, testfile=cog_dir.joinpath(fname.replace(\".GeoTiff\", \".nc\")), working_dir=CF_dir)"
]
},
{
"cell_type": "markdown",
"id": "1c580844",
"metadata": {},
"source": [
"##### Note, TIFFs are way less flexible in variables and therefore no CF compliancy check is needed. Data will always be an array with band, y, x as dimensions and band, y, x, spatial_ref as coordinates"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -701,8 +677,18 @@
"outputs": [],
"source": [
"data = rio.open_rasterio(outpath, masked=True)\n",
"#data.plot()\n",
"rds.plot()"
"data.plot()\n",
"#rds.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7c29375",
"metadata": {},
"outputs": [],
"source": [
"data"
]
},
{
Expand Down

0 comments on commit df53bf7

Please sign in to comment.