{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# xncml usage\n", "\n", "xncml serves two purposes: modifying NcML files, and opening NcML files as an `xarray.Dataset`. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "is_executing": true }, "outputs": [], "source": [ "import xncml\n", "from pathlib import Path\n", "from IPython.display import Code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modify an NcML document\n", "\n", "``xncml`` can add or remove global and variable attributes, and remove variables and dimensions. It can also be used to create NcML files from scratch. This is all done using the `xncml.Dataset` class and its methods.\n", "\n", "### Create an Ncml Dataset object from a local NcML file\n", "\n", "The `xncml.Dataset` class is instantiated by passing the NcML file location. Alternatively, the class can be created using its `from_text` classmethod." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="T" shape="time lat lon" type="double">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="nc/example1.nc"></netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t\t<remove name="units" type="attribute"></remove>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t\t<remove name="units" type="attribute"></remove>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"\t<remove name="title" type="attribute"></remove>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<attribute name="Conventions" type="String" value="CF-2.0"></attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t\t<remove name="units" type="attribute"></remove>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"\t<remove name="title" type="attribute"></remove>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<attribute name="Conventions" type="String" value="CF-2.0"></attribute>\n",
"\t<attribute name="source">\n",
"\t\t<orgName>Source</orgName>\n",
"\t</attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="C"></attribute>\n",
"\t\t<remove name="units" type="attribute"></remove>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"\t<remove name="title" type="attribute"></remove>\n",
"</netcdf>\n",
"<?xml version="1.0" encoding="utf-8"?>\n",
"<netcdf location="nc/example1.nc" xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2">\n",
"\t<dimension name="time" length="2" isUnlimited="true"></dimension>\n",
"\t<dimension name="lat" length="3"></dimension>\n",
"\t<dimension name="lon" length="4"></dimension>\n",
"\t<attribute name="title" type="String" value="Example Data"></attribute>\n",
"\t<attribute name="Conventions" type="String" value="CF-2.0"></attribute>\n",
"\t<attribute name="source">\n",
"\t\t<orgName>Source</orgName>\n",
"\t</attribute>\n",
"\t<variable name="rh" shape="time lat lon" type="int">\n",
"\t\t<attribute name="long_name" type="String" value="relative humidity"></attribute>\n",
"\t\t<attribute name="units" type="String" value="percent"></attribute>\n",
"\t</variable>\n",
"\t<variable name="Temp" shape="time lat lon" type="double" orgName="T">\n",
"\t\t<attribute name="long_name" type="String" value="surface temperature"></attribute>\n",
"\t\t<attribute name="units" type="String" value="Kelvin"></attribute>\n",
"\t\t<attribute name="Fill_value" type="String" value="-999999999.0"></attribute>\n",
"\t\t<remove name="units" type="attribute"></remove>\n",
"\t</variable>\n",
"\t<variable name="lat" shape="lat" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_north"></attribute>\n",
"\t\t<values>41.0 40.0 39.0</values>\n",
"\t</variable>\n",
"\t<variable name="lon" shape="lon" type="float">\n",
"\t\t<attribute name="units" type="String" value="degrees_east"></attribute>\n",
"\t\t<values>-109.0 -107.0 -105.0 -103.0</values>\n",
"\t</variable>\n",
"\t<variable name="time" shape="time" type="int">\n",
"\t\t<attribute name="units" type="String" value="hours"></attribute>\n",
"\t\t<values>6 18</values>\n",
"\t</variable>\n",
"\t<remove name="Temp" type="variable"></remove>\n",
"\t<remove name="title" type="attribute"></remove>\n",
"</netcdf>\n",
"<xarray.Dataset>\n",
"Dimensions: (time: 2, lat: 3, lon: 4)\n",
"Coordinates:\n",
" * lat (lat) float32 41.0 40.0 39.0\n",
" * lon (lon) float32 -109.0 -107.0 -105.0 -103.0\n",
" * time (time) int32 6 18\n",
"Data variables:\n",
" rh (time, lat, lon) int32 1 2 3 4 5 6 7 8 ... 25 26 27 28 29 30 31 32\n",
" T (time, lat, lon) float64 1.0 2.0 3.0 4.0 2.0 ... 7.5 15.0 22.5 30.0\n",
"Attributes:\n",
" title: Example Data