1+ # -*- coding: utf-8 -*-
2+ import database_connection as db
3+ import datetime
4+ import config
5+ import os
6+ cursor = db .cursor
7+
8+ def generateSEFs ():
9+ for sef_type in config .sef_type_to_field_id .keys ():
10+ generateSEF (sef_type )
11+
12+
13+ def correctValue (value ):
14+ if value == "empty" or value is None :
15+ return "-999"
16+ return value
17+
18+ def correctObservationDate (sef_type ,observation_date ):
19+ if getPeriod (sef_type )== "24" :
20+ observation_date .minute = 0
21+ observation_date .hour = 0
22+ elif config .sef_apply_utc == True :
23+ observation_date = observation_date + datetime .timedelta (hours = config .sef_utc_offset )
24+ return observation_date
25+
26+ def getPeriod (sef_type ):
27+ if config .sef_type_to_start [sef_type ]== "mean" or \
28+ config .sef_type_to_start [sef_type ]== "daily" or \
29+ config .sef_type_to_start [sef_type ]== "total" :
30+ return "24"
31+ else :
32+ return "0"
33+
34+
35+ def getFilename (sef_type ,type_result_set ):
36+ filename = os .environ .get ('DRAW_sef_folder' )+ os .sep + "McGill_DRAW_1491_"
37+
38+ if len (type_result_set )> 0 :
39+ sorted_type_results = sorted (type_result_set )
40+
41+
42+ # now we can complete the file name
43+ #we want to remove the leading -999 and the trailing -999
44+ #Get start
45+ start_found = 0
46+ index_start = 0
47+
48+ while start_found == 0 and index_start < len (type_result_set ):
49+ entry_value = sorted_type_results [index_start ].split ("\t " )[6 ]
50+ if entry_value == "-999" :
51+ index_start += 1
52+ else :
53+ start_found = 1
54+ #Get end
55+ end_found = 0
56+ index_end = - 1
57+
58+ while end_found == 0 and index_end > - len (type_result_set ):
59+ entry_value = sorted_type_results [index_end ].split ("\t " )[6 ]
60+ if entry_value == "-999" :
61+
62+ index_end -= 1
63+ else :
64+ end_found = 1
65+
66+ if index_start < len (type_result_set ):
67+ startStr = sorted_type_results [index_start ].split ("\t " )
68+ filename = filename + startStr [0 ]+ "-" + startStr [1 ]+ "_"
69+ endStr = sorted_type_results [index_end ].split ("\t " )
70+ filename = filename + endStr [0 ]+ "-" + endStr [1 ]+ "-" + sef_type + ".tsv"
71+ return (filename ,index_start ,index_end )
72+ else :
73+ return (None , None , None )
74+
75+
76+
77+
78+ def generateSEF (sef_type ):
79+ print ("Generating SEF for type: " + sef_type )
80+ if type (config .sef_type_to_field_id [sef_type ]) == int :
81+ query = "select value,observation_date from data_entries_corrected_final_iso where field_id = {} order by observation_date asc" .format (config .sef_type_to_field_id [sef_type ])
82+ else :
83+ query = "select value,observation_date from data_entries_corrected_final_iso where field_id in {} order by observation_date asc" .format (config .sef_type_to_field_id [sef_type ])
84+ cursor .execute (query )
85+ results = cursor .fetchall ()
86+ type_result_set = []
87+ for result in results :
88+ (value , observation_date )= result
89+ value = correctValue (value )
90+ observation_date = correctObservationDate (sef_type , observation_date )
91+ try :
92+ result_str = str (observation_date .year )+ "\t " + str (observation_date .month )+ "\t " + str (observation_date .day )+ \
93+ "\t " + str (observation_date .hour )+ "\t " + str (observation_date .minute )+ "\t " + getPeriod (sef_type )+ \
94+ "\t " + value + "\t |\t \n "
95+ type_result_set .append (result_str )
96+ except :
97+ print ("Couldn't generate SEF line for value=" + str (value )+ ", observation date =" + str (observation_date ))
98+
99+
100+ (filename ,index_start ,index_end )= getFilename (sef_type , type_result_set )
101+ if filename is not None :
102+ f = open (filename ,"w" )
103+ f .write ("SEF\t 1.0.0\n " )
104+ f .write ("ID\t ID\n " )
105+ f .write ("Name\t 1491\n " )
106+ f .write ("Lat\t 45.5\n " )
107+ f .write ("Lon\t -73.59\n " )
108+ f .write ("Alt\t 49\n " )
109+ f .write ("Source\McGill\n " )
110+ f .write ("Link\t https://draw.geog.mcgill.ca/\n " )
111+ f .write ("Vbl\t " + config .sef_type_to_unit [sef_type ]+ "\n " )
112+ f .write ("Stat\t " )
113+ if "mean" in config .sef_type_to_start [sef_type ]:
114+ f .write ("mean\n " )
115+ else :
116+ f .write ("point\n " )
117+ f .write ("Unit\t " + config .sef_type_to_unit [sef_type ]+ "\n " )
118+ f .write ("Meta\t " )
119+ f .write ("UTCOffset=" )
120+ if (config .sef_apply_utc == True ):
121+ f .write ("Applied\t UTCOffset=" + str (config .sef_utc_offset ))
122+ else :
123+ f .write ("NO" )
124+ f .write ("\n " )
125+ f .write ("Year\t Month\t Day\t Hour\t Minute\t Period\t Value\t |\t Meta\n " )
126+ index = 0
127+ sorted_type_results = sorted (type_result_set )
128+ for res in sorted_type_results :
129+ if index >= index_start and index <= index_end + len (sorted_type_results ):
130+ f .write (res )
131+ index += 1
132+
133+
134+
135+
136+
0 commit comments