Last active
May 28, 2022 12:33
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I tried your code but getting the following error. Could you please help me:
My dataset looks like:
I have 90 files in my folder