{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Extract a value from a curvilinear grid\n", "\n", "A small notebook showing how to lookup the ij index from a given longitude and latitude in a curvilinear grid" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (axis_nbounds: 2, deptht: 31, tbnds: 2, time_counter: 12, x: 182, y: 149)\n", "Coordinates:\n", " * time_counter (time_counter) object 0000-01-16 00:00:00 ... 0000-...\n", " nav_lat (y, x) float32 -78.19 -78.19 -78.19 ... 59.91 59.91\n", " nav_lon (y, x) float32 105.0 107.0 109.0 ... 106.0 106.0 106.0\n", " * deptht (deptht) float32 5.0 15.0 25.0 ... 4.75e+03 5.25e+03\n", " time_centered (time_counter) object 4679-07-16 00:00:00 ... 4680-...\n", "Dimensions without coordinates: axis_nbounds, tbnds, x, y\n", "Data variables: (12/64)\n", " time_counter_bnds (time_counter, tbnds) object 0000-01-01 00:00:00 .....\n", " deptht_bounds (deptht, axis_nbounds) float32 0.0 10.0 ... 5.501e+03\n", " time_centered_bounds (time_counter, axis_nbounds) object 4679-07-01 00:0...\n", " time_counter_bounds (time_counter, axis_nbounds) float64 8.645e+10 ... ...\n", " e3t (time_counter, deptht, y, x) float32 ...\n", " pbo (time_counter, y, x) float32 ...\n", " ... ...\n", " rain (time_counter, y, x) float32 ...\n", " snow_ao_cea (time_counter, y, x) float32 ...\n", " snow_ai_cea (time_counter, y, x) float32 ...\n", " evap_ao_cea (time_counter, y, x) float32 ...\n", " subl_ai_cea (time_counter, y, x) float32 ...\n", " sosflxdo (time_counter, y, x) float32 ...\n", "Attributes:\n", " name: C10M-PNG-560_1m_grid_T\n", " description: Created by xios\n", " title: Created by xios\n", " Conventions: CF-1.6\n", " timeStamp: 2019-Dec-10 12:48:46 GMT\n", " uuid: 0b59c577-6754-47de-a26a-a13d8a514539\n", " LongName: IPSL 10 Ma,2X, BC from PlioMIP except for Aust...\n", " history: Wed Dec 11 05:53:06 2019: ncrcat -C --buffer_s...\n", " NCO: "4.6.0"\n", " nco_openmp_thread_number: 1
array([cftime.Datetime360Day(0, 1, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 2, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 3, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 4, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 5, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 6, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 7, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 8, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 9, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 10, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 11, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 12, 16, 0, 0, 0, 0, has_year_zero=False)],\n", " dtype=object)
array([[-78.19058 , -78.19058 , -78.19058 , ..., -78.19058 , -78.19058 ,\n", " -78.19058 ],\n", " [-77.7742 , -77.7742 , -77.7742 , ..., -77.7742 , -77.7742 ,\n", " -77.7742 ],\n", " [-77.34337 , -77.34337 , -77.34337 , ..., -77.34337 , -77.34337 ,\n", " -77.34337 ],\n", " ...,\n", " [ 59.725872, 59.725872, 59.73183 , ..., 59.73183 , 59.725872,\n", " 59.725872],\n", " [ 59.910233, 59.910233, 59.910248, ..., 59.910248, 59.910233,\n", " 59.910233],\n", " [ 59.910233, 59.910233, 59.910248, ..., 59.910248, 59.910233,\n", " 59.910233]], dtype=float32)
array([[105. , 107. , 109. , ..., 103. , 105. ,\n", " 107. ],\n", " [105. , 107. , 109. , ..., 103. , 105. ,\n", " 107. ],\n", " [105. , 107. , 109. , ..., 103. , 105. ,\n", " 107. ],\n", " ...,\n", " [105.98919 , 106.01081 , 106.054634, ..., 105.945366, 105.98919 ,\n", " 106.01081 ],\n", " [105.999725, 106.000275, 106.00089 , ..., 105.99911 , 105.999725,\n", " 106.000275],\n", " [106. , 105.999725, 105.99911 , ..., 106.00089 , 106. ,\n", " 105.999725]], dtype=float32)
array([4.999938e+00, 1.500029e+01, 2.500176e+01, 3.500541e+01, 4.501332e+01,\n", " 5.502950e+01, 6.506181e+01, 7.512551e+01, 8.525037e+01, 9.549429e+01,\n", " 1.059699e+02, 1.168962e+02, 1.286979e+02, 1.421953e+02, 1.589606e+02,\n", " 1.819628e+02, 2.166479e+02, 2.724767e+02, 3.643030e+02, 5.115348e+02,\n", " 7.322009e+02, 1.033217e+03, 1.405698e+03, 1.830885e+03, 2.289768e+03,\n", " 2.768242e+03, 3.257479e+03, 3.752442e+03, 4.250401e+03, 4.749913e+03,\n", " 5.250227e+03], dtype=float32)
array([cftime.Datetime360Day(4679, 7, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 8, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 9, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 10, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 11, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 12, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 1, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 2, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 3, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 4, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 5, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 6, 16, 0, 0, 0, 0, has_year_zero=False)],\n", " dtype=object)
array([[cftime.Datetime360Day(0, 1, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 2, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 2, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 3, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 3, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 4, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 4, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 5, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 5, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 6, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 6, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 7, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 7, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 8, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 8, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 9, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 9, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 10, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 10, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 11, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 11, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 12, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(0, 12, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(1, 1, 1, 0, 0, 0, 0, has_year_zero=False)]],\n", " dtype=object)
array([[ 0. , 10.000015],\n", " [ 10.000015, 20.000834],\n", " [ 20.000834, 30.003216],\n", " [ 30.003216, 40.008644],\n", " [ 40.008644, 50.020004],\n", " [ 50.020004, 60.042915],\n", " [ 60.042915, 70.08833 ],\n", " [ 70.08833 , 80.17757 ],\n", " [ 80.17757 , 90.35213 ],\n", " [ 90.35213 , 100.692795],\n", " [ 100.692795, 111.35667 ],\n", " [ 111.35667 , 122.64877 ],\n", " [ 122.64877 , 135.15974 ],\n", " [ 135.15974 , 150.02681 ],\n", " [ 150.02681 , 169.41597 ],\n", " [ 169.41597 , 197.36778 ],\n", " [ 197.36778 , 241.12593 ],\n", " [ 241.12593 , 312.74466 ],\n", " [ 312.74466 , 429.72336 ],\n", " [ 429.72336 , 611.8891 ],\n", " [ 611.8891 , 872.8738 ],\n", " [ 872.8738 , 1211.588 ],\n", " [1211.588 , 1612.9757 ],\n", " [1612.9757 , 2057.1313 ],\n", " [2057.1313 , 2527.2168 ],\n", " [2527.2168 , 3011.8994 ],\n", " [3011.8994 , 3504.455 ],\n", " [3504.455 , 4001.159 ],\n", " [4001.159 , 4500.0215 ],\n", " [4500.0215 , 5000. ],\n", " [5000. , 5500.5645 ]], dtype=float32)
array([[cftime.Datetime360Day(4679, 7, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 8, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4679, 8, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 9, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4679, 9, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 10, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4679, 10, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 11, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4679, 11, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 12, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4679, 12, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 1, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 1, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 2, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 2, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 3, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 3, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 4, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 4, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 5, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 5, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 6, 1, 0, 0, 0, 0, has_year_zero=False)],\n", " [cftime.Datetime360Day(4680, 6, 1, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 7, 1, 0, 0, 0, 0, has_year_zero=False)]],\n", " dtype=object)
array([[8.645357e+10, 8.645616e+10],\n", " [8.645616e+10, 8.645875e+10],\n", " [8.645875e+10, 8.646134e+10],\n", " [8.646134e+10, 8.646394e+10],\n", " [8.646394e+10, 8.646653e+10],\n", " [8.646653e+10, 8.646912e+10],\n", " [8.646912e+10, 8.647171e+10],\n", " [8.647171e+10, 8.647430e+10],\n", " [8.647430e+10, 8.647690e+10],\n", " [8.647690e+10, 8.647949e+10],\n", " [8.647949e+10, 8.648208e+10],\n", " [8.648208e+10, 8.648467e+10]])
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[10087896 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
[325416 values with dtype=float32]
<xarray.DataArray 'tos' (time_counter: 12)>\n", "array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],\n", " dtype=float32)\n", "Coordinates:\n", " * time_counter (time_counter) object 0000-01-16 00:00:00 ... 0000-12-16 0...\n", " nav_lat float32 10.51\n", " nav_lon float32 11.0\n", " time_centered (time_counter) object 4679-07-16 00:00:00 ... 4680-06-16 0...\n", "Attributes:\n", " standard_name: sea_surface_temperature\n", " long_name: sea_surface_temperature\n", " units: degC\n", " online_operation: average\n", " interval_operation: 4800 s\n", " interval_write: 1 month\n", " cell_methods: time: mean (interval: 4800 s) time_counter: mean
array([nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],\n", " dtype=float32)
array([cftime.Datetime360Day(0, 1, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 2, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 3, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 4, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 5, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 6, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 7, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 8, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 9, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 10, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 11, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(0, 12, 16, 0, 0, 0, 0, has_year_zero=False)],\n", " dtype=object)
array(10.509401, dtype=float32)
array(11., dtype=float32)
array([cftime.Datetime360Day(4679, 7, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 8, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 9, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 10, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 11, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4679, 12, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 1, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 2, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 3, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 4, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 5, 16, 0, 0, 0, 0, has_year_zero=False),\n", " cftime.Datetime360Day(4680, 6, 16, 0, 0, 0, 0, has_year_zero=False)],\n", " dtype=object)