### Build and Install netCDF4-Python from Source Source: https://github.com/unidata/netcdf4-python/blob/master/README.md Steps for a development installation, including cloning the repository, ensuring dependencies are met, building the package, and performing a local installation. ```shell python setup.py build ``` ```shell pip install -e . ``` -------------------------------- ### Introduction to netcdf4-python Source: https://github.com/unidata/netcdf4-python/blob/master/examples/README.md This script provides basic code examples from the introduction section of the documentation. It's a good starting point for understanding fundamental operations. ```python import netCDF4 import numpy # Create a new NetCDF file with netCDF4.Dataset('test.nc', 'w', format='NETCDF4') as ncfile: # Create dimensions ncfile.createDimension('x', 10) ncfile.createDimension('y', None) # Unlimited dimension # Create variables var = ncfile.createVariable('temperature', 'f4', ('x', 'y')) # Write data var[:] = numpy.random.rand(10, 5) # Add attributes ncfile.description = 'Example NetCDF file' var.units = 'Kelvin' # Read data from the NetCDF file with netCDF4.Dataset('test.nc', 'r') as ncfile: print(ncfile.description) temp_data = ncfile.variables['temperature'] print(temp_data.units) print(temp_data.shape) print(temp_data[0, :]) ``` -------------------------------- ### Master File Specification Example Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Example showing how to specify a master file for global metadata and structure, which can be useful if the first file differs from others. ```APIDOC ## Usage: Master File Specification ### Description This pattern demonstrates using a specific file to define the global metadata and structure for the MFDataset, ensuring consistency. ### Code Example ```python # Use specific file for global metadata and structure # Useful if first file differs from others mf = MFDataset('data_*.nc', master_file='data_00.nc') # Global attributes come from data_00.nc title = mf.getncattr('title') # Dimensions/variables aggregated from all files data = mf.variables['temperature'][:] ``` ``` -------------------------------- ### Install netCDF4-Python with Pip Source: https://github.com/unidata/netcdf4-python/blob/master/README.md Use this command to install the netCDF4-Python library via pip. ```shell pip install netCDF4 ``` -------------------------------- ### Spatial Tiling Example Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Example showing how MFDataset can be used to combine spatial tiles of a dataset, aggregating data across different geographical regions. ```APIDOC ## Usage: Spatial Tiling ### Description This pattern illustrates combining data from files that represent spatial tiles of a larger dataset, aggregating across these tiles. ### Code Example ```python # Files represent tiles of a global dataset # Each covers different lat/lon regions tile_files = [] for lat_idx in range(4): for lon_idx in range(4): tile_files.append(f'tile_{lat_idx}_{lon_idx}.nc') # If latitude is the aggregation dimension mf = MFDataset(tile_files, aggdim='latitude') data = mf.variables['data'][:] # Aggregated across spatial tiles ``` ``` -------------------------------- ### Ensemble Model Output Example Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Example illustrating the use of MFDataset for aggregating output from multiple ensemble members of a model simulation. ```APIDOC ## Usage: Ensemble Model Output ### Description This pattern demonstrates aggregating data from different ensemble members, where each file represents one member's output. ### Code Example ```python # Each file is output from one ensemble member files = [f'ensemble_member_{i:02d}.nc' for i in range(100)] mf = MFDataset(files, check=True) # Data dimension combines all ensemble members all_forecasts = mf.variables['temperature'][:] print(all_forecasts.shape) # Example output: (100, ntimes, nlats, nlons) # Access metadata print(mf.getncattr('model_version')) print(mf.getncattr('run_date')) ``` ``` -------------------------------- ### Get and Set Runtime Configuration Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/configuration.md Use `rc_get` to retrieve a configuration value and `rc_set` to change it. Values are typically strings, even for numeric settings. ```python from netCDF4 import rc_get, rc_set # Get configuration val = rc_get('NC_BLKSIZE') # Set configuration rc_set('NC_BLKSIZE', '262144') # 256KB ``` -------------------------------- ### Install netCDF4-Python with Conda Source: https://github.com/unidata/netcdf4-python/blob/master/README.md Use this command to install the netCDF4-Python library via conda from the conda-forge channel. ```shell conda install -c conda-forge netCDF4 ``` -------------------------------- ### Create and Read Multi-File Dataset with MFDataset Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md This example demonstrates how to create 12 monthly NetCDF files with identical structures and then read them collectively using MFDataset. It shows setting up dimensions, variables, and writing sample data before opening and querying the combined dataset. ```python from netCDF4 import Dataset import numpy as np # Create 12 monthly files with same structure for month in range(1, 13): filename = f'climate_{month:02d}.nc' ds = Dataset(filename, 'w', format='NETCDF4') # Unlimited time dimension (size 1 per file) ds.createDimension('time', 1) ds.createDimension('lat', 180) ds.createDimension('lon', 360) # Create variables time_var = ds.createVariable('time', 'f8', ('time',)) time_var.units = 'days since 2024-01-01' temp_var = ds.createVariable('temperature', 'f4', ('time', 'lat', 'lon')) temp_var.units = 'K' # Write data time_var[0] = month * 30 # Approx days temp_var[0, :, :] = np.random.random((180, 360)) * 50 + 273.15 ds.close() # Now read with MFDataset mf = MFDataset('climate_*.nc') print(mf.dimensions['time'].size) # 12 annual_temp = mf.variables['temperature'][:] # (12, 180, 360) mf.close() ``` -------------------------------- ### Time Series Concatenation Example Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Example demonstrating how to use MFDataset to concatenate time-series data from multiple files, where each file contains monthly data. ```APIDOC ## Usage: Time Series Concatenation ### Description This pattern shows how to aggregate monthly data files into a single dataset representing a full year. ### Code Example ```python from netCDF4 import MFDataset import numpy as np # Files contain monthly data # Each has dimensions: time (1), lat (180), lon (360) mf = MFDataset('climate_*.nc', aggdim='time') # Aggregated dataset has time dimension = 12 months print(mf.dimensions['time'].size) # Output: 12 # Read full annual data temp = mf.variables['temperature'][:] # Shape: (12, 180, 360) precip = mf.variables['precipitation'][:] mf.close() ``` ``` -------------------------------- ### Excluding Variables Example Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Example demonstrating how to exclude specific variables from the aggregation process using the `exclude` parameter. ```APIDOC ## Usage: Excluding Variables ### Description This pattern shows how to prevent certain variables from being included in the aggregated MFDataset. ### Code Example ```python # If some variables shouldn't be concatenated mf = MFDataset('output_*.nc', exclude=['metadata', 'version']) # 'metadata' and 'version' not included in virtual dataset vars = mf.variables.keys() # This will not include 'metadata' and 'version' ``` ``` -------------------------------- ### Getting Variable Attributes Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Illustrates how to retrieve attribute values using `getncattr()` or simplified dot notation. ```python units = var.getncattr('units') # Or simplified: units = var.units ``` -------------------------------- ### Writing NetCDF with IPython Notebook Source: https://github.com/unidata/netcdf4-python/blob/master/examples/README.md An IPython notebook example demonstrating how to write NetCDF files. This is suitable for creating or modifying NetCDF datasets interactively within a Jupyter environment. ```python # This is a placeholder for the content of writing_netcdf.ipynb # Actual code would be within the notebook cells. print("Writing NetCDF data using IPython notebook...") ``` -------------------------------- ### Configure Compression Settings Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/configuration.md Create NetCDF4 files with optimized compression settings. This example uses zlib compression with a compression level of 5, shuffle filter enabled, and specifies chunk sizes for better performance and compression ratios. ```python ds = Dataset('compressed.nc', 'w', format='NETCDF4') # Good compression + performance var = ds.createVariable( 'data', 'f4', ('x', 'y', 'z'), compression='zlib', complevel=5, shuffle=True, chunksizes=[100, 100, 100] ) ``` -------------------------------- ### Reading NetCDF with IPython Notebook Source: https://github.com/unidata/netcdf4-python/blob/master/examples/README.md An IPython notebook example demonstrating how to read NetCDF files. This is suitable for interactive data exploration and analysis within a Jupyter environment. ```python # This is a placeholder for the content of reading_netcdf.ipynb # Actual code would be within the notebook cells. print("Reading NetCDF data using IPython notebook...") ``` -------------------------------- ### Set HDF5 Plugin Path for Compression Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/configuration.md Set the HDF5_PLUGIN_PATH environment variable if compression plugins are installed in a non-standard location. This is necessary for source builds. ```bash export HDF5_PLUGIN_PATH=/usr/local/hdf5/plugins ``` -------------------------------- ### Create Variable with Specific Chunk Sizes Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/README.md Improve read/write performance by defining appropriate chunk sizes that match expected data access patterns. This example shows how to set chunk sizes when creating a variable. ```python # Match expected access patterns var = ds.createVariable('data', 'f4', ('time', 'y', 'x'), chunksizes=[10, 180, 360]) ``` -------------------------------- ### Manage Multiple Models with Weak References Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/configuration.md When dealing with a large number of groups, such as in multi-model datasets, use weak references to manage memory efficiently. This example demonstrates creating many groups and accessing them by path. ```python # Use weak references for many groups ds = Dataset('multimodel.nc', 'w', keepweakref=True) # Access groups/variables by path for i in range(1000): ds.createGroup(f'model_{i}') ``` -------------------------------- ### Get Variable Filters and Compression Settings Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Retrieve the active filters and compression settings for a variable. This includes zlib, szip, zstd, bzip2, blosc, shuffle, complevel, and fletcher32. ```python var = ds.createVariable('data', 'f4', ('x',), zlib=True, complevel=5, shuffle=True) filters = var.filters() # {'zlib': True, 'szip': False, 'zstd': False, 'bzip2': False, 'blosc': False, # 'shuffle': True, 'complevel': 5, 'fletcher32': False} ``` -------------------------------- ### Creating and Organizing Groups and Variables in NetCDF4 Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/group.md This example demonstrates how to create nested groups, dimensions, and variables within a NetCDF4 file using the netCDF4-python library. It shows how to access groups and variables using path-like strings. ```python from netCDF4 import Dataset # Create root dataset ds = Dataset('hierarchical.nc', 'w', format='NETCDF4') # Create groups model_grp = ds.createGroup('model') forecast_grp = model_grp.createGroup('forecast') analysis_grp = model_grp.createGroup('analysis') # Add dimensions to groups ds.createDimension('time', None) ds.createDimension('level', 10) model_grp.createDimension('x', 100) model_grp.createDimension('y', 100) # Create variables in groups temp = ds.createVariable('global_temp', 'f4', ('time', 'level')) model_temp = model_grp.createVariable('model_temp', 'f4', ('time', 'x', 'y')) forecast_temp = forecast_grp.createVariable('temp_fcst', 'f4', ('time', 'x', 'y')) # Access groups via paths forecast = ds['/model/forecast'] var = ds['/model/forecast/temp_fcst'] ds.close() ``` -------------------------------- ### Create and Read NetCDF File with Dataset Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Demonstrates creating a new netCDF4 file, defining dimensions, adding global attributes, and then reading the file back. Also shows how to use the Dataset as a context manager. ```python from netCDF4 import Dataset # Create a new netCDF4 file ds = Dataset('example.nc', 'w', format='NETCDF4') # Add dimensions time_dim = ds.createDimension('time', None) # unlimited lat_dim = ds.createDimension('lat', 180) lon_dim = ds.createDimension('lon', 360) # Add global attributes ds.title = 'Temperature Data' ds.history = 'Created with netCDF4-python' ds.close() # Read existing file ds = Dataset('example.nc', 'r') print(ds.dimensions) print(ds.variables) ds.close() # Use as context manager with Dataset('example.nc', 'r') as ds: temp = ds.variables['temp'][:] ``` -------------------------------- ### Create and Configure netCDF Variables Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Demonstrates creating basic, compressed, and quantized variables within a netCDF file. Includes setting variable attributes and writing initial data. ```python ds = Dataset('climate.nc', 'w', format='NETCDF4') ds.createDimension('time', None) # Unlimited ds.createDimension('x', 360) ds.createDimension('y', 180) # Create basic variable temp = ds.createVariable('temperature', 'f4', ('time', 'y', 'x')) # Create variable with compression pressure = ds.createVariable('pressure', 'f4', ('time', 'y', 'x'), zlib=True, complevel=5, shuffle=True) # Create with quantization for better compression data = ds.createVariable('data', 'f8', ('time', 'y', 'x'), zlib=True, least_significant_digit=3) # Add attributes temp.units = 'K' temp.long_name = 'Surface Temperature' temp.missing_value = -999.0 # Write data import numpy as np temp[0, :, :] = np.random.random((180, 360)) * 30 + 273.15 ds.close() ``` -------------------------------- ### Create a NetCDF4 Dataset Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/README.md Demonstrates how to create a new NETCDF4 file, define dimensions (including unlimited), create variables with compression, and write data and attributes. ```python from netCDF4 import Dataset import numpy as np # Create new file ds = Dataset('example.nc', 'w', format='NETCDF4') # Create dimensions time_dim = ds.createDimension('time', None) # unlimited lat_dim = ds.createDimension('lat', 180) lon_dim = ds.createDimension('lon', 360) # Create variables times = ds.createVariable('time', 'f8', ('time',)) temps = ds.createVariable('temperature', 'f4', ('time', 'lat', 'lon'), zlib=True, complevel=5) # Add attributes ds.title = 'Global Temperature Data' temps.units = 'K' temps.long_name = 'Surface Temperature' # Write data times[:] = np.arange(10) temps[0, :, :] = np.random.random((180, 360)) * 30 + 273.15 ds.close() ``` -------------------------------- ### Create and Read MFDataset with MFTime Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Demonstrates creating two netCDF files, writing time-varying data, closing them, and then reading them together using MFDataset. It also shows how to use MFTime to create a consistent time axis across the files and access data. ```python import numpy as np f1 = Dataset("mftest_1.nc","w", format="NETCDF4_CLASSIC") f2 = Dataset("mftest_2.nc","w", format="NETCDF4_CLASSIC") f1.createDimension("time",None) f2.createDimension("time",None) t1 = f1.createVariable("time","i",("time",)) t2 = f2.createVariable("time","i",("time",)) t1.units = "days since 2000-01-01" t2.units = "days since 2000-02-01" t1.calendar = "standard" t2.calendar = "standard" t1[:] = np.arange(31) t2[:] = np.arange(30) f1.close() f2.close() # Read the two files in at once, in one Dataset. f = MFDataset("mftest_*nc") t = f.variables["time"] print(t.units) print(t[32]) # The value written in the file, inconsistent with the MF time units. 1 T = MFTime(t) print(T[32]) 32 ``` -------------------------------- ### Create and Read Multi-File NetCDF Datasets Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Demonstrates how to create a series of netCDF files with a shared unlimited dimension and then read them collectively using MFDataset. Ensure files are in NETCDF4_CLASSIC format. ```python import numpy as np from netCDF4 import Dataset, MFDataset # create a series of netCDF files with a variable sharing # the same unlimited dimension. for nf in range(10): with Dataset("mftest%s.nc" % nf, "w", format='NETCDF4_CLASSIC') as f: f.createDimension("x",None) x = f.createVariable("x","i",("x",)) x[0:10] = np.arange(nf*10,10*(nf+1)) # now read all those files in at once, in one Dataset. f = MFDataset("mftest*nc") print(f.variables["x"][:]) ``` -------------------------------- ### Create Variables with Compression and Quantization Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Illustrates creating NetCDF variables with different data types, dimensions, and applying compression (zlib) and quantization options. ```python ds = Dataset('data.nc', 'w', format='NETCDF4') ds.createDimension('time', None) ds.createDimension('x', 100) # Create float variable with gzip compression temp = ds.createVariable('temperature', 'f4', ('time', 'x'), zlib=True, complevel=4) # Create integer variable with quantization pressure = ds.createVariable('pressure', 'i2', ('time', 'x'), least_significant_digit=2) ``` -------------------------------- ### netCDF4.Dataset.getncattr Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Gets a global or variable attribute. ```APIDOC ## netCDF4.Dataset.getncattr ### Description Gets a global or variable attribute. ### Method `getncattr(attribute_name)` ### Parameters * **attribute_name** (str) - The name of the attribute to retrieve. ``` -------------------------------- ### netCDF4.rc_get Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Gets a runtime configuration value. ```APIDOC ## netCDF4.rc_get ### Description Gets a runtime configuration value. ### Method `rc_get(key)` ### Parameters * **key** (str) - The configuration key. ``` -------------------------------- ### netCDF4.get_alignment Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Gets the current memory alignment. ```APIDOC ## netCDF4.get_alignment ### Description Gets the current memory alignment. ### Method `get_alignment()` ``` -------------------------------- ### Create and Open a netCDF File Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Use the Dataset constructor to create a new netCDF file or open an existing one. Specify the file format (default is NETCDF4) and access mode. The data_model attribute shows the file format. ```python from netCDF4 import Dataset rootgrp = Dataset("test.nc", "w", format="NETCDF4") print(rootgrp.data_model) rootgrp.close() ``` -------------------------------- ### netCDF4.get_chunk_cache Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Gets the current chunk cache settings. ```APIDOC ## netCDF4.get_chunk_cache ### Description Gets the current chunk cache settings. ### Method `get_chunk_cache()` ### Returns A tuple containing (size, nelems, preemption). ``` -------------------------------- ### getncattr Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Get the value of a specific variable attribute. ```APIDOC ## getncattr(name: str, encoding: str = 'utf-8') -> Any ### Description Get attribute value. ### Method getncattr ### Parameters #### Path Parameters - **name** (str) - The name of the attribute to retrieve. - **encoding** (str, optional) - The encoding to use for string attributes. Defaults to 'utf-8'. ### Request Example ```python units = var.getncattr('units') # Or simplified: units = var.units ``` ### Response #### Success Response (200) - **Any**: The value of the requested attribute. ``` -------------------------------- ### Synchronize and Write Data Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Shows how to open a dataset in read-write mode, modify a variable, and then synchronize the changes to disk before closing. ```python ds = Dataset('data.nc', 'r+') ds.variables['temp'][0] = 100.0 ds.sync() # Ensure data written ds.close() ``` -------------------------------- ### ncattrs Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Get a list of all attribute names associated with the variable. ```APIDOC ## ncattrs() -> list[str] ### Description Get list of attribute names. ### Method ncattrs ### Request Example ```python var = ds.variables['temperature'] attrs = var.ncattrs() # ['units', 'long_name', 'missing_value', ...] ``` ### Response #### Success Response (200) - **list[str]**: A list of attribute names. ``` -------------------------------- ### getValue Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Get the entire variable data as a NumPy array or masked array. ```APIDOC ## getValue() -> ndarray | ma.MaskedArray ### Description Get entire variable data as array. ### Method getValue ### Request Example ```python ds = Dataset('data.nc', 'r') var = ds.variables['temperature'] all_data = var.getValue() # Equivalent to: all_data = var[:] ``` ### Response #### Success Response (200) - **ndarray | ma.MaskedArray**: The entire variable data. ``` -------------------------------- ### MFDataset Initialization Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mfdataset.md Instantiate MFDataset by providing a file pattern or a list of files. Optional arguments like `aggdim`, `check`, `exclude`, and `master_file` can be used to customize the aggregation behavior. ```APIDOC ## MFDataset(filenames, aggdim=None, check=False, exclude=None, master_file=None) ### Description Initializes an MFDataset object to aggregate multiple NetCDF files. ### Parameters - **filenames** (str or list[str]) - A file pattern (e.g., 'data_*.nc') or a list of NetCDF file paths to aggregate. - **aggdim** (str, optional) - The name of the dimension along which to aggregate the files. Defaults to None. - **check** (bool, optional) - If True, checks for consistency across files. Defaults to False. - **exclude** (list[str], optional) - A list of variable names to exclude from the aggregation. Defaults to None. - **master_file** (str, optional) - Specifies a master file to use for global attributes and structure. Defaults to None. ``` -------------------------------- ### Data Alignment Operations Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/INDEX.md Functions to get and set HDF5 data alignment settings. ```APIDOC ## get_alignment() ### Description Get HDF5 data alignment. ### Parameters (Details not provided in source) ### Returns (Details not provided in source) ``` ```APIDOC ## set_alignment() ### Description Set HDF5 data alignment. ### Parameters (Details not provided in source) ### Returns (Details not provided in source) ``` -------------------------------- ### Create Variable with GZIP Compression Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/README.md Use `zlib=True` and `complevel` to enable gzip compression. `complevel` ranges from 0 to 9. ```python # Method 1: gzip (zlib) var = ds.createVariable('data', 'f4', dimensions, zlib=True, complevel=4) ``` -------------------------------- ### Get Variable Dimensions and Shape Source: https://github.com/unidata/netcdf4-python/blob/master/examples/reading_netCDF.ipynb Retrieve the dimensions and shape attributes for a given NetCDF variable. ```python temp.dimensions ``` ```python temp.shape ``` -------------------------------- ### Create Variable with SZIP Compression Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/README.md Enable SZIP compression using `compression='szip'` and specify `szip_coding`. 'nn' is for nearest neighbor, 'ps' for pixel-seed. ```python # Method 2: SZIP var = ds.createVariable('data', 'f4', dimensions, compression='szip', szip_coding='nn') ``` -------------------------------- ### Get Parent Group of a Variable Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Obtain the Dataset object representing the parent group to which the variable belongs. ```python grp = var.group() ``` -------------------------------- ### Get Variable Fill Value Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Retrieve the fill value associated with a variable, which is used for missing data. ```python fill = var.get_fill_value() ``` -------------------------------- ### Compare MFTime with num2date Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mftime.md Illustrates the equivalence between using MFTime for datetime conversion and the lower-level num2date function, highlighting MFTime's convenience for multi-file datasets. ```python from netCDF4 import MFDataset, MFTime, num2date mf = MFDataset('data_*.nc') time_var = mf.variables['time'] # Using MFTime (simpler) times1 = MFTime(time_var) dates1 = times1[:] # Using num2date (more control) times2 = num2date(time_var[:], units=time_var.units, calendar=time_var.calendar) # Results equivalent assert all(times1[:] == times2) ``` -------------------------------- ### MFDataset.__init__() Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Initializes an MFDataset instance to open a Dataset spanning multiple files, aggregating variables that share a common dimension. ```APIDOC ## MFDataset.__init__ ### Description Open a Dataset spanning multiple files, making it look as if it was a single file. Variables in the list of files that share the same dimension (specified with the keyword `aggdim`) are aggregated. If `aggdim` is not specified, the unlimited is aggregated. Currently, `aggdim` must be the leftmost (slowest varying) dimension of each of the variables to be aggregated. ### Parameters * **files** (sequence or string) - Either a sequence of netCDF files or a string with a wildcard (converted to a sorted list of files using glob). If the `master_file` kwarg is not specified, the first file in the list will become the "master" file, defining all the variables with an aggregation dimension which may span subsequent files. Attribute access returns attributes only from "master" file. The files are always opened in read-only mode. * **check** (bool) - True if you want to do consistency checking to ensure the correct variables structure for all of the netCDF files. Checking makes the initialization of the MFDataset instance much slower. Default is False. * **aggdim** (string or None) - The name of the dimension to aggregate over (must be the leftmost dimension of each of the variables to be aggregated). If None (default), aggregate over the unlimited dimension. * **exclude** (list) - A list of variable names to exclude from aggregation. Default is an empty list. * **master_file** (string) - File to use as "master file", defining all the variables with an aggregation dimension and all global attributes. ### Method `__init__(self, files, check=False, aggdim=None, exclude=[], master_file=None)` ``` -------------------------------- ### Get Filepath Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Retrieves the full file path of the opened NetCDF dataset. An optional encoding can be specified. ```python ds = Dataset('data.nc', 'r') print(ds.filepath()) # Full path to the file print(ds.filepath(encoding='utf-8')) # With explicit encoding ds.close() ``` -------------------------------- ### Get Variable Attributes Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Retrieve a list of attribute names for a netCDF variable. Use this to inspect available metadata. ```python nc.Variable.ncattrs() ``` -------------------------------- ### Create and Use VLType for Strings and Integer Arrays Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/data-types.md Demonstrates creating variable-length string and integer array types using `createVLType` and assigning data to variables of these types. This is useful for storing data where elements have varying lengths. ```python from netCDF4 import Dataset ds = Dataset('vlen.nc', 'w') # Create variable-length string type str_vl = ds.createVLType(str, 'str_vl') # Create variable-length integer array type int_vl = ds.createVLType('i4', 'int_vl') # Create variable with VL type ds.createDimension('records', 100) str_var = ds.createVariable('strings', str_vl, ('records',)) int_var = ds.createVariable('int_arrays', int_vl, ('records',)) # Write variable-length data str_var[0] = 'short' str_var[1] = 'a much longer string' str_var[2] = 'medium' # Integer arrays of different lengths int_var[0] = [1, 2, 3] int_var[1] = [10, 20, 30, 40, 50] int_var[2] = [100] ds.close() ``` -------------------------------- ### Create Diskless (In-Memory) NetCDF Dataset Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Illustrates creating a netCDF Dataset that resides entirely in memory. It shows how to persist the in-memory dataset to disk upon closing and how to create a dataset from an existing Python memory buffer. ```python # create a diskless (in-memory) Dataset, # and persist the file to disk when it is closed. nc = Dataset('diskless_example.nc','w',diskless=True,persist=True) d = nc.createDimension('x',None) v = nc.createVariable('v',np.int32,'x') v[0:5] = np.arange(5) print(nc) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x(5) variables(dimensions): int32 v(x) groups: print(nc['v'][:]) [0 1 2 3 4] nc.close() # file saved to disk ``` ```python # create an in-memory dataset from an existing python # python memory buffer. # read the newly created netcdf file into a python # bytes object. with open('diskless_example.nc', 'rb') as f: nc_bytes = f.read() # create a netCDF in-memory dataset from the bytes object. nc = Dataset('inmemory.nc', memory=nc_bytes) print(nc) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x(5) variables(dimensions): int32 v(x) groups: print(nc['v'][:]) [0 1 2 3 4] nc.close() ``` ```python # create an in-memory Dataset and retrieve memory buffer # estimated size is 1028 bytes - this is actually only # used if format is NETCDF3 # (ignored for NETCDF4/HDF5 files). nc = Dataset('inmemory.nc', mode='w',memory=1028) d = nc.createDimension('x',None) v = nc.createVariable('v',np.int32,'x') v[0:5] = np.arange(5) nc_buf = nc.close() # close returns memoryview print(type(nc_buf)) # save nc_buf to disk, read it back in and check. with open('inmemory.nc', 'wb') as f: f.write(nc_buf) nc = Dataset('inmemory.nc') print(nc) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x(5) variables(dimensions): int32 v(x) groups: print(nc['v'][:]) [0 1 2 3 4] nc.close() ``` -------------------------------- ### Get Global Attribute Names in NetCDF4-Python Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Retrieve a list of all global attribute names using the `ncattrs` method. ```python ds = Dataset('data.nc', 'r') attrs = ds.ncattrs() # ['title', 'history', 'source', ...] ``` -------------------------------- ### Time Series Analysis with MFTime Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/mftime.md Demonstrates how to extract all dates using MFTime and then filter data based on specific months for time series analysis. ```python from netCDF4 import MFDataset, MFTime import numpy as np # Multi-file monthly data spanning multiple years mf = MFDataset('data_*.nc') times = MFTime(mf.variables['time']) # Get all dates all_dates = times[:] # Find data for specific month march_indices = [i for i, d in enumerate(all_dates) if d.month == 3] march_data = mf.variables['temperature'][march_indices, :, :] mf.close() ``` -------------------------------- ### Get Dimension Size Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dimension.md Retrieves the current size of a dimension. This can be done using the .size property or the len() function. ```python ds = Dataset('data.nc', 'r') time_dim = ds.dimensions['time'] num_times = time_dim.size # Equivalent to: len(time_dim) ``` ```python time_dim = ds.dimensions['time'] num_times = len(time_dim) # Equivalent to: time_dim.size ``` -------------------------------- ### Run netCDF4-Python Tests Source: https://github.com/unidata/netcdf4-python/blob/master/README.md Execute this command from the 'test' directory to run all the tests for netCDF4-Python. ```shell cd test && python run_all.py ``` -------------------------------- ### Get NetCDF Global Attribute Names Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Returns a list of netCDF global attribute names for the current Dataset or Group. ```python nc.ncattrs() ``` -------------------------------- ### Setting Variable Attributes Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Demonstrates setting individual variable attributes using `setncattr()` and simplified dot notation, including various data types. ```python var.setncattr('units', 'Kelvin') var.setncattr('long_name', 'Surface Air Temperature') var.setncattr('valid_range', [250.0, 330.0]) # Simplified attribute access var.units = 'K' var.standard_name = 'air_temperature' ``` -------------------------------- ### __len__() Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Get the length of the first dimension of a NetCDF variable. This is equivalent to accessing the first element of the variable's shape. ```APIDOC ## __len__() -> int ### Description Get the length of the first dimension of a NetCDF variable. ### Usage ```python var = ds.variables['time'] ntimes = len(var) # Equivalent to var.shape[0] ``` ``` -------------------------------- ### Compression Fallback in netCDF4-Python Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/errors.md Demonstrates how to create a variable with a preferred compression method (e.g., 'zstd') and fall back to 'zlib' or no compression if the preferred method is unavailable due to `NetCDF4MissingFeatureException`. It also shows setting compression level. ```python from netCDF4 import Dataset, NetCDF4MissingFeatureException def create_compressed_variable(ds, name, datatype, dimensions, preferred_compression='zstd'): """Create variable with fallback compression""" try: var = ds.createVariable(name, datatype, dimensions, compression=preferred_compression, complevel=5) print(f"Using {preferred_compression} compression") except NetCDF4MissingFeatureException: print(f"{preferred_compression} not available, using zlib") try: var = ds.createVariable(name, datatype, dimensions, zlib=True, complevel=4) except NetCDF4MissingFeatureException: print("No compression available") var = ds.createVariable(name, datatype, dimensions) return var ``` -------------------------------- ### Get HDF5 Data Alignment Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/functions.md Retrieve the current HDF5 data alignment settings, which include the threshold and alignment boundary. ```python from netCDF4 import get_alignment threshold, alignment = get_alignment() print(f"Threshold: {threshold} bytes, Alignment: {alignment} bytes") ``` -------------------------------- ### Getting Variable Attribute Names Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Shows how to retrieve a list of all attribute names associated with a variable using the `ncattrs()` method. ```python var = ds.variables['temperature'] attrs = var.ncattrs() # ['units', 'long_name', 'missing_value', ...] ``` -------------------------------- ### Create Variable with Zlib Compression and Least Significant Digit Quantization Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html This snippet demonstrates creating a variable with zlib compression and quantizing the data to a specific least significant digit. This can significantly improve compression for data with limited precision. ```python temp = rootgrp.createVariable("temp","f4",("time","level","lat","lon",),compression='zlib',least_significant_digit=3) ``` -------------------------------- ### Attribute Management Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dataset.md Functions for managing global attributes of a NetCDF file, including getting, setting, deleting, and renaming attributes. ```APIDOC ## ncattrs() ### Description Get list of global attribute names. ### Method ncattrs ### Request Example ```python ds = Dataset('data.nc', 'r') attrs = ds.ncattrs() # ['title', 'history', 'source', ...] ``` ## setncattr name: str, value ### Description Set a global attribute. ### Method setncattr ### Parameters #### Path Parameters - **name** (str) - Required - The name of the attribute. - **value** - Required - The value of the attribute. ### Request Example ```python ds = Dataset('data.nc', 'w') ds.setncattr('title', 'Global Climate Model Output') ds.setncattr('version', 1) ``` ## setncattr_string name: str, value ### Description Set a global string attribute (as NC_STRING in NETCDF4, NC_CHAR in classic formats). ### Method setncattr_string ### Parameters #### Path Parameters - **name** (str) - Required - The name of the attribute. - **value** - Required - The string value of the attribute. ### Request Example ```python ds = Dataset('data.nc', 'w', format='NETCDF4') ds.setncattr_string('title', 'Model Output') # Stored as NC_STRING ``` ## setncatts attdict: Mapping[str, Any] ### Description Set multiple global attributes from dictionary. ### Method setncatts ### Parameters #### Path Parameters - **attdict** (Mapping[str, Any]) - Required - A dictionary where keys are attribute names and values are attribute values. ### Request Example ```python ds = Dataset('data.nc', 'w') attrs = {'title': 'Climate Data', 'version': 2, 'source': 'Model X'} ds.setncatts(attrs) ``` ## getncattr name: str, encoding: str = 'utf-8' ### Description Get global attribute value. ### Method getncattr ### Parameters #### Path Parameters - **name** (str) - Required - The name of the attribute to retrieve. - **encoding** (str) - Optional - The encoding to use for string attributes. Defaults to 'utf-8'. ### Request Example ```python ds = Dataset('data.nc', 'r') title = ds.getncattr('title') # Or simpler attribute access title = ds.title ``` ## delncattr name: str ### Description Delete a global attribute. ### Method delncattr ### Parameters #### Path Parameters - **name** (str) - Required - The name of the attribute to delete. ### Request Example ```python ds = Dataset('data.nc', 'r+') ds.delncattr('obsolete_attr') ``` ## renameAttribute oldname: str, newname: str ### Description Rename a global attribute. ### Method renameAttribute ### Parameters #### Path Parameters - **oldname** (str) - Required - The current name of the attribute. - **newname** (str) - Required - The new name for the attribute. ### Request Example ```python ds = Dataset('data.nc', 'r+') ds.renameAttribute('old_name', 'new_name') ``` ``` -------------------------------- ### Manage Attributes in Groups Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/group.md Illustrates how to set and retrieve attributes for a group, similar to how attributes are managed for a Dataset object. ```python grp = ds.createGroup('metadata') # Set group attributes (same as Dataset) grp.title = 'Model Output' grp.source = 'Weather Model' grp.description = 'Forecast data' # Get group attributes attrs = grp.ncattrs() ``` -------------------------------- ### Get Variable Byte Order (Endianness) Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Determine the byte order of the variable data. Possible values are 'native', 'little', or 'big'. ```python endian = var.endian() ``` -------------------------------- ### Create Coordinate Variable and Data Variable Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dimension.md Shows how to create a NetCDF file with a dimension and a corresponding coordinate variable. It also demonstrates creating a data variable that uses this dimension and assigning units and axis attributes. ```python ds = Dataset('coords.nc', 'w') # Create dimension ds.createDimension('time', 12) # Create coordinate variable (same name as dimension) time_coord = ds.createVariable('time', 'f8', ('time',)) time_coord.units = 'hours since 2024-01-01' time_coord.calendar = 'gregorian' time_coord.axis = 'T' # Data variable using time dimension import numpy as np data_var = ds.createVariable('temperature', 'f4', ('time',)) data_var.long_name = 'Monthly Temperature' data_var.units = 'K' # Assign coordinate values time_coord[:] = np.arange(12) * 730 # Monthly intervals # Dimension is tied to coordinate variable print(ds.dimensions['time'].size) # 12 print(len(time_coord)) # 12 ``` -------------------------------- ### Get Variable Chunking Scheme Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Obtain the chunking scheme for a variable. This can be 'contiguous' for non-chunked data or a list of chunk sizes for each dimension. ```python var = ds.createVariable('data', 'f4', ('time', 'x', 'y'), chunksizes=[10, 180, 360]) chunks = var.chunking() # [10, 180, 360] var2 = ds.createVariable('data2', 'f4', ('time', 'x', 'y'), contiguous=True) chunks2 = var2.chunking() # 'contiguous' ``` -------------------------------- ### Apply Scaling and Masking to Variables Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Demonstrates how to apply scaling and offset to stored integer data, allowing it to be read back as floating-point values. A missing value can also be specified. ```python ds = Dataset('scaled.nc', 'w') ds.createDimension('x', 1000) # Store scaled integers, retrieve as floats var = ds.createVariable('data', 'i2', ('x',)) var.scale_factor = 0.01 var.add_offset = 273.15 var.missing_value = -9999 # Write floating-point data var[:] = np.random.random(1000) * 100 + 273.15 # Read back as floats data = var[:] # Automatically scaled ``` -------------------------------- ### Retrieving Entire Variable Data Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/variable.md Demonstrates how to get all data from a variable as a NumPy array using the `getValue()` method or slice assignment. ```python ds = Dataset('data.nc', 'r') var = ds.variables['temperature'] all_data = var.getValue() # Equivalent to: all_data = var[:] ``` -------------------------------- ### Create a netCDF4 File Source: https://github.com/unidata/netcdf4-python/blob/master/examples/writing_netCDF.ipynb This snippet demonstrates how to create a new netCDF file with the format set to 'NETCDF4', enabling advanced features like groups and compression. ```python ncfile = netCDF4.Dataset('data/new2.nc','w',format='NETCDF4') print(ncfile) ``` -------------------------------- ### netCDF4.Group.__init__ Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Constructor for Group. It is recommended to use Dataset.createGroup() or Group.createGroup() instead. ```APIDOC ## netCDF4.Group.__init__ (Not Recommended for Direct Use) ### Description Initializes a Group. This constructor is intended for internal use; users should typically use the `Dataset.createGroup()` or `Group.createGroup()` methods. ### Method `__init__(self, parent, name)` ### Parameters * **parent** (`netCDF4.Group` or `netCDF4.Dataset`) - The parent group instance. Use a `Dataset` instance for the root group. * **name** (string) - The name of the group. ``` -------------------------------- ### Get netCDF-C Library Version Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/functions.md Retrieve the version string of the underlying netCDF-C library. This is useful for compatibility checks or informational purposes. ```python from netCDF4 import getlibversion version = getlibversion() print(version) # e.g., '4.9.2' ``` -------------------------------- ### Get Parent Group of a Dimension Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/dimension.md Retrieves the parent group to which a dimension belongs. This is useful for understanding the hierarchical structure of the netCDF file. ```python dim = ds.dimensions['time'] parent = dim.group() ``` -------------------------------- ### Variable.get_var_chunk_cache Source: https://github.com/unidata/netcdf4-python/blob/master/_autodocs/api-reference/functions.md Gets the chunk cache settings for a specific NetCDF variable. This method returns the cache size, number of elements, and preemption policy. ```APIDOC ## Variable.get_var_chunk_cache() ### Description See Variable documentation for per-variable chunk cache methods. ### Returns - tuple[int, int, float]: Tuple of (size, nelems, preemption) ### Example ```python # Assuming 'ds' is an opened netCDF4 Dataset object var = ds.variables['temperature'] size, nelems, preemption = var.get_var_chunk_cache() ``` ``` -------------------------------- ### Create multi-file netCDF dataset for reading Source: https://github.com/unidata/netcdf4-python/blob/master/docs/index.html Create multiple netCDF files with a shared unlimited dimension using NETCDF4_CLASSIC format. These files can then be read together using MFDataset. ```python import numpy as np from netCDF4 import Dataset for nf in range(10): with Dataset("mftest%s.nc" % nf, "w", format="NETCDF4_CLASSIC") as f: _ = f.createDimension("x",None) x = f.createVariable("x","i",("x",)) x[0:10] = np.arange(nf*10,10*(nf+1)) ```