|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "b959fb73-7ccf-4fa0-a5b7-d23b26a3e634", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Sphere Mesh\n", |
| 9 | + "\n", |
| 10 | + "Generate a spherical mesh for use in plotting 3D globe data\n", |
| 11 | + "\n", |
| 12 | + "Uses specified radius, add an offset to plot data just about topography etc" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": null, |
| 18 | + "id": "c086c0ed-e736-4fb5-917f-312507f6ef60", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "#Earth's radius ~6371km - we'll use 1000's of km units (Mm)\n", |
| 23 | + "radius = 6371 * 1e-3 #Radius in Mm\n", |
| 24 | + "\n", |
| 25 | + "#Radius + small offset for stratosphere\n", |
| 26 | + "s_radius = radius * 1.016\n", |
| 27 | + "\n", |
| 28 | + "#Sphere mesh quality (higher = more triangles)\n", |
| 29 | + "QUALITY=256" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": null, |
| 35 | + "id": "e63255a8-e12b-4316-b18d-85bdf46940b3", |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "import lavavu\n", |
| 40 | + "lv = lavavu.Viewer(border=False, axis=False, resolution=[1280,720], background='black')" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": null, |
| 46 | + "id": "e695a61a-4197-4abd-bd91-f78e7d281559", |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "tris0 = lv.spheres(\"sphere\", scaling=s_radius, segments=QUALITY, colour=\"grey\", vertices=[0,0,0], fliptexture=False)\n", |
| 51 | + "tris0['rotate'] = [0,-90,0] #This rotates the sphere coords to align with [0,360] longitude texture\n", |
| 52 | + "tris0['texture'] = 'blank.png' #Need an initial texture or texcoords will not be generated\n", |
| 53 | + "tris0['renderer'] = 'sortedtriangles'\n", |
| 54 | + "lv.render()\n", |
| 55 | + "\n", |
| 56 | + "#Generate and extract sphere vertices, texcoords etc\n", |
| 57 | + "lv.bake(7)\n", |
| 58 | + "#lv['cullface'] = False #Must disable this for the ozone plot\n", |
| 59 | + "#tris0[\"rotate\"] = [0,0,0]\n", |
| 60 | + "#tris0[\"alpha\"] = 0.6\n", |
| 61 | + "lv.display((400,400))" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "id": "f39d7030-cc23-4783-8e9b-47cc5a77c794", |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [], |
| 70 | + "source": [ |
| 71 | + "tris0.data[0]\n", |
| 72 | + "#DrawData(\"triangles\") ==> {'vertices': (65792, 3), 'normals': (65792, 3), 'indices': (196608,), 'texcoords': (65792, 2)}" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "id": "e52c2419-e3e6-4349-be13-0f84cba06339", |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "sdata = {}\n", |
| 83 | + "element = tris0.data[0]\n", |
| 84 | + "keys = element.available.keys()\n", |
| 85 | + "for k in keys:\n", |
| 86 | + " print(k)\n", |
| 87 | + " sdata[k] = tris0.data[k][0]\n", |
| 88 | + "print(sdata)" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "code", |
| 93 | + "execution_count": null, |
| 94 | + "id": "85d8ff63-7e99-4dad-947c-cad318efa25f", |
| 95 | + "metadata": {}, |
| 96 | + "outputs": [], |
| 97 | + "source": [ |
| 98 | + "#Save compressed vertex data\n", |
| 99 | + "import numpy as np\n", |
| 100 | + "np.savez_compressed(f'Sphere_{s_radius:.4f}', **sdata)" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "id": "718b2386-1daf-4865-bde9-1b57fa25570e", |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [], |
| 109 | + "source": [ |
| 110 | + "!ls -ltrh *.npz" |
| 111 | + ] |
| 112 | + }, |
| 113 | + { |
| 114 | + "cell_type": "code", |
| 115 | + "execution_count": null, |
| 116 | + "id": "64718aa0-da17-424f-b579-a98d1588d378", |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [] |
| 120 | + } |
| 121 | + ], |
| 122 | + "metadata": { |
| 123 | + "kernelspec": { |
| 124 | + "display_name": "Python 3 (ipykernel)", |
| 125 | + "language": "python", |
| 126 | + "name": "python3" |
| 127 | + }, |
| 128 | + "language_info": { |
| 129 | + "codemirror_mode": { |
| 130 | + "name": "ipython", |
| 131 | + "version": 3 |
| 132 | + }, |
| 133 | + "file_extension": ".py", |
| 134 | + "mimetype": "text/x-python", |
| 135 | + "name": "python", |
| 136 | + "nbconvert_exporter": "python", |
| 137 | + "pygments_lexer": "ipython3", |
| 138 | + "version": "3.10.12" |
| 139 | + } |
| 140 | + }, |
| 141 | + "nbformat": 4, |
| 142 | + "nbformat_minor": 5 |
| 143 | +} |
0 commit comments