0

I am working with oceanographic data in Python. I am currently trying to create a contour plot of time on the x axis, pressure on the y axis, and temperature as the contours. Right now I am trying to format the time array with values that I got from the data itself. The code I have only produces the day of the year without the corresponding hours added onto it. I need a way to add on 6 hours, 12 hours, and 18 hours to each day because the data came from profiles of the water column that ran 4 times each day, separated by 6 hours. There were 1260 profiles and i corresponds to the file for each profile.

import numpy
from matplotlib import pyplot as plt
import math
from datetime import tzinfo, timedelta, datetime, time
import time

i = 1
temp = numpy.zeros((1260, 1000))
pressure = numpy.zeros((1260, 1000))
time2 = numpy.zeros(1260)
for i in range(1, 1260):
    filename = '/Users/ryanschubert/InternProject/itp47final/itp47grd' + str(i).zfill(4) + '.dat'
    if os.path.isfile(filename):
        data = open(filename, "r")
    else:
        continue
    depthcounter=-1
    text = data.read()
    data.close()
    text = text.replace('   ', ',')  #this line and the next 5 lines are 
    text = text.replace('  ', ',')   #fixing formatting errors in the code
    text = text.replace(' ', '')
    text = text.replace('NaN', '-98')
    text = text.replace(',,', ',')
    text = text.split('\n')
    for line in text:
        depthcounter=depthcounter+1
        if depthcounter == 1:
            if i ==1:
                words4 = line.split(',')
                year = int(words4[0])
                doy = float(words4[1]) - 1
                hour = int((doy - math.floor(doy))*24)
                t = datetime(year=year, month=1, day=1, hour=hour,tzinfo=utc) + timedelta(days=int(doy))
                time1 = t.timetuple().tm_yday
                timetemp = numpy.array([t + timedelta(days=j/4.) for j in xrange(1260)])
            else:
                time2[i] = timetemp[i].timetuple().tm_yday
                print(time2[i])`

The code currently returns values like this;

102
102.0
102.0
103.0
103.0
103.0
103.0
104.0
104.0
104.0
104.0
105.0
105.0
105.0
105.0
106.0
106.0
106.0
106.0
107.0
107.0
107.0
107.0

and so on...

I need help adding 6 hours to each previous day of year so that the array looks like-

102
102.25
102.5
102.75
103.0
103.25
103.5
103.75
104.0

and so on.

1 Answers1

0

Instead of adding a timedelta of 1/4. day, you could add a timedelta(hours=6).

Otherwise, since you know you want your time to increase in 0.25 increments, you could simply generate an array of floats from the get go.

Note that, on my version of python3, timetuple().tm_yday returns an integer, not a float.

Gunee
  • 431
  • 3
  • 16