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()