I want to calculate the distance between each lat and long in two consecutive rows, I use these commands to do it for a single value but I am not sure how can I run a loop to extract lat and long from each row and get distance with respect to next line.
I've moved your topic to the Programming category.
In order to be able to handle two consecutive lines at a time, you have to remember the previous line:
with open('infile') as ifile:
# skip header
ifile.readline()
# old data line
d0 = None
for d1 in ifile:
if d0:
lon0, lat0 = d0.split()[2:4] # prev data
lon1, lat1 = d1.split()[2:4] # actual data
# get 1st element of returned tuple by the function
dist_m = gps2dist_azimuth(lat0, lon0, lat1, lon1)[0]
# set old line to actual one
d0 = d1
_ is a throwaway variable, the value of which can be ignored. So it doesn't make sense to print it.
Assuming Python is a procedural language, the following should work with syntax changes:
read prev_station prev_lat prev_long prev_elev
while read station lat long elev
do
distance = (lat - prev_lat) .......
print prev_station "to" station distance
prev_station=station
prev_lat=lat
prev_long=long
prev_elev=elev...
done
Thank you so much for your reply.
I tried this script but it seems there is something wrong. I didn't get any output and even errors.
I am sorry if I am doing some stupid mistake.
from obspy.geodetics import gps2dist_azimuth
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
with open("array_loc.txt", 'r') as ifile:
# skip header
ifile.readline()
# old data line
d0 = None
for d1 in ifile:
if d0:
lon0, lat0 = d0.split()[2:4] # prev data
lon1, lat1 = d1.split()[2:4] # actual data
# get 1st element of returned tuple by the function
dist_m= gps2dist_azimuth(lat0, lon0, lat1, lon1)[0]
# set old line to actual one
d0 = d1
print(dist_m)
yes, I agreed with you, I modified the script accordingly but still seems, there is something wrong.
Maybe I am repeating some mistakes.
with open("array_loc.txt", 'r') as ifile:
# skip header
#ifile.readline()
# old data line
d0 = None
for d1 in ifile:
if d0:
lon0, lat0 = d0.split()[1:3] # prev data
lon1, lat1 = d1.split()[1:3] # actual data
# get 1st element of returned tuple by the function
dist = gps2dist_azimuth(lat0, lon0, lat1, lon1)[0]
# set old line to actual one
d0 = d1
print(dist)
Yes, even without replacing, text3, I added print (d1) and I got the original file as below:
Maybe there is some errors in later part.
code:
with open("array_loc.txt", 'r') as ifile:
# skip header
ifile.readline()
# old data line
d0 = None
for d1 in ifile:
print(d1)
if d0:
lon0, lat0 = d0.split()[1:3] # prev data
lon1, lat1 = d1.split()[1:3] # actual data
# get 1st element of returned tuple by the function
dist = gps2dist_azimuth(lat0, lon0, lat1, lon1)[0]
# set old line to actual one
d0 = d1
print(dist)
you have a wrong indentation in your version. I've seen that too late, sorry. Instead of
if d0:
...
# set old line to actual one
d0 = d1
print(dist)
it has to be
if d0:
...
print(dist)
# set old line to actual one
d0 = d1
In your version, the conditional if d0: never matches, cause d0 = d1 would only be set if d0 has some value, but that never happens. As you can see, indentation is essential in python