Last active
August 29, 2015 14:02
-
-
Save selimnairb/b43beb73a477ea8d9c24 to your computer and use it in GitHub Desktop.
Bare bones tool for writing a UNC-RENCI Hydro-NEXRAD rainfall accumulation data file (in netCDF format) to CSV.
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
#!/usr/bin/env python | |
""" Bare bones tool for writing a UNC-RENCI Hydro-NEXRAD rainfall | |
accumulation data file (in netCDF format) to CSV. Writes output | |
to standard out. | |
@author brian_miles@unc.edu | |
Requires Unidata's netcdf4 Python library: https://github.com/Unidata/netcdf4-python | |
""" | |
import os, sys | |
import netCDF4 | |
sep = ',' | |
variables = ['time','latitude','longitude','accumulation_mm'] | |
if __name__ == '__main__': | |
infile = sys.argv[1] | |
nc = netCDF4.Dataset(infile, 'r') | |
out = sys.stdout | |
header = sep.join(variables) | |
header = "{0}{1}".format(header, os.linesep) | |
out.write(header) | |
var_time = nc.variables['time'] | |
var_lat = nc.variables['latitude'] | |
lats = range(len(var_lat)) | |
var_lon = nc.variables['longitude'] | |
lons = range(len(var_lon)) | |
var_acc = nc.variables['accumulation'] | |
for (i, t) in enumerate(var_time): | |
time_str = str(netCDF4.num2date(t, var_time.units)) | |
for lat_idx in lats: | |
for lon_idx in lons: | |
proto = "{t}{sep}{lat}{sep}{lon}{sep}{acc}{lnsep}" | |
line = proto.format(sep=sep, | |
t=time_str, | |
lat=var_lat[lat_idx], | |
lon=var_lon[lon_idx], | |
acc=var_acc[i][lat_idx][lon_idx], | |
lnsep=os.linesep) | |
out.write(line) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment