Reputation: 940
I'm writing a basic simulation for an astronomy application. I have a .csv file which is a list of observations. It has 11 columns where columns 2, 3, 4, 5, are decimal hour, day, month, year respectfully. The rest are names, coordinates etc. Each night, a selection of objects are observed once per night.
Each entry in these columns corresponds to when an observation was made. For example: 22.583, 5, 1, 2015
is equivalent to 22:35pm on the 5th Jan 2015 (0.583 is 35/60 minutes as a fraction). The whole file is 'resolved' to 5 minute intervals.
I also have written some code that produces data at a given time interval.
What I want is a way to interpret the 4 time and date columns so that I have a timestamp that is a simple value of 'how many 5 minutes since t=0'.
This would result in a single time-date array that reads 0,1,2,3,4... for the first 5 possible observations. It's worth noting here that in the original csv file that the data doesn't occupy every possible 5 minute interval, there are gaps of varying length.
Ultimately I want to be able to have a list of time-date values when each object was observed.
As an example, what I have now is (for columns 1,2,3,4,5 only):
star1, 0.250, 1, 1, 2015
star2, 0.583, 1, 1, 2015
star3, 0.916, 1, 1, 2015
star1, 0.250, 2, 1, 2015
star2, 0.583, 2, 1, 2015
star3, 0.916, 2, 1, 2015...
Which I'd like to turn into:
t_star1 = [3, 291,...]
t_star2 = [7, 295,...]
t_star3 = [11, 299,...]
Here I used 00:00 on 1st Jan 2015 as t=0, so 00:05/1/1/2015 is t=1 and so on every 5 minutes. My csv file actually runs for 10 years and has about 70,000 entries.
What makes this complicated (for me) is that in the time column the hours/minutes are expressed in a funny way, and that each month has a different number of days in it. So to find the timestamp of something isn't a simple operation.
Thanks!
Upvotes: 0
Views: 101
Reputation: 60974
Consider using the standard datetime
module: https://docs.python.org/3.5/library/datetime.html
from datetime import datetime
hourminute = .25
day = 1
month = 1
year = 2015
datetime(year, month, day, int(hourminute//1), round(int((hourminute%1) * 60)))
Then datetime
has a bunch of methods for converting between time repersentations.
Upvotes: 2