Skip to content

Instantly share code, notes, and snippets.

@lukecampbell
Created September 9, 2016 12:43
Show Gist options
  • Save lukecampbell/68c5c1b1ec996252a329f05e3daad7ba to your computer and use it in GitHub Desktop.
Save lukecampbell/68c5c1b1ec996252a329f05e3daad7ba to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from argparse import ArgumentParser
from netCDF4 import Dataset
import subprocess
import tempfile
import os
def main():
'''
Apply a netCDF CDL file to an existing dataset
'''
parser = ArgumentParser(description=main.__doc__)
parser.add_argument('input')
parser.add_argument('output')
args = parser.parse_args()
fd, path = tempfile.mkstemp()
os.close(fd)
subprocess.check_call(['ncgen', '-o', path, args.input])
if os.path.exists(args.output):
with Dataset(args.output, 'r') as nc_in:
with Dataset(path, 'r+') as nc_out:
for variable in nc_out.variables:
nc_out.variables[variable][:] = nc_in.variables[variable][:]
os.rename(path, args.output)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment