Convert netCDF files to Cloud-Optimized GeoTIFF format with advanced compression
Project description
nc2cog - netCDF to Cloud-Optimized GeoTIFF Converter
Convert netCDF files to Cloud-Optimized GeoTIFF format with advanced compression and performance settings.
🚀 Features
- Format Conversion: Convert netCDF files to Cloud-Optimized GeoTIFF (COG)
- Batch Processing: Process entire directories of netCDF files
- Advanced Compression: Support for deflate, lzw, and jpeg compression with configurable levels
- Performance Optimization: Configurable tile and block sizes for optimal performance
- Pyramid Structure: Customizable overview (pyramid) levels for multi-scale access
- GDAL Optimized: Eliminated GDAL warnings and optimized driver-specific parameters
- Parallel Processing: Multi-threaded conversion for faster processing
- Resume Capability: Resume interrupted conversions
- Projection Transformation: Reproject data from source to target coordinate systems during conversion
- Flexible Output: Output to a directory or directly to a specified
.tiffile - Rich Metadata: 18 metadata fields automatically written to output COG (source, extent, resolution, min/max, unit, etc.)
- Clean COG Layout: Metadata written during conversion pipeline — no post-creation modifications that break COG layout optimization
📋 Requirements
- Python 3.9+
🛠️ Installation
From PyPI (Recommended)
pip install nc2cog
From Source
# Clone the repository
git clone https://github.com/cyberpsyche/nc2cog.git
cd nc2cog
# Install with uv
uv pip install .
# Or with pip
pip install .
System Dependencies
nc2cog requires GDAL with Python bindings:
- macOS:
brew install gdal - Ubuntu:
sudo apt-get install gdal-bin libgdal-dev - Windows: Use OSGeo4W installer
🚀 Quick Start
Single File Conversion
Convert a single netCDF file to COG TIFF:
nc2cog input.nc output/
Specify the output filename:
nc2cog input.nc output/my_custom_name.tif
Batch Conversion
Convert all netCDF files in a directory:
nc2cog input_dir/ output/
Multi-dimensional NetCDF Files
For netCDF files containing multiple variables and time dimensions (e.g., PRE(time, lat, lon), REF(time, lat, lon)), the tool automatically detects the structure and converts each variable to a separate multi-band COG file, with time steps as bands.
# Auto-detect and convert all variables
nc2cog MPF_V4_20251113144500.nc output/
# Produces: output/PRE.tif (N bands), output/REF.tif (N bands)
# Convert only specific variables to a directory
nc2cog --variables PRE MPF_V4_20251113144500.nc output/
# Convert a single variable to a specific output file
nc2cog --variables PRE MPF_V4_20251113144500.nc output/MPF_v4_PRE.tif
# Produces: output/MPF_v4_PRE.tif (N bands, each band is a time step)
Advanced Usage
With custom compression and performance settings:
nc2cog input.nc output/ \
--compression deflate \
--zlevel 9 \
--tile-size 1024 \
--block-size 512 \
--resampling cubic \
--overview-levels 2,4,8,16,32 \
--metadata-source "My Satellite Data"
With parallel processing:
nc2cog input_dir/ output/ --threads 4
⚙️ Command Line Options
Compression Options
--compression[deflate|lzw|jpeg]: Choose compression algorithm--zlevel[1-9]: Set compression level for deflate (default: 6)
Performance Options
--tile-sizeINTEGER: Tile size for COG (default: 512)--block-sizeINTEGER: Block size for compression (default: 256)
Pyramid/Overview Options
--resampling[nearest|bilinear|cubic|...]: Resampling method for overviews (default: nearest)--overview-levelsTEXT: Overview levels (comma-separated, default: 2,4,8,16)
General Options
--overwrite: Overwrite existing output files--dry-run: Show what would be processed without doing it--verbose,-v: Enable verbose logging--resume: Resume from last processed file--threadsINTEGER: Number of parallel processing threads (default: 1)--src-projTEXT: Source projection in EPSG format (e.g., EPSG:4326)--dst-projTEXT: Target projection in EPSG format (e.g., EPSG:3857)--variablesTEXT: Variables to convert in multi-dimensional NC files (comma-separated, e.g.,PRE,REF). If omitted, all data variables are auto-detected.--metadata-sourceTEXT: Custom source name for COG metadata. If omitted, auto-detected from netCDF global attributes (source,platform,institution).--version,-V: Show version information and exit.
📁 Output Path Rules
The behavior of the output_path argument depends on its format:
| Output path ends with | Behavior |
|---|---|
.tif |
Output to the specified file directly |
/ or no extension |
Output to a directory (one file per variable for multi-dim) |
Examples:
# Directory output: creates output/PRE.tif and output/REF.tif
nc2cog MPF_V4_20251113144500.nc output/
# File output: creates the exact file specified
nc2cog --variables PRE MPF_V4_20251113144500.nc output/MPF_v4_PRE.tif
Note: File output (.tif ending) only works when converting a single variable. If multiple variables are specified with a .tif output path, only the first variable will be converted.
🔧 Configuration File
Create a config.yaml for complex setups:
# Processing parameters
compression: "deflate"
zlevel: 6
tile_size: [512, 512]
block_size: [256, 256]
# Output options
overviews:
resampling: "nearest"
levels: [2, 4, 8, 16]
# Metadata options
metadata:
source: "" # Custom source name (auto-detected from netCDF if empty)
offset: 0.0 # Data offset
scale: 1.0 # Data scale factor
unit: "" # Data unit (auto-detected from netCDF if empty)
# Processing control
overwrite: false
skip_errors: true
Use with: nc2cog --config config.yaml input.nc output/
📊 GDAL Optimization
This tool eliminates common GDAL warnings by using driver-appropriate parameters:
- GTiff driver: Uses
BLOCKXSIZE/BLOCKYSIZEinstead ofTILEWIDTH/TILEHEIGHT - COG driver: Uses
BLOCKSIZEinstead ofBLOCKXSIZE/BLOCKYSIZE - Overview handling: Optimized to avoid
COPY_SRC_OVERVIEWSconflicts - Clean COG Layout: Metadata is written to the intermediate GeoTIFF before COG conversion. The GDAL COG driver automatically carries metadata from source to output, eliminating the need for post-creation file modifications that would break COG layout optimization (no "IFD has been rewritten" warnings).
📋 COG Metadata
Each output COG file contains 18 metadata fields written during the conversion pipeline:
| Field | Description | Example |
|---|---|---|
Coordinate System |
CRS with projection info | WGS84 (EPSG:4326) |
Band Count |
Number of bands (time steps) | 10 |
Data Type |
Pixel data type | Float32 |
Resolution |
Pixel resolution in degrees | 0.01000000 |
Extent |
Bounding box (minLon, minLat, maxLon, maxLat) | 97.095, 37.295, 126.105, 53.405 |
Creation Time |
UTC timestamp of conversion | 2026-05-26 12:08:29 |
Source |
Data source name | hfioi |
Compression |
Compression algorithm | DEFLATE |
startX / startY |
Upper-left corner coordinates | 97.095000 / 53.405000 |
endX / endY |
Lower-right corner coordinates | 126.105000 / 37.295000 |
min / max |
Global data value range | 0.00 / 7.91 |
offset / scale |
Linear transformation parameters | 0.0000 / 1.0000 |
unit |
Data unit from netCDF variable | mm |
NoData |
No-data value | -9999.0 |
Metadata Source Configuration
The Source field is determined by the following priority:
--metadata-sourceCLI parameter (highest priority) — explicitly set source name- netCDF global attributes — auto-detected from
source,platform, orinstitutionattributes - Empty string (default fallback)
# Use custom source name
nc2cog --metadata-source "FY-4 Satellite" input.nc output/
# Auto-detect from netCDF attributes (default behavior)
nc2cog input.nc output/
Viewing Metadata
# View all metadata fields
gdalinfo output.tif
# View metadata as JSON
gdalinfo -json output.tif | python -c "
import json, sys
d = json.load(sys.stdin)
m = d.get('metadata', {}).get('', {})
for k, v in sorted(m.items()):
print(f'{k}: {v}')
"
🎯 Use Cases
Climate/Meteorological Data
nc2cog climate_data.nc output/ \
--compression deflate \
--zlevel 9 \
--resampling cubic \
--overview-levels 2,4,8,16,32
Oceanographic Data
nc2cog ocean_data.nc output/ \
--compression lzw \
--tile-size 1024 \
--resampling bilinear \
--overview-levels 2,4,8
Large Dataset Processing
nc2cog large_dataset/ output/ \
--threads 4 \
--compression deflate \
--zlevel 7 \
--tile-size 512
Projection Transformations
With the new projection support, you can transform coordinate systems during conversion:
Convert with reprojection from WGS84 to Web Mercator:
nc2cog --src-proj EPSG:4326 --dst-proj EPSG:3857 input.nc output/
Or specify only target projection (source will be detected automatically):
nc2cog --dst-proj EPSG:3857 input.nc output/
Combine with other conversion options:
nc2cog --src-proj EPSG:4326 --dst-proj EPSG:3857 \
--compression deflate \
--zlevel 9 \
--tile-size 1024 \
input.nc output/
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
📄 License
MIT License
🐛 Issues
Report issues on the GitHub repository.
📚 Documentation
For more detailed documentation:
ℹ️ Additional Information
Overview Level Generation
When using --overview-levels, note that GDAL intelligently determines which overview levels to actually create based on the source image size. For smaller images, GDAL may generate fewer overview levels than specified to avoid creating overly small and potentially useless overviews.
For example, with --overview-levels 2,4,8,16,32 on a 1781x1572 image, GDAL may only generate levels 2 and 4 (890x786 and 445x393 pixels respectively) as the smaller levels would be too small to be useful.
Made with ❤️ for the geospatial community.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nc2cog-0.1.5.tar.gz.
File metadata
- Download URL: nc2cog-0.1.5.tar.gz
- Upload date:
- Size: 34.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.10 {"installer":{"name":"uv","version":"0.11.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb041071655e4f0963ec5632cba4efcdee211a85bb081de55c1241d751c58ad0
|
|
| MD5 |
25578f2e3c40dca553b7f902c462a65a
|
|
| BLAKE2b-256 |
b1f1d0243e701207b37fe6717fa26dab404b4f0823c7b2657a2e2db9f370383f
|
File details
Details for the file nc2cog-0.1.5-py3-none-any.whl.
File metadata
- Download URL: nc2cog-0.1.5-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.10 {"installer":{"name":"uv","version":"0.11.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08931b89419fb193e97f32a167956bbc09b6e702cdcd1f502218cc2c3caaa313
|
|
| MD5 |
0e96898ffcc21c6af1aaddfb54c7b647
|
|
| BLAKE2b-256 |
5efeeb784fccacf489a108f29577a64c4e2ad0895e650b8d29f1cfd5d0971071
|