forked from openbermuda/karmapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.py
98 lines (56 loc) · 1.63 KB
/
grid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
Data from a lat lon grid
Fast access to:
* all data for a day
* all data for a lat-lon
"""
import datetime
START = datetime.date(1979, 1, 1)
DELTA = 0.75
LATITUDE_START = 90.0
LONGITUDE_START = 0.0
def records_per_day():
""" One record per lat and lon """
return longitudes() * latitudes()
def longitudes():
return int(360 / DELTA)
def latitudes():
return int(1 + (180 / DELTA))
def latitude_index(lat):
return int((LATITUDE_START - lat) / DELTA)
def longitude_index(lon):
return int((lon - LONGITUDE_START) / DELTA)
def calculate_record_number(date, lat=90, lon=0.0, start=None):
""" Calculate the record number for given date, lat, lon """
if start is None:
start = START
days = (date - start).days
lat_index = latitude_index(lat)
lon_index = longitude_index(lon)
number = days * records_per_day()
number += lon_index * latitudes()
number += lat_index
return number
def get_data(date, infile, size=9):
pos = calculate_record_number(date)
infile.seek(pos * size)
return infile.read(size * records_per_day())
def get_data_for_pos(lat, lon, infile, size=9):
pos = calculate_record_number(START, lat, lon)
infile.seek(pos * size)
data = ''
while True:
record = infile.read(size)
if not record:
return data
data += record
pos += size * records_per_day()
infile.seek(pos)
def get_data(path):
""" Retrieve data for path
path is something like {source}/{field}/{field}/../{value}
Eg:
euro/1994/8/22/tmax
euro/295.5/32.25/tmax
"""
pass