from netCDF3 import Dataset import numpy as np in_file='/home/phil/public_html/courses/atsc500/code/matlab/BOMEX_256x256x150_25m_20m_2s_16_0000009240.nc' out_file='subset.nc' new_x=np.arange(128,178) new_y=np.arange(128,198) nc_in=Dataset(in_file) try: nc_out=Dataset(out_file,'ws','NETCDF3_CLASSIC') except: os.unlink('outfile.nc') nc_out=Dataset('outfile.nc','ws','NETCDF3_CLASSIC') ## copy attributes for the_att in nc_in.ncattrs(): setattr(nc_out,the_att,getattr(nc_in,the_att).strip()) ##get one-d vars npz_vars={} one_d={} out_vars={} for the_dim in ['x','y','z']: one_d[the_dim]=nc_in.variables[the_dim] npz_vars={} sub_x=one_d['x'][new_x] sub_y=one_d['y'][new_y] sub_z=one_d['z'][:] nc_out.createDimension('x',len(sub_x))
from netCDF3 import Dataset from scipy.interpolate import UnivariateSpline import matplotlib.pyplot as plt import numpy as np filename='soundings.nc'; nc_file=Dataset(filename) var_names=nc_file.variables.keys() print "variable names: ",var_names print "global attributes: ",nc_file.ncattrs() #print "col_names: ",nc_file.col_names This line gives me an error fig=plt.figure(1) fig.clf() ax1=fig.add_subplot(111) z_interp=np.arange(2000.,25000.,100.) Temp_array = np.zeros(len(z_interp)) #initial array to start stacking Temperatures for var_name,the_var in nc_file.variables.items(): print var_name, the_var interp_temp=UnivariateSpline(the_var[:,1],the_var[:,2]) #note that Feb-28-2 # 0012-00z only goe #s up to 11820 Temp_array = np.vstack((Temp_array, interp_temp(z_interp))) #stacking Temperatures ax1.plot(interp_temp(z_interp),z_interp) fig.canvas.draw() plt.title('Interpolated Temperatures vs Height') plt.xlabel('Temperature(C)') plt.ylabel('Height(m)') plt.show() Temp_array = np.delete(Temp_array, 0, 0) #deleting initial row of zeros