def main(what, calc_id: int = -1, webapi=False, local=False, *, extract_dir='.'): """ Extract an output from the datastore and save it into an .hdf5 file. By default uses the WebAPI, otherwise the extraction is done locally. """ with performance.Monitor('extract', measuremem=True) as mon: if local: if calc_id == -1: calc_id = logs.dbcmd('get_job', calc_id).id aw = WebExtractor(calc_id, 'http://localhost:8800', '').get(what) elif webapi: aw = WebExtractor(calc_id).get(what) else: aw = Extractor(calc_id).get(what) w = what.replace('/', '-').replace('?', '-') if hasattr(aw, 'array') and isinstance(aw.array, str): # CSV string fname = os.path.join(extract_dir, '%s_%d.csv' % (w, calc_id)) with open(fname, 'w', encoding='utf-8') as f: f.write(aw.array) else: # save as npz fname = os.path.join(extract_dir, '%s_%d.npz' % (w, calc_id)) hdf5.save_npz(aw, fname) print('Saved', fname) if mon.duration > 1: print(mon)
def webex(self, calc_id, what): """Extract data from a remote calculation""" ex = WebExtractor(calc_id) try: return ex.get(what) finally: ex.close()
def ex(self, calc_id, what): """Extract data from a local engine server""" ex = WebExtractor(calc_id, 'http://localhost:8800', '', '') try: return ex.get(what) finally: ex.close()
def plot(what, calc_id=-1, other_id=None, webapi=False): """ Generic plotter for local and remote calculations. """ if '?' not in what: raise SystemExit('Missing ? in %r' % what) prefix, rest = what.split('?', 1) if prefix in 'hcurves hmaps' and 'imt=' not in rest: raise SystemExit('Missing imt= in %r' % what) elif prefix == 'uhs' and 'imt=' in rest: raise SystemExit('Invalid IMT in %r' % what) elif prefix in 'hcurves uhs disagg' and 'site_id=' not in rest: what += '&site_id=0' if prefix == 'disagg' and 'poe=' not in rest: what += '&poe_id=0' if webapi: xs = [WebExtractor(calc_id)] if other_id: xs.append(WebExtractor(other_id)) else: xs = [Extractor(calc_id)] if other_id: xs.append(Extractor(other_id)) make_figure = globals()['make_figure_' + prefix] plt = make_figure(xs, what) plt.show()
def plot(what='examples', calc_id=-1, other_id=None, webapi=False): """ Generic plotter for local and remote calculations. """ if what == 'examples': help_msg = ['Examples of possible plots:'] for k, v in globals().items(): if k.startswith('make_figure_'): help_msg.append(v.__doc__) raise SystemExit(''.join(help_msg)) if '?' not in what: raise SystemExit('Missing ? in %r' % what) prefix, rest = what.split('?', 1) if prefix in 'hcurves hmaps' and 'imt=' not in rest: raise SystemExit('Missing imt= in %r' % what) elif prefix == 'uhs' and 'imt=' in rest: raise SystemExit('Invalid IMT in %r' % what) elif prefix in 'hcurves uhs disagg' and 'site_id=' not in rest: what += '&site_id=0' if prefix == 'disagg' and 'poe=' not in rest: what += '&poe_id=0' if webapi: xs = [WebExtractor(calc_id)] if other_id: xs.append(WebExtractor(other_id)) else: xs = [Extractor(calc_id)] if other_id: xs.append(Extractor(other_id)) make_figure = globals()['make_figure_' + prefix] plt = make_figure(xs, what) plt.show()
def plot(what, calc_id=-1, other_id=None, webapi=False): """ Hazard curves plotter. Here are a few examples of use:: $ oq plot 'hcurves?kind=mean&imt=PGA&site_id=0' $ oq plot 'hmaps?kind=mean&imt=PGA' $ oq plot 'uhs?kind=mean&site_id=0' """ if '?' not in what: raise SystemExit('Missing ? in %r' % what) elif 'kind' not in what: raise SystemExit('Missing kind= in %r' % what) prefix, rest = what.split('?', 1) assert prefix in 'hcurves hmaps uhs', prefix if prefix in 'hcurves hmaps' and 'imt=' not in rest: raise SystemExit('Missing imt= in %r' % what) elif prefix == 'uhs' and 'imt=' in rest: raise SystemExit('Invalid IMT in %r' % what) elif prefix in 'hcurves uhs' and 'site_id=' not in rest: what += '&site_id=0' if webapi: xs = [WebExtractor(calc_id)] if other_id: xs.append(WebExtractor(other_id)) else: xs = [Extractor(calc_id)] if other_id: xs.append(Extractor(other_id)) make_figure = globals()['make_figure_' + prefix] plt = make_figure(xs, what) plt.show()
def importcalc(calc_id): """ Import a remote calculation into the local database. server, username and password must be specified in an openquake.cfg file. NB: calc_id can be a local pathname to a datastore not already present in the database: in that case it is imported in the db. """ dbserver.ensure_on() try: calc_id = int(calc_id) except ValueError: # assume calc_id is a pathname calc_id, datadir = datastore.extract_calc_id_datadir(calc_id) status = 'complete' remote = False else: remote = True job = logs.dbcmd('get_job', calc_id) if job is not None: sys.exit('There is already a job #%d in the local db' % calc_id) if remote: datadir = datastore.get_datadir() webex = WebExtractor(calc_id) status = webex.status['status'] hc_id = webex.oqparam.hazard_calculation_id if hc_id: sys.exit('The job has a parent (#%d) and cannot be ' 'downloaded' % hc_id) webex.dump('%s/calc_%d.hdf5' % (datadir, calc_id)) webex.close() with datastore.read(calc_id) as dstore: engine.expose_outputs(dstore, status=status) logging.info('Imported calculation %d successfully', calc_id)
def plot_hmaps(imt, calc_id, webapi=False): """ Mean hazard maps plotter. """ extractor = WebExtractor(calc_id) if webapi else Extractor(calc_id) with extractor: oq = extractor.oqparam sitecol = extractor.get('sitecol').array hmaps = extractor.get('hmaps/%s' % str(imt)).array lons, lats = sitecol['lon'], sitecol['lat'] plt = make_figure(lons, lats, imt, oq.imtls[str(imt)], oq.poes, hmaps) plt.show()
def extract(what, calc_id, webapi=True): """ Extract an output from the datastore and save it into an .hdf5 file. By default uses the WebAPI, otherwise the extraction is done locally. """ with performance.Monitor('extract', measuremem=True) as mon: if webapi: obj = WebExtractor(calc_id).get(what) else: obj = Extractor(calc_id).get(what) fname = '%s_%d.hdf5' % (what.replace('/', '-').replace('?', '-'), calc_id) obj.save(fname) print('Saved', fname) if mon.duration > 1: print(mon)
def extract(what, calc_id=-1, webapi=False, local=False, extract_dir='.'): """ Extract an output from the datastore and save it into an .hdf5 file. By default uses the WebAPI, otherwise the extraction is done locally. """ with performance.Monitor('extract', measuremem=True) as mon: if local: if calc_id == -1: calc_id = logs.dbcmd('get_job', calc_id).id aw = WebExtractor(calc_id, 'http://localhost:8800', '').get(what) elif webapi: aw = WebExtractor(calc_id).get(what) else: aw = Extractor(calc_id).get(what) w = what.replace('/', '-').replace('?', '-') if isinstance(aw.array, str): # a big string fname = os.path.join(extract_dir, '%s_%d.csv' % (w, calc_id)) with open(fname, 'w', encoding='utf-8') as f: f.write(aw.array) elif aw.is_good(): # a regular ArrayWrapper fname = os.path.join(extract_dir, '%s_%d.npz' % (w, calc_id)) hdf5.save_npz(aw, fname) else: # ArrayWrapper of strings, dictionaries or other types fname = os.path.join(extract_dir, '%s_%d.txt' % (w, calc_id)) open(fname, 'w').write(aw.toml()) print('Saved', fname) if mon.duration > 1: print(mon)
def extract(what, calc_id=-1, webapi=True, local=False): """ Extract an output from the datastore and save it into an .hdf5 file. By default uses the WebAPI, otherwise the extraction is done locally. """ with performance.Monitor('extract', measuremem=True) as mon: if local: obj = WebExtractor(calc_id, 'http://localhost:8800', '').get(what) elif webapi: obj = WebExtractor(calc_id).get(what) else: obj = Extractor(calc_id).get(what) w = what.replace('/', '-').replace('?', '-') fname = '%s_%d.npz' % (w, calc_id) hdf5.save_npz(obj, fname) print('Saved', fname) if mon.duration > 1: print(mon)
def importcalc(calc_id): """ Import a remote calculation into the local database. server, username and password must be specified in an openquake.cfg file. NB: calc_id can be a local pathname to a datastore not already present in the database: in that case it is imported in the db. """ dbserver.ensure_on() try: calc_id = int(calc_id) except ValueError: # assume calc_id is a pathname remote = False else: remote = True job = logs.dbcmd('get_job', calc_id) if job is not None: sys.exit('There is already a job #%d in the local db' % calc_id) if remote: datadir = datastore.get_datadir() webex = WebExtractor(calc_id) hc_id = webex.oqparam.hazard_calculation_id if hc_id: sys.exit('The job has a parent (#%d) and cannot be ' 'downloaded' % hc_id) webex.dump('%s/calc_%d.hdf5' % (datadir, calc_id)) webex.close() with datastore.read(calc_id) as dstore: engine.expose_outputs(dstore, status='complete') logging.info('Imported calculation %s successfully', calc_id)
def extract(what, calc_id=-1, webapi=True, local=False): """ Extract an output from the datastore and save it into an .hdf5 file. By default uses the WebAPI, otherwise the extraction is done locally. """ with performance.Monitor('extract', measuremem=True) as mon: if local: obj = WebExtractor(calc_id, 'http://localhost:8800', '').get(what) elif webapi: obj = WebExtractor(calc_id).get(what) else: obj = Extractor(calc_id).get(what) w = what.replace('/', '-').replace('?', '-') if not obj.shape: # is a dictionary of arrays fname = '%s_%d.txt' % (w, calc_id) open(fname, 'w').write(obj.toml()) else: # a regular ArrayWrapper fname = '%s_%d.hdf5' % (w, calc_id) obj.save(fname) print('Saved', fname) if mon.duration > 1: print(mon)