# -*- coding: utf-8 -*-# vim: tabstop=4 shiftwidth=4 softtabstop=4## Copyright (C) 2015-2023 GEM Foundation## OpenQuake is free software: you can redistribute it and/or modify it# under the terms of the GNU Affero General Public License as published# by the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## OpenQuake is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.importosimportjsonimportloggingimportnumpyfromopenquake.baselibimporthdf5fromopenquake.commonlibimportdatastore,logsfromopenquake.calculators.viewsimportview,text_tablefromopenquake.calculators.extractimportextract
[docs]defprint_(aw):ifhasattr(aw,'json'):try:attrs=hdf5.get_shape_descr(aw.json)exceptKeyError:# no shape_descr, for instance for oqparamprint(json.dumps(json.loads(aw.json),indent=2))returnvars(aw).update(attrs)ifhasattr(aw,'shape_descr'):print(text_table(aw.to_dframe(),ext='org'))elifhasattr(aw,'array'):ifaw.array.dtype.name=='object':# array of objectsforelinaw.array:print(el.decode('utf-8')ifisinstance(el,bytes)elseel)else:# structured arrayprint(text_table(aw.array,ext='org'))elifisinstance(aw,numpy.ndarray):print(text_table(aw,ext='org'))else:print(aw)
[docs]defmain(what='contents',calc_id:str_or_int=-1,extra=()):""" Show the content of a datastore (by default the last one). """datadir=datastore.get_datadir()ifwhat=='all':# show allifnotos.path.exists(datadir):returnrows=[]forcalc_idinlogs.get_calc_ids(datadir):try:ds=datastore.read(calc_id)oq=ds['oqparam']cmode,descr=oq.calculation_mode,oq.descriptionexceptException:# invalid datastore file, or missing calculation_mode# and description attributes, perhaps due to a manual killf=os.path.join(datadir,'calc_%s.hdf5'%calc_id)logging.warning('Unreadable datastore %s',f)continueelse:rows.append((calc_id,cmode,descr.encode('utf-8')))forrowinsorted(rows,key=lambdarow:row[0]):# by calc_idprint('#%d%s: %s'%row)returnds=datastore.read(calc_id)# this part is experimentalifview.keyfunc(what)inview:print_(view(what,ds))elifwhat.split('/',1)[0]inextract:print_(extract(ds,what,*extra))elifwhatinds:obj=ds.getitem(what)if'__pdcolumns__'inobj.attrs:df=ds.read_df(what)print(df)elifhasattr(obj,'items'):# is a group of datasetsprint(obj)else:# is a single datasetobj.refresh()# for SWMR modeprint_(hdf5.ArrayWrapper.from_(obj))else:print('%s not found'%what)ds.close()
main.what='key or view of the datastore'main.calc_id='calculation ID or datastore path'main.extra=dict(help='extra arguments',nargs='*')