3

I am attempting to filter some time stamped data which I have enabled through the Convert Time Field tool, depending on a true/false condition. I am doing this through the field calculator within Model Builder (Arcmap 10.2).

The data is structured as follows:

Example data[1]

I am processing the data to remove points that are less than two hours apart from one another in order to standardize the time intervals. The data is also grouped by series ID (cat) and I have a assigned a sequential field to list the chronological order of the points grouped by ID. I have also added a field which should be populated by 0 or 1 depending as to whether the time condition is met.

How I am trying to implement this condition is through a series of IF ELIF statements. There are three options to this:

Firstly the record is returned true by default as it the start point of the series, this is shown by the sequence value = 0. The timestamp when sequence = 0 is also taken as the variable n.

Secondly, the record is returned as true if the timestamp is atleast 2 hours in the future from the previous record's timestamp (n). n Is then altered to be (n + 2 hours) to be used in the next record's condition.

Thirdly if the timestamp is not 2 hours ahead of the previous record's it is labelled as false and n does not change.

I am attempting this within a python codeblock in field calculator on the conditional field (time_frame_2hr) using the code:

  import datetime

  def fnTime(sequence,vms_time,time_con,order):
    n = df.loc[df[sequence]==0,vms_time]
    previous_time_con = df.loc[df[order],(order-1),time_con]
    if sequence == 0:
      return 1
    elif sequence != 0 and vms_time < (time(n) + (datetime.timedelta(hours=2))):
      return 0
    elif sequence != 0 and previous_time_con == 0 and vms_time >= (time(n) + (datetime.timedelta(hours=2))):
          return 1
    elif sequence != 0 and previous_time_con == 1:
          n = (time(n) + (datetime.timedelta(hours=2)))
          if sequence != 0 and vms_time >= (time(n) + (datetime.timedelta(hours=2))):
            return 1
          elif sequence != 0 and vms_time < (time(n) + (datetime.timedelta(hours=2))):
             return 0
          else:
             return 0

I have set the variables within field calculator using the fields:

fnTime( !sequence! . !date! . !time_frame_2hr! . !OBJECTID!)

The tool runs with no errors however it assigns a false value (0) to all records. I am still learning writing python functions so they're maybe a quicker or efficient way of doing this.

| improve this question | | | | |
  • 3
    Couple of immediate comments without looking too deep into the code are: 1) you are not showing us the full code, what is df which is on the third line? 2) when you call your function fnTime() fields should be separated by commas not full stops. – Hornbydd Jan 30 '19 at 10:06