def addArgos(row, tag_id, animal_id, timevalue, gt, bd): """ Append a new row to Argos, then to Transmit """ feature_id = 0 feature_type = 'argos' # Doesn't seem to matter if db receives datetime or string timeVal = util.date2Str(timevalue, rawFmt) if bd else timevalue try: dev = (tag_id, animal_id, timeVal, feature_type, gt ) # instantiate Argos object argosObj = tables.Argos(*dev, **row) # returns 0 if duplicate feature_id = dbutil.dbTransact(conn, argosObj.sql_insert(), argosObj.param_dict()) if feature_id: transmit_id = addTransmit(feature_id, row, bd) except Exception as e: print 'addArgos Error ' + e.message conn.rollback() finally: dev = None argosObj = None conn.commit() updateDevice(tag_id, timevalue, bd) return feature_id
def insertMeasData(vals): try: dataObj = tables.Measurement_Data(*vals) # Instantiate Measure_Data object data_id = db_util.dbTransact(conn, dataObj.sql_insert(), # Insert measure_data row dataObj.param_dict()) except Exception as e: conn.rollback() print 'insertMeasData function error: ' + e.message finally: return data_id
def Tel_Message(message_type, tagDict, reader, count): fmt = '%m/%d/%Y %H:%M:%S' ptt = 0 measure_ids = [] data_ids = [] try: while reader.line_num < count: # date format is funky! 2015/12/06 15:45:49 for row in reader: # New Ptt? if row['Ptt']: if int(row['Ptt']) != ptt: ptt = int(row['Ptt']) tag_id = tagDict.get(ptt)[0] #=integer pttStart = tagDict.get(ptt)[2] #=datetime pttStop = tagDict.get(ptt)[3] print 'Processing {0} for PTT: {1}'.format(message_type, ptt) #Compare: tag's start/stop date if str2Date(row['Receive Time'], fmt) <= pttStart: continue if str2Date(row['Receive Time'], fmt) >= pttStop: continue # Ready to add message row vals = (tag_id, message_type) messageObj = eval('tables.{}(*vals, **row)'.format(message_type)) # Instantiate Message object measurement_id = db_util.dbTransact(conn, messageObj.sql_insert(), # Insert message row messageObj.param_dict()) if measurement_id: measure_ids.append(measurement_id) print 'Message {0} at {1} added'.format(message_type, str2Date(row['Receive Time'],fmt)) # Iter thru dict of fieldnames for param, field in eval('params.{}.viewitems()'.format(message_type)): if field in reader.fieldnames: if row[field]: if param == 'low_voltage' and row[field] == 'No': pass elif param == 'sws_fail' and row[field] == 'No': pass else: vals = (param, row[field], measurement_id) # add measure_data row data_id = insertMeasData(vals) if data_id: data_ids.append(data_id) conn.commit() except Exception as e: conn.rollback() print 'Tel_Message function error message: ' + e.message #print datarow() finally: return (measure_ids, data_ids)
def addTransmit(feature_id, row, bd): transmit_id = 0 timevalue = format_date(row['ldatetime'], bd) try: feat = (timevalue, feature_id) transmitObj = tables.ArgosTx(*feat, **row) transmit_id = dbutil.dbTransact(conn, transmitObj.sql_insert(), transmitObj.param_dict()) except Exception as e: print 'addTransmit error ' + e.message conn.rollback() finally: feat = None transmitObj = None conn.commit() return transmit_id, timevalue
def addTransmit(feature_id, row, bd): transmit_id = 0 timeVal = util.date2Str(row['Msg Date'], rawFmt) if bd else row['Msg Date'] try: feat = (timeVal, feature_id) transmitObj = tables.ArgosTx(*feat, **row) transmit_id = dbutil.dbTransact(conn, transmitObj.sql_insert(), transmitObj.param_dict()) except Exception as e: print 'addTransmit error ' + e.message conn.rollback() finally: feat = None transmitObj = None conn.commit() updatePttList(int(row['Platform ID No.']), row['Msg Date'], bd) return transmit_id
def addArgos(row, tag_id, animal_id, timevalue, gt, bd): """ Append a new row to Argos then to Message and Transmit """ feature_id = 0 feature_type = 'argos' try: dev = (tag_id, animal_id, timevalue, feature_type, gt ) # instantiate Argos object argosObj = tables.Argos(*dev, **row) # returns 0 if duplicate feature_id = dbutil.dbTransact(conn, argosObj.sql_insert(), argosObj.param_dict()) if feature_id: transmit_id = addTransmit(feature_id, row, bd) except Exception as e: print 'addArgos Error ' + e.message conn.rollback() finally: dev = None argosObj = None conn.commit() return feature_id
def WC_FastGPS(message_type, tagDict, reader, count): fmt = '%m/%d/%Y %H:%M:%S' ptt = 0 feature_ids = [] feature_type = 'fastloc' try: while reader.line_num < count: for row in reader: # Trap: new Ptt if row['Name'] and int(row['Name']) != ptt: ptt = int(row['Name']) tag_id = tagDict.get(ptt)[0] animal_id = tagDict.get(ptt)[1] pttStart = tagDict.get(ptt)[2] # =datetime pttStop = tagDict.get(ptt)[3] print 'Processing FastLoc messages for Ptt: '+str(ptt) if row['Latitude'] and row['Latitude']: timevalue = row['Received'] #Compare: tag's start date if str2Date(timevalue,fmt) < pttStart: continue dev = (tag_id, animal_id, timevalue, feature_type) # Instantiate Encounter sub-object featureObj = tables.FastLoc(*dev, **row) # Insert feature row feature_id = db_util.dbTransact(conn, featureObj.sql_insert(), featureObj.param_dict()) if feature_id: feature_ids.append(feature_id) print 'Location at {} added'.format(timevalue) conn.commit() except Exception as e: conn.rollback() print 'WC_FastGPS function error message: ' + e.message finally: return (feature_ids)
# measure from new point to start vertex arcpy.Near_analysis(temp_points, in_point, None, None, None, "GEODESIC") with arcpy.da.SearchCursor(temp_points, "NEAR_DIST") as sCur: new_dist = sCur.next()[0] / 1000.0 # check speed(s) # calculate new timevalue factor = duration * (new_dist / dist) timeval = start_time + timedelta(seconds=factor) # get animal_id and tag_id from encounter conn = dbutil.getDbConn('wtg_gdb') sql = 'SELECT animal_id, tag_id from geodata.argos WHERE feature_id = %(fromPt)s' params = {'fromPt': fromPt} feat_row = dbutil.dbSelect(conn, sql, params) animID, tagID = feat_row[0][:] # build point for new proxy row with arcpy.da.SearchCursor(in_point, "shape_m") as sCur: row = sCur.next() #[0] proxy1 = arcpy.Point(*row[0]) vals = (tagID, animID, timeval, '') row = {"source": 'rubber band', "latitude": proxy1.Y, "longitude": proxy1.X} proxyObj = tables.Proxy(*vals, **row) feature_id = dbutil.dbTransact(conn, proxyObj.sql_insert(), proxyObj.param_dict()) conn.commit() arcpy.AddMessage(feature_id) #a_point = arcpy.SetParameter(2)
def main(tagDict, proj): global measures message_type = 'tel_counter' pttList = sorted([p for p in tagDict.keys()]) for ptt in pttList: print ptt tag_id = tagDict[ptt][0] # skip tag(s) # if tag_id not in [506]: # continue result = dbutil.getTelParam(conn, tag_id) param = result[0] proc = result[1] if param not in ('CTAS', 'CNOT', 'CNOS'): continue transmits = dbutil.getRawData(conn, tag_id) for row in transmits: # if row[2] < 508255: # continue val1, val2, valid = exam_data(row[4], proc) # process raw data if val1 == 0: # Skip weird string lengths continue print(ptt, row[3], valid, val1, val2) # build message for insert meas_row = { 'tag_id': tag_id, 'transmit_id': row[2], 'value_time': row[3], 'message_type': message_type, 'transmit_time': row[3], 'valid': valid } # insert message, return measurement_id vals = (tag_id, message_type) messageObj = eval('tables.{}(*vals, **meas_row)'.format( message_type)) # Instantiate Message object measurement_id = dbutil.dbTransact( conn, messageObj.sql_insert(), # Insert message row messageObj.param_dict()) if measurement_id: # get from DB if valid == 'False': for val in [str(val1), str(val2)]: data_row = val vals = (param, data_row, measurement_id) data_id = insertMeasData(vals) if data_id: conn.commit() else: print 'DB Error' elif valid == 'True': data_row = str(val1) vals = (param, data_row, measurement_id) data_id = insertMeasData(vals) if data_id: conn.commit() else: print 'DB Error'
def WC_Message(message_type, tagDict, reader, count): fmt = '%m/%d/%Y %H:%M:%S' ptt = 0 measure_ids = [] data_ids = [] histo_type = '' limit_type = '' datefld = params.wc_start[message_type] try: while reader.line_num < count: for row in reader: if message_type == 'wc_histos': # Trap empty rows! if row['HistType'] in params.limit_types: #Get parameter_id's datarow = {k:v for k,v in row.items() if v} histo_type, limit_type, skip = WC_HistoLimits(datarow, histo_type, limit_type) if skip: skip = False continue continue elif row['HistType'] in params.histo_skip: continue elif row['HistType'] in params.limit_skip: continue elif not row['HistType']: continue if message_type == 'wc_status': if not row['Received']: # Skip line without a date continue empty = False for param, field in eval('params.{}.viewitems()'.format(message_type)): if row[field]: empty = False else: empty = True if empty: continue if message_type == 'wc_behavior': # Trap: for 'What = Dive only if row['What'] != 'Dive': continue if row['Ptt']: # Compare: new Ptt if int(row['Ptt']) != ptt: ptt = int(row['Ptt']) tag_id = tagDict.get(ptt)[0] #=integer pttStart = tagDict.get(ptt)[2] #=datetime pttStop = tagDict.get(ptt)[3] print 'Processing {0} for PTT: {1}'.format(message_type, ptt) if str2Date(row[datefld],fmt) < pttStart: #Compare: tag's start date continue if str2Date(row[datefld],fmt) > pttStop: #Compare: tag's stop date continue # Ready to add message row vals = (tag_id, message_type) messageObj = eval('tables.{}(*vals, **row)'.format(message_type)) # Instantiate Message object measurement_id = db_util.dbTransact(conn, messageObj.sql_insert(), # Insert message row messageObj.param_dict()) # *****The Problem is that an empty status row creates a message with no data if measurement_id: measure_ids.append(measurement_id) print 'Message {0} at {1} added'.format(message_type, row[datefld]) if message_type == 'wc_histos': # Add measure_data to message # Capture histo values via parameter dict for bin, val in params.histo_bins[limit_type].items(): if bin in row.viewkeys(): if row[bin]!='0' and row[bin]!='': vals = (val, row[bin], measurement_id) # add measure_data row data_id = insertMeasData(vals) if data_id: data_ids.append(data_id) conn.commit() else: # Behavior or Status message # Iter thru dict of fieldnames for param, field in eval('params.{}.viewitems()'.format(message_type)): if field in reader.fieldnames: if row[field]: vals = (param, row[field], measurement_id) # add measure_data row data_id = insertMeasData(vals) if data_id: data_ids.append(data_id) conn.commit() except Exception as e: conn.rollback() print 'WC_Message function error message: ' + e.message #print datarow() finally: return (measure_ids, data_ids)