IN-CORE / incore-services

IN-CORE Web Services is a component of IN-CORE. IN-CORE uses a service oriented architecture with a REST API for communicating with the different services.
Mozilla Public License 2.0
5 stars 1 forks source link

Handle some inconsistencies in eq demand types and units naming on hazard service #15

Closed ghost closed 1 year ago

ghost commented 3 years ago

DFR3 service has many fragilities that are defined with demandType that contain the word "sec", example "1.0 sec SD". When pyincore maps this fragility and makes values call, it will pass the fragility in this format and our values endpoint behavior is not clear or buggy in this case(see 4th location of 3rd example above). It returns inaccurate demandType in response and seemd to always calculate for "SA" and not respecting the 3rd word in the demandType ("SD" here). We should find a solution to make this convention consistent in hazard and dfr3 services

The earthquake value calculation code is not returning the applied demandUnits but just returning what is passed. Even if we pass "blah" as the unit for PGA to /values endpoint, it calculated the "g" value, but doesn't return it, it still returns "blah" in the result.

longshuicy commented 2 years ago

notes:

When creating new DRF3 curves, now we check if the demand type is valid. For EQ we do these:

// check first if demand type exist
            // sa and sd are special cases
            if (demandType.contains("sa") || demandType.contains("sd") || demandType.contains("sv")) {
                String[] demandTypePhrase = demandType.split("\\s+");

                // check if first word is positive numeric value
                Pattern pattern = Pattern.compile("\\d+(\\.\\d+)?");
                if (demandTypePhrase.length > 0 && demandTypePhrase.length <= 3
                    && pattern.matcher(demandTypePhrase[0]).matches()
                    && (demandTypePhrase[demandTypePhrase.length - 1].equals("sa")
                    || demandTypePhrase[demandTypePhrase.length - 1].equals("sd"))) {

                    // replace demand type 0.1 sec Sa with just sa
                    demandTypeEvaluated = demandTypePhrase[demandTypePhrase.length - 1];
                }
            }
longshuicy commented 2 years ago

When get the values of hazards, it try to parse the demand type:

public static String[] getHazardDemandComponents(String fullDemandType) {
        String[] demandParts = {"0.0", fullDemandType};
        if (fullDemandType.equalsIgnoreCase(PGA) || fullDemandType.equalsIgnoreCase(PGV) || fullDemandType.equalsIgnoreCase(PGD)) {
            return demandParts;
        } else {
            String[] demandSplit = fullDemandType.split(" ");
            if (demandSplit.length == 2) {
                demandParts[0] = demandSplit[0].trim();
                demandParts[1] = demandSplit[1].trim();
            } else if (demandSplit.length == 3) {
                // The assumption here is something like 0.3 Sec SA, 0.3 Sec SD, etc
                demandParts[0] = demandSplit[0].trim();
                demandParts[1] = demandSplit[2].trim();
            } else {
                // Unexpected format
                demandParts = null;
            }
        }
        return demandParts;
    }
longshuicy commented 2 years ago

demandUnit validation can be improved:

public static boolean verifyHazardDemandUnits(String demandType, String demandUnits) {
        if (demandType.equalsIgnoreCase(HazardUtil.PGA) || demandType.equalsIgnoreCase(HazardUtil.SA)) {
            return demandUnits.equalsIgnoreCase(HazardUtil.units_percg) || demandUnits.equalsIgnoreCase(HazardUtil.units_g);
        } else if (demandType.equalsIgnoreCase(HazardUtil.PGV) || demandType.equalsIgnoreCase(HazardUtil.SV)) {
            return demandUnits.equalsIgnoreCase(HazardUtil.units_cms) || demandUnits.equalsIgnoreCase(HazardUtil.units_ins);
        } else if (demandType.equalsIgnoreCase(HazardUtil.PGD) || demandType.equalsIgnoreCase(HazardUtil.SD)) {
            return demandUnits.equalsIgnoreCase(HazardUtil.units_cm) || demandUnits.equalsIgnoreCase(HazardUtil.units_m_abbr) ||
                demandUnits.equalsIgnoreCase(HazardUtil.units_in) || demandUnits.equalsIgnoreCase(HazardUtil.units_ft_abbr);
        }

        return false;
    }