Welcome to the NetCDF Converter Service documentation!¶
netcdf-convert.py¶
Functions related to converting a NetCDF file to other formats.
- net2cog.netcdf_convert._write_cogtiff(output_directory: str, nc_xarray: DataTree, variable_path: str, logger: Logger) str | None [source]¶
This function converts a variable inside a NetCDF file into a cloud optimized geotiff.
- Parameters:
output_directory (str) –
Path to temporary directory where output GeoTIFFs will be stored before being staged in S3. example :/home/dockeruser/converter/podaac/netcdf_converter/temp/
netcdf_converter/ RSS_smap_SSS_L3_8day_running_2020_037_FNL_v04.0_test
nc_xarray (xarray.DataTree) – xarray DataTree loaded from NetCDF file. This represents the whole file.
variable_path (str) – Full of the variable within the file to convert.
logger (logging.Logger) – Python Logger object for emitting log messages.
Notes
Assumption that 0 is always on the prime meridian/equator.
The output name for converted GeoTIFFs is <variable path>.tif, with any slashes replaced with underscores.
- net2cog.netcdf_convert.get_all_data_variables(root_datatree: DataTree, logger: Logger) list[str] [source]¶
Traverse tree and retrieve all data variables in all groups.
- Parameters:
root_datatree (xarray.DataTree) – DataTree object representing the root group of the NetCDF-4 file.
- Returns:
A list of paths to all variables in the data_vars property of any node in the DataTree. These variables are filtered to remove any variables that are 1-D, attribute-only (e.g., CRS definitions), dtype = string(S1/S2), or variable without dimensions.
- Return type:
list[str]
- net2cog.netcdf_convert.get_crs_from_grid_mapping(nc_xarray: DataTree, variable_path: str, logger: Logger) CRS [source]¶
Check the metadata attributes for the variable to find the associated grid mapping variable. If the grid mapping variable, as referred to in the grid_mapping CF-Convention metadata attribute, does not exist then default to “+proj=latlong”.
- Parameters:
nc_xarray (xarray.DataTree) – xarray DataTree loaded from NetCDF file. This represents the whole file.
variable_path (str) – Full of the variable within the file to convert.
logger (logging.Logger) – Python Logger object for emitting log messages.
- Returns:
Returns a CRS object corresponding to a grid mapping variable.
- Return type:
csr
- net2cog.netcdf_convert.netcdf_converter(input_nc_file: Path, output_directory: Path, var_list: list[str], logger: Logger) List[str] [source]¶
Primary function for beginning NetCDF conversion using rasterio, rioxarray and xarray
- Parameters:
input_nc_file (pathlib.Path) – Path to NetCDF file to process
output_directory (pathlib.Path) – Path to temporary directory into which results will be placed before staging in S3.
var_list (list[str]) – List of variable names to be converted to various single band cogs, ex: [‘gland’, ‘fland’, ‘sss_smap’]. For hierarchical granules, these names will be full paths to the variable location in the file, omitting the leading slash, e.g.: ‘Grid/precipitationCal’. If this list is empty, it is assumed that all data variables have been requested.
logger (logging.Logger) – Python Logger object for emitting log messages.
Notes
Currently uses local file paths, no s3 paths
netcdf_convert_harmony.py¶
Implementation of harmony-service-lib that invokes the netcdf converter.
- class net2cog.netcdf_convert_harmony.NetcdfConverterService(message, catalog=None, config=None)[source]¶
See https://github.com/nasa/harmony-service-lib-py for documentation and examples.
- __init__(message, catalog=None, config=None)[source]¶
Constructs the adapter
- Parameters:
message (harmony_service_lib.Message) – The Harmony input which needs acting upon
catalog (pystac.Catalog) – A STAC catalog containing the files on which to act
config (harmony_service_lib.util.Config) – The configuration values for this runtime environment.
- process_item(item: Item, source: Source) Item [source]¶
Performs net2cog on input STAC Item’s data, returning an output STAC item
- Parameters:
item (pystac.Item) – the item that should be coggified
source (harmony_service_lib.message.Source) – the input source defining the item
- Returns:
a STAC item describing the output
- Return type:
pystac.Item
- stage_output_and_create_output_stac(source_asset_basename: str, output_files: list[str], input_stac_item: Item) Item [source]¶
Iterate through all generated COGs and stage the results in S3. Also add a unique pystac.Asset for each COG to the pystac.Item returned to Harmony.
- Parameters:
output_files (list[str]) – the file names of generated COGs to be staged. It is assumed that the file names adhere to the convention of <variable_name>.tif.
input_stac_item (pystac.Item) – the input STAC for the request. This is the basis of the output STAC, which will replace the pystac.Assets with generated COGs.
- Returns:
a STAC item describing the output. If there are multiple variables, this STAC item will have multiple assets.
- Return type:
pystac.Item