{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Read MODIS HDF4 as xarray" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "from osgeo import gdal\n", "xr.set_options(display_style='html')\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Find full subdataset names" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def get_subdataset(file, varnames):\n", " g = gdal.Open(path)\n", " subdatasets = g.GetSubDatasets()\n", " l = []\n", " for varname in varnames:\n", " l.extend( [s[0] for s in subdatasets if varname in s[0].split(':')[-1] ])\n", " return l\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read subdatasets in xarray" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def read_hdf4(file, varnames):\n", " ld = []\n", " fname_list = get_subdataset(file, varnames)\n", " for fname in fname_list:\n", " myDataset = xr.open_rasterio(fname)\n", " ld.append(myDataset.to_dataset(name=fname.split(':')[-1]))\n", " return xr.merge(ld)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "varnames = ['cloud_top_temperature_1km', 'Cloud_Effective_Radius', 'Cloud_Optical_Thickness', 'Cloud_Multi_Layer_Flag']" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/opt/conda/envs/pangeo/lib/python3.8/site-packages/rasterio/__init__.py:221: NotGeoreferencedWarning: Dataset has no geotransform set. The identity matrix may be returned.\n", " s = DatasetReader(path, driver=driver, sharing=sharing, **kwargs)\n" ] } ], "source": [ "path = 'MYD06_L2.A2019001.1850.061.2019002154022.hdf'\n", "dset = read_hdf4(path, varnames)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (band: 1, x: 1354, y: 2030)\n", "Coordinates:\n", " * band (band) int64 1\n", " * y (y) float64 0.5 1.5 ... 2.03e+03\n", " * x (x) float64 0.5 1.5 ... 1.354e+03\n", "Data variables:\n", " cloud_top_temperature_1km (band, y, x) int16 ...\n", " Cloud_Effective_Radius (band, y, x) int16 ...\n", " Cloud_Effective_Radius_PCL (band, y, x) int16 ...\n", " Cloud_Effective_Radius_16 (band, y, x) int16 ...\n", " Cloud_Effective_Radius_16_PCL (band, y, x) int16 ...\n", " Cloud_Effective_Radius_37 (band, y, x) int16 ...\n", " Cloud_Effective_Radius_37_PCL (band, y, x) int16 ...\n", " Cloud_Effective_Radius_1621 (band, y, x) int16 ...\n", " Cloud_Effective_Radius_1621_PCL (band, y, x) int16 ...\n", " Cloud_Effective_Radius_Uncertainty (band, y, x) int16 ...\n", " Cloud_Effective_Radius_Uncertainty_16 (band, y, x) int16 ...\n", " Cloud_Effective_Radius_Uncertainty_37 (band, y, x) int16 ...\n", " Cloud_Effective_Radius_Uncertainty_1621 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_PCL (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_16 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_16_PCL (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_37 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_37_PCL (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_1621 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_1621_PCL (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_Uncertainty (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_Uncertainty_16 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_Uncertainty_37 (band, y, x) int16 ...\n", " Cloud_Optical_Thickness_Uncertainty_1621 (band, y, x) int16 ...\n", " Cloud_Multi_Layer_Flag (band, y, x) uint8 ...
array([1])
array([5.0000e-01, 1.5000e+00, 2.5000e+00, ..., 2.0275e+03, 2.0285e+03,\n", " 2.0295e+03])
array([5.0000e-01, 1.5000e+00, 2.5000e+00, ..., 1.3515e+03, 1.3525e+03,\n", " 1.3535e+03])
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=int16]
[2748620 values with dtype=uint8]