<commit_before>"""A NWS TextProduct that contains VTEC information
"""

# Stand Library Imports
import datetime

from pyiem.nws.product import TextProduct, TextProductException
from pyiem.nws.ugc import ugcs_to_text


def do_sql_hvtec(txn, segment):
    ''' Process the HVTEC in this product '''
    nwsli = segment.hvtec[0].nwsli.id
    if len(segment.bullets) < 4:
        return
    stage_text = ""
    flood_text = ""
    forecast_text = ""
    for _, bullet in enumerate(segment.bullets):
        # print("Enumerated bullet is ||%s||" % (bullet,))
        if bullet.strip().upper().find("FLOOD STAGE") == 0:
            flood_text = bullet
        if bullet.strip().upper().find("FORECAST") == 0:
            forecast_text = bullet
        if (bullet.strip().upper().find("AT ") == 0 and
                stage_text == ""):
            stage_text = bullet

    txn.execute("""
        INSERT into riverpro(nwsli, stage_text,
          flood_text, forecast_text, severity) VALUES
          (%s,%s,%s,%s,%s)
        """, (nwsli, stage_text, flood_text, forecast_text,
              segment.hvtec[0].severity))


class VTECProductException(TextProductException):
    ''' Something we can raise when bad things happen! '''
    pass


class VTECProduct(TextProduct):
    ''' Represents a text product of the LSR variety '''

    def __init__(self, text, utcnow=None, ugc_provider=None,
                 nwsli_provider=None):
        ''' constructor '''
        # Make sure we are CRLF above all else
        if text.find("\r\r\n") == -1:
            text = text.replace("\n", "\r\r\n")
        #  Get rid of extraneous whitespace on right hand side only
        text = "\r\r\n".join([a.rstrip() for a in text.split("\r\r\n")])

        TextProduct.__init__(self, text, utcnow, ugc_provider, nwsli_provider)
        self.nwsli_provider = nwsli_provider
        self.skip_con = self.get_skip_con()

    def debug_warning(self, txn, warning_table, ugcstring, vtec, segment,
                      ets):
        """ Get a more useful warning message for this failure """
        cnt = txn.rowcount
        txn.execute("""SELECT ugc,
                    issue at time zone 'UTC' as utc_issue,
                    expire at time zone 'UTC' as utc_expire,
                    updated at time zone 'UTC' as utc_updated,
                    status from """ + warning_table + """
                    WHERE wfo = %s and eventid = %s
                    and ugc in """+ugcstring+""" and significance = %s
                    and phenomena = %s ORDER by ugc ASC, issue ASC""",
                    (vtec.office, vtec.ETN, vtec.significance,
                     vtec.phenomena))
        debugmsg = "UGC    STA ISSUE            EXPIRE           UPDATED\n"

        def myfmt(val):
            """ Be more careful """
            if val is None:
                return '%-16s' % ('((NULL))',)
            return val.strftime("%Y-%m-%d %H:%M")

        for row in txn:
            debugmsg += "%s %s %s %s %s\n" % (row['ugc'], row['status'],
                                              myfmt(row['utc_issue']),
                                              myfmt(row['utc_expire']),
                                              myfmt(row['utc_updated']))
        return ('Warning: %s.%s.%s do_sql_vtec %s %s '
                'updated %s row, should %s rows\nUGCS: %s\n'
                'valid: %s expire: %s\n%s'
                ) % (vtec.phenomena, vtec.significance, vtec.ETN,
                     warning_table, vtec.action,
                     cnt, len(segment.ugcs), segment.ugcs, self.valid,
                     ets, debugmsg)

    def sql(self, txn):
        """Persist to the database

        Args:
          txn (psycopg2.transaction): A database transaction object that we can
            exec() database calls against.

        """
        for segment in self.segments:
            print segment.giswkt, segment.vtec
            if segment.giswkt and len(segment.vtec) == 0:
                self.warnings.append(("Product segment has LAT...LON, but "
                                      "does not have VTEC?"))
            if len(segment.ugcs) == 0 and len(segment.vtec) > 0:
                self.warnings.append(("UGC is missing for segment "
                                      "that has VTEC!"))
                continue
            if len(segment.ugcs) == 0:
                continue
            if len(segment.vtec) == 0:
                continue
            for vtec in segment.vtec:
                if vtec.status == 'T' or vtec.action == 'ROU':
                    continue
                if segment.sbw:
                    self.do_sbw_geometry(txn, segment, vtec)
                # Check for Hydro-VTEC stuff
                if (len(segment.hvtec) > 0 and
                        segment.hvtec[0].nwsli != "00000"):
                    do_sql_hvtec(txn, segment)

                self.do_sql_vtec(txn, segment, vtec)

    def which_warning_table(self, txn, vtec):
        """ Figure out which table we should work against """
        if vtec.action in ["NEW", ]:
            return "warnings_%s" % (self.valid.year,)
        # Lets query the database to look for any matching entries within
        # the past 3, 10, 31 days, to find with the product_issue was,<commit_msg>print statement sneaked into code :(<commit_after>"""A NWS TextProduct that contains VTEC information
"""

# Stand Library Imports
import datetime

from pyiem.nws.product import TextProduct, TextProductException
from pyiem.nws.ugc import ugcs_to_text


def do_sql_hvtec(txn, segment):
    ''' Process the HVTEC in this product '''
    nwsli = segment.hvtec[0].nwsli.id
    if len(segment.bullets) < 4:
        return
    stage_text = ""
    flood_text = ""
    forecast_text = ""
    for _, bullet in enumerate(segment.bullets):
        # print("Enumerated bullet is ||%s||" % (bullet,))
        if bullet.strip().upper().find("FLOOD STAGE") == 0:
            flood_text = bullet
        if bullet.strip().upper().find("FORECAST") == 0:
            forecast_text = bullet
        if (bullet.strip().upper().find("AT ") == 0 and
                stage_text == ""):
            stage_text = bullet

    txn.execute("""
        INSERT into riverpro(nwsli, stage_text,
          flood_text, forecast_text, severity) VALUES
          (%s,%s,%s,%s,%s)
        """, (nwsli, stage_text, flood_text, forecast_text,
              segment.hvtec[0].severity))


class VTECProductException(TextProductException):
    ''' Something we can raise when bad things happen! '''
    pass


class VTECProduct(TextProduct):
    ''' Represents a text product of the LSR variety '''

    def __init__(self, text, utcnow=None, ugc_provider=None,
                 nwsli_provider=None):
        ''' constructor '''
        # Make sure we are CRLF above all else
        if text.find("\r\r\n") == -1:
            text = text.replace("\n", "\r\r\n")
        #  Get rid of extraneous whitespace on right hand side only
        text = "\r\r\n".join([a.rstrip() for a in text.split("\r\r\n")])

        TextProduct.__init__(self, text, utcnow, ugc_provider, nwsli_provider)
        self.nwsli_provider = nwsli_provider
        self.skip_con = self.get_skip_con()

    def debug_warning(self, txn, warning_table, ugcstring, vtec, segment,
                      ets):
        """ Get a more useful warning message for this failure """
        cnt = txn.rowcount
        txn.execute("""SELECT ugc,
                    issue at time zone 'UTC' as utc_issue,
                    expire at time zone 'UTC' as utc_expire,
                    updated at time zone 'UTC' as utc_updated,
                    status from """ + warning_table + """
                    WHERE wfo = %s and eventid = %s
                    and ugc in """+ugcstring+""" and significance = %s
                    and phenomena = %s ORDER by ugc ASC, issue ASC""",
                    (vtec.office, vtec.ETN, vtec.significance,
                     vtec.phenomena))
        debugmsg = "UGC    STA ISSUE            EXPIRE           UPDATED\n"

        def myfmt(val):
            """ Be more careful """
            if val is None:
                return '%-16s' % ('((NULL))',)
            return val.strftime("%Y-%m-%d %H:%M")

        for row in txn:
            debugmsg += "%s %s %s %s %s\n" % (row['ugc'], row['status'],
                                              myfmt(row['utc_issue']),
                                              myfmt(row['utc_expire']),
                                              myfmt(row['utc_updated']))
        return ('Warning: %s.%s.%s do_sql_vtec %s %s '
                'updated %s row, should %s rows\nUGCS: %s\n'
                'valid: %s expire: %s\n%s'
                ) % (vtec.phenomena, vtec.significance, vtec.ETN,
                     warning_table, vtec.action,
                     cnt, len(segment.ugcs), segment.ugcs, self.valid,
                     ets, debugmsg)

    def sql(self, txn):
        """Persist to the database

        Args:
          txn (psycopg2.transaction): A database transaction object that we can
            exec() database calls against.

        """
        for segment in self.segments:
            if segment.giswkt and len(segment.vtec) == 0:
                self.warnings.append(("Product segment has LAT...LON, but "
                                      "does not have VTEC?"))
            if len(segment.ugcs) == 0 and len(segment.vtec) > 0:
                self.warnings.append(("UGC is missing for segment "
                                      "that has VTEC!"))
                continue
            if len(segment.ugcs) == 0:
                continue
            if len(segment.vtec) == 0:
                continue
            for vtec in segment.vtec:
                if vtec.status == 'T' or vtec.action == 'ROU':
                    continue
                if segment.sbw:
                    self.do_sbw_geometry(txn, segment, vtec)
                # Check for Hydro-VTEC stuff
                if (len(segment.hvtec) > 0 and
                        segment.hvtec[0].nwsli != "00000"):
                    do_sql_hvtec(txn, segment)

                self.do_sql_vtec(txn, segment, vtec)

    def which_warning_table(self, txn, vtec):
        """ Figure out which table we should work against """
        if vtec.action in ["NEW", ]:
            return "warnings_%s" % (self.valid.year,)
        # Lets query the database to look for any matching entries within
        # the past 3, 10, 31 days, to find with the product_issue was,
{'fraction_non_alphanumeric': 0.11611374407582939, 'fraction_numerical': 0.0051184834123222745, 'mean_word_length': 1.6490082852121517, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '210806', 'n_tokens_mistral': 3273, 'n_tokens_neox': 3018, 'n_words': 843}