import math


def adj_na(tm, na_conc, fgc, na_conc_0=None):
    """Adjust melting temperature of a duplexx based on sodium concentration.
    Based on Owczarzy et al, Biochemistry(43), 2004

    Args:
      tm (float): melting temperature at [Na] = 1 M.
      na_conc (float): monovalent species concentration in M.
      fgc (float): GC content fraction.
      na_conc_0 (float): initial monovalent species concentration.

    Returns:
      float: adjusted melting temperature.
    """

    # Default
    if type(None) is type(na_conc_0):
        na_conc_0 = 1.0
    if na_conc == na_conc_0:
        return tm
    Tm2 = tm

    # Parameters from paper
    na_a = 4.29e-5
    na_b = 3.95e-5
    na_c = 9.4e-6

    # Adjust
    if 1 == na_conc:
        Tm2r = 1.0 / tm
        Tm2r -= (na_a * fgc - na_b) * math.log(na_conc_0 / na_conc)
        Tm2r -= na_c * (math.log(na_conc_0 / na_conc) ** 2)
        Tm2 = 1.0 / Tm2r
    elif 0 < na_conc:
        Tm2r = 1.0 / tm
        Tm2r += (na_a * fgc - na_b) * math.log(na_conc / na_conc_0)
        Tm2r += na_c * (math.log(na_conc / na_conc_0) ** 2)
        Tm2 = 1.0 / Tm2r

    return Tm2