Last active
October 20, 2015 20:53
-
-
Save nikmolnar/994fcac0d1f25add336f to your computer and use it in GitHub Desktop.
This script takes a directory of NetCDF datasets and a single JSON style file and creates one ZIP archive per dataset, ready to upload to Data Basin.
This file contains 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
""" | |
This script takes a directory of NetCDF datasets and a single JSON style file and creates one ZIP archive | |
per dataset, ready to upload to Data Basin. | |
""" | |
import json | |
import os | |
import sys | |
from zipfile import ZipFile, ZIP_DEFLATED | |
def main(): | |
if len(sys.argv) < 3: | |
print('Usage: netcdf_for_databasin.py <directory> <style file>') | |
sys.exit(-1) | |
netcdf_path, style_path = sys.argv[1:3] | |
if not os.path.isdir(netcdf_path): | |
print('{0} is not a valid directory.'.format(netcdf_path)) | |
sys.exit(-1) | |
if not os.path.isfile(style_path): | |
print('{0} is not a valid file.'.format(style_path)) | |
sys.exit(-1) | |
with open(style_path, 'r') as f: | |
style_json = json.dumps(json.load(f, strict=False)) | |
output_path = os.path.join(netcdf_path, 'packages') | |
# Find a unique directory name if 'packages' exists. | |
if os.path.exists(output_path): | |
i = 1 | |
while os.path.exists('{0}_{1}'.format(output_path, i)): | |
i += 1 | |
output_path = '{0}_{1}'.format(output_path, i) | |
os.mkdir(output_path) | |
netcdf_files = (x for x in os.listdir(netcdf_path) if x.endswith('.nc')) | |
for name in netcdf_files: | |
path = os.path.join(netcdf_path, name) | |
base_name = name[:-3] | |
zip_name = '{0}.zip'.format(base_name) | |
print('Packaging {0}...'.format(zip_name)) | |
with ZipFile(os.path.join(output_path, zip_name), 'w', ZIP_DEFLATED) as zf: | |
zf.write(path, name) | |
zf.writestr('{0}_style.json'.format(base_name), style_json) | |
print('Done. Packages are located at: {0}'.format(output_path)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment