{ "cells": [ { "cell_type": "markdown", "id": "b1eefe41", "metadata": {}, "source": [ "# 4. 時間條件控制" ] }, { "cell_type": "markdown", "id": "ae5b58b4", "metadata": {}, "source": [ "## `xarray`時間條件控制\n", "\n", "在先前的範例中,已經示範如何用`slice`來選擇特定時間區間的資料,但若選擇的時間並不連續,用`slice`就不太方便。因此若能在時間加入一些條件控制,則可以只選擇滿足條件的資料。xarray轉譯出的時間屬於datetime物件,且存在年、月、日等時間單位的attribute,因此我們就可以針對它們進行條件控制。\n", "\n", "**Example 1: 選擇JAS(七至九月)季節的資料** \n", "\n", "首先準備資料:" ] }, { "cell_type": "code", "execution_count": 1, "id": "0721be78", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray 'olr' (time: 2208, lat: 90, lon: 360)>\n", "[71539200 values with dtype=float32]\n", "Coordinates:\n", " * time (time) datetime64[ns] 1998-07-01 1998-07-02 ... 2021-09-30\n", " * lon (lon) float32 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5\n", " * lat (lat) float32 -44.5 -43.5 -42.5 -41.5 -40.5 ... 41.5 42.5 43.5 44.5\n", "Attributes:\n", " standard_name: toa_outgoing_longwave_flux\n", " long_name: NOAA Climate Data Record of Daily Mean Upward Longwave Fl...\n", " units: W m-2\n", " cell_methods: time: mean area: mean
<xarray.DataArray 'olr' (time: 8760, lat: 90, lon: 360)>\n", "[283824000 values with dtype=float32]\n", "Coordinates:\n", " * time (time) datetime64[ns] 1998-01-01 1998-01-02 ... 2021-12-31\n", " * lon (lon) float32 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5\n", " * lat (lat) float32 -44.5 -43.5 -42.5 -41.5 -40.5 ... 41.5 42.5 43.5 44.5\n", "Attributes:\n", " standard_name: toa_outgoing_longwave_flux\n", " long_name: NOAA Climate Data Record of Daily Mean Upward Longwave Fl...\n", " units: W m-2\n", " cell_methods: time: mean area: mean
<xarray.DataArray 'strftime' (time: 8760)>\n", "array(['Jan 01 98', 'Jan 02 98', 'Jan 03 98', ..., 'Dec 29 21',\n", " 'Dec 30 21', 'Dec 31 21'], dtype=object)\n", "Coordinates:\n", " * time (time) datetime64[ns] 1998-01-01 1998-01-02 ... 2021-12-31