Python: Read a file with raw records and build a sea level timeseries from it

48 views Asked by At

I have an ASCII file with monthly mean sea level records with multiple lines in the following form:

1969.0417; 7121; 0;000

1969.1250;-99999;00;000

1969.2083; 7121; 0;000

.......................

.......................

.......................

1970.1250;-99999;00;000

1970.2083; 7151; 0;000

1970.2917; 7131; 0;000

This is a 4 columns file holding time as years with 4 decimals, sea level as mm, and some values holding zeros for records. NaN value is -99999

I want my code to build a time series (a two columns list) that will hold: i) the year values in the first column (as a float number) and ii) the sea level values in the second column (as a float number). Once I got the sea level time series as an array of two columns with float numbers I think I can manage the rest of the processing and of the plots I need to do.

I am new to Python, so I tried different strategies.

If I had to do this for a single line I would do the following:

t=[]
sl=[]
line =('1969.0417;  7121; 0;000').replace(" ", "")
time = line.split(';')
t = float(time[0])
sl = float(time[1])

but I have to do it while reading the whole file, thus:

  • First step, I open the file and read its lines:

    path=r"D:.....\4Python\1238.rlrdata" with open(path, "r") as f: for line in f: a = f.readlines()

  • Second step, I tried to split the file using as delimiter ";"

          seg = a.split(';')
    

but I received the following message

AttributeError: 'list' object has no attribute 'split'

  • The next steps include replacing the " " with "", and converting the string to float.

Can you propose me any method?

1

There are 1 answers

0
Yiannis On

After all here is the solution:

# Open the file and read the data
with open(path) as f:
    data = f.readlines()

# Initialize an empty array to hold the data
array = np.zeros((len(data), 2))

# Loop over the lines in the file and fill in the array
for i, line in enumerate(data):
    # Split the line into two parts
    parts = line.strip().split(';')
    # Convert the parts to floats and add them to the array
    array[i, 0] = float(parts[0])
    array[i, 1] = float(parts[1])