1

I have a csv file containing wave data (time, tidal elevation, wave period, wave height and wave direction) I want to know, at a given time, when the previous high tide was and the corresponding wave period, height and direction.

enter image description here

I have this code now which selects the line of the time that I'm looking for:

import csv

with open ('Waves_2019.csv') as f:
    reader = csv.reader (f)
    for line_num, content in enumerate(reader):
        if content [0] == '01/03/2019T08:00':
            a = line_num
            print (a)

The next step would then take the previous 12 hours of data to select the highest tidal elevation (0.77 at 01/03/2019T01:00 in example) and then return the other data (period, height, direction).

How could I amend the code that it looks for the max tidal elevation in column 2 based on the previous 12 data points of the selected time? And then return the other data during that high tide?

SiegmundNuyts
  • 67
  • 1
  • 12
  • 1
    Please make your data available in CSV to play with. – mac13k Feb 18 '20 at 11:17
  • Were you able to use the read line to make a list so if you want to read the contents of time='01/03/2019T08:00', you could simply do if csv_list[9][0]='01/03/2019T08:00': print(csv_list[9]). Let's say you want to get 12 previous values before this then simply index it to get all previous values, make a list, then find maximum of the list – Jeeva Bharathi Feb 18 '20 at 11:24
  • You can use this to read the csv into a list: https://stackoverflow.com/questions/24662571/python-import-csv-to-list – Jeeva Bharathi Feb 18 '20 at 11:26

1 Answers1

1

First we select the index of the date we require to examine

selected_index = df.loc[df["time"].eq("01/03/2019T08:00")].index[0]

Then obtain the id value having the maximum tidal_elevation from the previous 12 hours (since there are no missing rows in the dataset, we can safely assume that the previous 12 indices would indicate the 12 hours)

filt = df.loc[selected_index-12: selected_index, "tidal_elevation"].idxmax()

Now, we select other parameters for the index having maximum tidal_elevation

res = df.loc[filt, ["time", "tidal_elevation", "wave_period", "wave_height", "wave_direction"]]
print(res)

P.S. res = df.loc[filt, "time":"wave_direction"] would also work if the chronology of the columns is the same i.e. it is in the same order ["time", "tidal_elevation", "wave_period", "wave_height", "wave_direction"]

Edit: Taking average of values one hour before and after the maximum tidal_elevation

res_avg = df.loc[filt-1:filt+1, "time":"wave_direction"].mean()
print(res_avg)
Abercrombie
  • 1,012
  • 2
  • 13
  • 22
  • 1
    does the answer help your use-case? – Abercrombie Feb 19 '20 at 08:57
  • This worked perfectly! Thanks @JohnDoe. Would it be possible to amend this and take the average of the different columns after selecting high tide (1 hour before high tide + high tide + 1 hour after high tide)? – SiegmundNuyts Feb 19 '20 at 09:06
  • I wanted to build on your code that you helped me with by exporting all the necessary data to a csv file: https://stackoverflow.com/questions/60484795/python-select-different-row-values-from-csv-and-combine-them-in-new-csv @JohnDoe – SiegmundNuyts Mar 02 '20 at 13:48