{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport datetime\nfrom sklearn.metrics.pairwise import haversine_distances\nimport math\nimport gc\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-01T19:55:40.495040Z","iopub.execute_input":"2022-11-01T19:55:40.496124Z","iopub.status.idle":"2022-11-01T19:55:41.456934Z","shell.execute_reply.started":"2022-11-01T19:55:40.495975Z","shell.execute_reply":"2022-11-01T19:55:41.455794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_complete_cols = ['track_id', 'race_date', 'race_number', 'program_number', 'trakus_index', 'latitude', 'longitude', 'distance_id', 'course_type', 'track_condition', 'run_up_distance', 'race_type', 'purse', \n 'post_time', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\nnyra_complete_dtype = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'trakus_index':'int64', 'latitude':'float64', 'longitude':'float64', 'distance_id': 'int64',\n'course_type':'object', 'track_condition':'object', 'run_up_distance':'int64', 'race_type':'object', 'purse':'float64', 'post_time':'object', 'weight_carried':'int64', 'jockey':'object',\n'odds':'float64', 'position_at_finish':'int64'}\nnyra_complete = pd.read_csv('../input/big-data-derby-2022/nyra_2019_complete.csv', names=nyra_complete_cols, dtype=nyra_complete_dtype, parse_dates=True, header=None)\nnyra_complete.odds = nyra_complete.odds/100\n#nyra_complete.post_time.apply(lambda x: x.strftime(r'%H:%M'))","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:30.893346Z","iopub.execute_input":"2022-10-28T20:54:30.893720Z","iopub.status.idle":"2022-10-28T20:54:40.547108Z","shell.execute_reply.started":"2022-10-28T20:54:30.893690Z","shell.execute_reply":"2022-10-28T20:54:40.545871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"import datetime\nnyra_complete.post_time.apply(lambda x: datetime.datetime.fromtimestamp(x).strftime('%H:%M'))","metadata":{"execution":{"iopub.status.busy":"2022-09-18T18:37:50.649232Z","iopub.execute_input":"2022-09-18T18:37:50.650103Z","iopub.status.idle":"2022-09-18T18:38:07.130801Z","shell.execute_reply.started":"2022-09-18T18:37:50.650050Z","shell.execute_reply":"2022-09-18T18:38:07.129842Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"nyra_race_table_dtype = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'distance_id': 'int64',\n                        'course_type':'object', 'track_condition':'object', 'run_up_distance':'int64', 'race_type':'object',\n                        'purse':'float64', 'post_time':'object'}\nnyra_race_table = pd.read_csv('../input/big-data-derby-2022/nyra_race_table.csv', dtype=nyra_race_table_dtype, parse_dates=True)\nnyra_race_table.distance_id = nyra_race_table.distance_id/100","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:40.549683Z","iopub.execute_input":"2022-10-28T20:54:40.550439Z","iopub.status.idle":"2022-10-28T20:54:40.563474Z","shell.execute_reply.started":"2022-10-28T20:54:40.550393Z","shell.execute_reply":"2022-10-28T20:54:40.562435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"start_table_cols = ['track_id', 'race_date', 'race_number', 'program_number', 'weight_carried', 'jockey', 'odds', 'position_at_finish']\nnyra_start_table_dtypes = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'weight_carried':'int64', 'jockey':'object', 'odds':'float64', 'position_at_finish':'int64'}\nnyra_start_table = pd.read_csv('../input/big-data-derby-2022/nyra_start_table.csv', names=start_table_cols, dtype=nyra_start_table_dtypes, parse_dates=True)\nnyra_start_table.odds = nyra_start_table.odds/100\nnyra_start_table['program_number'] = nyra_start_table['program_number'].map(lambda x: x.strip())","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:40.565063Z","iopub.execute_input":"2022-10-28T20:54:40.565868Z","iopub.status.idle":"2022-10-28T20:54:40.593686Z","shell.execute_reply.started":"2022-10-28T20:54:40.565823Z","shell.execute_reply":"2022-10-28T20:54:40.592719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking_table_dtypes = {'track_id':'object', 'race_date':'object', 'race_number':'int64', 'program_number':'object', 'trakus_index':'int64', 'latitude':'float64', 'longitude':'float64'}\nnyra_tracking_table = pd.read_csv('../input/big-data-derby-2022/nyra_tracking_table.csv', dtype=nyra_tracking_table_dtypes, parse_dates=True)\nnyra_tracking_table = nyra_tracking_table.assign(lat_radian=nyra_tracking_table.latitude * math.pi/180.)\nnyra_tracking_table = nyra_tracking_table.assign(long_radian=nyra_tracking_table.longitude * math.pi/180.)\nnyra_tracking_table['program_number'] = nyra_tracking_table['program_number'].map(lambda x: x.strip())","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:40.596151Z","iopub.execute_input":"2022-10-28T20:54:40.596463Z","iopub.status.idle":"2022-10-28T20:54:46.199227Z","shell.execute_reply.started":"2022-10-28T20:54:40.596436Z","shell.execute_reply":"2022-10-28T20:54:46.198096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_start_table[:100] ","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.200781Z","iopub.execute_input":"2022-10-28T20:54:46.201111Z","iopub.status.idle":"2022-10-28T20:54:46.222559Z","shell.execute_reply.started":"2022-10-28T20:54:46.201081Z","shell.execute_reply":"2022-10-28T20:54:46.221444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"nyra_start_table[['track_id', 'race_date']]","metadata":{"execution":{"iopub.status.busy":"2022-10-09T16:03:40.110433Z","iopub.execute_input":"2022-10-09T16:03:40.111168Z","iopub.status.idle":"2022-10-09T16:03:40.129380Z","shell.execute_reply.started":"2022-10-09T16:03:40.111122Z","shell.execute_reply":"2022-10-09T16:03:40.128109Z"}}},{"cell_type":"code","source":"nyra_race_table[nyra_race_table['track_id']=='SAR'].course_type.unique()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.223824Z","iopub.execute_input":"2022-10-28T20:54:46.224133Z","iopub.status.idle":"2022-10-28T20:54:46.232699Z","shell.execute_reply.started":"2022-10-28T20:54:46.224106Z","shell.execute_reply":"2022-10-28T20:54:46.231930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_race_table[nyra_race_table['track_id']=='SAR'][nyra_race_table['course_type']=='M']","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.234140Z","iopub.execute_input":"2022-10-28T20:54:46.234475Z","iopub.status.idle":"2022-10-28T20:54:46.259935Z","shell.execute_reply.started":"2022-10-28T20:54:46.234431Z","shell.execute_reply":"2022-10-28T20:54:46.258741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"nyra_tracking_table = nyra_tracking_table[nyra_tracking_table['track_id']=='BEL'][nyra_tracking_table['race_date']=='2019-05-11']","metadata":{"execution":{"iopub.status.busy":"2022-10-27T08:07:21.221276Z","iopub.execute_input":"2022-10-27T08:07:21.221750Z","iopub.status.idle":"2022-10-27T08:07:22.321302Z","shell.execute_reply.started":"2022-10-27T08:07:21.221706Z","shell.execute_reply":"2022-10-27T08:07:22.320204Z"}}},{"cell_type":"code","source":"def distance_calculator(list_obj:list): # list of [[lat1,long1],[lat2,long2]]\n    p_1 = list_obj[0]\n    p_2 = list_obj[1]\n    distance_m = float(np.max(haversine_distances([p_1, p_2]) * 6371000)) # in meters\n    distance_ft = float(np.max(haversine_distances([p_1, p_2]) * 20.902e6)) # in feet\n    return distance_m, distance_ft","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.261418Z","iopub.execute_input":"2022-10-28T20:54:46.261968Z","iopub.status.idle":"2022-10-28T20:54:46.267260Z","shell.execute_reply.started":"2022-10-28T20:54:46.261938Z","shell.execute_reply":"2022-10-28T20:54:46.266373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def heading_calculator(list_obj:list):\n    p_1 = list_obj[0]\n    p_2 = list_obj[1]\n    p_m = [p_2[0], p_1[1]]\n    ver, _ = distance_calculator([p_1, p_m])\n    hor, _ = distance_calculator([p_m, p_2])\n    hyp = math.sqrt(hor**2 + ver**2)\n    \n    try:\n        theta = math.acos(ver/hyp) * (180./math.pi)\n    except ZeroDivisionError:\n        print(hor, ver, hyp)\n        \n    lat = p_2[0] - p_1[0]\n    long = p_2[1] - p_1[1]\n    \n    if (((hyp==0.) and (ver==0.)) and (hor==0.)):\n        lat == 0.\n        long == 0.\n    \n    if((lat<0.) & (long<0.)):\n        heading = 180. + abs(theta)\n    elif((lat<0.) & (long>0.)):\n        heading = 180. - abs(theta)\n    elif((lat>0.) & (long<0.)):\n        heading = 360. - abs(theta)\n    elif((lat>0.) & (long>0.)):\n        heading = abs(theta)\n    elif((lat==0.) & (long>0.)):\n        heading = 90.\n    elif((lat==0.) & (long<0.)):\n        heading = 270.\n    elif((lat>0.) & (long==0.)):\n        heading = 0.\n    elif((lat<0.) & (long==0.)):\n        heading = 180.    \n    else:    #this will be activated when both lat & long are zeros.\n        heading = float(\"Nan\")\n    \n    heading_rad = heading * math.pi/180.\n    heading_deg = heading\n    return heading_deg, heading_rad","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.268600Z","iopub.execute_input":"2022-10-28T20:54:46.268987Z","iopub.status.idle":"2022-10-28T20:54:46.283820Z","shell.execute_reply.started":"2022-10-28T20:54:46.268958Z","shell.execute_reply":"2022-10-28T20:54:46.282724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"# calculating track heading!\np_1a = [40.671363, -73.828386]\np_1b = [40.672329, -73.827942]\np_2a = [40.674348, -73.830598]\np_2b = [40.672855, -73.831285]\nconst = math.pi/180.\np_1a = [x*const for x in p_1a] #multiplied_list = [value * 3 for value in b_list]\np_1b = [x*const for x in p_1b]\np_2a = [x*const for x in p_2a]\np_2b = [x*const for x in p_2b]\nhead1,_ = heading_calculator([p_1a, p_1b])\nhead2,_ = heading_calculator([p_2a, p_2b])\nprint(head1, head2)","metadata":{"execution":{"iopub.status.busy":"2022-10-07T05:43:55.562187Z","iopub.execute_input":"2022-10-07T05:43:55.563479Z","iopub.status.idle":"2022-10-07T05:43:55.573111Z","shell.execute_reply.started":"2022-10-07T05:43:55.563437Z","shell.execute_reply":"2022-10-07T05:43:55.571784Z"}}},{"cell_type":"code","source":"def rotational_heading_diff (stride:float, perp_angle_diff:float, center_pt:float):\n    # given Stride, perp angle diff, center_pt dist,  \n    perp_angle_diff = abs(perp_angle_diff)*math.pi/180. # radians\n    # if the horse travelled in the tangential velocity direction its stride would be\n    tan_velocity_stride = stride / math.cos(perp_angle_diff) # stride in the direction of tangential velocity\n    betha_max = 0.\n    theta_max = 0.\n    stride = 0.\n    count = 0\n    for i in range(0, 90):\n        betha1 = 0.0008726646259971648 # 0.05 deg in radian\n        betha = i*betha1\n        stride_betha = tan_velocity_stride * math.cos(betha)\n        stride_betha_vert = stride_betha*math.cos(betha)\n        stride_betha_horiz = stride_betha*math.sin(betha)\n        center_horiz = center_pt - stride_betha_horiz\n        try:\n            theta1 = math.atan(stride_betha_vert/center_horiz)\n        except ValueError:\n            continue \n        #print(theta1)\n        if (theta1>theta_max):\n            theta_max = theta1\n            betha_max = betha\n            stride = stride_betha\n        if (theta1<theta_max):\n            count+=1\n        if (count>4):\n            break\n    try:\n        center_p2 = (stride/2)/math.sin(theta_max/2)\n    except ZeroDivisionError:\n        center_p2 = center_pt\n    return math.degrees(betha_max), center_p2","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.287428Z","iopub.execute_input":"2022-10-28T20:54:46.287804Z","iopub.status.idle":"2022-10-28T20:54:46.297566Z","shell.execute_reply.started":"2022-10-28T20:54:46.287775Z","shell.execute_reply":"2022-10-28T20:54:46.296650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"0.05*math.pi/180","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.299002Z","iopub.execute_input":"2022-10-28T20:54:46.299359Z","iopub.status.idle":"2022-10-28T20:54:46.313324Z","shell.execute_reply.started":"2022-10-28T20:54:46.299332Z","shell.execute_reply":"2022-10-28T20:54:46.312290Z"}}},{"cell_type":"raw","source":"rotational_heading_diff (stride=40, perp_angle_diff=15., center_pt=300)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.314862Z","iopub.execute_input":"2022-10-28T20:54:46.315924Z","iopub.status.idle":"2022-10-28T20:54:46.323947Z","shell.execute_reply.started":"2022-10-28T20:54:46.315873Z","shell.execute_reply":"2022-10-28T20:54:46.322827Z"}}},{"cell_type":"code","source":"def Heading_diff_track (region:str, heading:float, field_tilt:float, stride=0., curve_center=[0.,0.], pt_1=[0.,0.]):\n    # track headings may be part of the arguments\n    #radius\n    #stride/ pt_2\n    \n    heading_1 = 288.7701506927956 # starting line heading\n    heading_2 = 108.76893474694575 # finish line heading \n    heading_V = 327.5053747418201\n    heading_VI = 2.8536418023564187 \n    \n    #heading 0 to pt_2_2 = (2.8536418023564187, 0.049805445123664784)\n    #heading 5 to pt_1_1 = (327.5053747418201, 5.716047107222635)\n        \n    # pls note that im considering the races will be held always in the CCW direction.\n    if ((region == 'I') or (region == 'III')):\n        # the expected heading shall be tangent to the curve.\n        center_to_pt = [curve_center, pt_1] # all shall be given in radians\n        _, center_pt_distance = distance_calculator(center_to_pt)\n        heading_central, _ = heading_calculator(center_to_pt) #radial heading\n        if (heading_central>=90.0):\n            perp_heading_expected = heading_central-90.0\n        elif (heading_central<90.0):\n            perp_heading_expected = 360.0+(heading_central-90.0)\n            \n        if ((perp_heading_expected>(270.)) and (heading<(90.))): \n            perp_heading_diff = -((360.0-perp_heading_expected) + heading)\n        elif ((heading>(270.)) and (perp_heading_expected<(90.))):\n            perp_heading_diff = (360.0-heading) + perp_heading_expected\n        else:\n            perp_heading_diff = perp_heading_expected - heading # difference bn tangential velocity heading and heading\n            \n        # perp_heading_expected is the heading of the Tangential Velocity/Force.\n        # lets find the track heaading!\n        beta, center_p2_efficient = rotational_heading_diff (stride=stride, perp_angle_diff = abs(perp_heading_diff), center_pt=center_pt_distance)\n        # rotational_heading_diff func iterates over some beta's and finds the effiecnt betha and it returns it \n        # with center_p2 distance if the horse has kept the esfficient path. i will use this to explain circumscribed circle.\n        \n        track_heading = perp_heading_expected - beta\n        if (track_heading<0.):\n            track_heading = 360.+track_heading        \n                    \n        if ((track_heading>(270.)) and (heading<(90.))): \n            heading_diff = -((360.0-track_heading) + heading)\n        elif ((heading>(270.)) and (track_heading<(90.))):\n            heading_diff = (360.0-heading) + track_heading\n        else:\n            heading_diff = track_heading - heading\n            \n        # we just made sure heading_diff will be -Ve when heading away from the track edge.\n        #print ('heading, expected heading and heading diff are: {} {} {}'.format(heading, heading_expected, heading_diff))      \n    elif (region == 'II'):\n        heading_diff = heading_2-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.\n    \n    elif (region == 'IV'):\n        if (heading<(90.)): # this is if the horse is aproaching perpendicularlly\n            heading_diff = -((360.-heading_1)+heading)\n        else:\n            heading_diff = heading_1-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge. \n        \n    elif (region == 'V'):\n        if (heading<90.): \n            heading_diff = -((360.-heading_V)+heading)\n        else:\n            heading_diff = heading_V-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.    \n        \n    elif (region == 'VI'):\n        if (heading>270.): #perpendicular\n            heading_diff = ((360.-heading)+heading_VI)\n        else:\n            heading_diff = heading_VI-heading\n        # heading_diff will be -ve if the horse is directed away from the the track edge.    \n        \n    if ((region!='I') and (region!='III')):\n        beta=0.\n        center_p2_efficient = 0.\n    \n    return heading_diff, beta, center_p2_efficient # its in degree","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.325628Z","iopub.execute_input":"2022-10-28T20:54:46.326059Z","iopub.status.idle":"2022-10-28T20:54:46.344857Z","shell.execute_reply.started":"2022-10-28T20:54:46.326019Z","shell.execute_reply":"2022-10-28T20:54:46.343804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def prev_heading_diff_calculator (current_heading:float, prev_heading:float, field_tilt:float):\n    if ((current_heading>(270.)) and (prev_heading<(90.))): \n        heading_diff = (360.0-current_heading) + prev_heading\n    elif ((prev_heading>(270.)) and (current_heading<(90.))):\n        heading_diff = -((360.0-prev_heading) + current_heading)\n    else:\n        heading_diff = prev_heading - current_heading\n    return heading_diff","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.346503Z","iopub.execute_input":"2022-10-28T20:54:46.347188Z","iopub.status.idle":"2022-10-28T20:54:46.359066Z","shell.execute_reply.started":"2022-10-28T20:54:46.347145Z","shell.execute_reply":"2022-10-28T20:54:46.357822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nyra_tracking_table.drop_duplicates(inplace=True)\nnyra_tracking_table.dropna(inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:46.360415Z","iopub.execute_input":"2022-10-28T20:54:46.360720Z","iopub.status.idle":"2022-10-28T20:54:57.096092Z","shell.execute_reply.started":"2022-10-28T20:54:46.360691Z","shell.execute_reply":"2022-10-28T20:54:57.094563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:57.097714Z","iopub.execute_input":"2022-10-28T20:54:57.098120Z","iopub.status.idle":"2022-10-28T20:54:57.236576Z","shell.execute_reply.started":"2022-10-28T20:54:57.098084Z","shell.execute_reply":"2022-10-28T20:54:57.235574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted_nyra_tracking_table = nyra_tracking_table.sort_values(['track_id', 'race_date', 'race_number', 'trakus_index', 'program_number'])","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:54:57.237839Z","iopub.execute_input":"2022-10-28T20:54:57.238161Z","iopub.status.idle":"2022-10-28T20:55:00.612476Z","shell.execute_reply.started":"2022-10-28T20:54:57.238131Z","shell.execute_reply":"2022-10-28T20:55:00.611646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"sorted_nyra_tracking_table[sorted_nyra_tracking_table['track_id']=='SAR']","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:00.613711Z","iopub.execute_input":"2022-10-28T20:55:00.614520Z","iopub.status.idle":"2022-10-28T20:55:01.041987Z","shell.execute_reply.started":"2022-10-28T20:55:00.614487Z","shell.execute_reply":"2022-10-28T20:55:01.040835Z"}}},{"cell_type":"raw","source":"sorted_all_tables = pd.merge(sorted_nyra_tracking_table, nyra_race_table[['track_id','race_date', 'race_number',  how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nsorted_all_tables = pd.merge(sorted_all_tables, nyra_start_table,  how='left', left_on=['track_id','race_date', 'race_number', 'program_number'], right_on = ['track_id','race_date', 'race_number', 'program_number'])","metadata":{"execution":{"iopub.status.busy":"2022-10-07T07:07:41.118304Z","iopub.execute_input":"2022-10-07T07:07:41.118703Z","iopub.status.idle":"2022-10-07T07:07:48.069591Z","shell.execute_reply.started":"2022-10-07T07:07:41.118670Z","shell.execute_reply":"2022-10-07T07:07:48.068708Z"}}},{"cell_type":"raw","source":"sorted_all_tables","metadata":{"execution":{"iopub.status.busy":"2022-10-07T07:07:49.901411Z","iopub.execute_input":"2022-10-07T07:07:49.901852Z","iopub.status.idle":"2022-10-07T07:07:49.932534Z","shell.execute_reply.started":"2022-10-07T07:07:49.901818Z","shell.execute_reply":"2022-10-07T07:07:49.931454Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"sorted_all_tables = pd.merge(sorted_nyra_tracking_table, nyra_race_table[['track_id','race_date', 'race_number', 'course_type']], how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nsorted_all_tables","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:01.043789Z","iopub.execute_input":"2022-10-28T20:55:01.044597Z","iopub.status.idle":"2022-10-28T20:55:03.116503Z","shell.execute_reply.started":"2022-10-28T20:55:01.044541Z","shell.execute_reply":"2022-10-28T20:55:03.115441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"sorted_all_tables[sorted_all_tables['track_id']=='SAR'][sorted_all_tables['course_type']=='D']","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:03.117976Z","iopub.execute_input":"2022-10-28T20:55:03.118344Z","iopub.status.idle":"2022-10-28T20:55:05.069388Z","shell.execute_reply.started":"2022-10-28T20:55:03.118313Z","shell.execute_reply":"2022-10-28T20:55:05.068373Z"}}},{"cell_type":"raw","source":"const = math.pi/180.\n_, distance = distance_calculator([[40.67468889*const, -73.82996389*const], [40.67404444*const, -73.82760556*const]])\n_, heading = heading_calculator([[40.67468889*const, -73.82996389*const], [40.67404444*const, -73.82760556*const]])\n#print(distance, heading)\n\nR = 20.902e6 # radius of the earth\nbrng = heading #Bearing is 90 degrees converted to radians.\nd = distance/2 #Distance in km\n\n#lat2  52.20444 - the lat result I'm hoping for\n#lon2  0.36056 - the long result I'm hoping for.\n\nlat1 = 40.67468889*const #Current lat point converted to radians\nlon1 = -73.82996389*const #Current long point converted to radians\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\nprint(\"lat lon of mid point is {} {}\".format(math.degrees(lat2), math.degrees(lon2)))\n########\n\nheading_2 = heading+(math.pi/2.)\nbrng = heading_2\nd = 42\nlat1 = lat2\nlon1 = lon2\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\np_b = [[40.67468889*const, -73.82996389*const], [lat2,lon2]]\np_c = [[40.67404444*const, -73.82760556*const], [lat2,lon2]]\n_, distance2 = distance_calculator(p_b)\n_, distance3 = distance_calculator(p_c)\n\nprint(distance2, distance3)\n#print(lat2, lon2)\nlat2 = math.degrees(lat2)\nlon2 = math.degrees(lon2)\n\n#342.6313269375081\nprint(lat2)\nprint(lon2)\n","metadata":{"execution":{"iopub.status.busy":"2022-10-12T11:37:17.874464Z","iopub.execute_input":"2022-10-12T11:37:17.874833Z","iopub.status.idle":"2022-10-12T11:37:17.889383Z","shell.execute_reply.started":"2022-10-12T11:37:17.874800Z","shell.execute_reply":"2022-10-12T11:37:17.888039Z"}}},{"cell_type":"code","source":"def effective_distance (region:str, p_1:list, p_2:list, curve_center_1:list, curve_center_2:list, head_diff:float, stride:float, radius_1:float, radius_2:float, field_tilt:float):\n    center1_p1 = [curve_center_1, p_1]\n    center1_p2 = [curve_center_1, p_2]    \n    center2_p1 = [curve_center_2, p_1]\n    center2_p2 = [curve_center_2, p_2]    \n    \n    if (region == 'I'):\n        heading_1, _ = heading_calculator(center1_p1)\n        heading_2, _ = heading_calculator(center1_p2)\n        if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.)) and (heading_1<(90.))): # this has a chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius_1/180.)*head_diff\n        ang_velocity = (head_diff * math.pi/180.)/0.25 # angular velocity\n        \n    elif (region == 'III'):\n        heading_1, _ = heading_calculator(center2_p1)\n        heading_2, _ = heading_calculator(center2_p2)\n        if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n            head_diff = (360.-heading_1)+heading_2\n        elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n            head_diff = (360.-heading_2)+heading_1\n        else:\n            head_diff = abs(heading_1-heading_2)\n        eff_distance = (math.pi*radius_2/180.)*head_diff\n        ang_velocity = (head_diff * math.pi/180.)/0.25 # angular velocity\n    \n    elif (region == 'II'): \n        if (p_1[0] > ((2.230331271228713*p_1[1]) + 3.580519320058622)): # upper diameter equation\n            # effective distance for the roll_out part\n            if (p_2[0] >= ((2.230331271228713*p_2[1]) + 3.580519320058622)):   # upper diameter equation\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_1/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n        else:\n            # effective distance for the straight part\n            if (p_2[0] < ((2.230384220411843*p_2[1]) + 3.5803210032190362)): # lower diam eq. \n                heading_1, _ = heading_calculator(center2_p1)\n                heading_2, _ = heading_calculator(center2_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_2/180.)*head_diff\n            else:                   \n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride\n    elif (region == 'IV'): \n        if (p_1[0] < ((2.230384220411843*p_1[1]) + 3.5803210032190362)): # lower diameter equation\n            eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straight line path\n            #if (p_2[0] <= ((2.230384220411843*p_2[1]) + 3.5803210032190362)):  # lower diameter equation\n                #heading_1, _ = heading_calculator(center2_p1)\n                #heading_2, _ = heading_calculator(center2_p2)\n                #if ((heading_1>(270.)) and (heading_2<(90.))): # i dont think this has a chance\n                    #head_diff = (360.-heading_1)+heading_2\n                #elif ((heading_2>(270.)) and (heading_1<(90.))): # this has chance\n                    #head_diff = (360.-heading_2)+heading_1\n                #else:\n                    #head_diff = abs(heading_1-heading_2)\n                #eff_distance = (math.pi*radius_2/180.)*head_diff\n            #else:\n                #eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straight line path\n        else:\n            # effective distance for the straight part\n            if (p_2[0] > ((2.230331271228713*p_2[1]) + 3.580519320058622)): # upper diam eq., p_1 in IV and p_2 in I\n                heading_1, _ = heading_calculator(center1_p1)\n                heading_2, _ = heading_calculator(center1_p2)\n                if ((heading_1>(270.)) and (heading_2<(90.))): \n                    head_diff = (360.-heading_1)+heading_2\n                elif ((heading_2>(270.)) and (heading_1<(90.))): \n                    head_diff = (360.-heading_2)+heading_1\n                else:\n                    head_diff = abs(heading_1-heading_2)\n                eff_distance = (math.pi*radius_1/180.)*head_diff\n            else:\n                eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straignt line path\n    elif ((region=='V') or (region=='VI')):\n        eff_distance = math.cos(abs(head_diff)*math.pi/180)*stride #straignt line path\n        \n    if ((region!='I') and (region!='III')):\n        ang_velocity = 0.\n        \n    return eff_distance, ang_velocity","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:05.071065Z","iopub.execute_input":"2022-10-28T20:55:05.071361Z","iopub.status.idle":"2022-10-28T20:55:05.097628Z","shell.execute_reply.started":"2022-10-28T20:55:05.071335Z","shell.execute_reply":"2022-10-28T20:55:05.096601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Distance_from_edge (region:str, radius:float, field_tilt:float, p_2:list, tan_pt=[0.,0.], curve_center=[0.,0.]):\n    \n    # remember we need this part for drift.\n    if ((region == 'I') or (region == 'III')):\n        _, distance = distance_calculator([curve_center, p_2])\n        abcd = distance - radius\n        edge_distance = abcd\n        #print('section a {} {} {} {}'.format(region, edge_distance, radius, abcd))\n    \n    elif (region=='II'):\n        if (p_2[0] >= ((2.230331271228713*p_2[1]) + 3.580519320058622)): #upper diam eq.\n            _, distance = distance_calculator([curve_center, p_2])\n            edge_distance = distance - radius\n            #print('section b {} {}'.format(edge_distance, distance, radius))\n        \n        elif (p_2[0] < ((2.230331271228713*p_2[1]) + 3.580519320058622)):  #upper diam eq.\n            _, heading = heading_calculator([tan_pt, p_2])\n            _, distance = distance_calculator([p_2, tan_pt])\n            if (tan_pt[1]<p_2[1]):\n                heading = math.pi-heading\n                theta = field_tilt*(math.pi/180.)-heading\n            else:\n                heading = heading-math.pi\n                theta = field_tilt*(math.pi/180.)+heading\n            edge_distance = (distance*math.sin(theta)) \n                      \n            #print('section e {} {}'.format(edge_distance, distance))\n             \n    elif (region=='IV'):\n        # lets try it with tangent point. p_t=[0.,0.]\n        # center_curve shall be curve_center_2 (lower center)        \n        if (p_2[0] <= ((2.230384220411843*p_2[1]) + 3.5803210032190362)):  # lower diam eq.\n            # when p_2 is lying exactly on the diameter line\n            _, distance = distance_calculator([curve_center, p_2])\n            edge_distance = distance-radius\n            #print('section f {} {}'.format(edge_distance, distance))\n                            \n        elif (p_2[0] > ((2.230384220411843*p_2[1]) + 3.5803210032190362)):   # lower diam eq.\n            # above the lower diameter line\n            _, heading = heading_calculator([tan_pt, p_2])\n            _, distance = distance_calculator([tan_pt, p_2])\n            if (tan_pt[1]>p_2[1]):\n                heading = 2*math.pi-heading\n                theta = field_tilt*(math.pi/180.)-heading\n            else:\n                theta = field_tilt*(math.pi/180.)+heading\n            edge_distance = (distance*math.sin(theta))\n            #print('section j {} {}'.format(edge_distance, distance))\n            \n    elif (region=='V'):\n        _, heading = heading_calculator([tan_pt, p_2])\n        _, distance = distance_calculator([tan_pt, p_2])\n        if (tan_pt[1]<=p_2[1]):\n            theta = field_tilt*(math.pi/180.)+heading\n        else:\n            heading = 2*math.pi-heading\n            theta = field_tilt*(math.pi/180.)-heading\n        edge_distance = (distance*math.sin(theta))\n    elif (region=='VI'):\n        _, heading = heading_calculator([tan_pt, p_2])\n        _, distance = distance_calculator([tan_pt, p_2])\n        theta = heading-field_tilt*(math.pi/180.)\n        edge_distance = (distance*math.sin(theta))\n            \n    #print(distance, [math.degrees(x) for x in p_2], [math.degrees(x) for x in tan_pt])\n            \n    return edge_distance","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:05.099326Z","iopub.execute_input":"2022-10-28T20:55:05.099623Z","iopub.status.idle":"2022-10-28T20:55:05.117884Z","shell.execute_reply.started":"2022-10-28T20:55:05.099596Z","shell.execute_reply":"2022-10-28T20:55:05.116927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"center_1 = [[40.71593611, -73.72568889], [0.7106271431511522, -1.2867560144315036]]\ncenter_2 = [[40.71435556, -73.71955278], [0.7105995573496597, -1.286648919108739]]\nradius_1 = 528. #feet\nradius_2 = 528. #feet\nheading from center 2 to 1 = (288.76974988783803, 5.0399829157033)\nheading start line = (288.7701506927956, 5.039989911069467)\nheading finish line = (108.76893474694575, 1.8983760352210686)\ndistance bn center points = (546.2077767521381, 1792.0004629843338)\nfield_tilt_IV = 71.2298493072044\nfield_tilt_II = 71.23106525305425\ntangent_point_IV_1 = [[40.717306471929675, -73.72507445582002], [0.7106510604787689, -1.286745290532026]]\ntangent_point_IV_2 = [[40.71572592192977, -73.71893836040735], [0.7106234746772782, -1.2866381954638584]]\ntangent_point_II_1 = [[40.71456574481265, -73.72630329888578], [0.7106032257666782, -1.286766737889514]]\ntangent_point_II_2 = [[40.712985194812745, -73.72016717430046], [0.7105756399651875, -1.2866596423121874]]\n\np_1[0] =  ((-0.257582370157123*p_1[1]) + 0.37920815875501385) # start line equation\np_1[0] =  ((-0.2575811455466478*p_1[1]) + 0.37915637536977415) # finish line equation\np_1[0] =  ((2.230384220411843*p_1[1]) + 3.5803210032190362) # lower circle equation\np_1[0] =  ((2.230331271228713*p_1[1]) + 3.580519320058622) # upper circle equation\np_1[0] =  ((-0.25758175782378967*p_1[1]) + 0.3791822670635518) # center line\n","metadata":{"execution":{"iopub.status.busy":"2022-10-22T17:23:22.767722Z","iopub.execute_input":"2022-10-22T17:23:22.768550Z","iopub.status.idle":"2022-10-22T17:23:22.778236Z","shell.execute_reply.started":"2022-10-22T17:23:22.768513Z","shell.execute_reply":"2022-10-22T17:23:22.776710Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"const = math.pi/180.\nheading_calculator([[40.71412500*const, -73.71705556*const], [40.71511667*const, -73.71788889*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-26T05:34:59.987163Z","iopub.execute_input":"2022-10-26T05:34:59.987721Z","iopub.status.idle":"2022-10-26T05:34:59.998053Z","shell.execute_reply.started":"2022-10-26T05:34:59.987677Z","shell.execute_reply":"2022-10-26T05:34:59.996596Z"}}},{"cell_type":"raw","source":"heading 0 to pt_2_2 = (2.8536418023564187, 0.049805445123664784)\nheading 5 to pt_1_1 = (327.5053747418201, 5.716047107222635)","metadata":{}},{"cell_type":"raw","source":"const = math.pi/180.\ndistance_calculator([[40.71412500*const, -73.71705556*const], [40.71511667*const, -73.71788889*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-27T08:07:22.687224Z","iopub.execute_input":"2022-10-27T08:07:22.687653Z","iopub.status.idle":"2022-10-27T08:07:22.705534Z","shell.execute_reply.started":"2022-10-27T08:07:22.687620Z","shell.execute_reply":"2022-10-27T08:07:22.704303Z"}}},{"cell_type":"raw","source":"const = math.pi/180.\n[[40.71354167*const, -73.71745278*const], [40.71511667*const, -73.71788889*const]]","metadata":{"execution":{"iopub.status.busy":"2022-10-27T08:45:05.836365Z","iopub.execute_input":"2022-10-27T08:45:05.837098Z","iopub.status.idle":"2022-10-27T08:45:05.846276Z","shell.execute_reply.started":"2022-10-27T08:45:05.837043Z","shell.execute_reply":"2022-10-27T08:45:05.845023Z"}}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"y=−0.01714623672563829x+0.6885383703998347 # center to pt_2_2\ny=0.4574280751735761x+1.2991488958417325 # center to pt_1_1","metadata":{"execution":{"iopub.status.busy":"2022-10-23T16:05:33.429750Z","iopub.execute_input":"2022-10-23T16:05:33.430107Z","iopub.status.idle":"2022-10-23T16:05:33.533955Z","shell.execute_reply.started":"2022-10-23T16:05:33.430075Z","shell.execute_reply":"2022-10-23T16:05:33.532304Z"}}},{"cell_type":"raw","source":"-1.1503586411898947*(-1.286609406774336)-0.7694541104836017","metadata":{"execution":{"iopub.status.busy":"2022-10-26T05:04:53.773009Z","iopub.execute_input":"2022-10-26T05:04:53.773415Z","iopub.status.idle":"2022-10-26T05:04:53.781730Z","shell.execute_reply.started":"2022-10-26T05:04:53.773381Z","shell.execute_reply":"2022-10-26T05:04:53.780200Z"}}},{"cell_type":"raw","source":"p_1[0] = ((14.126462640979424*p_1[1]) + 18.885915934817806) #0\np_1[0] = ((22.822258598203565*p_1[1]) + 30.07393628609182) #10","metadata":{}},{"cell_type":"raw","source":"R = 20.902e6 # radius of the earth\nbrng = (math.pi*0.5) + (5.0399829157033) #Bearing is 90 degrees converted to radians.\n#brng = 5.0399829157033\nd = 528. #Distance in km\n\n#lat2  52.20444 - the lat result I'm hoping for\n#lon2  0.36056 - the long result I'm hoping for\n\nlat1 = 40.71435556*math.pi/180 #Current lat point converted to radians\nlon1 = -73.71955278*math.pi/180 #Current long point converted to radians\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\nprint([[math.degrees(lat2), math.degrees(lon2)], [lat2, lon2]], lat1, lon1)","metadata":{"execution":{"iopub.status.busy":"2022-10-22T17:35:13.300606Z","iopub.execute_input":"2022-10-22T17:35:13.301035Z","iopub.status.idle":"2022-10-22T17:35:13.311021Z","shell.execute_reply.started":"2022-10-22T17:35:13.301001Z","shell.execute_reply":"2022-10-22T17:35:13.309861Z"}}},{"cell_type":"raw","source":"_, distance_IV = distance_calculator([[0.7098946571653235, -1.288534465656788], [0.709827167374612, -1.2885676256672594]])\n_, distance_II = distance_calculator([[0.7099064148648944, -1.2885773625830519], [0.7098383663083734, -1.2886084816063457]])\ndegree_1 = 180+0.6582046927798011+0.48464731778784653\ndegree_2 = 180-(0.6582046927798011+0.48464731778784653)\nc_per_degree_1 = (math.pi*361.555520694114)/180.\nc_per_degree_2 = (math.pi*344.3731723149623)/180.\nc_1 = c_per_degree_1 * degree_1\nc_2 = c_per_degree_2 * degree_2\ntotal_c = distance_IV+distance_II+c_1+c_2\nprint(total_c)","metadata":{"execution":{"iopub.status.busy":"2022-10-13T10:44:48.157762Z","iopub.execute_input":"2022-10-13T10:44:48.158162Z","iopub.status.idle":"2022-10-13T10:44:48.169101Z","shell.execute_reply.started":"2022-10-13T10:44:48.158131Z","shell.execute_reply":"2022-10-13T10:44:48.167802Z"}}},{"cell_type":"raw","source":"R = 20.902e6 # radius of the earth\n_, brng = heading_calculator([[40.67435086298209*const, -73.82999346672229*const], [40.67312720830604*const, -73.82935732146701*const]])\n_, dist = distance_calculator([[40.67435086298209*const, -73.82999346672229*const], [40.67312720830604*const, -73.82935732146701*const]])\nd = 600\n\nbrng = brng - (math.pi/2)\n#lat2  52.20444 - the lat result I'm hoping for\n#lon2  0.36056 - the long result I'm hoping for.\n\nlat1 = 40.673739035963024*math.pi/180 #Current lat point converted to radians\nlon1 = -73.82967539156863*math.pi/180 #Current long point converted to radians\n\nlat2 = math.asin( math.sin(lat1)*math.cos(d/R) +\n     math.cos(lat1)*math.sin(d/R)*math.cos(brng))\n\nlon2 = lon1 + math.atan2(math.sin(brng)*math.sin(d/R)*math.cos(lat1),\n             math.cos(d/R)-math.sin(lat1)*math.sin(lat2))\n\nprint(math.degrees(lat2), math.degrees(lon2), lat1, lon1)","metadata":{"execution":{"iopub.status.busy":"2022-10-11T20:13:00.977347Z","iopub.execute_input":"2022-10-11T20:13:00.978047Z","iopub.status.idle":"2022-10-11T20:13:00.987931Z","shell.execute_reply.started":"2022-10-11T20:13:00.978015Z","shell.execute_reply":"2022-10-11T20:13:00.986918Z"}}},{"cell_type":"code","source":"def pivot_df (df:pd.DataFrame):\n    idxs = []\n    new_tr_idx = []\n    tr_idx = list(df.trakus_index)\n    for x in tr_idx:\n        #print(x)\n        a, b = x.split('_')\n        idxs.append(int(a))\n        idxs.append(int(b))\n    max_idx = max(idxs)\n    min_idx = min(idxs)\n    for i in range(min_idx, max_idx):\n        new_tr_idx.append(\"{}_{}\".format(i, i+1))\n    #new_tr_idx\n    df = df.pivot('trakus_index', 'program_number')\n    df = df.reindex(new_tr_idx)\n    return df","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:05.119341Z","iopub.execute_input":"2022-10-28T20:55:05.119624Z","iopub.status.idle":"2022-10-28T20:55:05.133328Z","shell.execute_reply.started":"2022-10-28T20:55:05.119598Z","shell.execute_reply":"2022-10-28T20:55:05.132226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def degrees_func (list_obj:list):\n    list_obj = [math.degrees(x) for x in list_obj]\n    return list_obj","metadata":{"execution":{"iopub.status.busy":"2022-11-01T19:56:07.250207Z","iopub.execute_input":"2022-11-01T19:56:07.250578Z","iopub.status.idle":"2022-11-01T19:56:07.255165Z","shell.execute_reply.started":"2022-11-01T19:56:07.250552Z","shell.execute_reply":"2022-11-01T19:56:07.254310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"degrees_func([0.7517479683493266, -1.287447932333447])","metadata":{"execution":{"iopub.status.busy":"2022-11-01T20:03:05.823719Z","iopub.execute_input":"2022-11-01T20:03:05.824123Z","iopub.status.idle":"2022-11-01T20:03:05.830455Z","shell.execute_reply.started":"2022-11-01T20:03:05.824088Z","shell.execute_reply":"2022-11-01T20:03:05.829539Z"}}},{"cell_type":"raw","source":"[0.7105853522894107, -1.286612267194447]\nelif (((22.822258598203565*p_1[1]) + 30.07393628609182) < p_1[0] < ((15.206057595535578*p_1[1]) + 20.274940495850483)):\n\n\n","metadata":{"execution":{"iopub.status.busy":"2022-10-10T14:58:49.751074Z","iopub.execute_input":"2022-10-10T14:58:49.751579Z","iopub.status.idle":"2022-10-10T14:58:49.779502Z","shell.execute_reply.started":"2022-10-10T14:58:49.751537Z","shell.execute_reply":"2022-10-10T14:58:49.777832Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"print((22.822258598203565*(-1.286612267194447)) + 30.07393628609182)\nprint((15.206057595535578*(-1.286612267194447)) + 20.274940495850483)","metadata":{"execution":{"iopub.status.busy":"2022-10-27T08:48:06.005580Z","iopub.execute_input":"2022-10-27T08:48:06.006016Z","iopub.status.idle":"2022-10-27T08:48:06.012382Z","shell.execute_reply.started":"2022-10-27T08:48:06.005979Z","shell.execute_reply":"2022-10-27T08:48:06.011066Z"}}},{"cell_type":"raw","source":"const=math.pi/180.  \ndistance_calculator([[40.67043056*const, -73.83065278*const], [40.6695542305408*const, -73.83014502291098*const]])","metadata":{"execution":{"iopub.status.busy":"2022-10-14T08:22:09.334261Z","iopub.execute_input":"2022-10-14T08:22:09.334768Z","iopub.status.idle":"2022-10-14T08:22:09.343638Z","shell.execute_reply.started":"2022-10-14T08:22:09.334728Z","shell.execute_reply":"2022-10-14T08:22:09.342494Z"}}},{"cell_type":"code","source":"# before u run it check for index i of p_'s and tr_idx\n# assumption abt the stride!\nBEL_T_df = sorted_all_tables[sorted_all_tables['track_id']=='BEL'][sorted_all_tables['course_type']=='T']\nunique_track_ids = list(BEL_T_df.track_id.unique())\ntracking_path = \"./tracking_2\"\ntracking_path_1 = \"./tracking_pvt\"\nif not os.path.exists(tracking_path):\n    os.makedirs(tracking_path)\nif not os.path.exists(tracking_path_1):\n    os.makedirs(tracking_path_1)\n#gen_data = pd.DataFrame()\nprint(1)\nfor tr_id in unique_track_ids:\n    track_id_df = BEL_T_df[BEL_T_df['track_id']==tr_id]\n    unique_race_date = list(track_id_df.race_date.unique())\n    for rc_date in unique_race_date:\n        race_date_df = track_id_df[track_id_df['race_date']==rc_date]\n        unique_race_number = list(race_date_df.race_number.unique())        \n        for rc_no in unique_race_number: #[:math.floor(len(unique_race_date)/2)]:\n            race_number_df = race_date_df[race_date_df['race_number']==rc_no]            \n            time = 0.25\n            gen_data = pd.DataFrame()\n            unique_program_number = list(race_number_df.program_number.unique())  \n            print(2)\n            \n            for pg_no in unique_program_number:\n                program_number_df = race_number_df[race_number_df['program_number']==pg_no].sort_values('trakus_index', ascending=True)\n                len_df = len(program_number_df)\n                for i in range(len_df-1): #range(1:df) and change p_1 and p_2.\n                    p_1 = [program_number_df.iloc[i, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i, program_number_df.columns.get_loc('long_radian')]]\n                    p_2 = [program_number_df.iloc[i+1, program_number_df.columns.get_loc('lat_radian')], program_number_df.iloc[i+1, program_number_df.columns.get_loc('long_radian')]]\n                    \n                    distance_m, distance_ft = distance_calculator([p_1, p_2]) # in meters, in ft\n                    heading_deg, heading_rad = heading_calculator([p_1, p_2]) # in deg, in rad\n                    if (np.isnan(heading_deg)):\n                        print(\"heading is nan at {} {} {} {} {}\".format(rc_date, rc_no, pg_no, p_1, p_2))                                        \n                    const = math.pi/180.                    \n                    radius_1 = 528. #feet\n                    radius_2 = 528. #feet               \n                    field_tilt_center = 71.23025011216197\n                    field_tilt_VI = 2.8536418023564187 #in degrees\n                    field_tilt_V = (360.- 327.5053747418201) # in degrees\n                    field_tilt_IV = 71.2298493072044 # in degrees\n                    field_tilt_II = 71.23106525305425 # in degrees\n                    tan_pt_II = [0.7106032257666782, -1.286766737889514]\n                    tan_pt_IV = [0.7106234746772782, -1.2866381954638584]\n                    tan_pt_V = [40.71412500*const, -73.71705556*const] #5\n                    tan_pt_VI = [40.71309722*const, -73.71768889*const] #0\n                    #tan_pt_II_2 = [0.7098383663083734, -1.2886084816063457]\n                    \n                    #heading 0 to pt_2_2 = (2.8536418023564187, 0.049805445123664784)\n                    #heading 5 to pt_1_1 = (327.5053747418201, 5.716047107222635)\n                    \n                    #heading from center 2 to 1 = (19.78284456311878, 0.3452757730366819)\n                    curve_center_1 = [40.71593611*const, -73.72568889*const] #center points in radian\n                    curve_center_2 = [40.71435556*const, -73.71955278*const]          \n                    \n                    tr_idx_1 = program_number_df.iloc[i, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx_2 = program_number_df.iloc[i+1, program_number_df.columns.get_loc('trakus_index')]\n                    tr_idx = \"{}_{}\".format(tr_idx_1, tr_idx_2)\n                    #print(3)\n                    \n                    if (p_1[0] >= ((2.230331271228713*p_1[1]) + 3.580519320058622)): # upper diameter equation\n                        #if (p_1[0] > ((-0.2575811455466478*p_1[1]) + 0.37915637536977415)): #finish line equation\n                        #Region I\n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='I', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_1, pt_1=p_1)\n                        edge_distance = Distance_from_edge (region='I', radius=radius_1, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_1)\n                        cum_dist1, ang_velocity = effective_distance (region='I', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                        region='I'\n                        _, center_to_pt = distance_calculator([curve_center_1, p_1])\n                        #print('region I')\n                        #else:\n                            #region II\n                           # heading_diff, beta, center_p2_efficient = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                            #edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                            #cum_dist1, ang_velocity = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                            #region='II'\n                            #center_to_pt = 0.\n                            #print('region II defined by 1st case')\n                    elif (p_1[0] <= ((2.230384220411843*p_1[1]) + 3.5803210032190362)): # lower diameter equation\n                        #if (p_1[0] < ((-0.257582370157123*p_1[1]) + 0.37920815875501385)): # start line equation\n                        _, distance1 = distance_calculator([curve_center_2, p_1])\n                        _, distance2 = distance_calculator([curve_center_2, [40.71440000*const, -73.71730000*const]]) # pt_3\n                        if (i>0):\n                            last_region = gen_data.iloc[-1, gen_data.columns.get_loc('region')]\n                        else:\n                            last_region = 'first'\n                        region_proposal = False\n                        if ((last_region=='V') or (last_region=='VI')):\n                            region_proposal = True\n\n                        #print(distance1, distance2)\n\n                        #p_1[0] = ((15.206057595535578*p_1[1]) + 20.274940495850483) # 0 to pt_2_2 R-V\n                        #p_1[0] = ((22.822258598203565*p_1[1]) + 30.07393628609182)  # 10 to 11    R-V\n                        #p_1[0] = ((-1.1900087600441343*p_1[1])-0.8204760853341113) # 5 to pt_1_1\n                        #p_1[0] = ((-1.1503586411898947*p_1[1])-0.7694541104836017) # 6 to untitled\n                        #y=−0.01714623672563829x+0.6885383703998347 # center to pt_2_2\n                        #y=0.4574280751735761x+1.2991488958417325 # center to pt_1_1\n                        if (distance1>distance2):                                \n                            if (((-1.1503586411898947*p_1[1])-0.7694541104836017) > p_1[0] > ((-1.1900087600441343*p_1[1])-0.8204760853341113)):\n                                #region V\n                                heading_diff, beta, center_p2_efficient = Heading_diff_track (region='V', heading=heading_deg, field_tilt=field_tilt_V)\n                                edge_distance = Distance_from_edge (region='V', radius=radius_2, field_tilt=field_tilt_V, p_2=p_2, tan_pt=tan_pt_V, curve_center=curve_center_2)\n                                cum_dist1, ang_velocity = effective_distance (region='V', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_V)\n                                region='V'\n                                center_to_pt = 0.\n                                #print('1st case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                            elif (((22.822258598203565*p_1[1]) + 30.07393628609182) < p_1[0] < ((15.206057595535578*p_1[1]) + 20.274940495850483)):\n                                #region VI\n                                heading_diff, beta, center_p2_efficient = Heading_diff_track (region='VI', heading=heading_deg, field_tilt=field_tilt_VI)\n                                edge_distance = Distance_from_edge (region='VI', radius=radius_2, field_tilt=field_tilt_VI, p_2=p_2, tan_pt=tan_pt_VI, curve_center=curve_center_2)\n                                cum_dist1, ang_velocity = effective_distance (region='VI', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_VI)\n                                region='VI' \n                                center_to_pt = 0.\n                                #print('2nd case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                            else:\n                                print('i have reached here but no match')\n                        elif ((distance1<=distance2) and region_proposal):\n                            if ((last_region=='V')): #and (p_1[0] < ((0.4574280751735761*p_1[1]) + 1.2991488958417325))): #less than tangent pt\n                                if (p_1[0] < ((0.4574280751735761*p_1[1]) + 1.2991488958417325)):\n                                    #region V\n                                    heading_diff, beta, center_p2_efficient = Heading_diff_track (region='V', heading=heading_deg, field_tilt=field_tilt_V)\n                                    edge_distance = Distance_from_edge (region='V', radius=radius_2, field_tilt=field_tilt_V, p_2=p_2, tan_pt=tan_pt_V, curve_center=curve_center_2)\n                                    cum_dist1, ang_velocity = effective_distance (region='V', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_V)\n                                    region='V'\n                                    center_to_pt = 0.\n                                    #cum_dist2 = gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                                    #print('3rd case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                                else: #if (cum_dist2>428.9218557728617): #428.9218557728617\n                                    #Region III\n                                    heading_diff, beta, center_p2_efficient = Heading_diff_track (region='III', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_2, pt_1=p_1)\n                                    edge_distance = Distance_from_edge (region='III', radius=radius_2, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_2)\n                                    cum_dist1, ang_velocity = effective_distance (region='III', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                                    region='III'\n                                    center_to_pt = distance1\n                                    #print('4th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n\n                            elif ((last_region=='VI')): #and (p_1[0] < ((-0.01714623672563829*p_1[1]) + 0.6885383703998347))): #less than tangent pt\n                                if (p_1[0] < ((-0.01714623672563829*p_1[1]) + 0.6885383703998347)):\n                                    #region VI\n                                    heading_diff, beta, center_p2_efficient = Heading_diff_track (region='VI', heading=heading_deg, field_tilt=field_tilt_VI)\n                                    edge_distance = Distance_from_edge (region='VI', radius=radius_2, field_tilt=field_tilt_VI, p_2=p_2, tan_pt=tan_pt_VI, curve_center=curve_center_2)\n                                    cum_dist1, ang_velocity = effective_distance (region='VI', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_VI)\n                                    region='VI' \n                                    center_to_pt = 0.\n                                    #cum_dist2 = gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                                    #print('5th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                                else: # (cum_dist2>447.4455405455664):                               \n                                    #Region III\n                                    heading_diff, beta, center_p2_efficient = Heading_diff_track (region='III', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_2, pt_1=p_1)\n                                    edge_distance = Distance_from_edge (region='III', radius=radius_2, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_2)\n                                    cum_dist1, ang_velocity = effective_distance (region='III', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                                    region='III' \n                                    center_to_pt = distance1\n                                    #print('6th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                        else:\n                            #Region III\n                            heading_diff, beta, center_p2_efficient = Heading_diff_track (region='III', heading=heading_deg, field_tilt=field_tilt_center, stride=distance_ft, curve_center=curve_center_2, pt_1=p_1)\n                            edge_distance = Distance_from_edge (region='III', radius=radius_2, field_tilt=field_tilt_center, p_2=p_2, curve_center=curve_center_2)\n                            cum_dist1, ang_velocity = effective_distance (region='III', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_center)\n                            region='III'  \n                            center_to_pt = distance1\n                            #print('7th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n\n                        #else:\n                            #region IV\n                            #heading_diff, beta, center_p2_efficient = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                            #edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                            #cum_dist1, ang_velocity = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                            #region='IV'\n                            #center_to_pt = 0.\n                            #print('8th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                    elif (p_1[0] < ((-0.25758175782378967*p_1[1]) + 0.3791822670635518)): #center line equation\n                        # region II\n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='II', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='II', radius=radius_1, field_tilt=field_tilt_II, p_2=p_2, tan_pt=tan_pt_II, curve_center=curve_center_1)\n                        cum_dist1, ang_velocity = effective_distance (region='II', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_II)\n                        region='II'\n                        center_to_pt = 0.\n                        #print('9th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                    elif (p_1[0] > ((-0.25758175782378967*p_1[1]) + 0.3791822670635518)): #center line equation  ((-0.257582370157123*p_1[1]) + 0.37920815875501385)\n                        # region IV  \n                        heading_diff, beta, center_p2_efficient = Heading_diff_track (region='IV', heading=heading_deg, field_tilt=field_tilt_center)\n                        edge_distance = Distance_from_edge (region='IV', radius=radius_2, field_tilt=field_tilt_IV, p_2=p_2, tan_pt=tan_pt_IV, curve_center=curve_center_2)\n                        cum_dist1, ang_velocity = effective_distance (region='IV', p_1=p_1, p_2=p_2, curve_center_1=curve_center_1, curve_center_2=curve_center_2, head_diff=heading_diff, stride=distance_ft, radius_1=radius_1, radius_2=radius_2, field_tilt=field_tilt_IV)\n                        region='IV'\n                        center_to_pt = 0.\n                        #print('10th case {} {} region {}'.format(degrees_func(p_1), degrees_func(p_2), region))\n                    else:\n                        print('no match found')\n                    #print(\"first point is: {} {}\".format(p_1, degrees_func(p_1)))                    \n                                                                            \n                    if (i == 0):\n                        if (distance_ft == 0.):\n                            cum_dist = 0.\n                            heading_deg = 0.\n                            heading_diff = 0.\n                        else:\n                            cum_dist = cum_dist1 # this shall be a line or curve!                            \n                        uncor_cum_dist = distance_ft\n                        prev_heading_diff = 0.\n                        \n                    elif (i > 0):     \n                        if (distance_ft == 0.):\n                            cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            heading_deg = 0.\n                            heading_diff = np.nan\n                            uncor_cum_dist = gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = np.nan\n                        else:\n                            cum_dist = cum_dist1 + gen_data.iloc[-1, gen_data.columns.get_loc('cum_distance')]\n                            uncor_cum_dist = distance_ft + gen_data.iloc[-1, gen_data.columns.get_loc('uncor_cum_distance')]\n                            prev_heading_diff = prev_heading_diff_calculator (heading_deg, gen_data.iloc[-1, gen_data.columns.get_loc('heading')], field_tilt=field_tilt_center)\n\n                    velocity_horizontal = distance_ft/0.25\n                    try:\n                        one_foot_speed = 0.25/distance_ft\n                    except ZeroDivisionError:\n                        one_foot_speed = 0.\n                    stride = distance_ft                     \n                    \n                    row_to_app = {'region':region, 'track_id':tr_id, 'race_date':rc_date, 'race_number':rc_no, 'program_number':pg_no, 'stride':stride, 'velocity_horizontal':velocity_horizontal, 'one_foot_speed':one_foot_speed, 'ang_velocity':ang_velocity, 'cum_distance':cum_dist, 'uncor_cum_distance':uncor_cum_dist,                                 \n                                   'center_to_pt1':center_to_pt, 'center_pt2_efficient':center_p2_efficient, 'heading': heading_deg, 'track_heading_diff':heading_diff, 'beta':beta, 'prev_heading_diff':prev_heading_diff, 'edge_distance':edge_distance, 'trakus_index':tr_idx}\n                    gen_data = gen_data.append(row_to_app, ignore_index=True)\n                    \n                    if ((edge_distance<0.) or (heading_diff>20.)):\n                        print(\"details are: {} {} {} {} {}\".format(tr_id, rc_date, rc_no, pg_no, tr_idx))\n                        print(\"first point is: {}\".format(degrees_func(p_1)))\n                        print(\"second point is: {}\".format(degrees_func(p_2)))\n                        print(\"current heading is: {}\".format(heading_deg))\n                        print(\"heading_diff is: {}\".format(heading_diff))\n                        print(\"edge_distance is: {}\".format(edge_distance))\n                        print(\"region is: {} \\n\".format(region))\n                \n            \n            file_path = os.path.join(tracking_path, \"{}-{}-{}.csv\".format(tr_id, rc_date, rc_no))\n            gen_data.to_csv(file_path, index=False) # ??pivot it before writing it.\n        #break\n    #break\n           ","metadata":{"execution":{"iopub.status.busy":"2022-10-28T20:55:05.146348Z","iopub.execute_input":"2022-10-28T20:55:05.147032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# pivoting\nfor x in os.listdir('./tracking_2'):\n    x_path = os.path.join('./tracking_2', x)\n    x_save = os.path.join('./tracking_pvt', x)\n    df = pd.read_csv(x_path, parse_dates=True)\n    df.race_number = [int(x) for x in df.race_number]\n    df.program_number = [str(x) for x in df.program_number]\n    df = pd.merge(df, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\n    df = pd.merge(df, nyra_start_table[['track_id', 'race_date', 'race_number', 'program_number', 'position_at_finish']],  how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number'])\n    df.distance_id = [(x*660) for x in df.distance_id]\n    df = pivot_df (df)\n    df.to_csv(x_save)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#lets create complete tables for each race. \ntracking_path_2 = \"./tracking_complete\" \nif not os.path.exists(tracking_path_2): \n    os.makedirs(tracking_path_2) \n\nfor x in os.listdir('./tracking_2'): \n    x_path = os.path.join('./tracking_2', x) \n    x_save = os.path.join('./tracking_complete', x) \n    df = pd.read_csv(x_path, parse_dates=True) \n    df.race_number = [int(x) for x in df.race_number] \n    df.program_number = [str(x) for x in df.program_number] \n    df = pd.merge(df, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number']) \n    df = pd.merge(df, nyra_start_table, how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number']) \n    df.distance_id = [(x*660) for x in df.distance_id] \n    df.to_csv(x_save, index=False)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"os.listdir('./tracking_pvt')","metadata":{"execution":{"iopub.status.busy":"2022-10-11T00:42:18.249244Z","iopub.execute_input":"2022-10-11T00:42:18.249804Z","iopub.status.idle":"2022-10-11T00:42:18.258529Z","shell.execute_reply.started":"2022-10-11T00:42:18.249756Z","shell.execute_reply":"2022-10-11T00:42:18.257297Z"}}},{"cell_type":"raw","source":"file_list = []\n\nfor dirname, _, filenames in os.walk('./'):\n    for filename in filenames:\n        file_list.append(file_name)\n         \nfile_list","metadata":{"execution":{"iopub.status.busy":"2022-10-10T10:44:08.326676Z","iopub.execute_input":"2022-10-10T10:44:08.327134Z","iopub.status.idle":"2022-10-10T10:44:08.335952Z","shell.execute_reply.started":"2022-10-10T10:44:08.327100Z","shell.execute_reply":"2022-10-10T10:44:08.334686Z"}}},{"cell_type":"raw","source":"file_path = os.path.join(tracking_path, \"gen_data.csv\")\ngen_data.to_csv(file_path) # ??pivot it before writing it.","metadata":{}},{"cell_type":"raw","source":"const = math.pi/180.\n#print([40.66670833*const, -73.83051944*const], [40.66924444*const, -73.82935278*const])\n[0.7098320194694541/const, -1.2885560343009308/const]\n","metadata":{"execution":{"iopub.status.busy":"2022-10-10T09:24:20.728508Z","iopub.execute_input":"2022-10-10T09:24:20.729112Z","iopub.status.idle":"2022-10-10T09:24:20.736897Z","shell.execute_reply.started":"2022-10-10T09:24:20.729061Z","shell.execute_reply":"2022-10-10T09:24:20.735958Z"}}},{"cell_type":"raw","source":"distance_calculator([[0.7097679563067046, -1.28858565268568], [0.7098122197763974, -1.2885652906274285]])","metadata":{"execution":{"iopub.status.busy":"2022-10-09T13:52:59.965807Z","iopub.execute_input":"2022-10-09T13:52:59.966236Z","iopub.status.idle":"2022-10-09T13:52:59.975322Z","shell.execute_reply.started":"2022-10-09T13:52:59.966199Z","shell.execute_reply":"2022-10-09T13:52:59.974041Z"}}},{"cell_type":"raw","source":"math.degrees(0.709815441142775), math.degrees(-1.2885621658617266)","metadata":{"execution":{"iopub.status.busy":"2022-10-09T15:57:44.445499Z","iopub.execute_input":"2022-10-09T15:57:44.445884Z","iopub.status.idle":"2022-10-09T15:57:44.455961Z","shell.execute_reply.started":"2022-10-09T15:57:44.445853Z","shell.execute_reply":"2022-10-09T15:57:44.454914Z"}}},{"cell_type":"raw","source":"details are: AQU 2019-01-01 1 1 10_11\nfirst point is: [0.7098173117289859, -1.28856165616125]\nsecond point is: [0.7098177888404736, -1.2885614463286537]\ncurrent heading is: 18.447571457939528\nheading_diff is: 20.81160473374776\nedge_distance is: 45.45687599277511\nregion is: V ","metadata":{}},{"cell_type":"raw","source":"print(-73.82726389/40.67200833, -73.82957778/40.67255833)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:27:50.425881Z","iopub.execute_input":"2022-10-08T15:27:50.426366Z","iopub.status.idle":"2022-10-08T15:27:50.432708Z","shell.execute_reply.started":"2022-10-08T15:27:50.426329Z","shell.execute_reply":"2022-10-08T15:27:50.431711Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67200833)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:01:14.580118Z","iopub.execute_input":"2022-10-08T16:01:14.580593Z","iopub.status.idle":"2022-10-08T16:01:14.611131Z","shell.execute_reply.started":"2022-10-08T16:01:14.580553Z","shell.execute_reply":"2022-10-08T16:01:14.609384Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67255833)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:23:37.549358Z","iopub.execute_input":"2022-10-08T16:23:37.549843Z","iopub.status.idle":"2022-10-08T16:23:37.558155Z","shell.execute_reply.started":"2022-10-08T16:23:37.549803Z","shell.execute_reply":"2022-10-08T16:23:37.557009Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.67153333)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:04:18.753383Z","iopub.execute_input":"2022-10-08T16:04:18.754244Z","iopub.status.idle":"2022-10-08T16:04:18.762814Z","shell.execute_reply.started":"2022-10-08T16:04:18.754187Z","shell.execute_reply":"2022-10-08T16:04:18.761539Z"}}},{"cell_type":"raw","source":"(0.4619447620307952*40.66939167)-92.61630179017385","metadata":{"execution":{"iopub.status.busy":"2022-10-08T16:13:06.220156Z","iopub.execute_input":"2022-10-08T16:13:06.220604Z","iopub.status.idle":"2022-10-08T16:13:06.227888Z","shell.execute_reply.started":"2022-10-08T16:13:06.220570Z","shell.execute_reply":"2022-10-08T16:13:06.226884Z"}}},{"cell_type":"raw","source":"So if it is greater than long then it has crossed the line","metadata":{}},{"cell_type":"raw","source":"const = math.pi/180.\n_, dis_r = distance_calculator([[40.67043262317046*const, -73.83059062029287*const], [40.67200833*const, -73.82726389*const]])\n_, dis_l = distance_calculator([[40.67043262317046*const, -73.83059062029287*const], [40.67255833*const, -73.82957778*const]])\nprint(dis_r*-1.81518609287716, dis_l*-1.8152184374776208)","metadata":{"execution":{"iopub.status.busy":"2022-10-08T15:35:33.291255Z","iopub.execute_input":"2022-10-08T15:35:33.291701Z","iopub.status.idle":"2022-10-08T15:35:33.300219Z","shell.execute_reply.started":"2022-10-08T15:35:33.291665Z","shell.execute_reply":"2022-10-08T15:35:33.299158Z"}}},{"cell_type":"raw","source":"file_path = os.path.join(tracking_path, \"aqu_D.csv\")\ngen_data.to_csv(file_path) # ??pivot it before writing it.","metadata":{"execution":{"iopub.status.busy":"2022-10-07T05:49:28.675318Z","iopub.execute_input":"2022-10-07T05:49:28.675824Z","iopub.status.idle":"2022-10-07T05:49:28.684041Z","shell.execute_reply.started":"2022-10-07T05:49:28.675786Z","shell.execute_reply":"2022-10-07T05:49:28.682583Z"}}},{"cell_type":"raw","source":"details are: AQU 2019-01-01 4 5 89_90\nfirst point is: [0.7098095661833876, -1.2885875616806186]\nsecond point is: [0.7098095635787577, -1.288586562348251]\ncurrent heading is: 90.19688355104213\nheading_diff is: -70.94956515364728\nedge_distance is: -303.41272131787963\nregion is: VI \n","metadata":{"execution":{"iopub.status.busy":"2022-10-07T03:59:41.444554Z","iopub.execute_input":"2022-10-07T03:59:41.445074Z","iopub.status.idle":"2022-10-07T03:59:41.455321Z","shell.execute_reply.started":"2022-10-07T03:59:41.445036Z","shell.execute_reply":"2022-10-07T03:59:41.453937Z"}}},{"cell_type":"raw","source":"[0.7098095635787577*180/math.pi, -1.288586562348251*180/math.pi]","metadata":{"execution":{"iopub.status.busy":"2022-10-07T04:32:26.509440Z","iopub.execute_input":"2022-10-07T04:32:26.509873Z","iopub.status.idle":"2022-10-07T04:32:26.519282Z","shell.execute_reply.started":"2022-10-07T04:32:26.509819Z","shell.execute_reply":"2022-10-07T04:32:26.517908Z"}}},{"cell_type":"raw","source":"x = \"hello\"\n\n#if condition returns False, AssertionError is raised:\nassert x == \"goodbye\", \"x should be 'hello'\"","metadata":{"execution":{"iopub.status.busy":"2022-10-07T04:09:33.380766Z","iopub.execute_input":"2022-10-07T04:09:33.381257Z","iopub.status.idle":"2022-10-07T04:09:33.404342Z","shell.execute_reply.started":"2022-10-07T04:09:33.381221Z","shell.execute_reply":"2022-10-07T04:09:33.402784Z"}}},{"cell_type":"raw","source":"path = \"./home\"\nos.path.join(path, \"User/Desktop\", \"file.txt\")","metadata":{}},{"cell_type":"raw","source":"pd.set_option('display.max_rows', None)\npd.set_option('display.max_columns', None)\npd.set_option('display.width', None)\npd.set_option('display.max_colwidth', -1)\n \ndisplay(gen_data)","metadata":{}},{"cell_type":"raw","source":"pd.reset_option('all')\ndisplay(gen_data)","metadata":{}},{"cell_type":"raw","source":"nyra_start_table.shape, nyra_race_table.shape, nyra_tracking_table.shape","metadata":{}},{"cell_type":"raw","source":"nyra_race_table[nyra_race_table.race_date == '2019-01-01']","metadata":{}},{"cell_type":"raw","source":"nyra_race_table.groupby(['race_number']).count()","metadata":{}},{"cell_type":"raw","source":"nyra_start_table[nyra_start_table['position_at_finish']==1].groupby(['weight_carried']).count().sort_values('position_at_finish')\n","metadata":{}},{"cell_type":"raw","source":"nyra_tracking_table.groupby(['race_date', 'race_number', 'trakus_index']).count()","metadata":{}},{"cell_type":"raw","source":"tracking_1st_date_1st_race = nyra_tracking_table[(nyra_tracking_table['race_date'] == '2019-01-01') & (nyra_tracking_table['race_number'] == 1)].sort_values(['trakus_index', 'program_number'])","metadata":{}},{"cell_type":"raw","source":"tracking_1st_date_1st_race.to_csv('./tracking_1st_date_1st_race.csv') ","metadata":{}},{"cell_type":"raw","source":"path = './tracking_2/AQU-2019-01-01-3.csv'\nrace_no_3 = pd.read_csv(path, index_col=0)\n#race_no_3.race_number = [int(x) for x in race_no_3.race_number]\n#race_no_3.program_number = [str(x) for x in race_no_3.program_number]\n#race_no_3_piv = race_no_3.pivot(index='trakus_index', columns='program_number')\nrace_no_3","metadata":{"execution":{"iopub.status.busy":"2022-10-10T13:10:11.656494Z","iopub.execute_input":"2022-10-10T13:10:11.656970Z","iopub.status.idle":"2022-10-10T13:10:11.719208Z","shell.execute_reply.started":"2022-10-10T13:10:11.656932Z","shell.execute_reply":"2022-10-10T13:10:11.717997Z"}}},{"cell_type":"raw","source":"path = './tracking_2/AQU-2019-01-01-3.csv'\nrace_no_9 = pd.read_csv(path, index_col=0)\nrace_no_9.race_number = [int(x) for x in race_no_9.race_number]\nrace_no_9.program_number = [str(x) for x in race_no_9.program_number]\n \n#display(race_no_7) #.sort_values('edge_distance', ascending=True))","metadata":{"execution":{"iopub.status.busy":"2022-10-09T16:31:42.646533Z","iopub.execute_input":"2022-10-09T16:31:42.646946Z","iopub.status.idle":"2022-10-09T16:31:42.669132Z","shell.execute_reply.started":"2022-10-09T16:31:42.646914Z","shell.execute_reply":"2022-10-09T16:31:42.668269Z"}}},{"cell_type":"raw","source":"race_no_3 = pd.merge(race_no_3, nyra_race_table, how='left', left_on=['track_id','race_date', 'race_number'], right_on = ['track_id','race_date', 'race_number'])\nrace_no_3 = pd.merge(race_no_3, nyra_start_table[['track_id', 'race_date', 'race_number', 'program_number', 'position_at_finish']],  how='left', left_on=['track_id', 'race_date', 'race_number', 'program_number'], right_on = ['track_id', 'race_date', 'race_number', 'program_number'])\nrace_no_3.distance_id = [(x*660) for x in race_no_3.distance_id]","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:09.088660Z","iopub.execute_input":"2022-10-10T12:53:09.089869Z","iopub.status.idle":"2022-10-10T12:53:09.123562Z","shell.execute_reply.started":"2022-10-10T12:53:09.089808Z","shell.execute_reply":"2022-10-10T12:53:09.122366Z"}}},{"cell_type":"raw","source":"print(sum(np.isnan(race_no_3['track_heading_diff']))) # checking if there is a horse that was at stand still\n#race_no_9","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:11.377717Z","iopub.execute_input":"2022-10-10T12:53:11.378146Z","iopub.status.idle":"2022-10-10T12:53:11.386438Z","shell.execute_reply.started":"2022-10-10T12:53:11.378109Z","shell.execute_reply":"2022-10-10T12:53:11.385009Z"}}},{"cell_type":"raw","source":"idxs = []\nnew_tr_idx = []\ntr_idx = list(race_no_3.trakus_index)\nfor x in tr_idx:\n    a, b = x.split('_')\n    idxs.append(int(a))\n    idxs.append(int(b))\nmax_idx = max(idxs)\nmin_idx = min(idxs)\nfor i in range(min_idx, max_idx):\n    new_tr_idx.append(\"{}_{}\".format(i, i+1))\n#new_tr_idx\nrace_no_3_piv = race_no_3.pivot(index='trakus_index', columns='program_number')\nrace_no_3_piv = race_no_3_piv.reindex(new_tr_idx)\nrace_no_3_piv\n#race_no_9_piv.to_csv('./race_no_9_piv_XX.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-10T12:53:17.186688Z","iopub.execute_input":"2022-10-10T12:53:17.187653Z","iopub.status.idle":"2022-10-10T12:53:17.234426Z","shell.execute_reply.started":"2022-10-10T12:53:17.187611Z","shell.execute_reply":"2022-10-10T12:53:17.232719Z"}}},{"cell_type":"raw","source":"pivoted_distance_df = distance_df.pivot('pg', 'trakus')['distance']\npivoted_distance_df","metadata":{"execution":{"iopub.status.busy":"2022-09-26T14:52:28.887615Z","iopub.execute_input":"2022-09-26T14:52:28.888059Z","iopub.status.idle":"2022-09-26T14:52:29.160079Z","shell.execute_reply.started":"2022-09-26T14:52:28.888020Z","shell.execute_reply":"2022-09-26T14:52:29.159168Z"}}},{"cell_type":"raw","source":"max_distance = np.array(pivoted_distance_df.max())\nmin_distance = np.array(pivoted_distance_df.min())\nlist(max_distance/min_distance).index(min(list(max_distance/min_distance)))\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T14:58:45.267568Z","iopub.execute_input":"2022-09-26T14:58:45.268006Z","iopub.status.idle":"2022-09-26T14:58:45.281038Z","shell.execute_reply.started":"2022-09-26T14:58:45.267972Z","shell.execute_reply":"2022-09-26T14:58:45.279875Z"}}},{"cell_type":"raw","source":"math.sin(30)","metadata":{"execution":{"iopub.status.busy":"2022-09-26T18:04:36.599344Z","iopub.execute_input":"2022-09-26T18:04:36.601431Z","iopub.status.idle":"2022-09-26T18:04:36.610398Z","shell.execute_reply.started":"2022-09-26T18:04:36.601320Z","shell.execute_reply":"2022-09-26T18:04:36.608618Z"}}},{"cell_type":"raw","source":"race_no_3_piv = race_no_3.pivot('trakus_index', 'program_numbers')\nrace_no_3_piv.reindex(new_tr_idx).to_csv('./race_no_7_piv.csv')\nrace_no_3_piv_ridx = race_no_3_piv.reindex(new_tr_idx)\nrace_no_3_piv_ridx","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:22.655038Z","iopub.execute_input":"2022-09-28T15:34:22.656240Z","iopub.status.idle":"2022-09-28T15:34:23.664786Z","shell.execute_reply.started":"2022-09-28T15:34:22.656195Z","shell.execute_reply":"2022-09-28T15:34:23.663310Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"def param_calculator1(idx_1:int, idx_2:int, sub_col:int, df:pd.DataFrame):\n    a, b = idx_1, idx_2\n    ratio_1 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][a])+abs(df['acceleration'][sub_col][b])))\n    time_ratio_1 = 0.25*ratio_1   \n    if (horse_raced_data_1['acceleration'][sub_col][b] > horse_raced_data_1['acceleration'][sub_col][b+1]):\n        ratio_2 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+time_ratio_2\n        stride = df['distance_ft'][sub_col][b]*ratio_1 + df['distance_ft'][sub_col][b+1]*ratio_2\n        v_int_x = stride / total_time\n        v_int_y = 32.17*total_time*0.5\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = df['cum_distance'][sub_col][b]-(v_int_x*time_ratio_1)\n        end_point = start_point + stride\n    else:\n        ratio_2 = (abs(df['acceleration'][sub_col][b+1])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b+2])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = df['distance_ft'][sub_col][b+1]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*32.17)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = df['cum_distance'][sub_col][b]-(v_int_x*time_ratio_1)\n        end_point = start_point + stride\n    return start_point, end_point, stride, total_time, v_int, theta, time_ratio_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:42.188463Z","iopub.execute_input":"2022-09-28T15:34:42.188995Z","iopub.status.idle":"2022-09-28T15:34:42.203364Z","shell.execute_reply.started":"2022-09-28T15:34:42.188954Z","shell.execute_reply":"2022-09-28T15:34:42.202280Z"}}},{"cell_type":"raw","source":"def param_calculator2(idx_1:int, start_point_2:float, time_ratio_1:float, sub_col:int, df:pd.DataFrame):\n    a, b = idx_1-1, idx_1  \n    v_int_x = (df['cum_distance'][sub_col][b]-start_point_2)/time_ratio_1\n    \n    if (horse_raced_data_1['acceleration'][sub_col][b] > horse_raced_data_1['acceleration'][sub_col][b+1]):\n        ratio_2 = (abs(df['acceleration'][sub_col][b])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+time_ratio_2\n        stride = v_int_x*total_time\n        v_int_y = 32.17*total_time*0.5\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        end_point = start_point_2 + stride\n    else:\n        ratio_2 = (abs(df['acceleration'][sub_col][b+1])/(abs(df['acceleration'][sub_col][b+1])+abs(df['acceleration'][sub_col][b+2])))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = df['distance_ft'][sub_col][b+1]\n        stride = v_int_x*total_time\n        v_int_y = 32.17*total_time*0.5 # 32.17m/s2 - gravity\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        end_point = start_point_2 + stride\n    return end_point, stride, total_time, v_int, theta, time_ratio_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:34:43.644708Z","iopub.execute_input":"2022-09-28T15:34:43.645310Z","iopub.status.idle":"2022-09-28T15:34:43.656394Z","shell.execute_reply.started":"2022-09-28T15:34:43.645265Z","shell.execute_reply":"2022-09-28T15:34:43.655169Z"}}},{"cell_type":"raw","source":"horse_raced_data_1 = race_no_3_piv_ridx[['distance_ft', 'acceleration', 'cum_distance']]\nsub_col_list = horse_raced_data_1['distance_ft'].columns\nhorse_raced_data_2 = pd.DataFrame(columns = ['pg_no','launch_pt','land_point','stride','stride_time','launch_speed','launch_angle'])\n\nfor sub_col in sub_col_list:\n    len_df = len(horse_raced_data_1)\n    for i in range(len_df):\n        if(horse_raced_data_1['cum_distance'][sub_col][i]>36.):\n            first_runup_idx = i\n            break\n    #print(horse_raced_data_1['v_final'][sub_col][i-1], horse_raced_data_1['v_final'][sub_col][i], horse_raced_data_1['v_final'][sub_col][i+1],horse_raced_data_1['v_final'][sub_col][i+2])\n    i = first_runup_idx\n    if (horse_raced_data_1['acceleration'][sub_col][i-1] > horse_raced_data_1['acceleration'][sub_col][i]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-1, i, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_1\".format(sub_col))\n        while ((i+4)<len_df):\n            start_point_2 = end_point\n            time_ratio_1 = 0.25-time_ratio_2\n            for j in range(len_df):\n                if(horse_raced_data_1['cum_distance'][sub_col][j]>start_point_2):\n                    start_idx = j\n                    break\n            i = start_idx\n            end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator2(i, start_point_2, time_ratio_1, sub_col, horse_raced_data_1)            \n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point_2,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n            print(\"{} is being processed by case_1\".format(sub_col))\n            \n    #break\n                                 \n    elif (horse_raced_data_1['acceleration'][sub_col][i-2] > horse_raced_data_1['acceleration'][sub_col][i-1]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-2, i-1, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_2\".format(sub_col))\n        while ((i+3)<len_df):\n            i=i+2\n            start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i-2, i-1, sub_col, horse_raced_data_1)\n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)            \n            print(\"{} is being processed by case_2\".format(sub_col))\n    \n        \n    elif (horse_raced_data_1['acceleration'][sub_col][i] > horse_raced_data_1['acceleration'][sub_col][i+1]):\n        start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i, i+1, sub_col, horse_raced_data_1)\n        row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n        horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)\n        print(\"{} is being processed by case_3\".format(sub_col))\n        while ((i+5)<len_df):\n            i=i+2\n            start_point, end_point, stride, total_time, v_int, theta, time_ratio_2 = param_calculator1(i, i+1, sub_col, horse_raced_data_1)\n            row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n            horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)            \n            print(\"{} is being processed by case_3\".format(sub_col))\n        \n    \n    else:\n        print(\"all conditions are not fulfilled:\", sub_col)\n                                      \n    #row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n    #horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)   \n        \n    #break\nhorse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T15:35:32.317446Z","iopub.execute_input":"2022-09-28T15:35:32.319298Z","iopub.status.idle":"2022-09-28T15:36:05.726390Z","shell.execute_reply.started":"2022-09-28T15:35:32.319244Z","shell.execute_reply":"2022-09-28T15:36:05.724980Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"horse_raced_data_2.pg_no = [int(x) for x in horse_raced_data_2.pg_no]\n\nhorse_raced_data_2.pivot(index=None, columns='pg_no')\n#horse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-28T12:10:46.113807Z","iopub.execute_input":"2022-09-28T12:10:46.114148Z","iopub.status.idle":"2022-09-28T12:10:47.077902Z","shell.execute_reply.started":"2022-09-28T12:10:46.114123Z","shell.execute_reply":"2022-09-28T12:10:47.076353Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"raw","source":"horse_raced_data_1 = race_no_3_piv_ridx[['distance_ft', 'v_final', 'cum_distance']]\nsub_col_list = horse_raced_data_1['distance_ft'].columns\nhorse_raced_data_2 = pd.DataFrame(columns = ['pg_no','launch_pt','land_point','stride','stride_time','launch_speed','launch_angle'])\n\nfor sub_col in sub_col_list:\n    len_df = len(horse_raced_data_1)\n    for i in range(len_df):\n        if(horse_raced_data_1['cum_distance'][sub_col][i]>36.):\n            first_runup_idx = i\n            break\n    #print(horse_raced_data_1['v_final'][sub_col][i-1], horse_raced_data_1['v_final'][sub_col][i], horse_raced_data_1['v_final'][sub_col][i+1],horse_raced_data_1['v_final'][sub_col][i+2])\n    i = first_runup_idx\n    if (horse_raced_data_1['v_final'][sub_col][i-1] > horse_raced_data_1['v_final'][sub_col][i]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i])/(horse_raced_data_1['v_final'][sub_col][i-1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i+1])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i+2]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i+1]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i+1]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_1\".format(sub_col))\n                                 \n    elif (horse_raced_data_1['v_final'][sub_col][i-2] > horse_raced_data_1['v_final'][sub_col][i-1]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i-1])/(horse_raced_data_1['v_final'][sub_col][i-1]+horse_raced_data_1['v_final'][sub_col][i-2]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i-1]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_2\".format(sub_col))\n        \n    elif (horse_raced_data_1['v_final'][sub_col][i] > horse_raced_data_1['v_final'][sub_col][i+1]):\n        ratio_1 = ((horse_raced_data_1['v_final'][sub_col][i+1])/(horse_raced_data_1['v_final'][sub_col][i+1]+horse_raced_data_1['v_final'][sub_col][i]))\n        time_ratio_1 = 0.25*ratio_1        \n        ratio_2 = ((horse_raced_data_1['v_final'][sub_col][i+2])/(horse_raced_data_1['v_final'][sub_col][i+2]+horse_raced_data_1['v_final'][sub_col][i+3]))\n        time_ratio_2 = 0.25*ratio_2\n        total_time = time_ratio_1+0.25+time_ratio_2\n        delta_dist = horse_raced_data_1['distance_ft'][sub_col][i+2]\n        stride = 4*total_time*delta_dist\n        v_int_y = (stride*9.81)/(8*delta_dist)\n        v_int_x = 4*delta_dist\n        v_int = math.sqrt((v_int_y**2)+(v_int_x**2))\n        theta = math.atan(v_int_y/v_int_x) * (180./math.pi)\n        start_point = horse_raced_data_1['cum_distance'][sub_col][i+1]-(v_int_x*time_ratio_1)\n        end_point = horse_raced_data_1['cum_distance'][sub_col][i+2]+(v_int_x*time_ratio_2)\n        print(\"{} is being processed by case_3\".format(sub_col))\n    \n    else:\n        print(\"all conditions are not fulfilled:\", sub_col)\n                                      \n    row_to_app = {'pg_no':sub_col,'launch_pt':start_point,'land_point':end_point,'stride':stride,'stride_time':total_time,'launch_speed':v_int,'launch_angle':theta}\n    horse_raced_data_2 = horse_raced_data_2.append(row_to_app, ignore_index=True)   \n        \n    #break\nhorse_raced_data_2","metadata":{"execution":{"iopub.status.busy":"2022-09-27T20:16:23.525581Z","iopub.execute_input":"2022-09-27T20:16:23.526074Z","iopub.status.idle":"2022-09-27T20:16:23.656878Z","shell.execute_reply.started":"2022-09-27T20:16:23.526038Z","shell.execute_reply":"2022-09-27T20:16:23.655328Z"},"jupyter":{"source_hidden":true}}},{"cell_type":"raw","source":"race_no_7_distance = race_no_3[['trakus_index', 'program_numbers', 'distance_ft', 'cum_distance']].pivot('trakus_index', 'program_numbers').reindex(new_tr_idx)\nrace_no_7_distance.to_csv('./race_no_7_distance.csv')\nrace_no_7_distance[:10] #['distance_ft'][9]\n","metadata":{"execution":{"iopub.status.busy":"2022-09-26T18:45:54.412424Z","iopub.execute_input":"2022-09-26T18:45:54.412917Z","iopub.status.idle":"2022-09-26T18:45:54.584895Z","shell.execute_reply.started":"2022-09-26T18:45:54.412881Z","shell.execute_reply":"2022-09-26T18:45:54.583638Z"},"jupyter":{"outputs_hidden":true}}},{"cell_type":"code","source":"[0.7517147279719464*180/math.pi, -1.2875302531280253*180/math.pi]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"race_no_3_piv = race_no_3.pivot('trakus_index', 'program_numbers')\nrace_no_3_piv.reindex(new_tr_idx)","metadata":{"execution":{"iopub.status.busy":"2022-09-25T13:58:21.339618Z","iopub.execute_input":"2022-09-25T13:58:21.340666Z","iopub.status.idle":"2022-09-25T13:58:22.319503Z","shell.execute_reply.started":"2022-09-25T13:58:21.340620Z","shell.execute_reply":"2022-09-25T13:58:22.318000Z"},"jupyter":{"outputs_hidden":true}}}]}