Uses Hanks & Kanamori formula for calculating moment magnitude from
    a scalar moment (Nm)
16817
def moment_magnitude_scalar(moment):
    '''
    
    '''
    if isinstance(moment, np.ndarray):
        return (2. / 3.) * (np.log10(moment) - 9.05)
    else:
        return (2. / 3.) * (log10(moment) - 9.05)
939576
def _get_magnitude_scaling(self, C, mag):
        
        if mag < self.CONSTANTS["mh"]:
            return C["e1"] + C["b1"] * (mag - self.CONSTANTS["mref"]) +\
                C["b2"] * ((mag - self.CONSTANTS["mref"]) ** 2.)
        else:
            d_m = self.CONSTANTS["mh"] - self.CONSTANTS["mref"]
            return C["e1"] + C["b3"] * (mag - self.CONSTANTS["mh"]) +\
                (C["b1"] * d_m) + C["b2"] * (d_m ** 2.)
872709
def _compute_magnitude_term(self, C, rup):
        
        return C['c2'] * (rup.mag - 8.0) + C['c3'] * (rup.mag - 8.0) ** 2
637401
def _get_moment_tensor_from_ndk_string(self, ndk_string):
        
        moment_tensor = GCMTMomentTensor('USE')
        tensor_data = _read_moment_tensor_from_ndk_string(ndk_string, 'USE')
        moment_tensor.tensor = tensor_data[0]
        moment_tensor.tensor_sigma = tensor_data[1]
        moment_tensor.exponent = tensor_data[2]
        return moment_tensor
1259062
def _calc(cls, **kwargs):
        
        zenith = 90.833333 # offical value

        jd = kwargs.get("jd", None)
        lat = kwargs.get("lat", None)
        lon = kwargs.get("lon", None)
        stage = kwargs.get("stage", None)
        if jd is None or stage is None or lat is None or lon is None:
            raise ValueError("Must supply an 'jd', 'lat, 'lon', and 'stage' parameter")

        if stage != SunCycles.RISING and stage != SunCycles.SETTING:
            raise ValueError("'stage' parameter must be %s or %s" % (SunCycles.RISING, SunCycles.SETTING))

        longhr = lon / 15.
        if stage == SunCycles.RISING:
            apx = jd + ( (6 - longhr) / 24 )
        elif stage == SunCycles.SETTING:
            apx = jd + ( (18 - longhr) / 24 )

        sun_mean_anom = ( 0.9856 * apx ) - 3.289 # sun's mean anomaly
        #sun's longitude
        sun_lon = sun_mean_anom + (1.916 * np.sin( np.radians(sun_mean_anom) )) \
                + (0.02 * np.sin( np.radians(2 * sun_mean_anom) )) + 282.634

        if sun_lon > 360:
            sun_lon = sun_lon - 360
        elif sun_lon < 0:
            sun_lon = sun_lon + 360

        right_ascension = np.degrees(np.arctan( 0.91764 * np.tan( np.radians(sun_lon) ) )) # sun's right ascension

        if right_ascension > 360:
            right_ascension = right_ascension - 360
        elif right_ascension < 0:
            right_ascension = right_ascension + 360

        # put sun's right ascension value in the same quadrant as the sun's
        # true longitude
        lQuad = 90. * np.floor(sun_lon / 90.)
        raQuad = 90. * np.floor(right_ascension / 90.)
        right_ascension = right_ascension + ( lQuad - raQuad)
        right_ascension = right_ascension / 15. # Convert to hours

        # Sun's declination
        sinDecl = 0.39782 * np.sin( np.radians(sun_lon) )
        cosDecl = np.cos( np.arcsin( sinDecl ) )

        # Sun's local hour angle
        cosHr = (np.cos( np.radians(zenith) ) - ( sinDecl * np.sin(np.radians(lat)) )) \
                / ( cosDecl * np.cos( np.radians(lat) ) )

        if cosHr > 1: # Sun doesnt rise on this loc on this date
            return -1, -1
        elif cosHr < -1: # Sun doesnt set on this location on this date
            return -1, -1
        elif stage == SunCycles.RISING: # Sunrise
            hr = 360 - np.degrees(np.arccos(cosHr))
        elif stage == SunCycles.SETTING:  # Sunset
            hr = np.degrees(np.arccos(cosHr))

        hr = hr / 15. # Convert angle to hours

        localTime = hr + right_ascension - ( 0.06571 * apx ) - 6.622# local meantime of rise/set

        UTtime = localTime - longhr # adjust to UTC

        if UTtime < 0:
            UTtime = UTtime + 24
        elif UTtime > 24:
            UTtime = UTtime - 24

        hour = np.floor(UTtime)

        minute = (UTtime - hour) * 60
        if minute == 60:
            hour = hour + 1
            minute = 0

        return hour, minute
1221958
def dim_dtau(self, pars):
        
        """
        self._set_parameters(pars)
        # term 1
        num1 = -self.m * (self.w ** self.c) * self.c\
            * (self.tau ** (self.c - 1)) * np.sin(self.ang)
        term1 = self.sigmai * num1 / self.denom

        # term 2
        num2a = -self.m * self.otc * np.sin(self.ang)
        num2b = 2 * (self.w ** 2.0) * self.c * (self.tau ** (self.c - 1)) *\
            np.cos(self.ang)
        num2c = 2 * self.c * (self.w ** (self.c * 2)) *\
            (self.tau ** (2 * self.c - 1))
        term2 = self.sigma0 * num2a * (num2b + num2c) / (self.denom ** 2)

        result = term1 + term2

        return result
576122
def _get_magnitude_term(self, C, mag):
        
        f_mag = C["c0"] + C["c1"] * mag
        if (mag > 4.5) and (mag <= 5.5):
            return f_mag + (C["c2"] * (mag - 4.5))
        elif (mag > 5.5) and (mag <= 6.5):
            return f_mag + (C["c2"] * (mag - 4.5)) + (C["c3"] * (mag - 5.5))
        elif mag > 6.5:
            return f_mag + (C["c2"] * (mag - 4.5)) + (C["c3"] * (mag - 5.5)) +\
                (C["c4"] * (mag - 6.5))
        else:
            return f_mag
277398
def get_conversion_factor(self, in_nc, num_nc_files):
        """"""
        data_in_nc = Dataset(in_nc)

        # convert from kg/m^2 (i.e. mm) to m
        conversion_factor = 0.001

        # ECMWF units are in m
        if data_in_nc.variables[self.runoff_vars[0]] \
                .getncattr("units") == "m":
            conversion_factor = 1

        # ftp://hydro1.sci.gsfc.nasa.gov/data/s4pa/GLDAS_V1/README.GLDAS.pdf
        if "s" in data_in_nc.variables[self.runoff_vars[0]] \
                .getncattr("units"):
            # that means kg/m^2/s in GLDAS v1 that is 3-hr avg,
            # so multiply by 3 hr (ex. 3*3600). Assumed same
            # for others (ex. 1*3600).
            # If combining files, need to take average of these,
            # so divide by number of files
            conversion_factor *= \
                self.simulation_time_step_seconds / \
                num_nc_files
        data_in_nc.close()

        return conversion_factor
1312500
def rho2sigma(self, rho0, Ra, Rs):
        
        return np.pi * rho0 * Ra * Rs / (Rs + Ra)
1180328
def get_wallace_tensor(self, tau):
        
        b = 0.5 * (np.einsum("ml,kn->klmn", tau, np.eye(3)) +
                   np.einsum("km,ln->klmn", tau, np.eye(3)) +
                   np.einsum("nl,km->klmn", tau, np.eye(3)) +
                   np.einsum("kn,lm->klmn", tau, np.eye(3)) +
                   -2*np.einsum("kl,mn->klmn", tau, np.eye(3)))
        strain = self.get_strain_from_stress(tau)
        b += self.get_effective_ecs(strain)
        return b
152796
function pm(m) {
    if (m[4] === 0 && m[5] === 0) {
      if (m[1] === 0 && m[2] === 0) {
        if (m[0] === 1 && m[3] === 1) {
          return '';
        }
        return 'scale(' + pf(m[0]) + ' ' + pf(m[3]) + ')';
      }
      if (m[0] === m[3] && m[1] === -m[2]) {
        var a = Math.acos(m[0]) * 180 / Math.PI;
        return 'rotate(' + pf(a) + ')';
      }
    } else {
      if (m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1) {
        return 'translate(' + pf(m[4]) + ' ' + pf(m[5]) + ')';
      }
    }
    return 'matrix(' + pf(m[0]) + ' ' + pf(m[1]) + ' ' + pf(m[2]) + ' ' +
      pf(m[3]) + ' ' + pf(m[4]) + ' ' + pf(m[5]) + ')';
  }
354935
def _get_magnitude_term(self, C, mag):
        
        lny = C['C1'] + (C['C3'] * ((8.5 - mag) ** 2.))
        if mag > 6.3:
            return lny + (-C['H'] * C['C5']) * (mag - 6.3)
        else:
            return lny + C['C2'] * (mag - 6.3)
1119231
def absolute_magnitude(distance_modulus,g,r,prob=None):
    
    V = g - 0.487*(g - r) - 0.0249
        
    flux = np.sum(10**(-(V-distance_modulus)/2.5))
    Mv = -2.5*np.log10(flux)
    return Mv
656768
def dim_dc(self, pars):
        
        """
        self._set_parameters(pars)
        # term 1
        num1a = self.m * np.sin(self.ang) * np.log(self.w * self.tau)\
            * self.otc
        num1b = self.m * self.otc * np.pi / 2 * np.cos(np.pi / 2)
        term1 = self.sigma0 * (-num1a - num1b) / self.denom

        # term 2
        num2a = -self.m * self.otc * np.cos(self.ang)
        num2b = -2 * np.log(self.w * self.tau) * self.otc * np.cos(self.ang)
        num2c = 2 * self.otc * np.pi / 2 * np.cos(self.ang)
        num2d = 2 * np.log(self.w * self.tau) * self.otc2
        numerator = num2a * (num2b + num2c) + num2d
        term2 = self.sigma0 * numerator / (self.denom ** 2)

        result = term1 + term2

        return result
121910
def _calculate_ms_from_base(self, size):
        """"""
        hw = size.x * 0.5
        hh = size.y * 0.5

        a = datatypes.Point(hw, hh).get_rotated(self.angle)
        b = datatypes.Point(-hw, hh).get_rotated(self.angle)
        c = datatypes.Point(hw, -hh).get_rotated(self.angle)
        d = datatypes.Point(-hw, -hh).get_rotated(self.angle)

        minp = a.get_minimum(b).get_minimum(c).get_minimum(d)
        maxp = a.get_maximum(b).get_maximum(c).get_maximum(d)
        return maxp - minp
944166
def _compute_magnitude(self, rup, C):
        
        m_h = 5.5
        return C['c1'] + (C['c2'] * (rup.mag - m_h))
702893
def magnitude(self):
    

    return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2 + self.w ** 2)