def test_dimension_type(self): "File.dimension_type()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) f.dimension_type("time", ctx.unit_system()) == PISM.T_AXIS f.dimension_type("x", ctx.unit_system()) == PISM.X_AXIS f.dimension_type("y", ctx.unit_system()) == PISM.Y_AXIS try: f.dimension_type("z", ctx.unit_system()) fail(backend) except RuntimeError: pass f.close() f = PISM.File(ctx.com(), self.file_dim_types, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) def check(names, axis_type): for c in names: assert f.dimension_type(c, ctx.unit_system()) == axis_type check(self.x_names + self.strange_x_names, PISM.X_AXIS) check(self.y_names + self.strange_y_names, PISM.Y_AXIS) check(self.z_names + self.strange_z_names, PISM.Z_AXIS) check(self.t_names + self.strange_t_names, PISM.T_AXIS) assert f.dimension_type("unknown_axis", ctx.unit_system()) == PISM.UNKNOWN_AXIS f.close()
def test_find_variable(self): "File.find_variable(short_name, standard_name)" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.find_variable("v", "standard_name").exists assert f.find_variable("v", "standard_name").found_using_standard_name assert f.find_variable("other_name", "standard_name").found_using_standard_name assert f.find_variable("other_name", "standard_name").name == "v" assert not f.find_variable("v", "").found_using_standard_name assert f.find_variable("missing", "other_standard_name").exists == False assert f.find_variable("missing", "other_standard_name").name == "" f.close() f = PISM.File(ctx.com(), self.file_inconsistent, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) try: f.find_variable("v", "standard_name") fail(backend) except RuntimeError: pass
def create_scalar_forcing(file_name, variable_name, units, values, times, time_bounds=None, time_name=None): "Create a dummy scalar forcing file (delta_T, etc)." ctx = PISM.Context() if time_name is None: time_name = ctx.config.get_string("time.dimension_name") bounds_name = time_name + "_bounds" if time_bounds is not None and times is None: # override times times = [] for k in range(len(values)): times.append(0.5 * (time_bounds[2 * k + 0] + time_bounds[2 * k + 1])) try: output = PISM.File(ctx.com, file_name, PISM.PISM_NETCDF3, PISM.PISM_READWRITE) except: output = PISM.File(ctx.com, file_name, PISM.PISM_NETCDF3, PISM.PISM_READWRITE_CLOBBER) output.define_dimension(time_name, len(times)) output.define_variable(time_name, PISM.PISM_DOUBLE, [time_name]) output.write_attribute(time_name, "long_name", "time") output.write_attribute(time_name, "axis", "T") output.write_attribute(time_name, "units", ctx.time.units_string()) output.write_attribute(time_name, "calendar", ctx.config.get_string("time.calendar")) output.define_variable(variable_name, PISM.PISM_DOUBLE, [time_name]) output.write_attribute(variable_name, "units", units) if time_bounds is not None: output.write_attribute(time_name, "bounds", bounds_name) output.define_dimension("bnds", 2) output.define_variable(bounds_name, PISM.PISM_DOUBLE, [time_name, "bnds"]) output.write_variable(bounds_name, [0, 0], [len(times), 2], np.array(time_bounds, dtype=np.float64).data) output.write_variable(time_name, [0], [len(times)], np.array(times, dtype=np.float64).data) output.write_variable(variable_name, [0], [len(values)], np.array(values, dtype=np.float64).data) output.close()
def test_backend(self): "File.backend()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.backend() == backend f.close()
def test_nvariables(self): "File.nvariables()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.nvariables() == 4 # time, x, y, v f.close()
def test_sync(self): "File.sync()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) f.sync() f.close()
def test_define_dimension(self): "File.define_dimension()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) f.define_dimension("dim_{}".format(backend), 10 + backend) f.close()
def test_find_dimension(self): "File.find_dimension()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.find_dimension("x") assert not f.find_dimension("z") f.close()
def readOldLog(self): """If the :file:`.nc` file we are logging to already has a log, read it in to the log we are about to make so that we append to it rather than overwriting it.""" d = PISM.File(PISM.Context().com, self.filename, PISM.PISM_NETCDF3, PISM.PISM_READONLY) self.log += d.read_text_attribute("PISM_GLOBAL", self.attr) d.close()
def test_nrecords(self): "File.nrecords()" for F in self.files: for backend in backends: f = PISM.File(ctx.com(), F, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.nrecords() == 1 f.close()
def test_attribute_name(self): "File.attribute_name()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.attribute_name("time", 0) == "units" assert f.attribute_name("PISM_GLOBAL", 0) == "global_text_attr" f.close()
def test_missing_file(self): for backend in backends: try: f = PISM.File(ctx.com(), "missing_file.nc", backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) fail(backend) except RuntimeError: pass
def test_create_clobber(self): "File(..., PISM_READWRITE_CLOBBER)" try: for backend in backends: f = PISM.File(ctx.com(), "test_filename.nc", backend, PISM.PISM_READWRITE_CLOBBER, ctx.pio_iosys_id()) finally: os.remove("test_filename.nc")
def test_find_variable(self): "File.find_variable(short_name)" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.find_variable("time") assert not f.find_variable("z") f.close()
def run(dt, restart=False): "Run the model for 1 time step, stop, save model state, restart, do 1 more step." grid = PISM.IceGrid.Shallow(ctx.ctx, Lx, Ly, 0, 0, N, N, PISM.CELL_CORNER, PISM.NOT_PERIODIC) model = PISM.LingleClark(grid) geometry = PISM.Geometry(grid) bed_uplift = PISM.IceModelVec2S(grid, "uplift", PISM.WITHOUT_GHOSTS) # start with a flat bed, no ice, and no uplift geometry.bed_elevation.set(0.0) geometry.ice_thickness.set(0.0) geometry.sea_level_elevation.set(0.0) geometry.ensure_consistency(0.0) bed_uplift.set(0.0) # initialize the model model.bootstrap(geometry.bed_elevation, bed_uplift, geometry.ice_thickness, geometry.sea_level_elevation) # add the disc load add_disc_load(geometry.ice_thickness, disc_radius, disc_thickness) # do 1 step model.step(geometry.ice_thickness, geometry.sea_level_elevation, dt) if restart: # save the model state filename = "lingle_clark_model_state.nc" try: PISM.util.prepare_output(filename) f = PISM.File(grid.com, filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE) model.write_model_state(f) f.close() # create a new model del model model = PISM.LingleClark(grid) # initialize ctx.config.set_string("input.file", filename) options = PISM.process_input_options(grid.com, ctx.config) model.init(options, geometry.ice_thickness, geometry.sea_level_elevation) finally: os.remove(filename) # do 1 more step model.step(geometry.ice_thickness, geometry.sea_level_elevation, dt) return model
def test_dimension_length(self): "File.dimension_length()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.dimension_length("time") == 1 assert f.dimension_length("x") == 3 assert f.dimension_length("y") == 5 assert f.dimension_length("z") == 0 f.close()
def __call__(self, message, verbosity): """Saves the message to our internal log string and writes the string out to the file.""" if verbosity <= self.verbosity_threshold: timestamp = time.strftime('%Y-%m-%d %H:%M:%S') self.log = "%s%s: %s" % (self.log, timestamp, message) d = PISM.File(PISM.Context().com, self.filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE) d.redef() d.write_attribute("PISM_GLOBAL", self.attr, self.log) d.close()
def test_nrecords_variable(self): "File.nrecords(variable)" for F in [self.file_with_time, self.file_without_time]: for backend in backends: f = PISM.File(ctx.com(), F, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.nrecords("v", "standard_name", ctx.unit_system()) == 1 # found using the standard name assert f.nrecords("w", "standard_name", ctx.unit_system()) == 1 assert f.nrecords("missing", "", ctx.unit_system()) == 0 f.close()
def test_attribute_type(self): "File.attribute_type()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) f.attribute_type("x", "units") == PISM.PISM_CHAR f.attribute_type("x", "spacing_meters") == PISM.PISM_DOUBLE f.attribute_type("x", "missing") == PISM.PISM_NAT f.attribute_type("PISM_GLOBAL", "global_text_att") == PISM.PISM_CHAR f.close()
def test_remove_attribute(self): "File.remove_attribute()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) assert f.attribute_type("time", "units") == PISM.PISM_CHAR f.remove_attribute("time", "units") assert f.attribute_type("time", "units") == PISM.PISM_NAT f.write_attribute("time", "units", "seconds since 1-1-1") assert f.attribute_type("time", "units") == PISM.PISM_CHAR f.close()
def write(self, filename=None, attribute=None): """Save a copy of our log to the specified file and attribute.""" if filename is None: filename = self.filename if attribute is None: attribute = self.attr d = PISM.File(PISM.Context().com, filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE) d.redef() d.write_attribute("PISM_GLOBAL", attribute, self.log) d.close()
def test_dimensions(self): "File.dimensions()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) assert f.dimensions("v") == ("time", "y", "x") variable_name = "scalar_variable_{}".format(backend) f.define_variable(variable_name, PISM.PISM_BYTE, []) assert f.dimensions(variable_name) == () f.close()
def logging_test(): "Test the PISM.logging module" grid = create_dummy_grid() import PISM.logging as L log_filename = filename("log") try: PISM.File(grid.com, log_filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE_MOVE) c = L.CaptureLogger(log_filename) L.clear_loggers() L.add_logger(L.print_logger) L.add_logger(c) L.log("log message\n", L.kError) L.logError("error message\n") L.logWarning("warning message\n") L.logMessage("log message (again)\n") L.logDebug("debug message\n") L.logPrattle("prattle message\n") c.write() # default arguments c.readOldLog() finally: os.remove(log_filename) log_filename = filename("other_log") try: PISM.File(grid.com, log_filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE_MOVE) c.write(log_filename, "other_log") # non-default arguments finally: os.remove(log_filename)
def test_create_move(self): "File(..., PISM_READWRITE_MOVE)" try: for backend in backends: f = PISM.File(ctx.com(), "test_filename.nc", backend, PISM.PISM_READWRITE_MOVE, ctx.pio_iosys_id()) finally: os.remove("test_filename.nc") try: os.remove("test_filename.nc~") except: pass
def test_write_attribute_string(self): "File.write_attribute(string)" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) f.write_attribute("v", "new_attribute", "test string") assert f.read_text_attribute("v", "new_attribute") == "test string" f.write_attribute("PISM_GLOBAL", "new_global_attr", "test_global") assert f.read_text_attribute("PISM_GLOBAL", "new_global_attr") == "test_global" f.close()
def write(self, filename, append=False): if not append: PISM.invert.ssa.SSAForwardRunFromInputFile.write(self, filename) else: grid = self.grid vecs = self.modeldata.vecs pio = PISM.File(grid.com, filename, PISM.PISM_NETCDF3, PISM.PISM_READWRITE) self.modeldata.vecs.write(filename) pio.close()
def test_close(self): "File.close()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) f.close() try: f.close() # closing twice is an error fail(backend) except RuntimeError: pass
def _initGrid(self): """Initialize grid size and periodicity. Called from :meth:`PISM.ssa.SSARun.setup`.""" if self.is_regional: registration = PISM.CELL_CORNER else: registration = PISM.CELL_CENTER ctx = PISM.Context().ctx pio = PISM.File(ctx.com(), self.input_filename, PISM.PISM_NETCDF3, PISM.PISM_READONLY) self.grid = PISM.IceGrid.FromFile(ctx, pio, "enthalpy", registration) pio.close()
def test_append_history(self): "File.read_text_attribute()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READWRITE, ctx.pio_iosys_id()) try: f.remove_attribute("PISM_GLOBAL", "history") except: pass f.append_history("one") f.append_history("two") assert f.read_text_attribute("PISM_GLOBAL", "history") == "twoone" f.close()
def test_nattributes(self): "File.nattributes()" for backend in backends: f = PISM.File(ctx.com(), self.file_with_time, backend, PISM.PISM_READONLY, ctx.pio_iosys_id()) assert f.nattributes( "time") == 4 # units, axis, calendar, long_name assert f.nattributes( "x" ) == 5 # units, axis, long_name, standard_name, spacing_meters assert f.nattributes("PISM_GLOBAL") == 2 f.close()