### Initialize MPI and netCDF4 Dataset Source: https://unidata.github.io/netcdf4-python Setup MPI communicator and import necessary libraries for parallel IO operations. ```python >>> from mpi4py import MPI >>> import numpy as np >>> from netCDF4 import Dataset >>> rank = MPI.COMM_WORLD.rank # The process ID (integer 0-3 for 4-process run) ``` -------------------------------- ### Configuration Functions Source: https://unidata.github.io/netcdf4-python Functions to get and set internal NetCDF-C runtime configuration values. ```APIDOC ## GET rc_get ### Description Returns the internal netcdf-c rc table value corresponding to key. See https://docs.unidata.ucar.edu/netcdf-c/current/md_auth.html for more information on rc files and values. ### Method GET ### Endpoint /rc_get ### Parameters #### Query Parameters - **key** (string) - Required - The key to retrieve the rc table value for. ### Response #### Success Response (200) - **value** (string) - The rc table value associated with the key. ### Response Example { "value": "some_rc_value" } ## POST rc_set ### Description Sets the internal netcdf-c rc table value corresponding to key. See https://docs.unidata.ucar.edu/netcdf-c/current/md_auth.html for more information on rc files and values. ### Method POST ### Endpoint /rc_set ### Parameters #### Request Body - **key** (string) - Required - The key to set the rc table value for. - **value** (string) - Required - The value to set for the key. ### Request Example { "key": "some_key", "value": "some_value" } ### Response #### Success Response (200) - **message** (string) - Confirmation message. ### Response Example { "message": "rc value set successfully" } ``` -------------------------------- ### Create Diskless NetCDF Dataset and Persist to Disk Source: https://unidata.github.io/netcdf4-python Use `diskless=True` to create an in-memory dataset. Set `persist=True` to save the dataset to a file when `close()` is called. This example demonstrates creating a dataset, adding dimensions and variables, writing data, and then closing it to persist the file. ```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 ``` -------------------------------- ### Inspect dimension properties Source: https://unidata.github.io/netcdf4-python Use len() to get the current size and isunlimited() to check if a dimension is appendable. ```python >>> print(len(lon)) 144 >>> print(lon.isunlimited()) False >>> print(time.isunlimited()) True ``` -------------------------------- ### netCDF Variable Fancy Indexing Example Source: https://unidata.github.io/netcdf4-python Illustrates advanced slicing (fancy indexing) on a netCDF variable using boolean and integer sequence indexing. This powerful feature allows complex data extraction based on logical conditions. ```python tempdat = temp[::2, [1,3,6], lats>0, lons>0] print("shape of fancy temp slice = {}".format(tempdat.shape)) ``` -------------------------------- ### Variable Endianness and Quantization Source: https://unidata.github.io/netcdf4-python Methods to get the endian-ness of a variable and information about its quantization. ```APIDOC ## endian(self) ### Description Return endian-ness (`little,big,native`) of variable (as stored in HDF5 file). ### Method `endian` ### Parameters None ## quantization(self) ### Description Return number of significant digits and the algorithm used in quantization. Returns None if quantization not active. ### Method `quantization` ### Parameters None ``` -------------------------------- ### Create and Read Multi-file NetCDF Dataset Source: https://unidata.github.io/netcdf4-python Demonstrates creating a series of NetCDF files and then reading them together using MFDataset. Ensure files are in a compatible format like NETCDF4_CLASSIC. ```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 and Open a NetCDF File Source: https://unidata.github.io/netcdf4-python Use the `Dataset` constructor to create a new netCDF file or open an existing one. Specify the file format using the `format` keyword argument. The default format is `NETCDF4`. Close the file using the `close()` method. ```python from netCDF4 import Dataset rootgrp = Dataset("test.nc", "w", format="NETCDF4") print(rootgrp.data_model) rootgrp.close() ``` -------------------------------- ### Fill Value and Chunk Cache Source: https://unidata.github.io/netcdf4-python Methods to get the fill value of a variable and its chunk cache information. ```APIDOC ## get_fill_value(self) ### Description Return the fill value associated with this `Variable` (returns `None` if data is not pre-filled). Works even if default fill value was used, and `_FillValue` attribute does not exist. ### Method `get_fill_value` ### Parameters None ## get_var_chunk_cache(self) ### Description Return variable chunk cache information in a tuple (size,nelems,preemption). See netcdf C library documentation for `nc_get_var_chunk_cache` for details. ### Method `get_var_chunk_cache` ### Parameters None ``` -------------------------------- ### Get MFDataset Global Attributes Source: https://unidata.github.io/netcdf4-python Retrieves the names of all global attributes present in the master file of the MFDataset. ```python f.ncattrs() ``` -------------------------------- ### Create Nested Groups Source: https://unidata.github.io/netcdf4-python Shows how to create nested groups using unix-like path strings. ```python >>> fcstgrp1 = rootgrp.createGroup("/forecasts/model1") >>> fcstgrp2 = rootgrp.createGroup("/forecasts/model2") ``` -------------------------------- ### Create and Use MFTime for Consistent Time Units Source: https://unidata.github.io/netcdf4-python Demonstrates creating two NetCDF files with different time units and calendars, then reading them with MFDataset and applying MFTime to harmonize the time variable. MFTime ensures a unique common time unit and/or calendar across all files. ```python import numpy as np from netCDF4 import Dataset, MFDataset, MFTime 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. T = MFTime(t) print(T[32]) ``` -------------------------------- ### Create and Access Groups Source: https://unidata.github.io/netcdf4-python Demonstrates creating top-level groups within a Dataset and accessing them via the groups attribute. ```python >>> rootgrp = Dataset("test.nc", "a") >>> fcstgrp = rootgrp.createGroup("forecasts") >>> analgrp = rootgrp.createGroup("analyses") >>> print(rootgrp.groups) {'forecasts': group /forecasts: dimensions(sizes): variables(dimensions): groups: , 'analyses': group /analyses: dimensions(sizes): variables(dimensions): groups: } ``` -------------------------------- ### Initialize MFDataset Source: https://unidata.github.io/netcdf4-python Opens a Dataset spanning multiple files, making them appear as a single file. Aggregates variables sharing a common dimension, defaulting to the unlimited dimension if `aggdim` is not specified. The first file in the list acts as the master file. ```python f = MFDataset("mftest*nc") ``` -------------------------------- ### Extract Data from Scalar netCDF Variables Source: https://unidata.github.io/netcdf4-python Shows how to extract data from a scalar netCDF variable. Use `numpy.asarray(v)` or `v[...]` to get a NumPy scalar array. ```python # To extract data from a scalar variable v with no associated dimensions, # use numpy.asarray(v) or v[...]. The result will be a numpy scalar array. ``` -------------------------------- ### Populate and inspect a vlen variable Source: https://unidata.github.io/netcdf4-python Fill a vlen variable using a NumPy object array containing 1-D arrays, then inspect the dataset structure. ```python >>> import random >>> random.seed(54321) >>> data = np.empty(len(y)*len(x),object) >>> for n in range(len(y)*len(x)): ... data[n] = np.arange(random.randint(1,10),dtype="int32")+1 >>> data = np.reshape(data,(len(y),len(x))) >>> vlvar[:] = data >>> print("vlen variable = {}".format(vlvar[:])) vlen variable = [[array([1, 2, 3, 4, 5, 6, 7, 8], dtype=int32) array([1, 2], dtype=int32) array([1, 2, 3, 4], dtype=int32)] [array([1, 2, 3], dtype=int32) array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32) array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)] [array([1, 2, 3, 4, 5, 6, 7], dtype=int32) array([1, 2, 3], dtype=int32) array([1, 2, 3, 4, 5, 6], dtype=int32)] [array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32) array([1, 2, 3, 4, 5], dtype=int32) array([1, 2], dtype=int32)]] >>> print(f) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x(3), y(4) variables(dimensions): int32 phony_vlen_var(y,x) groups: >>> print(f.variables["phony_vlen_var"]) vlen phony_vlen_var(y, x) vlen data type: int32 unlimited dimensions: current shape = (4, 3) >>> print(f.vltypes["phony_vlen"]) : name = 'phony_vlen', numpy dtype = int32 ``` -------------------------------- ### Retrieve Global Attribute Names Source: https://unidata.github.io/netcdf4-python Use the `ncattrs()` method on a Dataset, Group, or Variable instance to get a list of all attribute names. This is a convenient way to avoid private methods. ```python for name in rootgrp.ncattrs(): print("Global attr {} = {}".format(name, getattr(rootgrp, name))) ``` -------------------------------- ### Sync Method Source: https://unidata.github.io/netcdf4-python Synchronizes dataset changes to disk. ```APIDOC ## POST /sync ### Description Synchronizes all in-memory changes to the netCDF file on disk. This is crucial to ensure data persistence, especially after making modifications. ### Method POST ### Endpoint /sync ### Parameters None ### Request Example (No request body needed) ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Dataset synchronized to disk successfully." } ``` -------------------------------- ### Populate and inspect a vlen string variable Source: https://unidata.github.io/netcdf4-python Assign an object array of strings to a vlen string variable and verify the dataset structure. ```python >>> chars = "1234567890aabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" >>> data = np.empty(10,"O") >>> for n in range(10): ... stringlen = random.randint(2,12) ... data[n] = "".join([random.choice(chars) for i in range(stringlen)]) >>> strvar[:] = data >>> print("variable-length string variable: {}".format(strvar[:])) variable-length string variable: ['Lh' '25F8wBbMI' '53rmM' 'vvjnb3t63ao' 'qjRBQk6w' 'aJh' 'QF' 'jtIJbJACaQk4' '3Z5' 'bftIIq'] >>> print(f) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x(3), y(4), z(10) variables(dimensions): int32 phony_vlen_var(y,x), strvar(z) groups: >>> print(f.variables["strvar"]) vlen strvar(z) vlen data type: unlimited dimensions: current shape = (10,) ``` -------------------------------- ### Create multi-file netCDF dataset Source: https://unidata.github.io/netcdf4-python This code snippet demonstrates how to create multiple netCDF files, each containing a variable with an unlimited dimension. These files can later be read together using MFDataset. ```python >>> 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)) ``` -------------------------------- ### Variable Creation Options Source: https://unidata.github.io/netcdf4-python This section details the various optional keyword arguments that can be used when creating variables in a netCDF4 file, affecting compression, storage, and data integrity. ```APIDOC ## Variable Creation Options This section details the various optional keyword arguments that can be used when creating variables in a netCDF4 file, affecting compression, storage, and data integrity. ### Compression - **`compression`** (string) - Optional - Specifies the compression algorithm. Supported values include `zlib`, `szip`, `zstd`, `bzip2`, `blosc_lz`, `blosc_lz4`, `blosc_lz4hc`, `blosc_zstd`. Default is `None` (no compression). - **`zlib`** (boolean) - Deprecated in favor of `compression='zlib'`. If `True`, enables zlib compression. Default is `False`. - **`complevel`** (integer) - Optional - Compression level (0-9). Default is 4. Ignored if `compression=None`. - **`shuffle`** (boolean) - Optional - Enables HDF5 shuffle filter before zlib compression. Default is `True`. Ignored if `zlib=False`. - **`blosc_shuffle`** (integer) - Optional - Shuffle mode for Blosc compressor: 0 (no shuffle), 1 (byte-wise), 2 (bit-wise). Default is 1. Ignored unless Blosc is used. - **`szip_coding`** (string) - Optional - Szip coding method: `ec` (entropy coding) or `nn` (nearest neighbor). Default is `nn`. Ignored unless szip is used. - **`szip_pixels_per_block`** (integer) - Optional - Szip pixels per block. Possible values: 4, 8, 16, 32. Default is 8. Ignored unless szip is used. ### Data Integrity and Storage - **`fletcher32`** (boolean) - Optional - Activates Fletcher32 HDF5 checksum algorithm for error detection. Default is `False`. - **`contiguous`** (boolean) - Optional - Stores variable data contiguously on disk. Default is `False`. Cannot be set if a variable has an unlimited dimension. Fixed-size variables without compression are contiguous by default. - **`chunksizes`** (tuple of integers) - Optional - Manually specifies HDF5 chunk sizes for each dimension. Cannot be set if `contiguous=True`. - **`endian`** (string) - Optional - Controls data endianness on disk. Possible values: `little`, `big`, `native`. Default is `native`. ### Fill Value and Quantization - **`fill_value`** (any) - Optional - Overrides the default netCDF `_FillValue`. If set to `False`, the variable is not pre-filled. Default is `netCDF4.default_fillvals`. - **`least_significant_digit`** (integer) - Optional - Quantizes variable data to a specified decimal place precision. Used for lossy compression with zlib. Default is `None`. - **`significant_digits`** (integer) - Optional - Quantizes variable data to retain a specified number of significant digits. Only available with netcdf-c >= 4.9.0 and `NETCDF4`/`NETCDF4_CLASSIC` formats. - **`quantize_mode`** (string) - Optional - Algorithm for quantization. Available modes: `BitGroom` (default), `BitRound`, `GranularBitRound`. ### Chunk Cache - **`chunk_cache`** (tuple of integers) - Optional - Reduces or increases the size of the default chunk cache for a variable. Setting this can impact performance. Default settings may consume significant memory for many variables. ``` -------------------------------- ### Save and Read In-memory Dataset from Memory Buffer Source: https://unidata.github.io/netcdf4-python This snippet shows how to take the `memoryview` object returned by `Dataset.close()`, write it to a file, and then reopen it as a standard NetCDF Dataset. This demonstrates the complete cycle of creating, accessing, and persisting an in-memory buffer. ```python >>> # 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() ``` -------------------------------- ### Open Dataset with Parallel Access Source: https://unidata.github.io/netcdf4-python Enable parallel access by setting the parallel keyword to True when opening or creating a dataset. ```python >>> nc = Dataset('parallel_test.nc','w',parallel=True) ``` -------------------------------- ### Static Method: fromcdl Source: https://unidata.github.io/netcdf4-python Creates a netCDF Dataset from a CDL text representation by calling ncgen via subprocess. ```APIDOC ## fromcdl(cdlfilename, ncfilename=None, mode='a', format='NETCDF4') ### Description Creates a Dataset from a CDL text representation by calling ncgen via subprocess. Requires ncgen to be installed and in the system PATH. ### Parameters #### Query Parameters - **cdlfilename** (string) - Required - The path to the CDL file. - **ncfilename** (string) - Optional - The netCDF file to create. If not provided, the CDL filename with the suffix replaced by .nc is used. - **mode** (string) - Optional - Access mode to open the Dataset. Default is 'a'. - **format** (string) - Optional - The underlying file format to use ('NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_CLASSIC', 'NETCDF3_64BIT_OFFSET', or 'NETCDF3_64BIT_DATA'). Default is 'NETCDF4'. ### Response - **Dataset** (object) - Returns a Dataset instance for the created ncfilename. ``` -------------------------------- ### Dataset Constructor Source: https://unidata.github.io/netcdf4-python The Dataset constructor is used to create or open a netCDF file. It accepts various arguments to control file access, format, and behavior. ```APIDOC ## Dataset Constructor ### Description Initializes a netCDF Dataset object. This can be used to create a new netCDF file or open an existing one for reading or writing. ### Method `__init__` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Arguments - **filename** (str or pathlib.Path or url) - Name of the netCDF file or URL of an OpenDAP dataset. If `memory` is set, this is used to set the `filepath()`. - **mode** (str) - Access mode. Options include 'r' (read-only), 'w' (write, clobbers existing), 'x' (write, fails if exists), 'a' or 'r+' (append/read-write). Appending 's' enables unbuffered shared access for NETCDF3 formats. - **clobber** (bool) - If True (default) and mode is 'w', an existing file with the same name will be deleted. If False, an exception is raised. Ignored if mode is 'x'. - **diskless** (bool) - If True, the dataset is stored in memory. - **persist** (bool) - If True, the dataset remains in memory after closing. - **keepweakref** (bool) - If True, child objects only keep weak references to the parent Dataset or Group. - **memory** (bytes or bytearray) - If provided, the dataset is created from this in-memory buffer. - **encoding** (str) - Encoding to use for the dataset. - **parallel** (bool) - If True, enables parallel I/O. - **comm** (mpi4py.MPI.Comm) - MPI communicator for parallel I/O. - **info** (str) - Information string. - **format** (str) - The netCDF format. Defaults to 'NETCDF4'. Options include 'NETCDF3_CLASSIC', 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_64BIT_OFFSET', 'NETCDF3_64BIT_DATA'. ### Request Example ```python from netCDF4 import Dataset # Create a new netCDF4 file dataset = Dataset('my_file.nc', 'w', format='NETCDF4') # Open an existing file for reading # dataset = Dataset('existing_file.nc', 'r') ``` ### Response #### Success Response (200) Returns a Dataset object. #### Response Example ```python # Example of a Dataset object (not a direct output of __init__) # ``` ``` -------------------------------- ### Inspect Compound Data Types Source: https://unidata.github.io/netcdf4-python Shows how to print Dataset, Variable, and CompoundType objects to inspect their structure and metadata. ```python >>> print(f) root group (NETCDF4 data model, file format HDF5): dimensions(sizes): x_dim(3) variables(dimensions): {'names':['real','imag'], 'formats':['>> print(f.variables["cmplx_var"]) compound cmplx_var(x_dim) compound data type: {'names':['real','imag'], 'formats':['>> print(f.cmptypes) {'complex128': : name = 'complex128', numpy dtype = {'names':['real','imag'], 'formats':['>> print(f.cmptypes["complex128"]) : name = 'complex128', numpy dtype = {'names':['real','imag'], 'formats':['>> # 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)) ``` -------------------------------- ### File Path and Filter Information Source: https://unidata.github.io/netcdf4-python Methods for retrieving the file path and checking for the availability of compression filters. ```APIDOC ## File Path and Filter Methods ### `filepath(encoding=None)` **Description**: Get the file system path (or the opendap URL) which was used to open/create the Dataset. Requires netcdf >= 4.1.2. **Parameters**: - `encoding` (string, optional): The encoding to use for decoding the path string. Defaults to `sys.getfilesystemencoding()`. ### Filter Availability - `has_blosc_filter()`: Returns `True` if the blosc compression filter is available. - `has_bzip2_filter()`: Returns `True` if the bzip2 compression filter is available. - `has_szip_filter()`: Returns `True` if the szip compression filter is available. - `has_zstd_filter()`: Returns `True` if the zstd compression filter is available. ``` -------------------------------- ### Dataset Synchronization Source: https://unidata.github.io/netcdf4-python The `sync` method writes any buffered data for the Dataset to the disk file, ensuring data persistence. ```APIDOC ## sync(self) ### Description Writes all buffered data in the `Dataset` to the disk file. ### Method `sync` ``` -------------------------------- ### Create and Read Compound Data Types Source: https://unidata.github.io/netcdf4-python Demonstrates defining a compound type for complex numbers, writing it to a netCDF file, and reading it back into a numpy structured array. ```python >>> f = Dataset("complex.nc","w") >>> size = 3 # length of 1-d complex array >>> # create sample complex data. >>> datac = np.exp(1j*(1.+np.linspace(0, np.pi, size))) >>> # create complex128 compound data type. >>> complex128 = np.dtype([("real",np.float64),("imag",np.float64)]) >>> complex128_t = f.createCompoundType(complex128,"complex128") >>> # create a variable with this data type, write some data to it. >>> x_dim = f.createDimension("x_dim",None) >>> v = f.createVariable("cmplx_var",complex128_t,"x_dim") >>> data = np.empty(size,complex128) # numpy structured array >>> data["real"] = datac.real; data["imag"] = datac.imag >>> v[:] = data # write numpy structured array to netcdf compound var >>> # close and reopen the file, check the contents. >>> f.close(); f = Dataset("complex.nc") >>> v = f.variables["cmplx_var"] >>> datain = v[:] # read in all the data into a numpy structured array >>> # create an empty numpy complex array >>> datac2 = np.empty(datain.shape,np.complex128) >>> # .. fill it with contents of structured array. >>> datac2.real = datain["real"]; datac2.imag = datain["imag"] >>> print('{}: {}'.format(datac.dtype, datac)) # original data complex128: [ 0.54030231+0.84147098j -0.84147098+0.54030231j -0.54030231-0.84147098j] >>> >>> print('{}: {}'.format(datac2.dtype, datac2)) # data from file complex128: [ 0.54030231+0.84147098j -0.84147098+0.54030231j -0.54030231-0.84147098j] ``` -------------------------------- ### Initialize MFTime Source: https://unidata.github.io/netcdf4-python Creates a time variable interface for a MFDataset, imposing consistent time units and/or calendar across all files. If `units` or `calendar` are not provided, they are inferred from the master variable, with checks for consistency. ```python T = MFTime(t) ``` -------------------------------- ### Create netCDF dimensions Source: https://unidata.github.io/netcdf4-python Use createDimension to define dimensions. Set size to None or 0 for unlimited dimensions. ```python >>> level = rootgrp.createDimension("level", None) >>> time = rootgrp.createDimension("time", None) >>> lat = rootgrp.createDimension("lat", 73) >>> lon = rootgrp.createDimension("lon", 144) ``` -------------------------------- ### Convert Strings to Character Arrays Source: https://unidata.github.io/netcdf4-python Demonstrates manual and automatic conversion between numpy string arrays and netCDF character arrays using stringtochar and the _Encoding attribute. ```python >>> from netCDF4 import stringtochar >>> nc = Dataset('stringtest.nc','w',format='NETCDF4_CLASSIC') >>> _ = nc.createDimension('nchars',3) >>> _ = nc.createDimension('nstrings',None) >>> v = nc.createVariable('strings','S1',('nstrings','nchars')) >>> datain = np.array(['foo','bar'],dtype='S3') >>> v[:] = stringtochar(datain) # manual conversion to char array >>> print(v[:]) # data returned as char array [[b'f' b'o' b'o'] [b'b' b'a' b'r']] >>> v._Encoding = 'ascii' # this enables automatic conversion >>> v[:] = datain # conversion to char array done internally >>> print(v[:]) # data returned in numpy string array ['foo' 'bar'] >>> nc.close() ``` -------------------------------- ### Create NetCDF Variable with Zlib Compression and Least Significant Digit Quantization Source: https://unidata.github.io/netcdf4-python Applies zlib compression and lossy quantization based on the least significant digit. This sacrifices some precision for greater compression. Set `least_significant_digit` to the power of ten of the smallest reliable decimal place. ```python >>> temp = rootgrp.createVariable("temp","f4",("time","level","lat","lon",),compression='zlib',least_significant_digit=3) ``` -------------------------------- ### Create NetCDF Variable with Zlib Compression Source: https://unidata.github.io/netcdf4-python Enables zlib compression for the netCDF variable. This is a lossless compression method that can significantly reduce file size. `complevel` can be adjusted for speed vs. compression ratio. ```python >>> temp = rootgrp.createVariable("temp","f4",("time","level","lat","lon",),compression='zlib') ``` -------------------------------- ### String Conversion Utilities Source: https://unidata.github.io/netcdf4-python Functions to convert strings to character arrays with specified padding and data types. ```APIDOC ## POST stringtoarr ### Description Convert a string to a character array of length `NUMCHARS`. ### Method POST ### Endpoint /stringtoarr ### Parameters #### Request Body - **string** (string) - Required - Input python string. - **NUMCHARS** (integer) - Required - Number of characters used to represent string (if len(a) < `NUMCHARS`, it will be padded on the right with blanks). - **dtype** (string) - Optional - Type of numpy array to return. Default is `'S'`, which means an array of dtype `'S1'` will be returned. If dtype=`'U'`, a unicode array (dtype = `'U1'`) will be returned. ### Request Example { "string": "hello", "NUMCHARS": 10, "dtype": "S" } ### Response #### Success Response (200) - **char_array** (numpy.ndarray) - A rank 1 numpy character array of length NUMCHARS with datatype `'S1'` (default) or `'U1'` (if dtype=`'U'`). ### Response Example { "char_array": [104, 101, 108, 108, 111, 32, 32, 32, 32, 32] } ## POST stringtochar ### Description Convert a string array to a character array with one extra dimension. ### Method POST ### Endpoint /stringtochar ### Parameters #### Request Body - **a** (numpy.ndarray) - Required - Input numpy string array with numpy datatype `'SN'` or `'UN'`, where N is the number of characters in each string. Will be converted to an array of characters (datatype `'S1'` or `'U1'`) of shape `a.shape + (N,)`. - **encoding** (string) - Optional - Can be used to specify character encoding (default `utf-8` for dtype=`'UN'` or `ascii` for dtype=`'SN'`). If `encoding` is 'none' or 'bytes', a `numpy.string_` the input array is treated a raw byte strings (`numpy.string_`). - **n_strlen** (integer) - Optional - The number of characters in each string. Default is None, which means `n_strlen` will be set to a.itemsize (the number of bytes used to represent each string in the input array). ### Request Example { "a": ["hello", "world"], "encoding": "utf-8", "n_strlen": 5 } ### Response #### Success Response (200) - **char_array** (numpy.ndarray) - A numpy character array with datatype `'S1'` or `'U1'` and shape `a.shape + (N,)`, where N is the length of each string in a. ### Response Example { "char_array": [[104, 101, 108, 108, 111], [119, 111, 114, 108, 100]] } ``` -------------------------------- ### Inspect variable metadata Source: https://unidata.github.io/netcdf4-python Printing a Variable instance provides summary information including shape, dimensions, and fill values. ```python >>> print(temp) float32 temp(time, level, lat, lon) units: K unlimited dimensions: time, level current shape = (0, 0, 73, 144) filling on, default _FillValue of 9.969209968386869e+36 used ``` -------------------------------- ### Create Variable Source: https://unidata.github.io/netcdf4-python Creates a new variable with specified name, data type, and dimensions. Supports various compression and storage options. ```python def createVariable(self, varname, datatype, dimensions=(), compression=None, zlib=False, complevel=4, shuffle=True, szip_coding='nn', szip_pixels_per_block=8, blosc_shuffle=1, fletcher32=False, contiguous=False, chunksizes=None, endian='native', least_significant_digit=None, significant_digits=None, quantize_mode='BitGroom', fill_value=None, chunk_cache=None) ``` -------------------------------- ### getlibversion() Source: https://unidata.github.io/netcdf4-python Retrieves the version information of the netCDF library. ```APIDOC ## getlibversion() ### Description Returns a string describing the version of the netCDF library used to build the module and its build timestamp. ### Response - **string** - Version and build information. ``` -------------------------------- ### Fill Mode Settings Source: https://unidata.github.io/netcdf4-python Methods to control the fill mode for datasets open for writing. ```APIDOC ## POST /set_fill_off ### Description Sets the fill mode for a `Dataset` open for writing to `off`. This will prevent the data from being pre-filled with fill values, which may result in some performance improvements. However, you must then make sure the data is actually written before being read. ### Method POST ### Endpoint /set_fill_off ### Parameters None ### Request Example (No request body needed) ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Fill mode set to off." } ## POST /set_fill_on ### Description Sets the fill mode for a `Dataset` open for writing to `on`. This causes data to be pre-filled with fill values. The fill values can be controlled by the variable's `_Fill_Value` attribute, but is usually sufficient to the use the netCDF default `_Fill_Value` (defined separately for each variable type). The default behavior of the netCDF library corresponds to `set_fill_on`. Data which are equal to the `_Fill_Value` indicate that the variable was created, but never written to. ### Method POST ### Endpoint /set_fill_on ### Parameters None ### Request Example (No request body needed) ### Response #### Success Response (200) - **message** (string) - Confirmation message. #### Response Example { "message": "Fill mode set to on." } ``` -------------------------------- ### Inspect Parallel Output with ncdump Source: https://unidata.github.io/netcdf4-python Verify the contents of the parallel-written netCDF file using the ncdump utility. ```text % ncdump parallel_test.nc netcdf parallel_test { dimensions: dim = 4 ; variables: int64 var(dim) ; data: var = 0, 1, 2, 3 ; } ```