def new_to_old(filename): f = DataFile(filename) newfile = DataFile(os.path.splitext(filename)[0] + str(".BOUT_metrics.nc"), create=True) name_changes = { "g_yy": "g_22", "gyy": "g22", "gxx": "g11", "gxz": "g13", "gzz": "g33", "g_xx": "g_11", "g_xz": "g_13", "g_zz": "g_33" } for key in f.keys(): name = key if name in name_changes: name = name_changes[name] newfile.write(name, np.asarray(f.read(key))) f.close() newfile.close() newfile.list()
def listKeys(self, simIndex=0, simType='1-base'): if simType == '1-base': os.chdir('{}/{}'.format(self.outDir, simIndex)) else: os.chdir('{}/{}/{}'.format(self.outDir, simIndex, simType)) datFile = DataFile('BOUT.dmp.0.nc') self.datFile = datFile return datFile.keys()
def change_variable(filename, variable, new_value): f = DataFile(filename) newfile = DataFile(os.path.splitext(filename)[0] + str(variable) + "." + str(new_value), create=True) var_changes = {str(variable)} for key in f.keys(): name = key if name in var_changes: name = name_changes[name] newfile.write(name, np.asarray(f.read(key))) f.close() newfile.close() newfile.list()
def slice(infile, outfile, region = None, xind=None, yind=None): """ xind, yind - index ranges. Range includes first point, but not last point """ # Open input and output files indf = DataFile(infile) outdf = DataFile(outfile, create=True) nx = indf["nx"][0] ny = indf["ny"][0] if region: # Select a region of the mesh xind = [0, nx] if region == 0: # Lower inner leg yind = [0, indf["jyseps1_1"][0]+1] elif region == 1: # Inner core yind = [indf["jyseps1_1"][0]+1, indf["jyseps2_1"][0]+1] elif region == 2: # Upper inner leg yind = [indf["jyseps2_1"][0]+1, indf["ny_inner"][0]] elif region == 3: # Upper outer leg yind = [indf["ny_inner"][0], indf["jyseps1_2"][0]+1] elif region == 4: # Outer core yind = [indf["jyseps1_2"][0]+1, indf["jyseps2_2"][0]+1] else: # Lower outer leg yind = [indf["jyseps2_2"][0]+1, ny] else: # Use indices if not xind: xind = [0, nx] if not yind: yind = [0, ny] print("Indices: [%d:%d, %d:%d]" % (xind[0], xind[1], yind[0], yind[1])) # List of variables requiring special handling special = ["nx", "ny", "ny_inner", "ixseps1", "ixseps2", "jyseps1_1", "jyseps1_2", "jyseps2_1", "jyseps2_2", "ShiftAngle"] outdf["nx"] = xind[1] - xind[0] outdf["ny"] = yind[1] - yind[0] outdf["ny_inner"] = indf["ny_inner"][0] - yind[0] outdf["ixseps1"] = indf["ixseps1"][0] outdf["ixseps2"] = indf["ixseps2"][0] outdf["jyseps1_1"] = indf["jyseps1_1"][0] - yind[0] outdf["jyseps2_1"] = indf["jyseps2_1"][0] - yind[0] outdf["jyseps1_2"] = indf["jyseps1_2"][0] - yind[0] outdf["jyseps2_2"] = indf["jyseps2_2"][0] - yind[0] outdf["ShiftAngle"] = indf["ShiftAngle"][xind[0]:xind[1]] # Loop over all variables for v in list(indf.keys()): if v in special: continue # Skip these variables ndims = indf.ndims(v) if ndims == 0: # Copy scalars print("Copying variable: " + v) outdf[v] = indf[v][0] elif ndims == 2: # Assume [x,y] print("Slicing variable: " + v); outdf[v] = indf[v][xind[0]:xind[1], yind[0]:yind[1]] else: # Skip print("Skipping variable: " + v) indf.close() outdf.close()
def slice(infile, outfile, region=None, xind=None, yind=None): """ xind, yind - index ranges. Range includes first point, but not last point """ # Open input and output files indf = DataFile(infile) outdf = DataFile(outfile, create=True) nx = indf["nx"][0] ny = indf["ny"][0] if region: # Select a region of the mesh xind = [0, nx] if region == 0: # Lower inner leg yind = [0, indf["jyseps1_1"][0] + 1] elif region == 1: # Inner core yind = [indf["jyseps1_1"][0] + 1, indf["jyseps2_1"][0] + 1] elif region == 2: # Upper inner leg yind = [indf["jyseps2_1"][0] + 1, indf["ny_inner"][0]] elif region == 3: # Upper outer leg yind = [indf["ny_inner"][0], indf["jyseps1_2"][0] + 1] elif region == 4: # Outer core yind = [indf["jyseps1_2"][0] + 1, indf["jyseps2_2"][0] + 1] else: # Lower outer leg yind = [indf["jyseps2_2"][0] + 1, ny] else: # Use indices if not xind: xind = [0, nx] if not yind: yind = [0, ny] print("Indices: [%d:%d, %d:%d]" % (xind[0], xind[1], yind[0], yind[1])) # List of variables requiring special handling special = [ "nx", "ny", "ny_inner", "ixseps1", "ixseps2", "jyseps1_1", "jyseps1_2", "jyseps2_1", "jyseps2_2", "ShiftAngle" ] outdf["nx"] = xind[1] - xind[0] outdf["ny"] = yind[1] - yind[0] outdf["ny_inner"] = indf["ny_inner"][0] - yind[0] outdf["ixseps1"] = indf["ixseps1"][0] outdf["ixseps2"] = indf["ixseps2"][0] outdf["jyseps1_1"] = indf["jyseps1_1"][0] - yind[0] outdf["jyseps2_1"] = indf["jyseps2_1"][0] - yind[0] outdf["jyseps1_2"] = indf["jyseps1_2"][0] - yind[0] outdf["jyseps2_2"] = indf["jyseps2_2"][0] - yind[0] outdf["ShiftAngle"] = indf["ShiftAngle"][xind[0]:xind[1]] # Loop over all variables for v in list(indf.keys()): if v in special: continue # Skip these variables ndims = indf.ndims(v) if ndims == 0: # Copy scalars print("Copying variable: " + v) outdf[v] = indf[v][0] elif ndims == 2: # Assume [x,y] print("Slicing variable: " + v) outdf[v] = indf[v][xind[0]:xind[1], yind[0]:yind[1]] else: # Skip print("Skipping variable: " + v) indf.close() outdf.close()
def slice(infile, outfile, region=None, xind=None, yind=None): """Copy an X-Y slice from one DataFile to another Parameters ---------- infile : str Name of DataFile to read slice from outfile : str Name of DataFile to write slice to. File will be created, and will be overwritten if it already exists region : {0, 1, 2, 3, 4, 5, None}, optional Copy a whole region. The available regions are: - 0: Lower inner leg - 1: Inner core - 2: Upper inner leg - 3: Upper outer leg - 4: Outer core - 5: Lower outer leg xind, yind : (int, int), optional Index ranges for x and y. Range includes first point, but not last point TODO ---- - Rename to not clobber builtin `slice` - Better regions? """ # Open input and output files indf = DataFile(infile) outdf = DataFile(outfile, create=True) nx = indf["nx"][0] ny = indf["ny"][0] if region: # Select a region of the mesh xind = [0, nx] if region == 0: # Lower inner leg yind = [0, indf["jyseps1_1"][0] + 1] elif region == 1: # Inner core yind = [indf["jyseps1_1"][0] + 1, indf["jyseps2_1"][0] + 1] elif region == 2: # Upper inner leg yind = [indf["jyseps2_1"][0] + 1, indf["ny_inner"][0]] elif region == 3: # Upper outer leg yind = [indf["ny_inner"][0], indf["jyseps1_2"][0] + 1] elif region == 4: # Outer core yind = [indf["jyseps1_2"][0] + 1, indf["jyseps2_2"][0] + 1] else: # Lower outer leg yind = [indf["jyseps2_2"][0] + 1, ny] else: # Use indices if not xind: xind = [0, nx] if not yind: yind = [0, ny] print("Indices: [%d:%d, %d:%d]" % (xind[0], xind[1], yind[0], yind[1])) # List of variables requiring special handling special = [ "nx", "ny", "ny_inner", "ixseps1", "ixseps2", "jyseps1_1", "jyseps1_2", "jyseps2_1", "jyseps2_2", "ShiftAngle", ] outdf["nx"] = xind[1] - xind[0] outdf["ny"] = yind[1] - yind[0] outdf["ny_inner"] = indf["ny_inner"][0] - yind[0] outdf["ixseps1"] = indf["ixseps1"][0] outdf["ixseps2"] = indf["ixseps2"][0] outdf["jyseps1_1"] = indf["jyseps1_1"][0] - yind[0] outdf["jyseps2_1"] = indf["jyseps2_1"][0] - yind[0] outdf["jyseps1_2"] = indf["jyseps1_2"][0] - yind[0] outdf["jyseps2_2"] = indf["jyseps2_2"][0] - yind[0] outdf["ShiftAngle"] = indf["ShiftAngle"][xind[0]:xind[1]] # Loop over all variables for v in list(indf.keys()): if v in special: continue # Skip these variables ndims = indf.ndims(v) if ndims == 0: # Copy scalars print("Copying variable: " + v) outdf[v] = indf[v][0] elif ndims == 2: # Assume [x,y] print("Slicing variable: " + v) outdf[v] = indf[v][xind[0]:xind[1], yind[0]:yind[1]] else: # Skip print("Skipping variable: " + v) indf.close() outdf.close()
def slice(infile, outfile, region=None, xind=None, yind=None): """Copy an X-Y slice from one DataFile to another Parameters ---------- infile : str Name of DataFile to read slice from outfile : str Name of DataFile to write slice to. File will be created, and will be overwritten if it already exists region : {0, 1, 2, 3, 4, 5, None}, optional Copy a whole region. The available regions are: - 0: Lower inner leg - 1: Inner core - 2: Upper inner leg - 3: Upper outer leg - 4: Outer core - 5: Lower outer leg xind, yind : (int, int), optional Index ranges for x and y. Range includes first point, but not last point TODO ---- - Rename to not clobber builtin `slice` - Better regions? """ # Open input and output files indf = DataFile(infile) outdf = DataFile(outfile, create=True) nx = indf["nx"][0] ny = indf["ny"][0] if region: # Select a region of the mesh xind = [0, nx] if region == 0: # Lower inner leg yind = [0, indf["jyseps1_1"][0]+1] elif region == 1: # Inner core yind = [indf["jyseps1_1"][0]+1, indf["jyseps2_1"][0]+1] elif region == 2: # Upper inner leg yind = [indf["jyseps2_1"][0]+1, indf["ny_inner"][0]] elif region == 3: # Upper outer leg yind = [indf["ny_inner"][0], indf["jyseps1_2"][0]+1] elif region == 4: # Outer core yind = [indf["jyseps1_2"][0]+1, indf["jyseps2_2"][0]+1] else: # Lower outer leg yind = [indf["jyseps2_2"][0]+1, ny] else: # Use indices if not xind: xind = [0, nx] if not yind: yind = [0, ny] print("Indices: [%d:%d, %d:%d]" % (xind[0], xind[1], yind[0], yind[1])) # List of variables requiring special handling special = ["nx", "ny", "ny_inner", "ixseps1", "ixseps2", "jyseps1_1", "jyseps1_2", "jyseps2_1", "jyseps2_2", "ShiftAngle"] outdf["nx"] = xind[1] - xind[0] outdf["ny"] = yind[1] - yind[0] outdf["ny_inner"] = indf["ny_inner"][0] - yind[0] outdf["ixseps1"] = indf["ixseps1"][0] outdf["ixseps2"] = indf["ixseps2"][0] outdf["jyseps1_1"] = indf["jyseps1_1"][0] - yind[0] outdf["jyseps2_1"] = indf["jyseps2_1"][0] - yind[0] outdf["jyseps1_2"] = indf["jyseps1_2"][0] - yind[0] outdf["jyseps2_2"] = indf["jyseps2_2"][0] - yind[0] outdf["ShiftAngle"] = indf["ShiftAngle"][xind[0]:xind[1]] # Loop over all variables for v in list(indf.keys()): if v in special: continue # Skip these variables ndims = indf.ndims(v) if ndims == 0: # Copy scalars print("Copying variable: " + v) outdf[v] = indf[v][0] elif ndims == 2: # Assume [x,y] print("Slicing variable: " + v); outdf[v] = indf[v][xind[0]:xind[1], yind[0]:yind[1]] else: # Skip print("Skipping variable: " + v) indf.close() outdf.close()