Skip to content

isimip_utils.netcdf

Functions to open and read NetCDF files using netCDF4.

open_dataset(file_path, mode='r')

Open a NetCDF dataset (just a wrapper for netcdf.Dataset).

Parameters:

Name Type Description Default
file_path str | Path

Path to the NetCDF file.

required
mode str
'r'

Returns: NetCDF4 Dataset object opened in the selected mode.

open_dataset_read(file_path)

Open a NetCDF dataset in read-only mode.

Parameters:

Name Type Description Default
file_path str | Path

Path to the NetCDF file.

required

Returns:

Type Description
Dataset

NetCDF4 Dataset object opened in read mode.

open_dataset_write(file_path)

Open a NetCDF dataset in read/write mode.

Parameters:

Name Type Description Default
file_path str | Path

Path to the NetCDF file.

required

Returns:

Type Description
Dataset

NetCDF4 Dataset object opened in read/write mode.

init_dataset(file_path, diskless=False, overwrite=False, lon=720, lat=360, time=None, time_unit='days since 1601-1-1 00:00:00', time_calendar='proleptic_gregorian', attrs=None, **variables)

Initialize a new NetCDF4 dataset with standard dimensions and variables.

Parameters:

Name Type Description Default
file_path str | Path

Path where the NetCDF file will be created.

required
diskless bool

If True, create dataset in memory (default: False).

False
overwrite bool

If True, overwrite existing dataset (default: False).

False
lon int

Number of longitude points (default: 720).

720
lat int

Number of latitude points (default: 360).

360
time ndarray

Time dimension configuration (default: None).

None
time_unit str

Units for the time dimension (default: 'days since 1601-1-1 00:00:00').

'days since 1601-1-1 00:00:00'
time_calendar str

Calendar type for time dimension (default: 'proleptic_gregorian').

'proleptic_gregorian'
attrs dict

Dictionary of attributes for variables and global attributes.

None
**variables Any

Data variables to create in the dataset.

{}

Returns:

Type Description
Dataset

Initialized NetCDF4 Dataset object.

get_data_model(dataset)

Get the data model of a NetCDF dataset.

Parameters:

Name Type Description Default
dataset Dataset

NetCDF4 Dataset object.

required

Returns:

Type Description
str

String representing the data model (e.g., 'NETCDF4', 'NETCDF4_CLASSIC').

get_dimensions(dataset)

Get dimensions from a NetCDF dataset.

Parameters:

Name Type Description Default
dataset Dataset

NetCDF4 Dataset object.

required

Returns:

Type Description
dict[str, int]

Dictionary mapping dimension names to their sizes.

get_variables(dataset, convert=False)

Get variables and their attributes from a NetCDF dataset.

Parameters:

Name Type Description Default
dataset Dataset

NetCDF4 Dataset object.

required
convert bool

If True, convert numpy types to Python types (default: False).

False

Returns:

Type Description
dict[str, Any]

Dictionary mapping variable names to their attributes and dimensions.

get_global_attributes(dataset, convert=False)

Get global attributes from a NetCDF dataset.

Parameters:

Name Type Description Default
dataset Dataset

NetCDF4 Dataset object.

required
convert bool

If True, convert numpy types to Python types (default: False).

False

Returns:

Type Description
dict[str, Any]

Dictionary of global attributes.

convert_attribute(value)

Convert numpy types to Python native types.

Parameters:

Name Type Description Default
value Any

Value to convert (may be numpy array, float, int, or other type).

required

Returns:

Type Description
Any

Converted value with Python native types.

update_global_attributes(dataset, set_attributes=None, delete_attributes=None)

Update global attributes of a NetCDF dataset.

Parameters:

Name Type Description Default
dataset Dataset

NetCDF4 Dataset object.

required
set_attributes dict

Dictionary of attributes to set or update.

None
delete_attributes list

List of attribute names to delete.

None

value2string(value)

Convert a value to string representation.

Parameters:

Name Type Description Default
value Any

Value to convert. Datetime objects get ISO format with 'Z' suffix.

required

Returns:

Type Description
str

String representation of the value.