Skip to content

Instantly share code, notes, and snippets.

@brews
Last active January 3, 2016 06:19
Show Gist options
  • Save brews/8422189 to your computer and use it in GitHub Desktop.
Save brews/8422189 to your computer and use it in GitHub Desktop.
This script adds `Time` variable to all netCDF files in the CWD and then concatenates them all into a single netCDF file.
#! /usr/bin/env python
# Copyright 2014 by S. Brewster Malevich <[email protected]>
# This script adds `Time` variable to all netCDF files in the current working
# directory (CWD) and then concatenates them all into a single netCDF file
# `all.nc` in the CWD. This this uses Bash's `ncks` and `ncecat`.
import os
from shutil import rmtree
TEMP_DIR = "/tmp/nc_combiner/"
target_files = [f for f in os.listdir(".")
if os.path.isfile(f) and f.endswith(".nc")]
target_files.sort()
os.mkdir(TEMP_DIR)
for f in target_files:
os.system("ncks --mk_rec_dmn Time " + f + " " + TEMP_DIR + f)
os.system("ncecat -u Time " + TEMP_DIR + "*.nc ./all.nc")
rmtree(TEMP_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment