def add_htm_ids_to_mysql_database_table(<EOL>raColName,<EOL>declColName,<EOL>tableName,<EOL>dbConn,<EOL>log,<EOL>primaryIdColumnName="<STR_LIT>",<EOL>cartesian=False,<EOL>batchSize=<NUM_LIT>,<EOL>reindex=False,<EOL>dbSettings=False):
log.debug('<STR_LIT>')<EOL>sqlQuery = """<STR_LIT>"""<EOL>rows = readquery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn<EOL>)<EOL>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>tableList = []<EOL>for row in rows:<EOL><INDENT>tableList.append(list(row.values())[<NUM_LIT:0>].lower())<EOL><DEDENT>if tableName.lower() not in tableList:<EOL><INDENT>message = "<STR_LIT>" % (tableName,)<EOL>log.critical(message)<EOL>raise IOError(message)<EOL><DEDENT>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>cursor = dbConn.cursor(ms.cursors.DictCursor)<EOL>sqlQuery = """<STR_LIT>""" % (tableName,)<EOL>cursor.execute(sqlQuery)<EOL>rows = cursor.fetchall()<EOL>desc = cursor.description<EOL>existingColumns = []<EOL>for i in range(len(desc)):<EOL><INDENT>existingColumns.append(desc[i][<NUM_LIT:0>])<EOL><DEDENT>if (raColName not in existingColumns) or (declColName not in existingColumns):<EOL><INDENT>message = '<STR_LIT>'<EOL>log.critical(message)<EOL>raise IOError(message)<EOL><DEDENT>if cartesian:<EOL><INDENT>htmCols = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>}<EOL><DEDENT>else:<EOL><INDENT>htmCols = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': '<STR_LIT>'<EOL>}<EOL><DEDENT>for key in list(htmCols.keys()):<EOL><INDENT>try:<EOL><INDENT>log.debug(<EOL>'<STR_LIT>' %<EOL>(tableName, ))<EOL>colExists ="""<STR_LIT>"""% (key, tableName)<EOL>colExists = readquery(<EOL>log=log,<EOL>sqlQuery=colExists,<EOL>dbConn=dbConn<EOL>)<EOL>switch = <NUM_LIT:0><EOL>if not colExists:<EOL><INDENT>if switch == <NUM_LIT:0>:<EOL><INDENT>print("<STR_LIT>" % locals())<EOL>switch = <NUM_LIT:1><EOL><DEDENT>sqlQuery = '<STR_LIT>' + tableName + '<STR_LIT>' +key + '<STR_LIT:U+0020>' + htmCols[key] + '<STR_LIT>'<EOL>writequery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn,<EOL>)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>log.critical('<STR_LIT>'<EOL>% (tableName, str(e)))<EOL>raise e<EOL><DEDENT><DEDENT>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>if reindex:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % locals()<EOL>keyname = readquery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn<EOL>)[<NUM_LIT:0>]["<STR_LIT>"]<EOL>if keyname != "<STR_LIT>":<EOL><INDENT>log.error('<STR_LIT>' % locals())<EOL>return<EOL><DEDENT>sqlQuery = """<STR_LIT>""" % locals()<EOL>writequery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn<EOL>)<EOL>sqlQuery = """<STR_LIT>""" % locals(<EOL>)<EOL><DEDENT>elif cartesian:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % locals(<EOL>)<EOL><DEDENT>else:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % locals(<EOL>)<EOL><DEDENT>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>rowCount = readquery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn,<EOL>quiet=False<EOL>)<EOL>totalCount = rowCount[<NUM_LIT:0>]["<STR_LIT:count>"]<EOL>total = totalCount<EOL>batches = int(total / batchSize)<EOL>count = <NUM_LIT:0><EOL>lastId = False<EOL>for i in range(batches + <NUM_LIT:1>):<EOL><INDENT>if total == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>count += batchSize<EOL>if count > batchSize:<EOL><INDENT>sys.stdout.write("<STR_LIT>")<EOL><DEDENT>if count > totalCount:<EOL><INDENT>count = totalCount<EOL><DEDENT>start = time.time()<EOL>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>if reindex:<EOL><INDENT>if lastId:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % (<EOL>primaryIdColumnName, raColName, declColName, tableName, primaryIdColumnName,  lastId, primaryIdColumnName, batchSize)<EOL><DEDENT>else:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % (<EOL>primaryIdColumnName, raColName, declColName, tableName, primaryIdColumnName, batchSize)<EOL><DEDENT><DEDENT>elif cartesian:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % (<EOL>primaryIdColumnName, raColName, declColName, tableName, raColName, raColName, batchSize)<EOL><DEDENT>else:<EOL><INDENT>sqlQuery = """<STR_LIT>""" % (<EOL>primaryIdColumnName, raColName, declColName, tableName, raColName, raColName, batchSize)<EOL><DEDENT>batch = readquery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn<EOL>)<EOL>if reindex and len(batch):<EOL><INDENT>lastId = batch[-<NUM_LIT:1>][primaryIdColumnName]<EOL><DEDENT>log.debug(<EOL>"""<STR_LIT>""" % locals())<EOL>raList = []<EOL>decList = []<EOL>pIdList = []<EOL>raList[:] = [r[raColName] for r in batch]<EOL>decList[:] = [r[declColName] for r in batch]<EOL>pIdList[:] = [r[primaryIdColumnName] for r in batch]<EOL>from HMpTy import htm<EOL>mesh16 = htm.HTM(<NUM_LIT:16>)<EOL>mesh13 = htm.HTM(<NUM_LIT>)<EOL>mesh10 = htm.HTM(<NUM_LIT:10>)<EOL>log.debug(<EOL>'<STR_LIT>' % (batchSize, tableName, ))<EOL>htm16Ids = mesh16.lookup_id(raList, decList)<EOL>htm13Ids = mesh13.lookup_id(raList, decList)<EOL>htm10Ids = mesh10.lookup_id(raList, decList)<EOL>log.debug(<EOL>'<STR_LIT>' % (batchSize, tableName, ))<EOL>if cartesian:<EOL><INDENT>log.debug(<EOL>'<STR_LIT>' % (batchSize, tableName, ))<EOL>cx = []<EOL>cy = []<EOL>cz = []<EOL>for r, d in zip(raList, decList):<EOL><INDENT>r = math.radians(r)<EOL>d = math.radians(d)<EOL>cos_dec = math.cos(d)<EOL>cx.append(math.cos(r) * cos_dec)<EOL>cy.append(math.sin(r) * cos_dec)<EOL>cz.append(math.sin(d))<EOL><DEDENT>updates = []<EOL>updates[:] = [{"<STR_LIT>": h16, "<STR_LIT>": h13, "<STR_LIT>": h10, primaryIdColumnName: pid, "<STR_LIT>": ccx, "<STR_LIT>": ccy, "<STR_LIT>": ccz} for h16,<EOL>h13, h10, pid, ccx, ccy, ccz in zip(htm16Ids, htm13Ids, htm10Ids, pIdList, cx, cy, cz)]<EOL>log.debug(<EOL>'<STR_LIT>' % (<EOL>batchSize, tableName, ))<EOL><DEDENT>else:<EOL><INDENT>log.debug('<STR_LIT>')<EOL>updates = []<EOL>updates[:] = [{"<STR_LIT>": h16, "<STR_LIT>": h13, "<STR_LIT>": h10, primaryIdColumnName: pid} for h16,<EOL>h13, h10, pid in zip(htm16Ids, htm13Ids, htm10Ids, pIdList)]<EOL>log.debug('<STR_LIT>')<EOL><DEDENT>if len(updates):<EOL><INDENT>log.debug(<EOL>'<STR_LIT>' % (tableName, ))<EOL>insert_list_of_dictionaries_into_database_tables(<EOL>dbConn=dbConn,<EOL>log=log,<EOL>dictList=updates,<EOL>dbTableName=tableName,<EOL>uniqueKeyList=[],<EOL>dateModified=False,<EOL>batchSize=<NUM_LIT>,<EOL>replace=True,<EOL>dbSettings=dbSettings,<EOL>dateCreated=False<EOL>)<EOL>log.debug(<EOL>'<STR_LIT>' % (tableName, ))<EOL><DEDENT>else:<EOL><INDENT>log.debug(<EOL>'<STR_LIT>' % (tableName, ))<EOL><DEDENT>percent = float(count) * <NUM_LIT> / float(totalCount)<EOL>print("<STR_LIT>" % locals())<EOL>end = time.time()<EOL>timediff = end - start<EOL>timediff = timediff * <NUM_LIT> / float(batchSize)<EOL>print("<STR_LIT>" % locals())<EOL><DEDENT>sqlQuery = "<STR_LIT>"<EOL>for index in ["<STR_LIT>", "<STR_LIT>", "<STR_LIT>"]:<EOL><INDENT>log.debug('<STR_LIT>' % locals())<EOL>iname = "<STR_LIT>" + index<EOL>asqlQuery = """<STR_LIT>""" % locals()<EOL>count = readquery(<EOL>log=log,<EOL>sqlQuery=asqlQuery,<EOL>dbConn=dbConn<EOL>)[<NUM_LIT:0>]["<STR_LIT>"]<EOL>if count == <NUM_LIT:0>:<EOL><INDENT>if not len(sqlQuery):<EOL><INDENT>sqlQuery += """<STR_LIT>""" % locals()<EOL><DEDENT>else:<EOL><INDENT>sqlQuery += """<STR_LIT>""" % locals()<EOL><DEDENT><DEDENT><DEDENT>if len(sqlQuery):<EOL><INDENT>writequery(<EOL>log=log,<EOL>sqlQuery=sqlQuery + "<STR_LIT:;>",<EOL>dbConn=dbConn,<EOL>)<EOL><DEDENT>log.debug('<STR_LIT>' % locals())<EOL>if reindex:<EOL><INDENT>print("<STR_LIT>" % locals())<EOL>sqlQuery = """<STR_LIT>""" % locals()<EOL>writequery(<EOL>log=log,<EOL>sqlQuery=sqlQuery,<EOL>dbConn=dbConn<EOL>)<EOL><DEDENT>print("<STR_LIT>" % locals())<EOL>log.debug('<STR_LIT>')<EOL>return None<EOL>
*Given a database connection, a name of a table and the column names for RA and DEC, generates ID for one or more HTM level in the table*

    **Key Arguments:**
        - ``raColName`` -- ra in sexegesimal
        - ``declColName`` -- dec in sexegesimal
        - ``tableName`` -- name of table to add htmid info to
        - ``dbConn`` -- database hosting the above table
        - ``log`` -- logger
        - ``primaryIdColumnName`` -- the primary id for the table
        - ``cartesian`` -- add cartesian columns. Default *False*
        - ``batchSize`` -- the size of the batches of rows to add HTMIds to concurrently. Default *2500*
        - ``reindex`` -- reindex the entire table
        - ``dbSettings`` -- yaml settings for database

    **Return:**
        - None

    **Usage:**

        .. code-block:: python 

            from HMpTy.mysql import add_htm_ids_to_mysql_database_table
            add_htm_ids_to_mysql_database_table(
                raColName="raDeg",
                declColName="decDeg",
                tableName="my_big_star_table",
                dbConn=dbConn,
                log=log,
                primaryIdColumnName="primaryId",
                reindex=False
            )