def main(self): (self.options, self.args) = self.parser.parse_args() self._validate_options() if len(sys.argv) < 2: print(self.parser.error(_("Please enter at least 2 args"))) sys.edit(1) # do the work self._do_command()
def __init__(self, parent=None, device=None, **kwargs): if device is None: device = OpenCVCamera(**kwargs) ui = Ui_QOpenCVWidget() super(QOpenCV, self).__init__(parent, device=device, ui=ui) self.read = self.device.read def configureUi(self): logger.debug('configuring UI') self.ui.width.setMaximum(self.device.width) self.ui.height.setMaximum(self.device.height) self.widthChanged = self.ui.width.valueChanged self.heightChanged = self.ui.height.valueChanged def close(self): logger.debug('Closing camera interface') self.device = None def closeEvent(self): self.close() if __name__ == '__main__': from PyQt5.QtWidgets import QApplication import sys app = QApplication(sys.argv) wid = QOpenCV() wid.show() sys.edit(app.exec_())
def run(): context = PISM.Context() config = context.config com = context.com PISM.set_abort_on_sigint(True) WIDE_STENCIL = int(config.get_double("grid_max_stencil_width")) usage = \ """ pismi.py [-i IN.nc [-o OUT.nc]]/[-a INOUT.nc] [-inv_data inv_data.nc] [-inv_forward model] [-inv_design design_var] [-inv_method meth] where: -i IN.nc is input file in NetCDF format: contains PISM-written model state -o OUT.nc is output file in NetCDF format to be overwritten -a INOUT.nc is input/output file in NetCDF format to be appended to -inv_data inv_data.nc is data file containing extra inversion data (e.g. observed surface velocities) -inv_forward model forward model: only 'ssa' supported -inv_design design_var design variable name; one of 'tauc'/'hardav' for SSA inversions -inv_method meth algorithm for inversion [sd,nlcg,ign,tikhonov_lmvm] notes: * only one of -i/-a is allowed; both specify the input file * only one of -o/-a is allowed; both specify the output file * if -o is used, only the variables involved in inversion are written to the output file. * if -a is used, the varaibles involved in inversion are appended to the given file. No original variables in the file are changed. """ append_mode = False PISM.setVerbosityLevel(1) input_filename = PISM.optionsString("-i", "input file") append_filename = PISM.optionsString("-a", "append file", default=None) output_filename = PISM.optionsString("-o", "output file", default=None) if (input_filename is None) and (append_filename is None): PISM.verbPrintf(1, com, "\nError: No input file specified. Use one of -i [file.nc] or -a [file.nc].\n") sys.exit(0) if (input_filename is not None) and (append_filename is not None): PISM.verbPrintf(1, com, "\nError: Only one of -i/-a is allowed.\n") sys.exit(0) if (output_filename is not None) and (append_filename is not None): PISM.verbPrintf(1, com, "\nError: Only one of -a/-o is allowed.\n") sys.edit(0) if append_filename is not None: input_filename = append_filename output_filename = append_filename append_mode = True inv_data_filename = PISM.optionsString("-inv_data", "inverse data file", default=input_filename) verbosity = PISM.optionsInt("-verbose", "verbosity level", default=2) do_plotting = PISM.optionsFlag("-inv_plot", "perform visualization during the computation", default=False) do_final_plot = PISM.optionsFlag("-inv_final_plot", "perform visualization at the end of the computation", default=False) Vmax = PISM.optionsReal("-inv_plot_vmax", "maximum velocity for plotting residuals", default=30) design_var = PISM.optionsList("-inv_ssa", "design variable for inversion", "tauc,hardav", "tauc") do_pause = PISM.optionsFlag("-inv_pause", "pause each iteration", default=False) do_restart = PISM.optionsFlag("-inv_restart", "Restart a stopped computation.", default=False) use_design_prior = PISM.optionsFlag("-inv_use_design_prior", "Use prior from inverse data file as initial guess.", default=True) prep_module = PISM.optionsString("-inv_prep_module", "Python module used to do final setup of inverse solver", default=None) is_regional = PISM.optionsFlag("-regional", "Compute SIA/SSA using regional model semantics", default=False) using_zeta_fixed_mask = PISM.optionsFlag("-inv_use_zeta_fixed_mask", "Enforce locations where the parameterized design variable should be fixed. (Automatically determined if not provided)", default=True) inv_method = config.get_string("inv_ssa_method") if output_filename is None: output_filename = "pismi_" + os.path.basename(input_filename) saving_inv_data = (inv_data_filename != output_filename) PISM.setVerbosityLevel(verbosity) forward_run = SSAForwardRun(input_filename, inv_data_filename, design_var) forward_run.setup() design_param = forward_run.designVariableParameterization() solver = PISM.invert.ssa.createInvSSASolver(forward_run) modeldata = forward_run.modeldata vecs = modeldata.vecs grid = modeldata.grid # Determine the prior guess for tauc/hardav. This can be one of # a) tauc/hardav from the input file (default) # b) tauc/hardav_prior from the inv_datafile if -inv_use_design_prior is set design_prior = createDesignVec(grid, design_var, '%s_prior' % design_var) long_name = design_prior.metadata().get_string("long_name") units = design_prior.metadata().get_string("units") design_prior.set_attrs("", "best prior estimate for %s (used for inversion)" % long_name, units, "") if PISM.util.fileHasVariable(inv_data_filename, "%s_prior" % design_var) and use_design_prior: PISM.logging.logMessage(" Reading '%s_prior' from inverse data file %s.\n" % (design_var, inv_data_filename)) design_prior.regrid(inv_data_filename, critical=True) vecs.add(design_prior, writing=saving_inv_data) else: if not PISM.util.fileHasVariable(input_filename, design_var): PISM.verbPrintf(1, com, "Initial guess for design variable is not available as '%s' in %s.\nYou can provide an initial guess in the inverse data file.\n" % (design_var, input_filename)) exit(1) PISM.logging.logMessage("Reading '%s_prior' from '%s' in input file.\n" % (design_var, design_var)) design = createDesignVec(grid, design_var) design.regrid(input_filename, True) design_prior.copy_from(design) vecs.add(design_prior, writing=True) if using_zeta_fixed_mask: if PISM.util.fileHasVariable(inv_data_filename, "zeta_fixed_mask"): zeta_fixed_mask = PISM.model.createZetaFixedMaskVec(grid) zeta_fixed_mask.regrid(inv_data_filename) vecs.add(zeta_fixed_mask) else: if design_var == 'tauc': logMessage(" Computing 'zeta_fixed_mask' (i.e. locations where design variable '%s' has a fixed value).\n" % design_var) zeta_fixed_mask = PISM.model.createZetaFixedMaskVec(grid) zeta_fixed_mask.set(1) mask = vecs.mask with PISM.vec.Access(comm=zeta_fixed_mask, nocomm=mask): mq = PISM.MaskQuery(mask) for (i, j) in grid.points(): if mq.grounded_ice(i, j): zeta_fixed_mask[i, j] = 0 vecs.add(zeta_fixed_mask) adjustTauc(vecs.mask, design_prior) elif design_var == 'hardav': PISM.logging.logPrattle("Skipping 'zeta_fixed_mask' for design variable 'hardav'; no natural locations to fix its value.") pass else: raise NotImplementedError("Unable to build 'zeta_fixed_mask' for design variable %s.", design_var) # Convert design_prior -> zeta_prior zeta_prior = PISM.IceModelVec2S() zeta_prior.create(grid, "zeta_prior", PISM.WITH_GHOSTS, WIDE_STENCIL) design_param.convertFromDesignVariable(design_prior, zeta_prior) vecs.add(zeta_prior, writing=True) # Determine the initial guess for zeta. If we are restarting, load it from # the output file. Otherwise, if 'zeta_inv' is in the inverse data file, use it. # If none of the above, copy from 'zeta_prior'. zeta = PISM.IceModelVec2S() zeta.create(grid, "zeta_inv", PISM.WITH_GHOSTS, WIDE_STENCIL) zeta.set_attrs("diagnostic", "zeta_inv", "1", "zeta_inv") if do_restart: # Just to be sure, verify that we have a 'zeta_inv' in the output file. if not PISM.util.fileHasVariable(output_filename, 'zeta_inv'): PISM.verbPrintf(1, com, "Unable to restart computation: file %s is missing variable 'zeta_inv'", output_filename) exit(1) PISM.logging.logMessage(" Inversion starting from 'zeta_inv' found in %s\n" % output_filename) zeta.regrid(output_filename, True) elif PISM.util.fileHasVariable(inv_data_filename, 'zeta_inv'): PISM.logging.logMessage(" Inversion starting from 'zeta_inv' found in %s\n" % inv_data_filename) zeta.regrid(inv_data_filename, True) else: zeta.copy_from(zeta_prior) vel_ssa_observed = None vel_ssa_observed = PISM.model.create2dVelocityVec(grid, '_ssa_observed', stencil_width=2) if PISM.util.fileHasVariable(inv_data_filename, "u_ssa_observed"): vel_ssa_observed.regrid(inv_data_filename, True) vecs.add(vel_ssa_observed, writing=saving_inv_data) else: if not PISM.util.fileHasVariable(inv_data_filename, "u_surface_observed"): PISM.verbPrintf(1, context.com, "Neither u/v_ssa_observed nor u/v_surface_observed is available in %s.\nAt least one must be specified.\n" % inv_data_filename) exit(1) vel_surface_observed = PISM.model.create2dVelocityVec(grid, '_surface_observed', stencil_width=2) vel_surface_observed.regrid(inv_data_filename, True) vecs.add(vel_surface_observed, writing=saving_inv_data) sia_solver = PISM.SIAFD if is_regional: sia_solver = PISM.SIAFD_Regional vel_sia_observed = PISM.sia.computeSIASurfaceVelocities(modeldata, sia_solver) vel_sia_observed.metadata(0).set_name('u_sia_observed') vel_sia_observed.metadata(0).set_string('long_name', "x-component of the 'observed' SIA velocities") vel_sia_observed.metadata(1).set_name('v_sia_observed') vel_sia_observed.metadata(1).set_string('long_name', "y-component of the 'observed' SIA velocities") vel_ssa_observed.copy_from(vel_surface_observed) vel_ssa_observed.add(-1, vel_sia_observed) vecs.add(vel_ssa_observed, writing=True) # If the inverse data file has a variable tauc/hardav_true, this is probably # a synthetic inversion. We'll load it now so that it will get written # out, if needed, at the end of the computation in the output file. if PISM.util.fileHasVariable(inv_data_filename, "%s_true" % design_var): design_true = createDesignVec(grid, design_var, '%s_true' % design_var) design_true.regrid(inv_data_filename, True) design_true.read_attributes(inv_data_filename) vecs.add(design_true, writing=saving_inv_data) # Establish a logger which will save logging messages to the output file. message_logger = PISM.logging.CaptureLogger(output_filename, 'pismi_log') PISM.logging.add_logger(message_logger) if append_mode or do_restart: message_logger.readOldLog() # Prep the output file from the grid so that we can save zeta to it during the runs. if not append_mode: pio = PISM.PIO(grid.com, "netcdf3") pio.open(output_filename, PISM.PISM_READWRITE_MOVE) PISM.define_time(pio, grid.ctx().config().get_string("time_dimension_name"), grid.ctx().config().get_string("calendar"), grid.ctx().time().units_string(), grid.ctx().unit_system()) PISM.append_time(pio, grid.ctx().config().get_string("time_dimension_name"), grid.ctx().time().current()) pio.close() zeta.write(output_filename) # Log the command line to the output file now so that we have a record of # what was attempted PISM.util.writeProvenance(output_filename) # Attach various iteration listeners to the solver as needed for: # Iteration report. solver.addIterationListener(PISM.invert.ssa.printIteration) # Misfit reporting/logging. misfit_logger = PISM.invert.ssa.MisfitLogger() solver.addIterationListener(misfit_logger) if inv_method.startswith('tikhonov'): solver.addIterationListener(PISM.invert.ssa.printTikhonovProgress) # Saving the current iteration solver.addDesignUpdateListener(PISM.invert.ssa.ZetaSaver(output_filename)) # Plotting if do_plotting: solver.addIterationListener(InvSSAPlotListener(grid, Vmax)) if solver.method == 'ign': solver.addLinearIterationListener(InvSSALinPlotListener(grid, Vmax)) # Solver is set up. Give the user's prep module a chance to do any final # setup. if prep_module is not None: if prep_module.endswith(".py"): prep_module = prep_module[0:-2] exec "import %s as user_prep_module" % prep_module user_prep_module.prep_solver(solver) # Pausing (add this after the user's listeners) if do_pause: solver.addIterationListener(PISM.invert.listener.pauseListener) # Run the inverse solver! if do_restart: PISM.logging.logMessage('************** Restarting inversion. ****************\n') else: PISM.logging.logMessage('============== Starting inversion. ==================\n') # Try solving reason = solver.solveInverse(zeta_prior, vel_ssa_observed, zeta) if reason.failed(): PISM.logging.logError("Inverse solve FAILURE:\n%s\n" % reason.nested_description(1)) quit() PISM.logging.logMessage("Inverse solve success (%s)!\n" % reason.description()) (zeta, u) = solver.inverseSolution() # It may be that a 'tauc'/'hardav' was read in earlier. We replace it with # our newly generated one. if vecs.has(design_var): design = vecs.get(design_var) design_param.convertToDesignVariable(zeta, design) else: # Convert back from zeta to tauc or hardav design = createDesignVec(grid, design_var) design_param.convertToDesignVariable(zeta, design) vecs.add(design, writing=True) vecs.add(zeta, writing=True) u.metadata(0).set_name("u_ssa_inv") u.metadata(0).set_string("long_name", "x-component of SSA velocity computed by inversion") u.metadata(1).set_name("v_ssa_inv") u.metadata(1).set_string("long_name", "y-component of SSA velocity computed by inversion") vecs.add(u, writing=True) residual = PISM.model.create2dVelocityVec(grid, name='_inv_ssa_residual') residual.copy_from(u) residual.add(-1, vel_ssa_observed) r_mag = PISM.IceModelVec2S() r_mag.create(grid, "inv_ssa_residual", PISM.WITHOUT_GHOSTS, 0) sys = grid.ctx().unit_system() r_mag.set_attrs("diagnostic", "magnitude of mismatch between observed surface velocities and their reconstrution by inversion", "m s-1", "inv_ssa_residual", 0) r_mag.metadata().set_double("_FillValue", PISM.convert(sys, -0.01, 'm/year', 'm/s')) r_mag.metadata().set_double("valid_min", 0.0) r_mag.metadata().set_string("glaciological_units", "m year-1") r_mag.write_in_glaciological_units = True r_mag.set_to_magnitude(residual) r_mag.mask_by(vecs.land_ice_thickness) vecs.add(residual, writing=True) vecs.add(r_mag, writing=True) # Write solution out to netcdf file forward_run.write(output_filename, append=append_mode) # If we're not in append mode, the previous command just nuked # the output file. So we rewrite the siple log. if not append_mode: message_logger.write(output_filename) # Save the misfit history misfit_logger.write(output_filename)
def CreateData(self, file): now = datetime.datetime.now() f = open(file, "w") print >> f, '''/* * Copyright (C) 1995-%s Opera Software ASA. All rights reserved. * * This file is part of the Opera web browser. * It may not be distributed under any circumstances. */ /* * Please do not modify this file. * * This file is automatically generated by viewers.py * * To change this file, change modules/viewers/module.viewers, and run * the operasetup.py script. */ #ifdef HAS_COMPLEX_GLOBALS const ViewerTypes Viewers::defaultOperaViewerTypes[] = { ''' % (now.year) for v in self.viewers: if not 'Type' in v.prop: print >> sys.stderr, "Viewer %s has no Type" % v.name sys.exit(1) if not 'Ext' in v.prop: print >> sys.stderr, "Viewer %s has no Ext" % v.name sys.exit(1) if not 'Action' in v.prop: print >> sys.stderr, "Viewer %s has no Action" % v.name sys.exit(1) if not 'CType' in v.prop: print >> sys.stderr, "Viewer %s has no CType" % v.name sys.exit(1) if not 'Web handler allowed' in v.prop: print >> sys.stderr, "Viewer %s has no Web handler allowed" % v.name sys.exit(1) if not 'Allow any extension' in v.prop: print >> sys.stderr, "Viewer %s has no Allow any extension" % v.name sys.edit(1) if v.prop['Web handler allowed'] == '0' and v.prop[ 'Action'] == 'VIEWER_WEB_APPLICATION': print >> sys.stderr, "Viewer %s has contradictory configuration of \'Action\' and \'Web handler allowed\' settings" % v.name sys.exit(1) if not 'Container type' in v.prop: print >> sys.stderr, "Viewer %s has no Container type" % v.name sys.exit(1) if v.prop['Container type'] == 'none': container = 'VIEWER_NULL' else: container = v.prop['Container type'] v.CreateIf(f) decl = "\t{ " + v.prop['Type'] + ", " + v.prop['Ext'] + ", " decl += v.prop[ 'Action'] + ", FROM_RANGED_ENUM(URLContentType, " + v.prop[ 'CType'] + "), " decl += container + ", " if v.prop['Web handler allowed'] == '1': decl += "true" else: decl += "false" decl += ", " if v.prop['Allow any extension'] == '1': decl += "true" else: decl += "false" decl += " }," print >> f, decl v.CreateEndif(f) print >> f, '''}; #else //HAS_COMPLEX_GLOBALS void Viewers::init_defaultOperaViewerTypes() { ViewerTypes *local = defaultOperaViewerTypes; int i = 0; ''' for v in self.viewers: v.CreateIf(f) decl = "\tlocal[i].type=" + v.prop['Type'] + ", " decl += "local[i].ext=" + v.prop['Ext'] + ", " decl += "local[i].action=" + v.prop['Action'] + ", " decl += "local[i].ctype=FROM_RANGED_ENUM(URLContentType," + v.prop[ 'CType'] + "), " decl += "local[i].container=" + container + ";" decl += "local[i].web_handler_allowed=" if v.prop['Web handler allowed'] == '1': decl += "true;" else: decl += "false;" decl += "local[i++].allow_any_extension=" if v.prop['Allow any extension'] == '1': decl += "true;" else: decl += "false;" print >> f, decl v.CreateEndif(f) print >> f, ''' OP_ASSERT(i == defaultOperaViewerTypes_SIZE); } #endif //HAS_COMPLEX_GLOBALS''' f.close()
def run(): context = PISM.Context() config = context.config com = context.com PISM.set_abort_on_sigint(True) WIDE_STENCIL = int(config.get_number("grid.max_stencil_width")) usage = \ """ pismi.py [-i IN.nc [-o OUT.nc]]/[-a INOUT.nc] [-inv_data inv_data.nc] [-inv_forward model] [-inv_design design_var] [-inv_method meth] where: -i IN.nc is input file in NetCDF format: contains PISM-written model state -o OUT.nc is output file in NetCDF format to be overwritten -a INOUT.nc is input/output file in NetCDF format to be appended to -inv_data inv_data.nc is data file containing extra inversion data (e.g. observed surface velocities) -inv_forward model forward model: only 'ssa' supported -inv_design design_var design variable name; one of 'tauc'/'hardav' for SSA inversions -inv_method meth algorithm for inversion [sd,nlcg,ign,tikhonov_lmvm] notes: * only one of -i/-a is allowed; both specify the input file * only one of -o/-a is allowed; both specify the output file * if -o is used, only the variables involved in inversion are written to the output file. * if -a is used, the varaibles involved in inversion are appended to the given file. No original variables in the file are changed. """ append_mode = False input_filename = config.get_string("input.file") if len(input_filename) == 0: input_filename = None append = PISM.OptionString("-a", "append file") append_filename = append.value() if append.is_set() else None output_filename = config.get_string("output.file_name") if len(output_filename) == 0: output_filename = None if (input_filename is None) and (append_filename is None): PISM.verbPrintf( 1, com, "\nError: No input file specified. Use one of -i [file.nc] or -a [file.nc].\n" ) sys.exit(0) if (input_filename is not None) and (append_filename is not None): PISM.verbPrintf(1, com, "\nError: Only one of -i/-a is allowed.\n") sys.exit(0) if (output_filename is not None) and (append_filename is not None): PISM.verbPrintf(1, com, "\nError: Only one of -a/-o is allowed.\n") sys.edit(0) if append_filename is not None: input_filename = append_filename output_filename = append_filename append_mode = True inv_data_filename = PISM.OptionString("-inv_data", "inverse data file", input_filename).value() do_plotting = PISM.OptionBool( "-inv_plot", "perform visualization during the computation") do_final_plot = PISM.OptionBool( "-inv_final_plot", "perform visualization at the end of the computation") Vmax = PISM.OptionReal(context.unit_system, "-inv_plot_vmax", "maximum velocity for plotting residuals", "m / year", 30) design_var = PISM.OptionKeyword("-inv_ssa", "design variable for inversion", "tauc,hardav", "tauc").value() do_pause = PISM.OptionBool("-inv_pause", "pause each iteration") do_restart = PISM.OptionBool("-inv_restart", "Restart a stopped computation.") use_design_prior = config.get_flag("inverse.use_design_prior") prep_module = PISM.OptionString( "-inv_prep_module", "Python module used to do final setup of inverse solver") prep_module = prep_module.value() if prep_module.is_set() else None is_regional = PISM.OptionBool( "-regional", "Compute SIA/SSA using regional model semantics") using_zeta_fixed_mask = config.get_flag("inverse.use_zeta_fixed_mask") inv_method = config.get_string("inverse.ssa.method") if output_filename is None: output_filename = "pismi_" + os.path.basename(input_filename) saving_inv_data = (inv_data_filename != output_filename) forward_run = SSAForwardRun(input_filename, inv_data_filename, design_var) forward_run.setup() design_param = forward_run.designVariableParameterization() solver = PISM.invert.ssa.createInvSSASolver(forward_run) modeldata = forward_run.modeldata vecs = modeldata.vecs grid = modeldata.grid # Determine the prior guess for tauc/hardav. This can be one of # a) tauc/hardav from the input file (default) # b) tauc/hardav_prior from the inv_datafile if -inv_use_design_prior is set design_prior = createDesignVec(grid, design_var, '%s_prior' % design_var) long_name = design_prior.metadata().get_string("long_name") units = design_prior.metadata().get_string("units") design_prior.set_attrs( "", "best prior estimate for %s (used for inversion)" % long_name, units, units, "", 0) if PISM.util.fileHasVariable(inv_data_filename, "%s_prior" % design_var) and use_design_prior: PISM.logging.logMessage( " Reading '%s_prior' from inverse data file %s.\n" % (design_var, inv_data_filename)) design_prior.regrid(inv_data_filename, critical=True) vecs.add(design_prior, writing=saving_inv_data) else: if not PISM.util.fileHasVariable(input_filename, design_var): PISM.verbPrintf( 1, com, "Initial guess for design variable is not available as '%s' in %s.\nYou can provide an initial guess in the inverse data file.\n" % (design_var, input_filename)) exit(1) PISM.logging.logMessage( "Reading '%s_prior' from '%s' in input file.\n" % (design_var, design_var)) design = createDesignVec(grid, design_var) design.regrid(input_filename, True) design_prior.copy_from(design) vecs.add(design_prior, writing=True) if using_zeta_fixed_mask: if PISM.util.fileHasVariable(inv_data_filename, "zeta_fixed_mask"): zeta_fixed_mask = PISM.model.createZetaFixedMaskVec(grid) zeta_fixed_mask.regrid(inv_data_filename) vecs.add(zeta_fixed_mask) else: if design_var == 'tauc': logMessage( " Computing 'zeta_fixed_mask' (i.e. locations where design variable '%s' has a fixed value).\n" % design_var) zeta_fixed_mask = PISM.model.createZetaFixedMaskVec(grid) zeta_fixed_mask.set(1) mask = vecs.mask with PISM.vec.Access(comm=zeta_fixed_mask, nocomm=mask): for (i, j) in grid.points(): if mask.grounded_ice(i, j): zeta_fixed_mask[i, j] = 0 vecs.add(zeta_fixed_mask) adjustTauc(vecs.mask, design_prior) elif design_var == 'hardav': PISM.logging.logPrattle( "Skipping 'zeta_fixed_mask' for design variable 'hardav'; no natural locations to fix its value." ) pass else: raise NotImplementedError( "Unable to build 'zeta_fixed_mask' for design variable %s.", design_var) # Convert design_prior -> zeta_prior zeta_prior = PISM.IceModelVec2S(grid, "zeta_prior", PISM.WITH_GHOSTS, WIDE_STENCIL) design_param.convertFromDesignVariable(design_prior, zeta_prior) vecs.add(zeta_prior, writing=True) # Determine the initial guess for zeta. If we are restarting, load it from # the output file. Otherwise, if 'zeta_inv' is in the inverse data file, use it. # If none of the above, copy from 'zeta_prior'. zeta = PISM.IceModelVec2S(grid, "zeta_inv", PISM.WITH_GHOSTS, WIDE_STENCIL) zeta.set_attrs("diagnostic", "zeta_inv", "1", "1", "zeta_inv", 0) if do_restart: # Just to be sure, verify that we have a 'zeta_inv' in the output file. if not PISM.util.fileHasVariable(output_filename, 'zeta_inv'): PISM.verbPrintf( 1, com, "Unable to restart computation: file %s is missing variable 'zeta_inv'", output_filename) exit(1) PISM.logging.logMessage( " Inversion starting from 'zeta_inv' found in %s\n" % output_filename) zeta.regrid(output_filename, True) elif PISM.util.fileHasVariable(inv_data_filename, 'zeta_inv'): PISM.logging.logMessage( " Inversion starting from 'zeta_inv' found in %s\n" % inv_data_filename) zeta.regrid(inv_data_filename, True) else: zeta.copy_from(zeta_prior) vel_ssa_observed = None vel_ssa_observed = PISM.model.create2dVelocityVec(grid, '_ssa_observed', stencil_width=2) if PISM.util.fileHasVariable(inv_data_filename, "u_ssa_observed"): vel_ssa_observed.regrid(inv_data_filename, True) vecs.add(vel_ssa_observed, writing=saving_inv_data) else: if not PISM.util.fileHasVariable(inv_data_filename, "u_surface_observed"): PISM.verbPrintf( 1, context.com, "Neither u/v_ssa_observed nor u/v_surface_observed is available in %s.\nAt least one must be specified.\n" % inv_data_filename) exit(1) vel_surface_observed = PISM.model.create2dVelocityVec( grid, '_surface_observed', stencil_width=2) vel_surface_observed.regrid(inv_data_filename, True) vecs.add(vel_surface_observed, writing=saving_inv_data) sia_solver = PISM.SIAFD if is_regional: sia_solver = PISM.SIAFD_Regional vel_sia_observed = PISM.sia.computeSIASurfaceVelocities( modeldata, sia_solver) vel_sia_observed.metadata(0).set_name('u_sia_observed') vel_sia_observed.metadata(0).set_string( 'long_name', "x-component of the 'observed' SIA velocities") vel_sia_observed.metadata(1).set_name('v_sia_observed') vel_sia_observed.metadata(1).set_string( 'long_name', "y-component of the 'observed' SIA velocities") vel_ssa_observed.copy_from(vel_surface_observed) vel_ssa_observed.add(-1, vel_sia_observed) vecs.add(vel_ssa_observed, writing=True) # If the inverse data file has a variable tauc/hardav_true, this is probably # a synthetic inversion. We'll load it now so that it will get written # out, if needed, at the end of the computation in the output file. if PISM.util.fileHasVariable(inv_data_filename, "%s_true" % design_var): design_true = createDesignVec(grid, design_var, '%s_true' % design_var) design_true.regrid(inv_data_filename, True) try: f = PISM.File(com, inv_data_filename, PISM.PISM_NETCDF3, PISM.PISM_READONLY) PISM.read_attributes(f, design_true.get_name(), design_true.metadata()) finally: f.close() vecs.add(design_true, writing=saving_inv_data) # Establish a logger which will save logging messages to the output file. message_logger = PISM.logging.CaptureLogger(output_filename, 'pismi_log') PISM.logging.add_logger(message_logger) if append_mode or do_restart: message_logger.readOldLog() # Prep the output file from the grid so that we can save zeta to it during the runs. if not append_mode: pio = PISM.util.prepare_output(output_filename) pio.close() zeta.write(output_filename) # Log the command line to the output file now so that we have a record of # what was attempted PISM.util.writeProvenance(output_filename) # Attach various iteration listeners to the solver as needed for: # Iteration report. solver.addIterationListener(PISM.invert.ssa.printIteration) # Misfit reporting/logging. misfit_logger = PISM.invert.ssa.MisfitLogger() solver.addIterationListener(misfit_logger) if inv_method.startswith('tikhonov'): solver.addIterationListener(PISM.invert.ssa.printTikhonovProgress) # Saving the current iteration solver.addDesignUpdateListener(PISM.invert.ssa.ZetaSaver(output_filename)) # Plotting if do_plotting: solver.addIterationListener(InvSSAPlotListener(grid, Vmax)) if solver.method == 'ign': solver.addLinearIterationListener(InvSSALinPlotListener( grid, Vmax)) # Solver is set up. Give the user's prep module a chance to do any final # setup. if prep_module is not None: if prep_module.endswith(".py"): prep_module = prep_module[0:-2] exec("import %s as user_prep_module" % prep_module) user_prep_module.prep_solver(solver) # Pausing (add this after the user's listeners) if do_pause: solver.addIterationListener(PISM.invert.listener.pauseListener) # Run the inverse solver! if do_restart: PISM.logging.logMessage( '************** Restarting inversion. ****************\n') else: PISM.logging.logMessage( '============== Starting inversion. ==================\n') # Try solving reason = solver.solveInverse(zeta_prior, vel_ssa_observed, zeta) if reason.failed(): PISM.logging.logError("Inverse solve FAILURE:\n%s\n" % reason.nested_description(1)) quit() PISM.logging.logMessage("Inverse solve success (%s)!\n" % reason.description()) (zeta, u) = solver.inverseSolution() # It may be that a 'tauc'/'hardav' was read in earlier. We replace it with # our newly generated one. if vecs.has(design_var): design = vecs.get(design_var) design_param.convertToDesignVariable(zeta, design) else: # Convert back from zeta to tauc or hardav design = createDesignVec(grid, design_var) design_param.convertToDesignVariable(zeta, design) vecs.add(design, writing=True) vecs.add(zeta, writing=True) u.metadata(0).set_name("u_ssa_inv") u.metadata(0).set_string( "long_name", "x-component of SSA velocity computed by inversion") u.metadata(1).set_name("v_ssa_inv") u.metadata(1).set_string( "long_name", "y-component of SSA velocity computed by inversion") vecs.add(u, writing=True) residual = PISM.model.create2dVelocityVec(grid, name='_inv_ssa_residual') residual.copy_from(u) residual.add(-1, vel_ssa_observed) r_mag = PISM.IceModelVec2S(grid, "inv_ssa_residual", PISM.WITHOUT_GHOSTS, 0) r_mag.set_attrs( "diagnostic", "magnitude of mismatch between observed surface velocities and their reconstrution by inversion", "m s-1", "m year-1", "inv_ssa_residual", 0) r_mag.metadata().set_number("_FillValue", convert(-0.01, 'm/year', 'm/s')) r_mag.metadata().set_number("valid_min", 0.0) PISM.compute_magnitude(residual, r_mag) PISM.apply_mask(vecs.land_ice_thickness, 0.0, r_mag) vecs.add(residual, writing=True) vecs.add(r_mag, writing=True) # Write solution out to netcdf file (always append because the file was created already) forward_run.write(output_filename, append=True) # If we're not in append mode, the previous command just nuked # the output file. So we rewrite the siple log. if not append_mode: message_logger.write(output_filename) # Save the misfit history misfit_logger.write(output_filename)
def CreateData(self, file): now = datetime.datetime.now() f = open(file, "w") print >> f, '''/* * Copyright (C) 1995-%s Opera Software ASA. All rights reserved. * * This file is part of the Opera web browser. * It may not be distributed under any circumstances. */ /* * Please do not modify this file. * * This file is automatically generated by viewers.py * * To change this file, change modules/viewers/module.viewers, and run * the operasetup.py script. */ #ifdef HAS_COMPLEX_GLOBALS const ViewerTypes Viewers::defaultOperaViewerTypes[] = { ''' % (now.year) for v in self.viewers: if not 'Type' in v.prop: print >> sys.stderr, "Viewer %s has no Type" % v.name sys.exit(1) if not 'Ext' in v.prop: print >> sys.stderr, "Viewer %s has no Ext" % v.name sys.exit(1) if not 'Action' in v.prop: print >> sys.stderr, "Viewer %s has no Action" % v.name sys.exit(1) if not 'CType' in v.prop: print >> sys.stderr, "Viewer %s has no CType" % v.name sys.exit(1) if not 'Web handler allowed' in v.prop: print >> sys.stderr, "Viewer %s has no Web handler allowed" % v.name sys.exit(1) if not 'Allow any extension' in v.prop: print >> sys.stderr, "Viewer %s has no Allow any extension" % v.name sys.edit(1) if v.prop['Web handler allowed'] == '0' and v.prop['Action'] == 'VIEWER_WEB_APPLICATION': print >> sys.stderr, "Viewer %s has contradictory configuration of \'Action\' and \'Web handler allowed\' settings" % v.name sys.exit(1) if not 'Container type' in v.prop: print >> sys.stderr, "Viewer %s has no Container type" % v.name sys.exit(1) if v.prop['Container type'] == 'none': container = 'VIEWER_NULL' else: container = v.prop['Container type'] v.CreateIf(f) decl = "\t{ " + v.prop['Type'] + ", " + v.prop['Ext'] + ", " decl += v.prop['Action'] + ", FROM_RANGED_ENUM(URLContentType, " + v.prop['CType'] + "), " decl += container + ", " if v.prop['Web handler allowed'] == '1': decl += "true" else: decl += "false" decl += ", " if v.prop['Allow any extension'] == '1': decl += "true" else: decl += "false" decl += " }," print >> f, decl v.CreateEndif(f) print >> f, '''}; #else //HAS_COMPLEX_GLOBALS void Viewers::init_defaultOperaViewerTypes() { ViewerTypes *local = defaultOperaViewerTypes; int i = 0; ''' for v in self.viewers: v.CreateIf(f) decl = "\tlocal[i].type=" + v.prop['Type'] + ", " decl += "local[i].ext=" + v.prop['Ext'] + ", " decl += "local[i].action=" + v.prop['Action'] + ", " decl += "local[i].ctype=FROM_RANGED_ENUM(URLContentType," + v.prop['CType'] + "), " decl += "local[i].container=" + container + ";" decl += "local[i].web_handler_allowed=" if v.prop['Web handler allowed'] == '1': decl += "true;" else: decl += "false;" decl += "local[i++].allow_any_extension=" if v.prop['Allow any extension'] == '1': decl += "true;" else: decl += "false;" print >> f, decl v.CreateEndif(f) print >> f, ''' OP_ASSERT(i == defaultOperaViewerTypes_SIZE); } #endif //HAS_COMPLEX_GLOBALS''' f.close()
#!/usr/bin/env python3 import sys if len(sys.argv) < 3: print("Wrong Parameter") print("./copyfile.py file1 file2") sys.edit(1) f1 = open(sys.argv[1]) s = f1.read() f1.close() f2 = open(sys.argv[2]. 'w') f2.write(s) f2.close()
#!/usr/bin/env python # coding: utf-8 # In[3]: import sys #imported the sys library to exit the program when need be x = int(input()) #declared variable to take in x coordinate if x < -1000 or x > 1000 or x == 0: sys.exit() y = int(input()) #declared variable to take in y coordinate if y < -1000 or y > 1000 or y == 0: sys.edit() if x >= 0 and y >= 0: #quadrant 1 print("1") elif x < 0 and y >= 0: #quadrant 2 print("2") elif x >= 0 and y < 0: #quadrant 4 print("4") else: #quadrant 3 print("3") # In[ ]: