{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Slicing Iregular Grids \n", "\n", "**Acknowledgement:** The material was provided by Mrs. Kuan-Yun Wang, UC Davis." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 非直線的剖面 (cross-section) 繪製\n", "\n", "本節學習如何使用xarray繪製「不是沿著相同經緯線」的cross section,這裡提供的範例是繪製渤海、黃海、東海到南海這個曲折線段上的 $V$ 風垂直剖面。\n", "\n", "首先引入需要的packages,以及讀取2016年1月份的V-wind資料,並取1月份平均。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import xarray as xr\n", "import matplotlib.pyplot as plt\n", "import cartopy.crs as ccrs\n", "import cmaps\n", "\n", "v = xr.open_dataset('data/vwnd.2016.nc').vwnd\n", "v_jan = v.sel(time=(v.time.dt.month.isin([1]))).mean('time')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "接下來讀取渤海、黃海、東海到南海這個曲折線段的經緯度,並選擇要繪製的經緯度範圍 (以[文字檔](https://wyhtsai.github.io/pyaos-wks/docs/sellatlon_EA.txt)儲存,其中第一欄為經度,第二欄為緯度)。" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "y_pos, x_pos= np.loadtxt('sellatlon_EA.txt', dtype=float, unpack=True)\n", "x_pos = x_pos[y_pos <= 50]\n", "y_pos = y_pos[y_pos <= 50]\n", "x_pos = x_pos[y_pos >= 0]\n", "y_pos = y_pos[y_pos >= 0]" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "為`x_pos`、`y_pos`建立新的DataArray,並設定擁有相同的dimension。接著,使用`xr.interp`函數對 $V$ 風場資料內插到所選的經緯度(x_pos, y_pos)上。" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'vwnd' (level: 17, lat: 500)>\n", "array([[-3.68268522, -3.66213074, -3.64047876, ..., -3.11352991,\n", " -3.05033255, -2.98713519],\n", " [-3.64299498, -3.61555066, -3.58706748, ..., -3.33184726,\n", " -3.2842215 , -3.23659573],\n", " [-7.25529253, -7.23878454, -7.22188098, ..., -1.58555811,\n", " -1.56942647, -1.55329484],\n", " ...,\n", " [ 4.52398556, 4.62559524, 4.72667311, ..., 0.30175616,\n", " 0.31368649, 0.32561683],\n", " [11.18332733, 11.25741096, 11.33039131, ..., -0.15216129,\n", " -0.15066706, -0.14917283],\n", " [20.89200851, 20.86511519, 20.83686176, ..., 1.41306749,\n", " 1.41367915, 1.41429081]])\n", "Coordinates:\n", " * level (level) float32 1e+03 925.0 850.0 700.0 ... 50.0 30.0 20.0 10.0\n", " * lat (lat) float64 50.0 49.9 49.8 49.7 49.6 49.5 ... 0.5 0.4 0.3 0.2 0.1\n", " lon (lat) float64 114.4 114.5 114.6 114.6 ... 108.0 108.0 108.0 108.0