Created
September 9, 2016 12:43
-
-
Save lukecampbell/68c5c1b1ec996252a329f05e3daad7ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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