<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://anwoybarua.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://anwoybarua.github.io/" rel="alternate" type="text/html" /><updated>2026-11-09T04:55:19+00:00</updated><id>https://anwoybarua.github.io/feed.xml</id><title type="html">About Me</title><subtitle>Anwoy&apos;s academic portfolio</subtitle><author><name>Anwoy Barua</name><email>anwoybarua@gmail.com</email></author><entry><title type="html">Xarray Guidebook Part 1: Working with Multidimensional Climate Data</title><link href="https://anwoybarua.github.io/posts/2026/10/xarray-guidebook-part1/" rel="alternate" type="text/html" title="Xarray Guidebook Part 1: Working with Multidimensional Climate Data" /><published>2026-10-07T00:00:00+00:00</published><updated>2026-10-07T00:00:00+00:00</updated><id>https://anwoybarua.github.io/posts/2026/10/Xarray-guidebook-part1</id><content type="html" xml:base="https://anwoybarua.github.io/posts/2026/10/xarray-guidebook-part1/"><![CDATA[<h1 id="working-with-multidimensional-climate-data-using-python-part-1">Working with multidimensional climate data using Python (Part 1)</h1>

<p>In this notebook, I will go through the basics of xarray and other necessary packages that are used to explore and analyze multidimensional climate datasets (<strong>CMIP6, ERA5, CRU, CHRIPS etc.</strong>)</p>

<p>This notebook is structured as follows:</p>
<ol>
  <li>Reading dataset</li>
  <li>Inspecting the dataset</li>
  <li>filtering by time and location</li>
</ol>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">xarray</span> <span class="k">as</span> <span class="n">xr</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="n">pd</span>
</code></pre></div></div>

<p><strong>The main xarray objects</strong><br /></p>
<blockquote>
  <p>DataArray: one labeled variable, for example daily precipitation or tasmax.</p>
</blockquote>

<blockquote>
  <p>Dataset: a container holding one or more variables, coordinates, and metadata.</p>
</blockquote>

<blockquote>
  <p>Dimensions: axes of an array, often time, latitude, longitude, or level.</p>
</blockquote>

<blockquote>
  <p>Coordinates: labels that identify positions along dimensions, such as dates and grid-cell locations.</p>
</blockquote>

<blockquote>
  <p>Attributes (attrs): metadata such as units, long name, and source.</p>
</blockquote>

<h2 id="0-introduction-to-necessary-file-formats">0. Introduction to necessary file formats:</h2>
<p><strong>About NetCDF File:</strong><br /></p>
<blockquote>
  <p><strong><em>NetCDF (Network Common Data Form)</em></strong> is a file format designed to store scientific data, especially multidimensional data such as temperature, precipitation, humidity, and wind speed over time and geographical locations.<br /></p>
  <ul>
    <li>Structure: Stores data in dimensions (e.g., time, latitude, longitude), variables, and metadata.</li>
    <li>Common uses: Climate model outputs, satellite observations, and reanalysis datasets such as ERA5.</li>
    <li>Python tools: xarray, netCDF4, and h5netcdf.<br /></li>
  </ul>
</blockquote>

<p><strong>About GRIB File:</strong><br /></p>
<blockquote>
  <p><strong><em>GRIB (GRIdded Binary)</em></strong> is a compact binary file format developed by the World Meteorological Organization (WMO) for storing and transmitting meteorological and weather forecast data.<br /></p>
  <ul>
    <li>Structure: Stores gridded weather variables along with information about time, geographical coordinates, forecast steps, and data levels.</li>
    <li>Common uses: Numerical weather prediction, weather forecasts, and meteorological reanalysis datasets.</li>
    <li>Python tools: cfgrib with xarray, and eccodes.</li>
  </ul>
</blockquote>

<h2 id="1-reading-datasets">1. Reading Datasets</h2>

<h3 id="11-opening-a-single-netcdf-file">1.1 Opening a single netcdf file</h3>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">temp_df</span> <span class="o">=</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_dataset</span><span class="p">(</span><span class="s">"D:\CMIP6 Project\Thesis work\Data Analysis\Data\ERA5_Data\ERA5_Daily_Tmin_Tmax_1989_2019.nc"</span><span class="p">)</span>
<span class="n">temp_df</span>
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>c:\Users\Anwoy\miniconda3\envs\cmip6\Lib\site-packages\xarray\backends\plugins.py:110: RuntimeWarning: Engine 'cfgrib' loading failed:
Cannot find the ecCodes library
  external_backend_entrypoints = backends_dict_from_pkg(entrypoints_unique)
</code></pre></div></div>

<div><svg style="position: absolute; width: 0; height: 0; overflow: hidden">
<defs>
<symbol id="icon-database" viewBox="0 0 32 32">
<path d="M16 0c-8.837 0-16 2.239-16 5v4c0 2.761 7.163 5 16 5s16-2.239 16-5v-4c0-2.761-7.163-5-16-5z"></path>
<path d="M16 17c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
<path d="M16 26c-8.837 0-16-2.239-16-5v6c0 2.761 7.163 5 16 5s16-2.239 16-5v-6c0 2.761-7.163 5-16 5z"></path>
</symbol>
<symbol id="icon-file-text2" viewBox="0 0 32 32">
<path d="M28.681 7.159c-0.694-0.947-1.662-2.053-2.724-3.116s-2.169-2.030-3.116-2.724c-1.612-1.182-2.393-1.319-2.841-1.319h-15.5c-1.378 0-2.5 1.121-2.5 2.5v27c0 1.378 1.122 2.5 2.5 2.5h23c1.378 0 2.5-1.122 2.5-2.5v-19.5c0-0.448-0.137-1.23-1.319-2.841zM24.543 5.457c0.959 0.959 1.712 1.825 2.268 2.543h-4.811v-4.811c0.718 0.556 1.584 1.309 2.543 2.268zM28 29.5c0 0.271-0.229 0.5-0.5 0.5h-23c-0.271 0-0.5-0.229-0.5-0.5v-27c0-0.271 0.229-0.5 0.5-0.5 0 0 15.499-0 15.5 0v7c0 0.552 0.448 1 1 1h7v19.5z"></path>
<path d="M23 26h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 22h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
<path d="M23 18h-14c-0.552 0-1-0.448-1-1s0.448-1 1-1h14c0.552 0 1 0.448 1 1s-0.448 1-1 1z"></path>
</symbol>
</defs>
</svg>
<style>/* CSS stylesheet for displaying xarray objects in jupyterlab.
 *
 */

:root {
  --xr-font-color0: var(
    --jp-content-font-color0,
    var(--pst-color-text-base rgba(0, 0, 0, 1))
  );
  --xr-font-color2: var(
    --jp-content-font-color2,
    var(--pst-color-text-base, rgba(0, 0, 0, 0.54))
  );
  --xr-font-color3: var(
    --jp-content-font-color3,
    var(--pst-color-text-base, rgba(0, 0, 0, 0.38))
  );
  --xr-border-color: var(
    --jp-border-color2,
    hsl(from var(--pst-color-on-background, white) h s calc(l - 10))
  );
  --xr-disabled-color: var(
    --jp-layout-color3,
    hsl(from var(--pst-color-on-background, white) h s calc(l - 40))
  );
  --xr-background-color: var(
    --jp-layout-color0,
    var(--pst-color-on-background, white)
  );
  --xr-background-color-row-even: var(
    --jp-layout-color1,
    hsl(from var(--pst-color-on-background, white) h s calc(l - 5))
  );
  --xr-background-color-row-odd: var(
    --jp-layout-color2,
    hsl(from var(--pst-color-on-background, white) h s calc(l - 15))
  );
}

html[theme="dark"],
html[data-theme="dark"],
body[data-theme="dark"],
body.vscode-dark {
  --xr-font-color0: var(
    --jp-content-font-color0,
    var(--pst-color-text-base, rgba(255, 255, 255, 1))
  );
  --xr-font-color2: var(
    --jp-content-font-color2,
    var(--pst-color-text-base, rgba(255, 255, 255, 0.54))
  );
  --xr-font-color3: var(
    --jp-content-font-color3,
    var(--pst-color-text-base, rgba(255, 255, 255, 0.38))
  );
  --xr-border-color: var(
    --jp-border-color2,
    hsl(from var(--pst-color-on-background, #111111) h s calc(l + 10))
  );
  --xr-disabled-color: var(
    --jp-layout-color3,
    hsl(from var(--pst-color-on-background, #111111) h s calc(l + 40))
  );
  --xr-background-color: var(
    --jp-layout-color0,
    var(--pst-color-on-background, #111111)
  );
  --xr-background-color-row-even: var(
    --jp-layout-color1,
    hsl(from var(--pst-color-on-background, #111111) h s calc(l + 5))
  );
  --xr-background-color-row-odd: var(
    --jp-layout-color2,
    hsl(from var(--pst-color-on-background, #111111) h s calc(l + 15))
  );
}

.xr-wrap {
  display: block !important;
  min-width: 300px;
  max-width: 700px;
}

.xr-text-repr-fallback {
  /* fallback to plain text repr when CSS is not injected (untrusted notebook) */
  display: none;
}

.xr-header {
  padding-top: 6px;
  padding-bottom: 6px;
  margin-bottom: 4px;
  border-bottom: solid 1px var(--xr-border-color);
}

.xr-header > div,
.xr-header > ul {
  display: inline;
  margin-top: 0;
  margin-bottom: 0;
}

.xr-obj-type,
.xr-array-name {
  margin-left: 2px;
  margin-right: 10px;
}

.xr-obj-type {
  color: var(--xr-font-color2);
}

.xr-sections {
  padding-left: 0 !important;
  display: grid;
  grid-template-columns: 150px auto auto 1fr 0 20px 0 20px;
}

.xr-section-item {
  display: contents;
}

.xr-section-item input {
  display: inline-block;
  opacity: 0;
  height: 0;
}

.xr-section-item input + label {
  color: var(--xr-disabled-color);
  border: 2px solid transparent !important;
}

.xr-section-item input:enabled + label {
  cursor: pointer;
  color: var(--xr-font-color2);
}

.xr-section-item input:focus + label {
  border: 2px solid var(--xr-font-color0) !important;
}

.xr-section-item input:enabled + label:hover {
  color: var(--xr-font-color0);
}

.xr-section-summary {
  grid-column: 1;
  color: var(--xr-font-color2);
  font-weight: 500;
}

.xr-section-summary > span {
  display: inline-block;
  padding-left: 0.5em;
}

.xr-section-summary-in:disabled + label {
  color: var(--xr-font-color2);
}

.xr-section-summary-in + label:before {
  display: inline-block;
  content: "►";
  font-size: 11px;
  width: 15px;
  text-align: center;
}

.xr-section-summary-in:disabled + label:before {
  color: var(--xr-disabled-color);
}

.xr-section-summary-in:checked + label:before {
  content: "▼";
}

.xr-section-summary-in:checked + label > span {
  display: none;
}

.xr-section-summary,
.xr-section-inline-details {
  padding-top: 4px;
  padding-bottom: 4px;
}

.xr-section-inline-details {
  grid-column: 2 / -1;
}

.xr-section-details {
  display: none;
  grid-column: 1 / -1;
  margin-bottom: 5px;
}

.xr-section-summary-in:checked ~ .xr-section-details {
  display: contents;
}

.xr-array-wrap {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 20px auto;
}

.xr-array-wrap > label {
  grid-column: 1;
  vertical-align: top;
}

.xr-preview {
  color: var(--xr-font-color3);
}

.xr-array-preview,
.xr-array-data {
  padding: 0 5px !important;
  grid-column: 2;
}

.xr-array-data,
.xr-array-in:checked ~ .xr-array-preview {
  display: none;
}

.xr-array-in:checked ~ .xr-array-data,
.xr-array-preview {
  display: inline-block;
}

.xr-dim-list {
  display: inline-block !important;
  list-style: none;
  padding: 0 !important;
  margin: 0;
}

.xr-dim-list li {
  display: inline-block;
  padding: 0;
  margin: 0;
}

.xr-dim-list:before {
  content: "(";
}

.xr-dim-list:after {
  content: ")";
}

.xr-dim-list li:not(:last-child):after {
  content: ",";
  padding-right: 5px;
}

.xr-has-index {
  font-weight: bold;
}

.xr-var-list,
.xr-var-item {
  display: contents;
}

.xr-var-item > div,
.xr-var-item label,
.xr-var-item > .xr-var-name span {
  background-color: var(--xr-background-color-row-even);
  border-color: var(--xr-background-color-row-odd);
  margin-bottom: 0;
  padding-top: 2px;
}

.xr-var-item > .xr-var-name:hover span {
  padding-right: 5px;
}

.xr-var-list > li:nth-child(odd) > div,
.xr-var-list > li:nth-child(odd) > label,
.xr-var-list > li:nth-child(odd) > .xr-var-name span {
  background-color: var(--xr-background-color-row-odd);
  border-color: var(--xr-background-color-row-even);
}

.xr-var-name {
  grid-column: 1;
}

.xr-var-dims {
  grid-column: 2;
}

.xr-var-dtype {
  grid-column: 3;
  text-align: right;
  color: var(--xr-font-color2);
}

.xr-var-preview {
  grid-column: 4;
}

.xr-index-preview {
  grid-column: 2 / 5;
  color: var(--xr-font-color2);
}

.xr-var-name,
.xr-var-dims,
.xr-var-dtype,
.xr-preview,
.xr-attrs dt {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-right: 10px;
}

.xr-var-name:hover,
.xr-var-dims:hover,
.xr-var-dtype:hover,
.xr-attrs dt:hover {
  overflow: visible;
  width: auto;
  z-index: 1;
}

.xr-var-attrs,
.xr-var-data,
.xr-index-data {
  display: none;
  border-top: 2px dotted var(--xr-background-color);
  padding-bottom: 20px !important;
  padding-top: 10px !important;
}

.xr-var-attrs-in + label,
.xr-var-data-in + label,
.xr-index-data-in + label {
  padding: 0 1px;
}

.xr-var-attrs-in:checked ~ .xr-var-attrs,
.xr-var-data-in:checked ~ .xr-var-data,
.xr-index-data-in:checked ~ .xr-index-data {
  display: block;
}

.xr-var-data > table {
  float: right;
}

.xr-var-data > pre,
.xr-index-data > pre,
.xr-var-data > table > tbody > tr {
  background-color: transparent !important;
}

.xr-var-name span,
.xr-var-data,
.xr-index-name div,
.xr-index-data,
.xr-attrs {
  padding-left: 25px !important;
}

.xr-attrs,
.xr-var-attrs,
.xr-var-data,
.xr-index-data {
  grid-column: 1 / -1;
}

dl.xr-attrs {
  padding: 0;
  margin: 0;
  display: grid;
  grid-template-columns: 125px auto;
}

.xr-attrs dt,
.xr-attrs dd {
  padding: 0;
  margin: 0;
  float: left;
  padding-right: 10px;
  width: auto;
}

.xr-attrs dt {
  font-weight: normal;
  grid-column: 1;
}

.xr-attrs dt:hover span {
  display: inline-block;
  background: var(--xr-background-color);
  padding-right: 10px;
}

.xr-attrs dd {
  grid-column: 2;
  white-space: pre-wrap;
  word-break: break-all;
}

.xr-icon-database,
.xr-icon-file-text2,
.xr-no-icon {
  display: inline-block;
  vertical-align: middle;
  width: 1em;
  height: 1.5em !important;
  stroke-width: 0;
  stroke: currentColor;
  fill: currentColor;
}

.xr-var-attrs-in:checked + label > .xr-icon-file-text2,
.xr-var-data-in:checked + label > .xr-icon-database,
.xr-index-data-in:checked + label > .xr-icon-database {
  color: var(--xr-font-color0);
  filter: drop-shadow(1px 1px 5px var(--xr-font-color2));
  stroke-width: 0.8px;
}
</style><pre class="xr-text-repr-fallback">&lt;xarray.Dataset&gt; Size: 75MB
Dimensions:     (valid_time: 11322, latitude: 33, longitude: 25)
Coordinates:
  * latitude    (latitude) float64 264B 28.0 27.75 27.5 ... 20.5 20.25 20.0
  * longitude   (longitude) float64 200B 87.0 87.25 87.5 ... 92.5 92.75 93.0
  * valid_time  (valid_time) datetime64[ns] 91kB 1989-01-01 ... 2019-12-31
Data variables:
    tasmax      (valid_time, latitude, longitude) float32 37MB ...
    tasmin      (valid_time, latitude, longitude) float32 37MB ...
Attributes:
    GRIB_centre:             ecmf
    GRIB_centreDescription:  European Centre for Medium-Range Weather Forecasts
    GRIB_subCentre:          0
    Conventions:             CF-1.7
    institution:             European Centre for Medium-Range Weather Forecasts
    history:                 2026-02-18T15:22 GRIB to CDM+CF via cfgrib-0.9.1...</pre><div class="xr-wrap" style="display:none"><div class="xr-header"><div class="xr-obj-type">xarray.Dataset</div></div><ul class="xr-sections"><li class="xr-section-item"><input id="section-71d5a694-235c-4ea1-805b-9d038ac82daf" class="xr-section-summary-in" type="checkbox" disabled="" /><label for="section-71d5a694-235c-4ea1-805b-9d038ac82daf" class="xr-section-summary" title="Expand/collapse section">Dimensions:</label><div class="xr-section-inline-details"><ul class="xr-dim-list"><li><span class="xr-has-index">valid_time</span>: 11322</li><li><span class="xr-has-index">latitude</span>: 33</li><li><span class="xr-has-index">longitude</span>: 25</li></ul></div><div class="xr-section-details"></div></li><li class="xr-section-item"><input id="section-8818b9de-0399-405e-9ae4-2a09e4900941" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-8818b9de-0399-405e-9ae4-2a09e4900941" class="xr-section-summary">Coordinates: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">latitude</span></div><div class="xr-var-dims">(latitude)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">28.0 27.75 27.5 ... 20.5 20.25 20.0</div><input id="attrs-15412e0c-b9e1-46d4-a4d5-f3c5a31a0cb0" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-15412e0c-b9e1-46d4-a4d5-f3c5a31a0cb0" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-2e58ff9b-4764-417f-915e-0b3d1d917829" class="xr-var-data-in" type="checkbox" /><label for="data-2e58ff9b-4764-417f-915e-0b3d1d917829" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_north</dd><dt><span>standard_name :</span></dt><dd>latitude</dd><dt><span>long_name :</span></dt><dd>latitude</dd><dt><span>stored_direction :</span></dt><dd>decreasing</dd></dl></div><div class="xr-var-data"><pre>array([28.  , 27.75, 27.5 , 27.25, 27.  , 26.75, 26.5 , 26.25, 26.  , 25.75,
       25.5 , 25.25, 25.  , 24.75, 24.5 , 24.25, 24.  , 23.75, 23.5 , 23.25,
       23.  , 22.75, 22.5 , 22.25, 22.  , 21.75, 21.5 , 21.25, 21.  , 20.75,
       20.5 , 20.25, 20.  ])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">longitude</span></div><div class="xr-var-dims">(longitude)</div><div class="xr-var-dtype">float64</div><div class="xr-var-preview xr-preview">87.0 87.25 87.5 ... 92.5 92.75 93.0</div><input id="attrs-6ca189b4-bcdc-4208-9214-09d31449725d" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-6ca189b4-bcdc-4208-9214-09d31449725d" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-1bf31a6c-12f5-429a-8117-f934d1c6fe93" class="xr-var-data-in" type="checkbox" /><label for="data-1bf31a6c-12f5-429a-8117-f934d1c6fe93" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>units :</span></dt><dd>degrees_east</dd><dt><span>standard_name :</span></dt><dd>longitude</dd><dt><span>long_name :</span></dt><dd>longitude</dd></dl></div><div class="xr-var-data"><pre>array([87.  , 87.25, 87.5 , 87.75, 88.  , 88.25, 88.5 , 88.75, 89.  , 89.25,
       89.5 , 89.75, 90.  , 90.25, 90.5 , 90.75, 91.  , 91.25, 91.5 , 91.75,
       92.  , 92.25, 92.5 , 92.75, 93.  ])</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span class="xr-has-index">valid_time</span></div><div class="xr-var-dims">(valid_time)</div><div class="xr-var-dtype">datetime64[ns]</div><div class="xr-var-preview xr-preview">1989-01-01 ... 2019-12-31</div><input id="attrs-79866c8f-121b-4236-8078-6874d1a2be10" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-79866c8f-121b-4236-8078-6874d1a2be10" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-8367ed13-162f-4378-b16c-f6a3ffade938" class="xr-var-data-in" type="checkbox" /><label for="data-8367ed13-162f-4378-b16c-f6a3ffade938" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>long_name :</span></dt><dd>time</dd><dt><span>standard_name :</span></dt><dd>time</dd><dt><span>time_shift :</span></dt><dd>0 days 06:00:00</dd></dl></div><div class="xr-var-data"><pre>array([&#x27;1989-01-01T00:00:00.000000000&#x27;, &#x27;1989-01-02T00:00:00.000000000&#x27;,
       &#x27;1989-01-03T00:00:00.000000000&#x27;, ..., &#x27;2019-12-29T00:00:00.000000000&#x27;,
       &#x27;2019-12-30T00:00:00.000000000&#x27;, &#x27;2019-12-31T00:00:00.000000000&#x27;],
      dtype=&#x27;datetime64[ns]&#x27;)</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-bc4ffea4-1138-4496-8c1b-f482bda39af9" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-bc4ffea4-1138-4496-8c1b-f482bda39af9" class="xr-section-summary">Data variables: <span>(2)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-var-name"><span>tasmax</span></div><div class="xr-var-dims">(valid_time, latitude, longitude)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-bbf5037b-e1d5-44d0-a7c4-e10fc7913132" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-bbf5037b-e1d5-44d0-a7c4-e10fc7913132" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-a3dc76de-4c82-4627-a7fb-bbeeb8b51316" class="xr-var-data-in" type="checkbox" /><label for="data-a3dc76de-4c82-4627-a7fb-bbeeb8b51316" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>GRIB_paramId :</span></dt><dd>167</dd><dt><span>GRIB_dataType :</span></dt><dd>an</dd><dt><span>GRIB_numberOfPoints :</span></dt><dd>825</dd><dt><span>GRIB_typeOfLevel :</span></dt><dd>surface</dd><dt><span>GRIB_stepUnits :</span></dt><dd>1</dd><dt><span>GRIB_stepType :</span></dt><dd>instant</dd><dt><span>GRIB_gridType :</span></dt><dd>regular_ll</dd><dt><span>GRIB_uvRelativeToGrid :</span></dt><dd>0</dd><dt><span>GRIB_NV :</span></dt><dd>0</dd><dt><span>GRIB_Nx :</span></dt><dd>25</dd><dt><span>GRIB_Ny :</span></dt><dd>33</dd><dt><span>GRIB_cfName :</span></dt><dd>unknown</dd><dt><span>GRIB_cfVarName :</span></dt><dd>t2m</dd><dt><span>GRIB_gridDefinitionDescription :</span></dt><dd>Latitude/Longitude Grid</dd><dt><span>GRIB_iDirectionIncrementInDegrees :</span></dt><dd>0.25</dd><dt><span>GRIB_iScansNegatively :</span></dt><dd>0</dd><dt><span>GRIB_jDirectionIncrementInDegrees :</span></dt><dd>0.25</dd><dt><span>GRIB_jPointsAreConsecutive :</span></dt><dd>0</dd><dt><span>GRIB_jScansPositively :</span></dt><dd>0</dd><dt><span>GRIB_latitudeOfFirstGridPointInDegrees :</span></dt><dd>28.0</dd><dt><span>GRIB_latitudeOfLastGridPointInDegrees :</span></dt><dd>20.0</dd><dt><span>GRIB_longitudeOfFirstGridPointInDegrees :</span></dt><dd>87.0</dd><dt><span>GRIB_longitudeOfLastGridPointInDegrees :</span></dt><dd>93.0</dd><dt><span>GRIB_missingValue :</span></dt><dd>3.4028234663852886e+38</dd><dt><span>GRIB_name :</span></dt><dd>2 metre temperature</dd><dt><span>GRIB_shortName :</span></dt><dd>2t</dd><dt><span>GRIB_totalNumber :</span></dt><dd>0</dd><dt><span>GRIB_units :</span></dt><dd>K</dd><dt><span>long_name :</span></dt><dd>Daily maximum near-surface air temperature</dd><dt><span>units :</span></dt><dd>°C</dd><dt><span>standard_name :</span></dt><dd>unknown</dd><dt><span>GRIB_surface :</span></dt><dd>0.0</dd></dl></div><div class="xr-var-data"><pre>[9340650 values with dtype=float32]</pre></div></li><li class="xr-var-item"><div class="xr-var-name"><span>tasmin</span></div><div class="xr-var-dims">(valid_time, latitude, longitude)</div><div class="xr-var-dtype">float32</div><div class="xr-var-preview xr-preview">...</div><input id="attrs-eb57c4ad-e654-4229-b743-5a3e9971731c" class="xr-var-attrs-in" type="checkbox" /><label for="attrs-eb57c4ad-e654-4229-b743-5a3e9971731c" title="Show/Hide attributes"><svg class="icon xr-icon-file-text2"><use xlink:href="#icon-file-text2"></use></svg></label><input id="data-4ae4fe2c-88d6-4ae0-a966-bc57bcb1c133" class="xr-var-data-in" type="checkbox" /><label for="data-4ae4fe2c-88d6-4ae0-a966-bc57bcb1c133" title="Show/Hide data repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-var-attrs"><dl class="xr-attrs"><dt><span>GRIB_paramId :</span></dt><dd>167</dd><dt><span>GRIB_dataType :</span></dt><dd>an</dd><dt><span>GRIB_numberOfPoints :</span></dt><dd>825</dd><dt><span>GRIB_typeOfLevel :</span></dt><dd>surface</dd><dt><span>GRIB_stepUnits :</span></dt><dd>1</dd><dt><span>GRIB_stepType :</span></dt><dd>instant</dd><dt><span>GRIB_gridType :</span></dt><dd>regular_ll</dd><dt><span>GRIB_uvRelativeToGrid :</span></dt><dd>0</dd><dt><span>GRIB_NV :</span></dt><dd>0</dd><dt><span>GRIB_Nx :</span></dt><dd>25</dd><dt><span>GRIB_Ny :</span></dt><dd>33</dd><dt><span>GRIB_cfName :</span></dt><dd>unknown</dd><dt><span>GRIB_cfVarName :</span></dt><dd>t2m</dd><dt><span>GRIB_gridDefinitionDescription :</span></dt><dd>Latitude/Longitude Grid</dd><dt><span>GRIB_iDirectionIncrementInDegrees :</span></dt><dd>0.25</dd><dt><span>GRIB_iScansNegatively :</span></dt><dd>0</dd><dt><span>GRIB_jDirectionIncrementInDegrees :</span></dt><dd>0.25</dd><dt><span>GRIB_jPointsAreConsecutive :</span></dt><dd>0</dd><dt><span>GRIB_jScansPositively :</span></dt><dd>0</dd><dt><span>GRIB_latitudeOfFirstGridPointInDegrees :</span></dt><dd>28.0</dd><dt><span>GRIB_latitudeOfLastGridPointInDegrees :</span></dt><dd>20.0</dd><dt><span>GRIB_longitudeOfFirstGridPointInDegrees :</span></dt><dd>87.0</dd><dt><span>GRIB_longitudeOfLastGridPointInDegrees :</span></dt><dd>93.0</dd><dt><span>GRIB_missingValue :</span></dt><dd>3.4028234663852886e+38</dd><dt><span>GRIB_name :</span></dt><dd>2 metre temperature</dd><dt><span>GRIB_shortName :</span></dt><dd>2t</dd><dt><span>GRIB_totalNumber :</span></dt><dd>0</dd><dt><span>GRIB_units :</span></dt><dd>K</dd><dt><span>long_name :</span></dt><dd>Daily minimum near-surface air temperature</dd><dt><span>units :</span></dt><dd>°C</dd><dt><span>standard_name :</span></dt><dd>unknown</dd><dt><span>GRIB_surface :</span></dt><dd>0.0</dd></dl></div><div class="xr-var-data"><pre>[9340650 values with dtype=float32]</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-002d6a0c-bbe4-4860-b3d9-8a63f2336247" class="xr-section-summary-in" type="checkbox" /><label for="section-002d6a0c-bbe4-4860-b3d9-8a63f2336247" class="xr-section-summary">Indexes: <span>(3)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><ul class="xr-var-list"><li class="xr-var-item"><div class="xr-index-name"><div>latitude</div></div><div class="xr-index-preview">PandasIndex</div><input type="checkbox" disabled="" /><label></label><input id="index-04a919c5-974b-4bec-bba5-ee7f1655f040" class="xr-index-data-in" type="checkbox" /><label for="index-04a919c5-974b-4bec-bba5-ee7f1655f040" title="Show/Hide index repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-index-data"><pre>PandasIndex(Index([ 28.0, 27.75,  27.5, 27.25,  27.0, 26.75,  26.5, 26.25,  26.0, 25.75,
        25.5, 25.25,  25.0, 24.75,  24.5, 24.25,  24.0, 23.75,  23.5, 23.25,
        23.0, 22.75,  22.5, 22.25,  22.0, 21.75,  21.5, 21.25,  21.0, 20.75,
        20.5, 20.25,  20.0],
      dtype=&#x27;float64&#x27;, name=&#x27;latitude&#x27;))</pre></div></li><li class="xr-var-item"><div class="xr-index-name"><div>longitude</div></div><div class="xr-index-preview">PandasIndex</div><input type="checkbox" disabled="" /><label></label><input id="index-32ad3afe-0a96-4525-ab3b-627ddbc0e3b5" class="xr-index-data-in" type="checkbox" /><label for="index-32ad3afe-0a96-4525-ab3b-627ddbc0e3b5" title="Show/Hide index repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-index-data"><pre>PandasIndex(Index([ 87.0, 87.25,  87.5, 87.75,  88.0, 88.25,  88.5, 88.75,  89.0, 89.25,
        89.5, 89.75,  90.0, 90.25,  90.5, 90.75,  91.0, 91.25,  91.5, 91.75,
        92.0, 92.25,  92.5, 92.75,  93.0],
      dtype=&#x27;float64&#x27;, name=&#x27;longitude&#x27;))</pre></div></li><li class="xr-var-item"><div class="xr-index-name"><div>valid_time</div></div><div class="xr-index-preview">PandasIndex</div><input type="checkbox" disabled="" /><label></label><input id="index-2a184090-5f17-4dd0-ab9d-ca87c3c77fe4" class="xr-index-data-in" type="checkbox" /><label for="index-2a184090-5f17-4dd0-ab9d-ca87c3c77fe4" title="Show/Hide index repr"><svg class="icon xr-icon-database"><use xlink:href="#icon-database"></use></svg></label><div class="xr-index-data"><pre>PandasIndex(DatetimeIndex([&#x27;1989-01-01&#x27;, &#x27;1989-01-02&#x27;, &#x27;1989-01-03&#x27;, &#x27;1989-01-04&#x27;,
               &#x27;1989-01-05&#x27;, &#x27;1989-01-06&#x27;, &#x27;1989-01-07&#x27;, &#x27;1989-01-08&#x27;,
               &#x27;1989-01-09&#x27;, &#x27;1989-01-10&#x27;,
               ...
               &#x27;2019-12-22&#x27;, &#x27;2019-12-23&#x27;, &#x27;2019-12-24&#x27;, &#x27;2019-12-25&#x27;,
               &#x27;2019-12-26&#x27;, &#x27;2019-12-27&#x27;, &#x27;2019-12-28&#x27;, &#x27;2019-12-29&#x27;,
               &#x27;2019-12-30&#x27;, &#x27;2019-12-31&#x27;],
              dtype=&#x27;datetime64[ns]&#x27;, name=&#x27;valid_time&#x27;, length=11322, freq=None))</pre></div></li></ul></div></li><li class="xr-section-item"><input id="section-43f75958-f53a-4125-b5f0-df216bdc6969" class="xr-section-summary-in" type="checkbox" checked="" /><label for="section-43f75958-f53a-4125-b5f0-df216bdc6969" class="xr-section-summary">Attributes: <span>(6)</span></label><div class="xr-section-inline-details"></div><div class="xr-section-details"><dl class="xr-attrs"><dt><span>GRIB_centre :</span></dt><dd>ecmf</dd><dt><span>GRIB_centreDescription :</span></dt><dd>European Centre for Medium-Range Weather Forecasts</dd><dt><span>GRIB_subCentre :</span></dt><dd>0</dd><dt><span>Conventions :</span></dt><dd>CF-1.7</dd><dt><span>institution :</span></dt><dd>European Centre for Medium-Range Weather Forecasts</dd><dt><span>history :</span></dt><dd>2026-02-18T15:22 GRIB to CDM+CF via cfgrib-0.9.15.1/ecCodes-2.42.0 with {&quot;source&quot;: &quot;2m_temperature.grib&quot;, &quot;filter_by_keys&quot;: {&quot;stream&quot;: [&quot;oper&quot;]}, &quot;encode_cf&quot;: [&quot;parameter&quot;, &quot;time&quot;, &quot;geography&quot;, &quot;vertical&quot;]}
earthkit.transforms.aggregate.temporal.daily_reduce(2m_temperature_stream-oper, how=max, **{&#x27;time_shift&#x27;: {&#x27;hours&#x27;: 6}})</dd></dl></div></li></ul></div></div>

<h3 id="12-opening-multiple-datasets">1.2 Opening multiple datasets</h3>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Files should have compatible dimensions and coordinates
</span><span class="n">ds</span> <span class="o">=</span> <span class="n">xr</span><span class="p">.</span><span class="n">open_mfdataset</span><span class="p">(</span>
    <span class="sa">r</span><span class="s">"D:\CMIP6 Project\Thesis work\Data Analysis\Data\ERA5_Data\tasmax\era5_max*.nc"</span><span class="p">,</span>
    <span class="n">combine</span><span class="o">=</span><span class="s">"by_coords"</span>
<span class="p">)</span>
</code></pre></div></div>

<h3 id="13-opening-grib-file">1.3 Opening GRIB file</h3>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">cfgrib</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">cfgrib</span><span class="p">.</span><span class="n">open_datasets</span><span class="p">(</span><span class="n">file_location</span><span class="p">)</span>
</code></pre></div></div>
<p><strong>Note: If you face an error while importing cfgrib, use this code.</strong><br /></p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>

<span class="n">env</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">sys</span><span class="p">.</span><span class="n">prefix</span><span class="p">)</span>
<span class="n">dll_dir</span> <span class="o">=</span> <span class="n">env</span> <span class="o">/</span> <span class="s">"Library"</span> <span class="o">/</span> <span class="s">"bin"</span>

<span class="k">print</span><span class="p">(</span><span class="s">"DLL directory exists:"</span><span class="p">,</span> <span class="n">dll_dir</span><span class="p">.</span><span class="n">exists</span><span class="p">())</span>
<span class="k">print</span><span class="p">(</span><span class="s">"ecCodes DLL exists:"</span><span class="p">,</span> <span class="p">(</span><span class="n">dll_dir</span> <span class="o">/</span> <span class="s">"eccodes.dll"</span><span class="p">).</span><span class="n">exists</span><span class="p">())</span>

<span class="k">if</span> <span class="n">dll_dir</span><span class="p">.</span><span class="n">exists</span><span class="p">():</span>
    <span class="n">os</span><span class="p">.</span><span class="n">environ</span><span class="p">[</span><span class="s">"PATH"</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">dll_dir</span><span class="p">)</span> <span class="o">+</span> <span class="n">os</span><span class="p">.</span><span class="n">pathsep</span> <span class="o">+</span> <span class="n">os</span><span class="p">.</span><span class="n">environ</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="s">"PATH"</span><span class="p">,</span> <span class="s">""</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">os</span><span class="p">,</span> <span class="s">"add_dll_directory"</span><span class="p">):</span>
        <span class="n">os</span><span class="p">.</span><span class="n">add_dll_directory</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">dll_dir</span><span class="p">))</span>

<span class="kn">import</span> <span class="nn">eccodes</span>
<span class="k">print</span><span class="p">(</span><span class="s">"ecCodes version:"</span><span class="p">,</span> <span class="n">eccodes</span><span class="p">.</span><span class="n">codes_get_api_version</span><span class="p">())</span>
</code></pre></div></div>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code>
</code></pre></div></div>

<h2 id="2-inspecting-the-dataset">2. Inspecting the dataset</h2>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">print</span><span class="p">(</span><span class="n">temp_df</span><span class="p">.</span><span class="n">data_vars</span><span class="p">)</span>        <span class="c1"># available data variables
</span><span class="k">print</span><span class="p">(</span><span class="n">temp_df</span><span class="p">.</span><span class="n">coords</span><span class="p">)</span>           <span class="c1"># coordinate names and values
</span><span class="k">print</span><span class="p">(</span><span class="n">temp_df</span><span class="p">.</span><span class="n">dims</span><span class="p">)</span>             <span class="c1"># dimension sizes
</span><span class="k">print</span><span class="p">(</span><span class="n">temp_df</span><span class="p">.</span><span class="n">attrs</span><span class="p">)</span>            <span class="c1"># global metadata
</span><span class="k">print</span><span class="p">(</span><span class="n">temp_df</span><span class="p">[</span><span class="s">"tasmax"</span><span class="p">].</span><span class="n">attrs</span><span class="p">)</span>      <span class="c1"># metadata for one variable, if it is named tp
</span></code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>Data variables:
    tasmax   (valid_time, latitude, longitude) float32 37MB ...
    tasmin   (valid_time, latitude, longitude) float32 37MB ...
Coordinates:
  * latitude    (latitude) float64 264B 28.0 27.75 27.5 ... 20.5 20.25 20.0
  * longitude   (longitude) float64 200B 87.0 87.25 87.5 ... 92.5 92.75 93.0
  * valid_time  (valid_time) datetime64[ns] 91kB 1989-01-01 ... 2019-12-31
FrozenMappingWarningOnValuesAccess({'valid_time': 11322, 'latitude': 33, 'longitude': 25})
{'GRIB_centre': 'ecmf', 'GRIB_centreDescription': 'European Centre for Medium-Range Weather Forecasts', 'GRIB_subCentre': 0, 'Conventions': 'CF-1.7', 'institution': 'European Centre for Medium-Range Weather Forecasts', 'history': '2026-02-18T15:22 GRIB to CDM+CF via cfgrib-0.9.15.1/ecCodes-2.42.0 with {"source": "2m_temperature.grib", "filter_by_keys": {"stream": ["oper"]}, "encode_cf": ["parameter", "time", "geography", "vertical"]}\nearthkit.transforms.aggregate.temporal.daily_reduce(2m_temperature_stream-oper, how=max, **{\'time_shift\': {\'hours\': 6}})'}
{'GRIB_paramId': 167, 'GRIB_dataType': 'an', 'GRIB_numberOfPoints': 825, 'GRIB_typeOfLevel': 'surface', 'GRIB_stepUnits': 1, 'GRIB_stepType': 'instant', 'GRIB_gridType': 'regular_ll', 'GRIB_uvRelativeToGrid': 0, 'GRIB_NV': 0, 'GRIB_Nx': 25, 'GRIB_Ny': 33, 'GRIB_cfName': 'unknown', 'GRIB_cfVarName': 't2m', 'GRIB_gridDefinitionDescription': 'Latitude/Longitude Grid', 'GRIB_iDirectionIncrementInDegrees': 0.25, 'GRIB_iScansNegatively': 0, 'GRIB_jDirectionIncrementInDegrees': 0.25, 'GRIB_jPointsAreConsecutive': 0, 'GRIB_jScansPositively': 0, 'GRIB_latitudeOfFirstGridPointInDegrees': 28.0, 'GRIB_latitudeOfLastGridPointInDegrees': 20.0, 'GRIB_longitudeOfFirstGridPointInDegrees': 87.0, 'GRIB_longitudeOfLastGridPointInDegrees': 93.0, 'GRIB_missingValue': 3.4028234663852886e+38, 'GRIB_name': '2 metre temperature', 'GRIB_shortName': '2t', 'GRIB_totalNumber': 0, 'GRIB_units': 'K', 'long_name': 'Daily maximum near-surface air temperature', 'units': '°C', 'standard_name': 'unknown', 'GRIB_surface': 0.0}
</code></pre></div></div>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># List variable names
</span><span class="nb">list</span><span class="p">(</span><span class="n">temp_df</span><span class="p">.</span><span class="n">data_vars</span><span class="p">)</span>

<span class="c1"># Inspect a variable's dimensions, units, and missing values
</span><span class="n">var</span> <span class="o">=</span> <span class="n">temp_df</span><span class="p">[</span><span class="s">"tasmax"</span><span class="p">]</span>              <span class="c1"># replace tasmax with your variable name
</span><span class="k">print</span><span class="p">(</span><span class="n">var</span><span class="p">.</span><span class="n">dims</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">var</span><span class="p">.</span><span class="n">coords</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">var</span><span class="p">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">var</span><span class="p">.</span><span class="n">dtype</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">var</span><span class="p">.</span><span class="n">isnull</span><span class="p">().</span><span class="nb">sum</span><span class="p">().</span><span class="n">compute</span><span class="p">())</span>  <span class="c1"># compute if using Dask/lazy data
</span></code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>('valid_time', 'latitude', 'longitude')
Coordinates:
  * latitude    (latitude) float64 264B 28.0 27.75 27.5 ... 20.5 20.25 20.0
  * longitude   (longitude) float64 200B 87.0 87.25 87.5 ... 92.5 92.75 93.0
  * valid_time  (valid_time) datetime64[ns] 91kB 1989-01-01 ... 2019-12-31
(11322, 33, 25)
float32
&lt;xarray.DataArray 'tasmax' ()&gt; Size: 4B
array(0)
</code></pre></div></div>

<h2 id="3-filtering-dataset-by-time-and-location">3. Filtering dataset by time and location</h2>

<p>We use <code class="language-plaintext highlighter-rouge">.sel</code> method to filter our data based on a specific or a range of time and/or location.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># filtering by time
</span><span class="n">temp_df</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span><span class="n">valid_time</span> <span class="o">=</span> <span class="s">"2000"</span><span class="p">)</span>
<span class="n">temp_df</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span><span class="n">valid_time</span> <span class="o">=</span> <span class="s">"2000-07"</span><span class="p">)</span>

<span class="c1"># filtering by time range
</span><span class="n">temp_df</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span><span class="n">valid_time</span> <span class="o">=</span> <span class="nb">slice</span><span class="p">(</span><span class="s">"2000-01-01"</span><span class="p">,</span> <span class="s">"2000-12-31"</span><span class="p">))</span>
</code></pre></div></div>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">cell</span> <span class="o">=</span> <span class="n">temp_df</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span><span class="n">latitude</span><span class="o">=</span><span class="mf">23.8</span><span class="p">,</span> <span class="n">longitude</span><span class="o">=</span><span class="mf">90.4</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s">"nearest"</span><span class="p">)</span>
<span class="c1"># If we don't have the exact coordinates, we can use the method = "nearest" to select the nearest grid points.
</span>
<span class="c1"># Check which coordinates were selected
</span><span class="k">print</span><span class="p">(</span><span class="n">cell</span><span class="p">.</span><span class="n">latitude</span><span class="p">.</span><span class="n">values</span><span class="p">,</span> <span class="n">cell</span><span class="p">.</span><span class="n">longitude</span><span class="p">.</span><span class="n">values</span><span class="p">)</span>
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>23.75 90.5
</code></pre></div></div>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># First time step, first 10 latitude rows, first 15 longitude columns
</span><span class="n">subset</span> <span class="o">=</span> <span class="n">temp_df</span><span class="p">.</span><span class="n">isel</span><span class="p">(</span><span class="n">valid_time</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">latitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">),</span> <span class="n">longitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">15</span><span class="p">))</span>
</code></pre></div></div>

<h3 id="32-some-things-about-filter-based-on-geographic-region">3.2 Some things about filter based on geographic region</h3>

<blockquote>
  <p>The direction of latitude coordinates matters. Some datasets store latitude from north to south (descending), while others store it south to north (ascending). Check the coordinate values first.</p>
</blockquote>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">temp_df</span><span class="p">.</span><span class="n">latitude</span><span class="p">.</span><span class="n">values</span>
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([28.  , 27.75, 27.5 , 27.25, 27.  , 26.75, 26.5 , 26.25, 26.  ,
       25.75, 25.5 , 25.25, 25.  , 24.75, 24.5 , 24.25, 24.  , 23.75,
       23.5 , 23.25, 23.  , 22.75, 22.5 , 22.25, 22.  , 21.75, 21.5 ,
       21.25, 21.  , 20.75, 20.5 , 20.25, 20.  ])
</code></pre></div></div>

<p>This dataset has a descending latitude.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Example for descending latitude (common in some reanalysis files)
</span><span class="n">bd</span> <span class="o">=</span> <span class="n">temp_df</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span>
    <span class="n">latitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mf">26.7</span><span class="p">,</span> <span class="mf">20.5</span><span class="p">),</span>
    <span class="n">longitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mf">88.0</span><span class="p">,</span> <span class="mf">92.8</span><span class="p">)</span>
<span class="p">)</span>
<span class="n">bd</span><span class="p">.</span><span class="n">latitude</span><span class="p">.</span><span class="n">values</span>
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([26.5 , 26.25, 26.  , 25.75, 25.5 , 25.25, 25.  , 24.75, 24.5 ,
       24.25, 24.  , 23.75, 23.5 , 23.25, 23.  , 22.75, 22.5 , 22.25,
       22.  , 21.75, 21.5 , 21.25, 21.  , 20.75, 20.5 ])
</code></pre></div></div>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1"># Example for ascending latitude
</span><span class="n">bd</span> <span class="o">=</span> <span class="n">ds</span><span class="p">.</span><span class="n">sel</span><span class="p">(</span>
    <span class="n">latitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mf">20.5</span><span class="p">,</span> <span class="mf">26.7</span><span class="p">),</span>
    <span class="n">longitude</span><span class="o">=</span><span class="nb">slice</span><span class="p">(</span><span class="mf">88.0</span><span class="p">,</span> <span class="mf">92.8</span><span class="p">)</span>
<span class="p">)</span>

<span class="n">bd</span><span class="p">.</span><span class="n">latitude</span><span class="p">.</span><span class="n">values</span>
</code></pre></div></div>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>array([], dtype=float64)
</code></pre></div></div>

<p><strong>Tools &amp; Libraries:</strong> Python · Xarray · Jupyter Notebook · NumPy · Pandas · Matplotlib</p>]]></content><author><name>Anwoy Barua</name><email>anwoybarua@gmail.com</email></author><category term="Python" /><category term="Xarray" /><category term="Climate Data" /><category term="Data Analysis" /><summary type="html"><![CDATA[An introduction to working with multidimensional climate datasets using Python and Xarray, covering data loading, inspection, selection, and exploratory analysis.]]></summary></entry><entry><title type="html">The Impact of Climate Change on Dengue</title><link href="https://anwoybarua.github.io/posts/2024/08/climate_change_dengue/" rel="alternate" type="text/html" title="The Impact of Climate Change on Dengue" /><published>2024-08-12T00:00:00+00:00</published><updated>2024-08-12T00:00:00+00:00</updated><id>https://anwoybarua.github.io/posts/2024/08/ImpactOfClimateChangeOnDengue</id><content type="html" xml:base="https://anwoybarua.github.io/posts/2024/08/climate_change_dengue/"><![CDATA[<p style="text-align: justify;">
Dengue fever, a mosquito-borne viral infection, has become a significant public health concern in many parts of the world. According to WHO, an estimated <strong>100-400 million dengue infections occur yearly</strong> ,putting half the population at risk. Bangladesh is also susceptible to dengue due to its tropical climate. Dengue incidences have drastically increased in Bangladesh over the years, with the previous year reporting <strong>321,179 hospitalizations and 1,705 deaths </strong>, the highest in the nation's history.
</p>

<p style="text-align: justify;">
While the spread of dengue fever is influenced by numerous factors, climate change stands out as a critical element that is reshaping the dynamics of this disease. The most important climatic factors linked to dengue transmission are temperature, rainfall, and relative humidity.
</p>

<p style="text-align: justify;">
Usually at higher temperatures adult mosquitoes bite more frequently. Higher temperatures are suitable for dengue transmission as it affects the reproductive capacity of mosquitoes, stimulating egg hatching and accelerating growth of the larvae, thus decreasing the time to maturity. Warmer temperatures can extend the length of the season when mosquitoes are active, allowing a longer time period for dengue transmission.
</p>

<p style="text-align: justify;">
Mosquitoes spend their whole life cycle in water before hatching into adult mosquitoes. Increased rainfall creates new habitat for larvae, and also ensures that water remains available for mosquito breeding over an extended period, increasing the overall lifespan and activity of the mosquito population. This leads to more opportunities for mosquitoes to bite humans and transmit the dengue virus. Furthermore, heavy rainfall leads to more indoor activities where Aedes mosquitoes, which prefer indoor environments, can easily bite humans. However, excessive rainfall can sometimes have a negative impact on mosquito populations by washing away larvae from breeding sites, temporarily reducing mosquito numbers.
</p>

<p style="text-align: justify;">
A recent study shows that, when prior water availability is medium to low, heavy rainfall increases dengue risk by creating more mosquito habitat. Alongside, when prior water availability is high, heavy rainfall temporarily decreases the risk of dengue due to the flushing-out effect.
</p>

<p style="text-align: justify;">
Dengue cases were reported more during monsoon months when relative humidity was higher because the higher humidity during the rainy season facilitates the growth and survival of infected mosquitoes for the successful propagation of the virus. High humidity increases the survival, feeding behavior, and egg development of Aedes mosquitoes. Hence, high humidity contributes to dengue incidence timespan and intensity due to mosquitoes prolonged lifespan.
</p>

<p style="text-align: justify;">
Climate patterns in Bangladesh are changing dreadfully due to environmental pollution and the greenhouse effect, making the country increasingly suitable for the propagation of dengue and other vector-borne illnesses, including malaria and chikungunya.
</p>

<p style="text-align: justify;">
As the climate crisis intensifies, the risk to public health grows more urgent and severe. By implementing robust public health interventions and sustainable environmental policies, we can mitigate the impacts of climate change on vector-borne diseases. It's crucial for Bangladesh and the global community to work together towards a healthier and more resilient future.
</p>

<p><em>References:</em></p>
<ol>
  <li><a href="https://old.dghs.gov.bd/images/docs/vpr/20231011_dengue_all.pdf">https://old.dghs.gov.bd/images/docs/vpr/20231011_dengue_all.pdf</a></li>
  <li><a href="https://www.who.int/news-room/fact-sheets/detail/dengue-and-severe-dengue">https://www.who.int/news-room/fact-sheets/detail/dengue-and-severe-dengue</a></li>
  <li><a href="https://www.frontiersin.org/journals/public-health/articles/10.3389/fpubh.2022.884645/full#B7">https://www.frontiersin.org/journals/public-health/articles/10.3389/fpubh.2022.884645/full#B7</a></li>
  <li><a href="https://www.rockefellerfoundation.org/insights/perspective/the-increasing-burden-of-dengue-fever-in-a-changing-climate/#:~:text=At%20higher%20temperatures%2C%20adult%20mosquitoes,infect%20new%20hosts%20more%20quickly.">https://www.rockefellerfoundation.org/insights/perspective/the-increasing-burden-of-dengue-fever-in-a-changing-climate/#:~:text=At%20higher%20temperatures%2C%20adult%20mosquitoes,infect%20new%20hosts%20more%20quickly.</a></li>
  <li><a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10491345/">https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10491345/</a></li>
  <li><a href="https://www.sciencedirect.com/science/article/pii/S2468042723000428">https://www.sciencedirect.com/science/article/pii/S2468042723000428</a></li>
  <li><a href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10192530/">https://www.ncbi.nlm.nih.gov/pmc/articles/PMC10192530/</a></li>
</ol>]]></content><author><name>Anwoy Barua</name><email>anwoybarua@gmail.com</email></author><category term="Climate Change" /><category term="Dengue" /></entry><entry><title type="html">𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐅𝐞𝐚𝐭𝐮𝐫𝐞 𝐒𝐞𝐥𝐞𝐜𝐭𝐢𝐨𝐧 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞𝐬</title><link href="https://anwoybarua.github.io/posts/2024/06/feature_selection/" rel="alternate" type="text/html" title="𝐈𝐦𝐩𝐨𝐫𝐭𝐚𝐧𝐭 𝐅𝐞𝐚𝐭𝐮𝐫𝐞 𝐒𝐞𝐥𝐞𝐜𝐭𝐢𝐨𝐧 𝐓𝐞𝐜𝐡𝐧𝐢𝐪𝐮𝐞𝐬" /><published>2024-06-25T00:00:00+00:00</published><updated>2024-06-25T00:00:00+00:00</updated><id>https://anwoybarua.github.io/posts/2024/06/feature-selection-ML</id><content type="html" xml:base="https://anwoybarua.github.io/posts/2024/06/feature_selection/"><![CDATA[<p>In a Machine Learning problem, not all features may contribute to the final prediction. Sometimes, reducing the number of features can improve a model’s performance or interpretability. Regardless of the reason, it’s often beneficial to remove irrelevant or unwanted features.</p>

<p>Generally, 𝐭𝐡𝐫𝐞𝐞 methods are widely used for feature selection:</p>
<ul>
  <li>𝐅𝐢𝐥𝐭𝐞𝐫 𝐛𝐚𝐬𝐞𝐝 𝐦𝐞𝐭𝐡𝐨𝐝𝐬</li>
  <li>𝐖𝐫𝐚𝐩𝐩𝐞𝐫 𝐦𝐞𝐭𝐡𝐨𝐝𝐬</li>
  <li>𝐄𝐦𝐛𝐞𝐝𝐝𝐞𝐝 𝐦𝐞𝐭𝐡𝐨𝐝𝐬
In today’s post we will be going through the first one - 𝐅𝐢𝐥𝐭𝐞𝐫 𝐛𝐚𝐬𝐞𝐝 𝐦𝐞𝐭𝐡𝐨𝐝𝐬.
For python implementation of these methods visit my <a href="https://www.kaggle.com/code/anwoybarua/all-feature-selection-techniques">kaggle notebook</a>.</li>
</ul>

<p>𝐅𝐢𝐥𝐭𝐞𝐫 𝐛𝐚𝐬𝐞𝐝 𝐦𝐞𝐭𝐡𝐨𝐝:
	It’s usually best to begin your feature selection using a univariate method, as these are easy to implement and time-efficient.</p>

<ol>
  <li>
    <p>𝐃𝐞𝐥𝐞𝐭𝐢𝐧𝐠 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞 𝐜𝐨𝐥𝐮𝐦𝐧𝐬: Your dataset may contain duplicate columns, possibly with different column names. It’s always a good idea to identify and remove these duplicates.</p>
  </li>
  <li>
    <p>𝐕𝐚𝐫𝐢𝐚𝐧𝐜𝐞 𝐓𝐡𝐫𝐞𝐬𝐡𝐨𝐥𝐝: Columns with zero or low variability usually contribute little to changes in the target variable (y). Thus, you may want to remove them. You can do this manually, or use the dedicated function in scikit-learn for this task. One major challenge with this method is selecting the right threshold. It often requires personal experience and trial and error to determine the optimal threshold for your data.</p>
  </li>
  <li>
    <p>𝐂𝐨𝐫𝐫𝐞𝐥𝐚𝐭𝐢𝐨𝐧: The Pearson correlation coefficient is utilized to filter out features. Features with high correlation are considered to contribute equally to the final prediction, so it doesn’t hurt to remove one of them.</p>
  </li>
  <li>
    <p>𝐀𝐍𝐎𝐕𝐀: This method examines the relationship between each individual feature and the target variable (categorical). It determines the strength of the relationship using the p-value (from the F-statistic) for each feature, and retains the k best features. Scikit-learn provides a dedicated function called SelectKBest that can be used with f_classif or f_regression to select the k most important features. Finding the optimal value for k may require some trial and error or hyperparameter tuning.</p>
  </li>
  <li>
    <p>𝐂𝐡𝐢-𝐬𝐪𝐮𝐚𝐫𝐞: The chi-square test can be used to study the dependency between categorical features and a categorical target variable, retaining those that show significant dependency.</p>
  </li>
</ol>

<p>While these filter-based methods are a good starting point for feature selection, they have some major flaws:
    - Being a univariate method, they don’t consider the interaction between features and target variables.
    - There is a chance that we may delete features bearing low importance individually, but having significant impact when combined with other features.</p>

<p>On my next post I will be going through the wrapper method in details.</p>]]></content><author><name>Anwoy Barua</name><email>anwoybarua@gmail.com</email></author><category term="ML" /><category term="Feature Selection" /><summary type="html"><![CDATA[This post dives into the filter based feature selection methods in Machine Learning.]]></summary></entry></feed>