def convert_to_nc(variables): for variable in variables: filename = "{0}.nc".format(variable) dim = { 'time': range(niter), 'z': range(ktot), 'y': range(jtot), 'x': range(itot) } if variable is 'u': dim['xh'] = dim.pop('x') if variable is 'v': dim['yh'] = dim.pop('y') if variable is 'w': dim['zh'] = dim.pop('z') try: ncfile = mht.Create_ncfile(grid, filename, variable, dim, precision, compression) # Loop through the files and read 3d field for t in range(niter): otime = round((starttime + t * sampletime) / 10**iotimeprec) f_in = "{0:}.{1:07d}".format(variable, otime) try: fin = mht.Read_binary(grid, f_in) except Exception as ex: print(ex) raise Exception( 'Stopping: cannot find file {}'.format(f_in)) print("Processing %8s, time=%7i" % (variable, otime)) ncfile.dimvar['time'][t] = otime * 10**iotimeprec if (perslice): for k in range(ktot): ncfile.var[t, k, :, :] = fin.read(itot * jtot) else: ncfile.var[t, :, :, :] = fin.read(itot * jtot * ktot) fin.close() ncfile.close() except Exception as ex: print(ex) print("Failed to create %s" % filename)
def convert_to_nc(variables): for time in range(starttime,endtime, sampletime): otime = int(round(time / 10**iotimeprec)) if not glob.glob('*.{0:07d}'.format(otime)) endtime = time - sampletime break for variable in variables: try: filename = "{0}.nc".format(variable) dim = {'time' : range(niter), 'z' : range(ktot), 'y' : range(jtot), 'x': range(itot)} if variable is 'u': dim['xh'] = dim.pop('x') if variable is 'v': dim['yh'] = dim.pop('y') if variable is 'w': dim['zh'] = dim.pop('z') ncfile = mht.Create_ncfile(grid, filename, variable, dim, precision, compression) # Loop through the files and read 3d field for t in range(niter): otime = round((starttime + t*sampletime) / 10**iotimeprec) f_in = "{0:}.{1:07d}".format(variable, otime) print(f_in) try: fin = mht.Read_binary(grid, f_in) except: print('Stopping: cannot find file {}'.format(f_in)) ncfile.sync() stop = True break print("Processing %8s, time=%7i"%(variable, otime)) ncfile.dimvar['time'][t] = otime * 10**iotimeprec if (perslice): for k in range(ktot): ncfile.var[t,k,:,:] = fin.read(itot * jtot) else: ncfile.var[t,:,:,:] = fin.read(itot * jtot * ktot) fin.close() ncfile.close() except:
def convert_to_nc(variables): # Loop over the different variables and crosssections for variable in variables: for mode in modes: try: otime = int(round(starttime / 10**iotimeprec)) if os.path.isfile("{0}.xy.{1:07d}".format(variable, otime)): if mode != 'xy': continue at_surface = True else: at_surface = False filename = "{0}.{1}.nc".format(variable, mode) if not at_surface: if indexes == None: indexes_local = mht.get_cross_indices(variable, mode) else: indexes_local = indexes #dim = {'time' : range(niter), 'z' : range(ktot), 'y' : range(jtot), 'x' : range(itot)} dim = collections.OrderedDict() dim['time'] = range(niter) dim['z'] = range(ktot) dim['y'] = range(jtot) dim['x'] = range(itot) if at_surface: dim.pop('z') n = itot * jtot indexes_local = [-1] elif mode == 'xy': dim.update({'z': indexes_local}) n = itot * jtot elif mode == 'xz': dim.update({'y': indexes_local}) n = itot * ktot elif mode == 'yz': dim.update({'x': indexes_local}) n = ktot * jtot if variable is 'u': dim['xh'] = dim.pop('x') if variable is 'v': dim['yh'] = dim.pop('y') if variable is 'w': dim['zh'] = dim.pop('z') ncfile = mht.Create_ncfile(grid, filename, variable, dim, precision, compression) for t in range(niter): for k in range(len(indexes_local)): index = indexes_local[k] otime = int( round( (starttime + t * sampletime) / 10**iotimeprec)) if at_surface: f_in = "{0}.{1}.{2:07d}".format( variable, mode, otime) else: f_in = "{0:}.{1}.{2:05d}.{3:07d}".format( variable, mode, index, otime) try: fin = mht.Read_binary(grid, f_in) except Exception as ex: print(ex) raise Exception( 'Stopping: cannot find file {}'.format(f_in)) print("Processing %8s, time=%7i, index=%4i" % (variable, otime, index)) ncfile.dimvar['time'][t] = otime * 10**iotimeprec if at_surface: ncfile.var[t, :, :] = fin.read(n) elif mode == 'xy': ncfile.var[t, k, :, :] = fin.read(n) elif mode == 'xz': ncfile.var[t, :, k, :] = fin.read(n) elif mode == 'yz': ncfile.var[t, :, :, k] = fin.read(n) fin.close() ncfile.close() except Exception as ex: print(ex) print("Failed to create %s" % filename)