4 "cell_type": "markdown",
9 "Upgrading numpy first. pygrib requires up to date numpy while colab has an old version of numpy, perhaps for compatibility with tensorflow. If the upgrade was needed, the runtime kills itself and restarts, then you need to run the notebook again. "
19 "timestamp": 1633285232385,
21 "displayName": "WRF Fire",
22 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
23 "userId": "09833348162306222387"
31 "import numpy as np, os\n",
32 "if not [int(i) for i in np.__version__.split('.')] >= [1,20,1]: # check numpy version\n",
33 " print('Upgrading numpy and stopping RUNTIME! When the notebook completes, please run again.')\n",
34 " ! pip install --upgrade numpy # suggested by Efosa, see also https://github.com/jswhit/pygrib/issues/192\n",
35 " os.kill(os.getpid(), 9) # kill the runtime, need to run again from the beginning"
39 "cell_type": "markdown",
44 "Installing dependences:"
52 "base_uri": "https://localhost:8080/"
57 "timestamp": 1633285235583,
59 "displayName": "WRF Fire",
60 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
61 "userId": "09833348162306222387"
66 "outputId": "2eb14e59-9297-4dc1-c328-4d3527297eb6"
71 "output_type": "stream",
73 "Requirement already satisfied: pygrib in /Users/jmandel/opt/anaconda3/lib/python3.8/site-packages (2.1.4)\n",
74 "Requirement already satisfied: pyproj in /Users/jmandel/opt/anaconda3/lib/python3.8/site-packages (from pygrib) (3.2.1)\n",
75 "Requirement already satisfied: numpy in /Users/jmandel/opt/anaconda3/lib/python3.8/site-packages (from pygrib) (1.21.2)\n",
76 "Requirement already satisfied: certifi in /Users/jmandel/opt/anaconda3/lib/python3.8/site-packages (from pyproj->pygrib) (2020.6.20)\n",
77 "File ‘grib_file.py’ already there; not retrieving.\n",
83 "! pip install pygrib \n",
84 "! wget --no-clobber https://raw.githubusercontent.com/openwfm/wrfxpy/master/src/ingest/grib_file.py\n",
85 "from grib_file import GribFile # Martin's utility layer on top of pygrib,from wrfxpy\n",
86 "import numpy as np, os # imported before, just in case\n"
90 "cell_type": "markdown",
95 "Create a function to transfer RTMA file in GRIP2 format from the stash. The function returns zero if the file transfer succeeded. If the file is not available, it returns a nonzero value. Note: if needed, maybe in future add more sophisticated checks, check the return code of wget and if the file size is correct."
100 "execution_count": 3,
105 "timestamp": 1633285235584,
107 "displayName": "WRF Fire",
108 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
109 "userId": "09833348162306222387"
117 "import subprocess,os\n",
118 "def load_rtma(path,file):\n",
119 " url='http://math.ucdenver.edu/~jmandel/rtma/' + path\n",
120 " if os.path.exists(file):\n",
121 " print(file + ' already exists, removing')\n",
122 " os.remove(file)\n",
124 " ret = subprocess.check_output(['wget','--no-clobber','--output-document='+ file, url,],stderr=subprocess.STDOUT).decode() # execute command from python strings\n",
125 " if os.path.exists(file):\n",
126 " print('loaded ' + url + ' as ' + file)\n",
129 " print('file transfer completed, but the file is missing? ' + url) \n",
132 " print('file transfer failed: ' + url)\n",
137 "cell_type": "markdown",
142 "Testing the file transfer:"
147 "execution_count": 4,
150 "base_uri": "https://localhost:8080/"
155 "timestamp": 1633285236049,
157 "displayName": "WRF Fire",
158 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
159 "userId": "09833348162306222387"
163 "id": "Dwbt4UXfro5x",
164 "outputId": "99320267-88f4-4a7a-ba4f-944ccfccde1b"
169 "output_type": "stream",
171 "loaded http://math.ucdenver.edu/~jmandel/rtma/20180518/00/temp.grib as temp.grib\n"
176 "year, month, day, hour, variable = (2018, 5, 18, 00, 'temp') # year, month, day, hour, variable\n",
177 "file = variable + '.grib'\n",
178 "path = '%4i%02i%02i/%02i/%s' % (year, month, day, hour, file)\n",
180 "ret=load_rtma(path,file) # load the file; ret is the return value that can be tested in application\n",
185 "cell_type": "markdown",
190 "Trying to read the file and a basic sanity check if the values make sense:"
195 "execution_count": 5,
198 "base_uri": "https://localhost:8080/"
203 "timestamp": 1633285237026,
205 "displayName": "WRF Fire",
206 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
207 "userId": "09833348162306222387"
211 "id": "0uSEAB1dZc7P",
212 "outputId": "ad890053-1452-49c2-e955-966973932827"
217 "output_type": "stream",
219 "min lats 20.191999000000006 max 52.80766860325035\n",
220 "shape (1377, 2145)\n",
221 "min lons -130.10343806997807 max -60.885557729221034\n",
222 "shape (1377, 2145)\n",
223 "min temp 266.84000000000003 max 315.01\n",
224 "shape (1377, 2145)\n"
229 "gf = GribFile(file)[1] # grib file consists of \"messages\" (basically, variables), rtma files have only one\n",
230 "lats, lons = gf.latlons() # grid of geo coodinates (computed), should be the same for all rtma files here\n",
231 "lats = np.array(lats) # tuple to numpy array\n",
232 "lons = np.array(lons) # tuple to numpy array\n",
233 "temp = np.array(gf.values()) # the actual variable values (here, T at 2m in K)\n",
235 "print('min lats %s max %s' % (np.amin(lats),np.amax(lats)))\n",
236 "print('shape',lats.shape)\n",
237 "print('min lons %s max %s' % (np.amin(lons),np.amax(lons)))\n",
238 "print('shape',lons.shape)\n",
239 "print('min temp %s max %s' % (np.amin(temp),np.amax(temp)))\n",
240 "print('shape',temp.shape)\n"
244 "cell_type": "markdown",
249 "One special grib file with the terrain height is stored at the root of the stash. This file is a part of the RTMA dataset but no need to download and store every hour, the data should never change. Trying to read it and doing a sanity check. Also,checking if the grid coordinages in this file are the same as before.\n"
254 "execution_count": 6,
257 "base_uri": "https://localhost:8080/"
262 "timestamp": 1633285238477,
264 "displayName": "WRF Fire",
265 "photoUrl": "https://lh3.googleusercontent.com/a/default-user=s64",
266 "userId": "09833348162306222387"
270 "id": "JZlX8BVl4HRB",
271 "outputId": "77812006-9b98-4936-961c-8672822abfdc"
276 "output_type": "stream",
278 "ds.terrainh.bin already exists, removing\n",
279 "loaded http://math.ucdenver.edu/~jmandel/rtma/ds.terrainh.bin as ds.terrainh.bin\n",
280 "min height -76.60000000000001 max 4156.900000000001\n",
281 "shape (1377, 2145)\n",
282 "difference in lats 0.0 lons 0.0\n"
287 "hf='ds.terrainh.bin' # terrain height, same in rtma at all times\n",
288 "load_rtma(hf,hf)\n",
289 "gf = GribFile(hf)[1] \n",
290 "hgt = np.array(gf.values()) # height in m\n",
291 "print('min height %s max %s' % (np.amin(hgt),np.amax(hgt)))\n",
292 "print('shape',hgt.shape)\n",
293 "hlats, hlons = gf.latlons() # grid of geo coodinates (computed), should be the same for all rtma files here\n",
294 "hlats = np.array(hlats) # tuple to numpy array\n",
295 "hlons = np.array(hlons) \n",
296 "print('difference in lats %s lons %s' % (np.amax(np.absolute(lats-hlats)), np.amax(np.absolute(lons-hlons))))\n"
302 "authorship_tag": "ABX9TyP4ue0fNwFqLrtHTL/TAAi3",
303 "collapsed_sections": [],
304 "name": "load-rtma-demo.ipynb",
308 "display_name": "Python 3",
309 "language": "python",
317 "file_extension": ".py",
318 "mimetype": "text/x-python",
320 "nbconvert_exporter": "python",
321 "pygments_lexer": "ipython3",