def save_results(self): if self.nc is None: return output_file = self.get_output() if output_file is None: print "No output file selected; cannot proceed." return print "Saving the mask to %s" % output_file nc = NC(output_file, 'w') nc.createDimension('x', self.x.size) nc.createDimension('y', self.y.size) x = nc.createVariable("x", 'f8', ('x',)) y = nc.createVariable("y", 'f8', ('y',)) mask = nc.createVariable("ftt_mask", 'i4', ('y', 'x')) mask.long_name = "Drainage basin area for regional modeling" x_orig = self.nc.variables['x'] y_orig = self.nc.variables['y'] for var, old_var in zip([x,y], [x_orig, y_orig]): for attr in old_var.ncattrs(): var.setncattr(attr, old_var.getncattr(attr)) x[:] = self.x y[:] = self.y nc.variables['ftt_mask'][:] = (self.mask == 2) nc.close() print "Done."
epsilon=0.5, fill_value=fill_value, nprocs=nprocs) # binary density (0: no data, 1: data) rho = np.ones_like(result) rho[result==fill_value] = 0 # Make a quick plot fig_str = out_filename.split('.')[0] fig_name = fig_str + '.png' pr.plot.save_quicklook(fig_name, area_def, result, label='ice thickness') # Write the data: nc = CDF(out_filename, "w", format="NETCDF3_CLASSIC") nc.createDimension("y", size=northing.shape[0]) nc.createDimension("x", size=easting.shape[0]) x = nc.createVariable("x", 'f', dimensions=("x",)) x.units = "m"; x.long_name = "easting" x.standard_name = "projection_x_coordinate" y = nc.createVariable("y", 'f', dimensions=("y",)) y.units = "m"; y.long_name = "northing" y.standard_name = "projection_y_coordinate" mapping_var = 'mapping' mapping = nc.createVariable(mapping_var, 'b') mapping.grid_mapping_name = "polar_stereographic" mapping.latitude_of_projection_origin = 90.
if not os.path.exists(project_name): os.makedirs(project_name) out_dir = project_name + '/' output_filename = '_'.join([project_name, gs_str, alpha_str, gamma_str, vel_str, dHdt_str, bmelt_str,]) + '.nc' mesh_out = RectangleMesh(np.float(xmin), np.float(ymin), np.float(xmax), np.float(ymax), M, N) print "Creating output file..." nc = CDF('/'.join([project_name, output_filename]), 'w') nc.createDimension("y", size=y.shape[0]) nc.createDimension("x", size=x.shape[0]) nc.createDimension("time") x_var = nc.createVariable("x", 'f', dimensions=("x",)) x_var.units = "m"; x_var.long_name = "easting" x_var.standard_name = "projection_x_coordinate" x_var[:] = x y_var = nc.createVariable("y", 'f', dimensions=("y",)) y_var.units = "m"; y_var.long_name = "northing" y_var.standard_name = "projection_y_coordinate" y_var[:] = y
import numpy as np try: from netCDF3 import Dataset as CDF except: from netCDF4 import Dataset as CDF x, topg, thk = np.loadtxt('sg_35m_flowline.txt', unpack=True) output = 'storglaciaren_flowline.nc' # Write the data: print("Writing the data to '%s'... " % output) nc = CDF(output, "w") nc.createDimension("x", size=len(x)) x_var = nc.createVariable("x", 'f', dimensions=("x", )) x_var.units = "m" x_var[:] = x topg_var = nc.createVariable("topg", 'f', dimensions=("x", )) topg_var.units = "m" topg_var.standard_name = "bedrock_altitude" topg_var[:] = topg thk_var = nc.createVariable("thk", 'f', dimensions=("x", )) thk_var.units = "m" thk_var.standard_name = "land_ice_thickness" thk_var[:] = thk
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)) nc_out.createDimension('y',len(sub_y)) nc_out.createDimension('z',len(sub_z)) for the_dim in ['x','y','z']: out_vars[the_dim]=nc_out.createVariable(the_dim,sub_x.dtype,(the_dim,)) press_var=nc_in.variables['p'] npz_vars['p']=press_var out_vars['p']=nc_out.createVariable('p',press_var.dtype,press_var.dimensions) out_vars['p'][...]=nc_in.variables['p'][...] npz_vars['p']=nc_in.variables['p'][...] for the_att in nc_in.variables['p'].ncattrs(): setattr(out_vars['p'],the_att,getattr(nc_in.variables['p'],the_att)) out_vars['x'][:]=sub_x[:] npz_vars['x']=sub_x
import numpy as np try: from netCDF3 import Dataset as CDF except: from netCDF4 import Dataset as CDF x,topg,thk = np.loadtxt('sg_35m_flowline.txt',unpack=True) output = 'storglaciaren_flowline.nc' # Write the data: print("Writing the data to '%s'... " % output) nc = CDF(output, "w") nc.createDimension("x", size=len(x)) x_var = nc.createVariable("x", 'f', dimensions=("x",)) x_var.units = "m"; x_var[:] = x topg_var = nc.createVariable("topg", 'f', dimensions=("x",)) topg_var.units = "m"; topg_var.standard_name = "bedrock_altitude" topg_var[:] = topg thk_var = nc.createVariable("thk", 'f', dimensions=("x",)) thk_var.units = "m"; thk_var.standard_name = "land_ice_thickness" thk_var[:] = thk
if dim_name is None: dim_name='dim_%d' % dim_length try: ncfile.createDimension(dim_name,dim_length) except: pass return dim_name def add_var(ncfile,var_name,var_val): the_dim='dim_%d' % var_val.shape[0] new_var=ncfile.createVariable(var_name,var_val.dtype,(the_dim,'var_cols')) new_var[:,:]=var_val[...] return new_var dimnames=[add_dim(ncfile,item[0]) for item in keep_arrays] ncfile.createDimension('var_cols',6) varnames=[add_var(ncfile,item[2],item[1]) for item in keep_arrays] col_names=array_list[0][2].dtype.names col_units=[array_list[0][2].dtype.fields[name][2] for name in col_names] col_units=[item.split('_')[1] for item in col_units] ncfile.units=','.join(col_units) ncfile.col_names=','.join(col_names) ## array_lengths.sort() ## array_lengths=set(array_lengths) ncfile.close()