Skip to content

Instantly share code, notes, and snippets.

@robbibt
Last active May 28, 2022 12:33
Show Gist options
  • Select an option

  • Save robbibt/a1ea066d18ac26be0cec32b2eb78e5f7 to your computer and use it in GitHub Desktop.

Select an option

Save robbibt/a1ea066d18ac26be0cec32b2eb78e5f7 to your computer and use it in GitHub Desktop.
Load multiple netCDF files into a single xarray dataset, using data from global file attributes to populate a new dimension (e.g. time)
import glob
import xarray as xr
from datetime import datetime
# List all matching files
files = glob.glob('/g/data/r78/mc9153/tide_otps/L3_2008_nc3/*.L3m')
# Create list for
individual_files = []
# Loop through each file in the list
for i in files:
# Load a single dataset
timestep_ds = xr.open_dataset(i)
# Create a new variable called 'time' from the `time_coverage_start` field, and
# convert the string to a datetime object so xarray knows it is time data
timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
"%Y-%m-%dT%H:%M:%S.%fZ")
# Add the dataset to the list
individual_files.append(timestep_ds)
# Combine individual datasets into a single xarray along the 'time' dimension
modis_ds = xr.concat(individual_files, dim='time')
print(modis_ds)
@Anielal
Copy link
Copy Markdown

Anielal commented May 28, 2022

Hi I tried your code but getting the following error. Could you please help me:

Traceback (most recent call last):
  File "/home/hp/Documents/pycharm/programs/fog_fraction_01.py", line 22, in <module>
    timestep_ds['time'] = datetime.strptime(timestep_ds.time_coverage_start,
  File "/usr/lib/python3/dist-packages/xarray/core/common.py", line 232, in __getattr__
    raise AttributeError(
AttributeError: 'Dataset' object has no attribute 'time_coverage_start'

My dataset looks like:

<xarray.Dataset>
Dimensions:    (latitude: 180, longitude: 360)
Coordinates:
  * latitude   (latitude) float64 -89.5 -88.5 -87.5 -86.5 ... 87.5 88.5 89.5
  * longitude  (longitude) float64 -179.5 -178.5 -177.5 ... 177.5 178.5 179.5
Data variables:
    *empty*
Attributes:
    YAML_config:                       grid_settings:\n  gridsize: 1\n  proje...
    Yori_version:                      1.3.16
    daily_defn_of_day_adjustment:      False
    input_files:                       MCD06COSP_G3_MODIS_Aqua.A2012337.0000....
    history:                           
    source:                            idl 8.4, mcd06cosp_preyori 20191204-1,...
    date_created:                      2020-06-26T07:21:38Z
    product_name:                      MCD06COSP_D3_MODIS.A2012337.061.202017...
    LocalGranuleID:                    MCD06COSP_D3_MODIS.A2012337.061.202017...
    Conventions:                       CF-1.6, ACDD-1.3
    ShortName:                         MCD06COSP_D3_MODIS
    product_version:                   6.1.2
    AlgorithmType:                     OPS
    identifier_product_doi:            10.5067/MODIS/MCD06COSP_D3_MODIS.061
    identifier_product_doi_authority:  http://dx.doi.org
    ancillary_files:                   
    DataCenterId:                      UWI-MAD/SSEC/ASIPS
    project:                           NASA VIIRS Atmosphere SIPS
    creator_name:                      NASA VIIRS Atmosphere SIPS
    creator_url:                       https://sips.ssec.wisc.edu
    creator_email:                     sips.support@ssec.wisc.edu
    creator_institution:               Space Science & Engineering Center, Un...
    publisher_name:                    LAADS
    publisher_url:                     https://ladsweb.modaps.eosdis.nasa.gov/
    publisher_email:                   modis-ops@lists.nasa.gov
    publisher_institution:             NASA Level-1 and Atmosphere Archive & ...
    time_coverage_start:               2012-12-02T00:00:00.000000
    time_coverage_end:                 2012-12-02T23:59:59.000000
    xmlmetadata:                       <?xml version="1.0"?>\n<!DOCTYPE Granu...
    platform:                          Aqua, Terra
    instrument:                        MODIS
    processing_level:                  L3
    format:                            NetCDF4
    title:                             Aqua/Terra MODIS Cloud Properties Leve...
    long_name:                         MODIS (Aqua/Terra) Cloud Properties Le...
    version_id:                        061
    geospatial_lat_max:                90.0
    geospatial_lat_min:                -90.0
    geospatial_lon_min:                180.0
    geospatial_lon_max:                -180.0
    NorthBoundingCoordinate:           90.0
    SouthBoundingCoordinate:           -90.0
    EastBoundingCoordinate:            180.0
    WestBoundingCoordinate:            -180.0
    latitude_resolution:               1.0
    longitude_resolution:              1.0
    license:                           http://science.nasa.gov/earth-science/...
    stdname_vocabulary:                NetCDF Climate and Forecast (CF) Metad...
    keywords_vocabulary:               NASA Global Change Master Directory (G...
    keywords:                          EARTH SCIENCE > ATMOSPHERE > CLOUDS > ...
    naming_authority:                  gov.nasa.gsfc.sci.atmos

I have 90 files in my folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment