gem / oq-engine

OpenQuake Engine: a software for Seismic Hazard and Risk Analysis
https://github.com/gem/oq-engine/#openquake-engine
GNU Affero General Public License v3.0
382 stars 277 forks source link

BooreEtAl2014 get_mean_and_stddevs no longer working in newest openquake version #7894

Closed khud27 closed 2 years ago

khud27 commented 2 years ago

Hello, I had a script that worked before for calculating mean and standard deviation for a deterministic seismic hazard analysis on a series of sites from one rupture that does not work anymore. I have been trying to figure out what is wrong and trying to use the new "compute" function instead, but nothing is working for me. This is the error I get when I call the class I wrote (pasted below the error):

AttributeError                            Traceback (most recent call last)
Input In [33], in <module>
      2 #Calculate mdeian PGA (g) values of sites based on BSSA_14 ground motion model
      3 #This computation uses the segment by segment approach to rjb computation
      4 imts = [PGA()]
----> 5 mean_PGA, std = MedianBooreEtAl2014().get_mean(stations, rup, distances, imt)
      6 # mean_PGA, std = MedianBooreEtAl2014().get_mean(ctxs[0],imts)
      7 mean_PGA

Input In [32], in MedianBooreEtAl2014.get_mean(self, sites, rup, dists, imt)
      9         imt_per = imt.period
     10         stddev_types = set([StdDev.TOTAL, StdDev.INTRA_EVENT])
---> 11         [mean, std] = self.get_mean_and_stddevs(sites=sites, rup=rup, dists=dists, imt=imt, stddev_types=stddev_types)
     12 #         pga_rock = self._get_pga_on_rock(C_PGA, rup, dists)
     13 #         mean = (self._get_magnitude_scaling_term(C, rup) +
     14 #                 self._get_path_scaling(C, dists, rup.mag) +
     15 #                 self._get_site_scaling(C, pga_rock, sites, imt_per, dists.rjb))
     16         return(mean, std)

File ~/.local/lib/python3.9/site-packages/openquake/hazardlib/gsim/base.py:378, in GroundShakingIntensityModel.get_mean_and_stddevs(self, sites, rup, dists, imt, stddev_types)
    376 if self.compute.__annotations__.get("ctx") is numpy.recarray:
    377     cmaker = ContextMaker('*', [self], {'imtls': {imt: [0]}})
--> 378     ctx = cmaker.recarray([ctx])
    379 self.compute(ctx, [imt], mean, sig, tau, phi)
    380 stddevs = []

File ~/.local/lib/python3.9/site-packages/openquake/hazardlib/contexts.py:487, in ContextMaker.recarray(self, ctxs)
    485         val = numpy.nan
    486     else:  # never missing
--> 487         val = getattr(ctx, par)
    488     getattr(ra, par)[slc] = val
    489 ra.sids[slc] = ctx.sids

AttributeError: 'RuptureContext' object has no attribute 'rrup'

CLASS:
#Extract and modify code from Openquake to calculate median PGA and PGV values from BSSA_14 ground motion model
class MedianBooreEtAl2014(BooreEtAl2014):

    #Calculate median values
    def get_mean(self, sites, rup, dists, imt):
        C = self.COEFFS[imt]
        C_PGA = self.COEFFS[PGA()]
#         imt_per = 0 if imt.name == 'PGV' else imt.period
        imt_per = imt.period
        stddev_types = set([StdDev.TOTAL, StdDev.INTRA_EVENT])
        [mean, std] = self.get_mean_and_stddevs(sites=sites, rup=rup, dists=dists, imt=imt, stddev_types=stddev_types)
#         pga_rock = self._get_pga_on_rock(C_PGA, rup, dists)
#         mean = (self._get_magnitude_scaling_term(C, rup) +
#                 self._get_path_scaling(C, dists, rup.mag) +
#                 self._get_site_scaling(C, pga_rock, sites, imt_per, dists.rjb))
        return(mean, std)
micheles commented 2 years ago

You should use the mailing list for this kind of questions. Also, you should not be using the compute method directly, instead you should use get_mean_stds as documented in the manual: https://docs.openquake.org/oq-engine/advanced/developing.html#working-with-gmpes-directly-the-contextmaker