{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Convert profiles vertical coordinate from Hybrid-Pressure to pressure" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:02.091983Z", "iopub.status.busy": "2025-01-13T11:35:02.091760Z", "iopub.status.idle": "2025-01-13T11:35:02.656635Z", "shell.execute_reply": "2025-01-13T11:35:02.656103Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "# for figures in notebook\n", "\n", "# import & initialize epygram\n", "import epygram\n", "epygram.init_env()\n", "\n", "import os\n", "INPUTS_DIR = os.path.join('..', 'inputs')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:02.658702Z", "iopub.status.busy": "2025-01-13T11:35:02.658496Z", "iopub.status.idle": "2025-01-13T11:35:02.802603Z", "shell.execute_reply": "2025-01-13T11:35:02.802127Z" } }, "outputs": [], "source": [ "r = epygram.resources.meta_resource(os.path.join(INPUTS_DIR, 'ICMSHAROM+0022'), 'r', 'CL') # CL = CombineLevels\n", "# in a \"CL\" meta-resource, fields are to be grabbed through a GRIB2 nomenclature\n", "t_as_grib2 = dict(discipline=0, parameterCategory=0, parameterNumber=0, # this is temperature\n", " typeOfFirstFixedSurface=119) # hybrid-pressure levels" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:02.804454Z", "iopub.status.busy": "2025-01-13T11:35:02.804243Z", "iopub.status.idle": "2025-01-13T11:35:05.393154Z", "shell.execute_reply": "2025-01-13T11:35:05.392653Z" } }, "outputs": [], "source": [ "# read as 3D\n", "t3d = r.readfield(t_as_grib2)\n", "t3d.sp2gp()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:05.395252Z", "iopub.status.busy": "2025-01-13T11:35:05.395063Z", "iopub.status.idle": "2025-01-13T11:35:05.407071Z", "shell.execute_reply": "2025-01-13T11:35:05.406526Z" } }, "outputs": [], "source": [ "# extract profiles\n", "profiles = []\n", "for position in [(6.1,46.2), (6.8,45.8), (6,46.5)]:\n", " profgeo = t3d.geometry.make_profile_geometry(*position) # make a V1D geometry at that position\n", " profiles.append(t3d.extract_subdomain(profgeo)) # extract subdomain corresponding to the geometry of the profile" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:05.408695Z", "iopub.status.busy": "2025-01-13T11:35:05.408497Z", "iopub.status.idle": "2025-01-13T11:35:05.790485Z", "shell.execute_reply": "2025-01-13T11:35:05.789835Z" } }, "outputs": [], "source": [ "# plot with level as vertical coord\n", "fig = ax = None\n", "for p in profiles:\n", " fig, ax = p.plotfield(labels='Temperature @ ({}, {})'.format(p.geometry.grid['longitudes'][0],\n", " p.geometry.grid['latitudes'][0]),\n", " over=(fig, ax)) # superpose profiles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## and now on a Pressure z-axis" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:05.792338Z", "iopub.status.busy": "2025-01-13T11:35:05.792172Z", "iopub.status.idle": "2025-01-13T11:35:05.962004Z", "shell.execute_reply": "2025-01-13T11:35:05.961489Z" } }, "outputs": [], "source": [ "# for the conversion, we need Surface pressure\n", "ps = epygram.open(r.resource.container.abspath, 'r').readfield('SURFPRESSION')\n", "# Ps is spectral and ln(Ps)\n", "ps.sp2gp()\n", "ps.operation('exp')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:05.964278Z", "iopub.status.busy": "2025-01-13T11:35:05.964050Z", "iopub.status.idle": "2025-01-13T11:35:05.969792Z", "shell.execute_reply": "2025-01-13T11:35:05.969332Z" } }, "outputs": [], "source": [ "from epygram.geometries.VGeometry import hybridP2pressure\n", "\n", "for p in profiles:\n", " ps_local = ps.getvalue_ll(p.geometry.grid['longitudes'][0],\n", " p.geometry.grid['latitudes'][0])\n", " # create the P vertical coordinate\n", " p.geometry.vcoordinate = hybridP2pressure(p.geometry.vcoordinate,\n", " ps_local,\n", " vertical_mean='geometric')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "execution": { "iopub.execute_input": "2025-01-13T11:35:05.971851Z", "iopub.status.busy": "2025-01-13T11:35:05.971490Z", "iopub.status.idle": "2025-01-13T11:35:06.122404Z", "shell.execute_reply": "2025-01-13T11:35:06.121686Z" } }, "outputs": [], "source": [ "fig = ax = None\n", "for p in profiles:\n", " fig, ax = p.plotfield(labels='Temperature @ ({}, {})'.format(p.geometry.grid['longitudes'][0],\n", " p.geometry.grid['latitudes'][0]),\n", " over=(fig, ax)) # superpose profiles" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 1 }