Created
March 6, 2015 17:29
-
-
Save guziy/098ca51f5efad26a7489 to your computer and use it in GitHub Desktop.
Delete Feb29 fields from a netcdf file
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
import os | |
from netCDF4 import Dataset, num2date | |
import calendar | |
import numpy as np | |
def del_feb29_in_file(in_path, varname): | |
#input file | |
dsin = Dataset(in_path) | |
if not os.path.isdir(varname): | |
os.mkdir(varname) | |
out_path = "{}/{}".format(varname, os.path.basename(in_path)) | |
if os.path.isfile(out_path): | |
dsin.close() | |
print "{} already exists, will not redo!".format(out_path) | |
return | |
print "processing : {}".format(in_path) | |
#output file | |
dsout = Dataset(out_path, "w", format="NETCDF3_CLASSIC") | |
#Copy dimensions | |
for dname, the_dim in dsin.dimensions.iteritems(): | |
#print dname, len(the_dim) | |
dsout.createDimension(dname, len(the_dim) if not the_dim.isunlimited() else None) | |
# Delete all fileds on Feb 29 | |
time_var = dsin.variables["time"] | |
print time_var[:].min(), time_var[:].max() | |
dates = num2date(time_var[:], time_var.units) | |
time_indices = np.where([not (d.month == 2 and d.day == 29) for d in dates]) | |
#Copy variables | |
for v_name, varin in dsin.variables.iteritems(): | |
out_var = dsout.createVariable(v_name, varin.datatype, varin.dimensions) | |
print v_name | |
#print varin.datatype | |
if "time" not in varin.dimensions: | |
out_var[:] = varin[:] | |
else: | |
took = varin[:].take(time_indices, axis=varin.dimensions.index("time")) | |
print took.shape | |
out_var[:] = took.squeeze() | |
out_var.setncatts({k: varin.getncattr(k) for k in varin.ncattrs()}) | |
#close the output file | |
dsout.close() | |
dsin.close() | |
def main(): | |
in_folder = "../ERA-Interim_0.75_NEMO_pilot" | |
for vname in os.listdir(in_folder): | |
for fname in os.listdir(os.path.join(in_folder, vname)): | |
del_feb29_in_file(os.path.join(in_folder, vname, fname), vname) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment