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."
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. mapping.straight_vertical_longitude_from_pole = -45.0 mapping.standard_parallel = 70.0
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 t_var = nc.createVariable("time", 'f', dimensions=("time",)) t_var.units = "years" t_var.long_name = "time" t_var.axis = "T"
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 usurf_var = nc.createVariable("usurf", 'f', dimensions=("x", )) usurf_var.units = "m"
options = parser.parse_args() args = options.FILE infile = args[0] nc = CDF(infile, 'a') mapping_var = 'mapping' for var in nc.variables.keys(): if hasattr(var, 'grid_mapping'): mapping_var = var.grid_mapping pass if not var in nc.variables.keys(): mapping = nc.createVariable(mapping_var, 'b') else: mapping = nc.variables[mapping_var] mapping.grid_mapping_name = "polar_stereographic" mapping.latitude_of_projection_origin = 90. mapping.straight_vertical_longitude_from_pole = -45.0 mapping.standard_parallel = 70.0 mapping.false_easting = 0. mapping.false_northing = 0. mapping.units = "m" # Save the projection information: nc.proj4 = "+init=epsg:3413"
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 out_vars['y'][:]=sub_y[:] npz_vars['y']=sub_y out_vars['z'][:]=sub_z[:] npz_vars['z']=sub_z
M_out, N_out, area_extent) fill_value = 4 # ocean area_con_in_nn = pr.image.ImageContainerNearest(data, area_in_def, radius_of_influence=radius_of_influence, fill_value=fill_value, nprocs=nprocs) area_con_out_nn = area_con_in_nn.resample(area_out_def) result = area_con_out_nn.image_data pr.plot.save_quicklook('foo.png', area_out_def, result, label='mask') try: var = nc.createVariable("mask", 'b', dimensions=("y", "x")) except: var = nc.variables['mask'] #var[:] = np.flipud(result) var[:] = result # Save the projection information: nc.projection = proj4_str nc.Conventions = "CF-1.5" # writing global attributes import time script_command = ' '.join([time.ctime(), ':', __file__.split('/')[-1], ' '.join([str(l) for l in args])]) nc.history = script_command
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 usurf_var = nc.createVariable("usurf", 'f', dimensions=("x",)) usurf_var.units = "m";