Skip to content

Commit

Permalink
Work on renaming module
Browse files Browse the repository at this point in the history
  • Loading branch information
aaschwanden committed Nov 30, 2023
1 parent 376c7df commit af31f18
Show file tree
Hide file tree
Showing 9 changed files with 1,641 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ repos:
rev: v1.7.0
hooks:
- id: mypy
exclude: pypism/extract_profile.py

- repo: local
hooks:
Expand All @@ -54,6 +55,7 @@ repos:
"-rn", # Only display messages
"-sn", # Don't display the score
]
exclude: pypism/extract_profile.py

# - repo: https://github.com/srstevenson/nb-clean
# rev: 3.1.0
Expand Down
156 changes: 156 additions & 0 deletions notebooks/test_extract_profile.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "b2105341",
"metadata": {},
"outputs": [],
"source": [
"def create_dummy_input_file(filename, F):\n",
" \"\"\"Create an input file for testing. Does not use unlimited\n",
" dimensions, creates one time record only.\"\"\"\n",
"\n",
" import netCDF4\n",
"\n",
" nc = netCDF4.Dataset(filename, \"w\")\n",
"\n",
" Mx = 88\n",
" My = 152\n",
" Mz = 11\n",
" for name, length in [[\"x\", Mx], [\"y\", My], [\"z\", Mz], [\"time\", 1]]:\n",
" nc.createDimension(name, length)\n",
" nc.createVariable(name, \"f8\", (name,))\n",
"\n",
" # use X and Y ranges corresponding to a grid covering Greenland\n",
" x = np.linspace(-669650.0, 896350.0, Mx)\n",
" y = np.linspace(-3362600.0, -644600.0, My)\n",
" z = np.linspace(0, 4000.0, Mz)\n",
" # the single time record\n",
" time = [0.0]\n",
"\n",
" for name, data in [[\"x\", x], [\"y\", y], [\"z\", z], [\"time\", time]]:\n",
" nc.variables[name][:] = data\n",
"\n",
" nc.proj4 = \"epsg:3413\"\n",
"\n",
" xx, yy = np.meshgrid(x, y)\n",
"\n",
" def write(prefix, dimensions):\n",
" \"Write test data to the file using given storage order.\"\n",
" name = prefix + \"_\".join(dimensions)\n",
"\n",
" slices = {\"x\": slice(0, Mx), \"y\": slice(0, My), \"time\": 0, \"z\": None}\n",
"\n",
" if \"z\" in dimensions:\n",
" # set fill_value and coordinates in variables with the z\n",
" # dimensions and not others (just so that we can get\n",
" # better test coverage)\n",
" variable = nc.createVariable(name, \"f8\", dimensions, fill_value=-2e9)\n",
" variable.coordinates = \"lon lat\"\n",
" else:\n",
" variable = nc.createVariable(name, \"f8\", dimensions)\n",
"\n",
" variable.long_name = name + \" (let's make it long!)\"\n",
"\n",
" # set indexes for all dimensions (z index will be re-set below)\n",
" indexes = [Ellipsis] * len(dimensions)\n",
" for k, d in enumerate(dimensions):\n",
" indexes[k] = slices[d]\n",
"\n",
" # transpose 2D array if needed\n",
" if dimensions.index(\"y\") < dimensions.index(\"x\"):\n",
"\n",
" def T(x):\n",
" return x\n",
"\n",
" else:\n",
" T = np.transpose\n",
"\n",
" if \"z\" in dimensions:\n",
" for k in range(Mz):\n",
" indexes[dimensions.index(\"z\")] = k\n",
" variable[indexes] = T(F(xx, yy, z[k]))\n",
" else:\n",
" variable[indexes] = T(F(xx, yy, 0))\n",
"\n",
" from itertools import permutations\n",
"\n",
" def P(x):\n",
" return list(permutations(x))\n",
"\n",
" for d in sorted(P([\"x\", \"y\"]) + P([\"time\", \"x\", \"y\"])):\n",
" write(\"test_2D_\", d)\n",
"\n",
" for d in sorted(P([\"x\", \"y\", \"z\"]) + P([\"time\", \"x\", \"y\", \"z\"])):\n",
" write(\"test_3D_\", d)\n",
"\n",
" nc.close()\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "a641f41f",
"metadata": {},
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'netCDF4'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[2], line 13\u001b[0m\n\u001b[1;32m 10\u001b[0m fd, filename \u001b[38;5;241m=\u001b[39m tempfile\u001b[38;5;241m.\u001b[39mmkstemp(suffix\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m.nc\u001b[39m\u001b[38;5;124m\"\u001b[39m, prefix\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mextract_profile_test_\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 11\u001b[0m os\u001b[38;5;241m.\u001b[39mclose(fd)\n\u001b[0;32m---> 13\u001b[0m \u001b[43mcreate_dummy_input_file\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfilename\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mF\u001b[49m\u001b[43m)\u001b[49m\n",
"Cell \u001b[0;32mIn[1], line 5\u001b[0m, in \u001b[0;36mcreate_dummy_input_file\u001b[0;34m(filename, F)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcreate_dummy_input_file\u001b[39m(filename, F):\n\u001b[1;32m 2\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Create an input file for testing. Does not use unlimited\u001b[39;00m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;124;03m dimensions, creates one time record only.\"\"\"\u001b[39;00m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mnetCDF4\u001b[39;00m\n\u001b[1;32m 7\u001b[0m nc \u001b[38;5;241m=\u001b[39m netCDF4\u001b[38;5;241m.\u001b[39mDataset(filename, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mw\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 9\u001b[0m Mx \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m88\u001b[39m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'netCDF4'"
]
}
],
"source": [
" def F(x, y, z):\n",
" \"\"\"A function linear in x, y, and z. Used to test our interpolation\n",
" scheme.\"\"\"\n",
" return 10.0 + 0.01 * x + 0.02 * y + 0.03 + 0.04 * z\n",
"\n",
" # create a test file\n",
" import tempfile\n",
" import os\n",
"\n",
" fd, filename = tempfile.mkstemp(suffix=\".nc\", prefix=\"extract_profile_test_\")\n",
" os.close(fd)\n",
"\n",
" create_dummy_input_file(filename, F)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1944771d",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
File renamed without changes.
Loading

0 comments on commit af31f18

Please sign in to comment.