1
2
3 """
4 General class for observation dates for WSA and VSA.
5
6 @author: E. Sutorius
7 @org: WFAU, IfA, University of Edinburgh
8 """
9
10 from __future__ import division, print_function
11 from future_builtins import map
12
13 from collections import defaultdict, namedtuple
14 import mx.DateTime as mxTime
15 from operator import attrgetter
16 from wsatools.Utilities import Ratings
17 import wsatools.Utilities as utils
18
19
20
21 DateRange = namedtuple('DateRange', 'begin end')
26 """ Stores and provides access to observation night data.
27 """
28 earlyDefaultDateTime = mxTime.DateTime(1753, 1, 1)
29 """ A very early date. The earliest date that MS SQL Server can cope with
30 in its own datetime SQL type.
31 """
32 lateDefaultDateTime = mxTime.DateTime(9999, 12, 31)
33 """ A large date. The maximum date that MS SQL Server can cope with in its
34 own datetime SQL type.
35 """
36
37 semDates = {}
38
39 versNums = {}
40
41 notObsDateStr = {}
42
43 @staticmethod
45 """ Returns the values of the given constant in all subclasses.
46 """
47 return map(attrgetter(constantName), ObsCal.__subclasses__())
48
51
52
53
54
55
57 """ Check if given semester is in observation list.
58 """
59 for key in self.semDates:
60 if self.formatSemester(semester).upper() in key:
61 return [key]
62
63
64
66 """ Check in which semester the given date is.
67 """
68
69 for key in self.semDates:
70 startDate, endDate = self.getDates(key)
71 if startDate <= self.formatDate(dateStr) <= endDate:
72 return key
73
74
75
82
83
84
86 """ Converts supplied begin and/or end date in any format into a range
87 of mx.DateTime objects. Dates can be supplied as semesters.
88
89 @rtype: DateRange
90 """
91 semesterDates = dict((key[2:] if key.startswith("20") else key, value)
92 for key, value in self.semDates.iteritems())
93
94 begin = str(begin or self.earlyDefaultDateTime)
95 end = str(end or self.lateDefaultDateTime)
96 try:
97 if len(begin) < 8:
98 begin = semesterDates[begin.upper()].begin
99
100 if len(end) < 8:
101 end = semesterDates[end.upper()].end
102
103 except KeyError as error:
104 raise Exception("No such semester %s in %s.semDates"
105 % (error, self))
106
107 return DateRange(*map(mxTime.DateTimeFrom, (begin, end)))
108
109
110
111 - def getDates(self, semester, outFormat=None):
120
121
122
160
161
162
164 """
165 For the given night return the semester it belongs to.
166
167 @param utDate: Night to find semester of.
168 @type utDate: mx.DateTime
169
170 @return: Semester that given night belongs to, if none found then None.
171 @rtype: str
172
173 """
174 for semName, sem in self.semDates.items():
175 if utils.makeDateTime(sem.begin) <= utDate \
176 <= utils.makeDateTime(sem.end):
177
178 return semName
179
180
181
186
187
188
190 """Get a dictionary of months and their corresponding semester.
191 """
192 msDict = {}
193 for sem in self.semDates:
194 for month in self.getSemMonthList(sem):
195 msDict[month] = sem
196 return msDict
197
198
199
201 """Get a list of months ('YYYYMM') for a given semester.
202 """
203 startDate, endDate = self.getDates(self.formatSemester(semester))
204 monthList = []
205 date = startDate
206 while date <= endDate:
207 monthList.append('%d%02d' % (date.year, date.month))
208 date += mxTime.RelativeDateTime(months=1, day=1)
209 return monthList
210
211
212
224
225
226
227 - def getObsDates(self, beginDateStr, endDateStr, versionStr=None):
228 """Get a list of all obs dates between two dates.
229 """
230 startMJD = int(mxTime.strptime(beginDateStr, "%Y%m%d").mjd)
231 endMJD = int(mxTime.strptime(endDateStr, "%Y%m%d").mjd)
232 notObsDates = list(utils.unpackList(self.notObsDates().values()))
233 obsDates = []
234 for mjd in xrange(startMJD, endMJD + 1):
235 date = mxTime.DateTimeFromMJD(mjd)
236 dateStr = "%04d%02d%02d" % (date.year, date.month, date.day)
237 if self.formatDate(dateStr) in notObsDates:
238 continue
239 if versionStr:
240 dateStr = "%s_v%s" % (dateStr, versionStr)
241 obsDates.append(dateStr)
242 return sorted(obsDates)
243
244
245
247 """Get the first observation date.
248 """
249 nights = (self.getDates(sem, dateFmt)[0] for sem in self.getSemList())
250 return min(self.getToday(), min(nights))
251
252
253
255 """ Get the minimal version number for a given date.
256 """
257 return self.minVers(self.checkDate(dateStr))
258
259
260
262 """ Get the maximal version number for a given date.
263 """
264 return self.maxVers(self.checkDate(dateStr))
265
266
267
269 """ Get the minimum version number for a given semester.
270 """
271 return self.versNums[semester][0]
272
273
274
276 """ Get the maximum version number for a given semester.
277 """
278 return self.versNums[semester][-1]
279
280
281
282
283
284 @staticmethod
289
290
291
292 @staticmethod
298
299
300
301 @staticmethod
303 """Get the short month name in lower case.
304 """
305 return mxTime.Month[int(dateStr[4:6])].lower()[:3]
306
307
308
309 @staticmethod
311 """Get today's date.
312 """
313 return ObsCal.formatDate(mxTime.today().date, dateFmt="%Y-%m-%d",
314 outFormat=outFormat)
315
319 """Semester dates and missing observations for the OSA.
320 """
321 semDates = {"ALL": DateRange("20040101", "20201231")}
322
323 versNums = {"ALL": ["2.00"]}
324
325 notObsDateStr = {"ALL":()}
326
330 """Semester dates and missing observations for the OSA.
331 """
332 semDates = {"COMM": DateRange("20110816", "20111014"),
333 "P88": DateRange("20111015", "20120331"),
334 "P89": DateRange("20120401", "20120930"),
335 "P90": DateRange("20121001", "20130331"),
336 "P91": DateRange("20130401", "20130930"),
337 "P92": DateRange("20131001", "20140331"),
338 "P93": DateRange("20140401", "20140930"),
339 "P94": DateRange("20141001", "20150331")
340
341 }
342
343 versNums = {"COMM": ["0.9", "1.0"],
344 "P88": ["1.0"],
345 "P89": ["1.0"],
346 "P90": ["1.0"],
347 "P91": ["1.0"],
348 "P92": ["1.0"],
349 "P93": ["1.0"],
350 "P94": ["1.0"]
351 }
352
353 notObsDateStr = {"COMM":("20110818", "20110906", "20110908",
354 "20110909", "20110916", "20110920",
355 "20111010", "20111013", "20111014"),
356 "P88":("20111016", "20111017", "20111028",
357 "20111205", "20111207", "20111208",
358 "20111209", "20111210", "20111223",
359 "20111224", "20111225", "20111227",
360 "20120102", "20120103", "20120104",
361 "20120106", "20120107", "20120108",
362 "20120129", "20120130", "20120203",
363 "20120204", "20120205", "20120206",
364 "20120207", "20120208", "20120209",
365 "20120213", "20120308", "20120309",
366 "20120310", "20120311"),
367 "P89":("20120409", "20120411", "20120412",
368 "20120413", "20120423", "20120427",
369 "20120513", "20120514", "20120524",
370 "20120525", "20120609", "20120613",
371 "20120619", "20120708", "20120709",
372 "20120727", "20120802", "20120830"),
373 "P90":("20121003", "20121004", "20121019",
374 "20121026", "20121027", "20121202",
375 "20130109", "20130119", "20130205",
376 "20130206", "20130307", "20130329",
377 "20130330", "20130331"),
378 "P91":("20130401", "20130423", "20130507",
379 "20130508", "20130509", "20130510",
380 "20130511", "20130512", "20130515",
381 "20130516", "20130517", "20130518",
382 "20130520", "20130522", "20130607",
383 "20130611", "20130612", "20130613",
384 "20130622", "20130623", "20130704",
385 "20130707", "20130708", "20130719",
386 "20130721", "20130722", "20130723",
387 "20130724", "20130725", "20130726",
388 "20130730", "20130807", "20130823",
389 "20130918", "20130926"),
390 "P92":("20131115", "20131129", "20131202",
391 "20131217", "20131225", "20140116",
392 "20140124", "20140125"),
393 "P93":(),
394 "P94":()
395 }
396
400 """Semester dates and missing observations for the WSA.
401 """
402 semDates = {"COMM": DateRange("20090927", "20091014"),
403 "SV": DateRange("20091015", "20091102"),
404 "DRYRUN": DateRange("20091103", "20100208"),
405 "P85": DateRange("20100209", "20100930"),
406 "P86": DateRange("20101001", "20110331"),
407 "P87": DateRange("20110401", "20110930"),
408 "P88": DateRange("20111001", "20120331"),
409 "P89": DateRange("20120401", "20120930"),
410 "P90": DateRange("20121001", "20130331"),
411 "P91": DateRange("20130401", "20130930"),
412 "P92": DateRange("20131001", "20140331"),
413 "P93": DateRange("20140401", "20140930"),
414 "P94": DateRange("20141001", "20150331")
415 }
416
417 versNums = {"COMM": ["0.5", "0.6"],
418 "SV": ["1.1", "1.3"],
419 "DRYRUN": ["1.1", "1.3"],
420 "P85": ["1.1", "1.3"],
421 "P86": ["1.1", "1.3"],
422 "P87": ["1.2", "1.3"],
423 "P88": ["1.2", "1.3"],
424 "P89": ["1.2", "1.3"],
425 "P90": ["1.2", "1.3"],
426 "P91": ["1.3"],
427 "P92": ["1.3"],
428 "P93": ["1.3"],
429 "P94": ["1.3"]
430 }
431
432 notObsDateStr = {"COMM":("20091004", "20091008", "20091009",
433 "20091010", "20091011", "20091012",
434 "20091013", "20091014"),
435 "SV":("20091028", "20091030", "20091031",
436 "20091101"),
437 "DRYRUN":("20091109", "20091123", "20091126",
438 "20091230", "20091231", "20100101",
439 "20100102", "20100105", "20100201"),
440 "P85":("20100416", "20100424", "20100425",
441 "20100426", "20100427", "20100428",
442 "20100429", "20100430", "20100501",
443 "20100502", "20100503", "20100504",
444 "20100505", "20100513", "20100514",
445 "20100515", "20100516", "20100521",
446 "20100526", "20100528", "20100602",
447 "20100610", "20100613", "20100616",
448 "20100617", "20100618", "20100619",
449 "20100716", "20100811", "20100820",
450 "20100827", "20100901", "20100902"),
451 "P86":("20101001", "20101011", "20101012",
452 "20101020", "20101119", "20110202",
453 "20110205", "20110214", "20110215",
454 "20110220", "20110225", "20110227",
455 "20110304", "20110305", "20110306",
456 "20110307", "20110308", "20110309",
457 "20110310", "20110311", "20110312",
458 "20110313", "20110314", "20110315",
459 "20110316", "20110317", "20110318",
460 "20110319", "20110320", "20110321",
461 "20110322", "20110323", "20110324",
462 "20110325", "20110326", "20110327",
463 "20110328", "20110329", "20110330",
464 "20110331"),
465 "P87":("20110401", "20110402", "20110403",
466 "20110404", "20110405", "20110406",
467 "20110407", "20110408", "20110409",
468 "20110410", "20110411", "20110412",
469 "20110413", "20110414", "20110415",
470 "20110416", "20110417", "20110418",
471 "20110419", "20110420", "20110421",
472 "20110422", "20110423", "20110424",
473 "20110425", "20110426", "20110427",
474 "20110428", "20110429", "20110430",
475 "20110501", "20110502", "20110503",
476 "20110504", "20110505", "20110507",
477 "20110617", "20110618", "20110619",
478 "20110627", "20110704", "20110705",
479 "20110707", "20110708", "20110709",
480 "20110731", "20110920"),
481 "P88":("20111013", "20111016", "20111017",
482 "20111130", "20111210", "20111211",
483 "20111212", "20111213", "20111214",
484 "20111215", "20111216", "20111217",
485 "20111223", "20111225", "20111227",
486 "20120103", "20120104", "20120130",
487 "20120206", "20120207", "20120208",
488 "20120209", "20120213"),
489 "P89":("20120409", "20120412", "20120413",
490 "20120423", "20120427", "20120502",
491 "20120503", "20120504", "20120505",
492 "20120513", "20120514", "20120524",
493 "20120525", "20120529", "20120613",
494 "20120619", "20120708", "20120709",
495 "20120802", "20120809"),
496 "P90":("20121026", "20121027", "20121208",
497 "20121209", "20121210", "20121211",
498 "20121212", "20121213", "20121214",
499 "20121215", "20121216", "20121217",
500 "20121219", "20130202", "20130205",
501 "20130206", "20130307"),
502 "P91":("20130423", "20130516", "20130517",
503 "20130518", "20130522", "20130607",
504 "20130612", "20130613", "20130704",
505 "20130707", "20130708", "20130719",
506 "20130725", "20130726", "20130823",
507 "20130824"),
508 "P92":("20131114", "20131115", "20131214",
509 "20140116", "20140124", "20140125"),
510 "P93":(),
511 "P94":()
512 }
513
517 """Semester dates and missing observations for the WSA.
518 """
519 semDates = {"2005A": DateRange("20050401", "20050620"),
520 "2005B": DateRange("20050825", "20060206"),
521 "2006A": DateRange("20060428", "20060731"),
522 "2006B": DateRange("20061026", "20070331"),
523 "2007A": DateRange("20070401", "20070521"),
524 "2007B": DateRange("20070801", "20071203"),
525 "2008A": DateRange("20080206", "20080616"),
526 "2008B": DateRange("20080918", "20081201"),
527 "2009A": DateRange("20090128", "20090726"),
528 "2009B": DateRange("20090727", "20100131"),
529 "2010A": DateRange("20100201", "20100731"),
530 "2010B": DateRange("20100801", "20110131"),
531 "2011A": DateRange("20110201", "20110731"),
532 "2011B": DateRange("20110801", "20120131"),
533 "2012A": DateRange("20120201", "20120731"),
534 "2012B": DateRange("20120801", "20130131"),
535 "2013A": DateRange("20130201", "20131231"),
536 "2014A": DateRange("20140101", "20141231")
537 }
538
539 versNums = {"2005A":['1', '2', '3', '4', '5'],
540 "2005B":['1', '2', '3', '4', '5'],
541 "2006A":['1', '2', '3', '4', '5'],
542 "2006B":['1', '2', '3', '4', '5'],
543 "2007A":['1', '2', '3', '4', '5'],
544 "2007B":['1', '2', '3', '4', '5'],
545 "2008A":['1', '2', '3', '4', '5'],
546 "2008B":['1', '2', '3', '4', '5'],
547 "2009A":['1', '2', '3', '4', '5'],
548 "2009B":['1', '2', '3', '4', '5'],
549 "2010A":['1', '2', '3', '4', '5'],
550 "2010B":['1', '2', '3', '4', '5'],
551 "2011A":['1', '2', '3', '4', '5'],
552 "2011B":['1', '5'],
553 "2012A":['1', '5'],
554 "2012B":['5'],
555 "2013A":['5'],
556 "2014A":['5']}
557
558 notObsDateStr = {"2005A":("20050402", "20050403", "20050404",
559 "20050405", "20050406", "20050427",
560 "20050428", "20050512", "20050523"),
561 "2005B":("20050825", "20050914", "20050915",
562 "20050916", "20050923", "20051001",
563 "20051003", "20051109", "20051114",
564 "20051117", "20051205", "20051206",
565 "20051208", "20051219", "20051225",
566 "20051226", "20060112", "20060122",
567 "20060123", "20060124", "20060125",
568 "20060126", "20060127", "20060128",
569 "20060129", "20060131"),
570 "2006A":("20060506", "20060507", "20060508",
571 "20060509", "20060627", "20060714",
572 "20060731"),
573 "2006B":("20061102", "20061103", "20061104",
574 "20061105", "20061106", "20061108",
575 "20061118", "20061202", "20061203",
576 "20061204", "20061225", "20061226",
577 "20070105", "20070107", "20070108",
578 "20070109", "20070110", "20070111",
579 "20070131", "20070201", "20070202",
580 "20070203", "20070204", "20070209",
581 "20070210", "20070211", "20070217",
582 "20070218", "20070226", "20070227",
583 "20070228", "20070301", "20070311",
584 "20070312", "20070314", "20070315"),
585 "2007A":(),
586 "2007B":("20070801", "20070802", "20070803",
587 "20070814", "20070815", "20070816",
588 "20070817", "20070829", "20070901",
589 "20070902", "20070903", "20070904",
590 "20070905", "20070906", "20070907",
591 "20070908", "20070909", "20070910",
592 "20070911", "20070927", "20071023",
593 "20071104", "20071105", "20071106",
594 "20071107", "20071108", "20071109",
595 "20071110", "20071111", "20071112",
596 "20071113", "20071114", "20071115",
597 "20071116", "20071127", "20071128",
598 "20071129", "20071203"),
599 "2008A":("20080206", "20080207", "20080208",
600 "20080209", "20080210", "20080211",
601 "20080212", "20080213", "20080319",
602 "20080330", "20080409", "20080410",
603 "20080411", "20080429", "20080430",
604 "20080609"),
605 "2008B":("20080918", "20080926", "20081023",
606 "20081028", "20081029", "20081030",
607 "20081119", "20081121", "20081123"),
608 "2009A":("20090128", "20090129", "20090203",
609 "20090206", "20090207", "20090208",
610 "20090210", "20090211", "20090212",
611 "20090221", "20090224", "20090305",
612 "20090306", "20090307", "20090308",
613 "20090309", "20090310", "20090311",
614 "20090313", "20090315", "20090316",
615 "20090317", "20090318", "20090319",
616 "20090320", "20090321", "20090325",
617 "20090401", "20090404", "20090405",
618 "20090409", "20090410", "20090411",
619 "20090412", "20090420", "20090421",
620 "20090422", "20090518", "20090519",
621 "20090611", "20090612", "20090626",
622 "20090707"),
623 "2009B":("20090802", "20090803", "20090829",
624 "20090915", "20090916", "20090917",
625 "20090918", "20090919", "20090920",
626 "20090921", "20090922", "20090923",
627 "20090924", "20090925", "20090926",
628 "20090927", "20090928", "20090929",
629 "20090930", "20091006", "20091105",
630 "20091110", "20091111", "20091112",
631 "20091113", "20091126", "20091127",
632 "20091219", "20091221", "20091225",
633 "20091226"),
634 "2010A":("20100203", "20100218", "20100323",
635 "20100503", "20100525", "20100526",
636 "20100527", "20100528", "20100529",
637 "20100530", "20100531", "20100601",
638 "20100602", "20100603", "20100604",
639 "20100605", "20100606", "20100607",
640 "20100608", "20100609", "20100628"),
641 "2010B":("20100803", "20100823", "20101025",
642 "20101026", "20101106", "20101119",
643 "20101120", "20101210", "20101211",
644 "20101212", "20101213", "20101220",
645 "20101221", "20101222", "20101223",
646 "20101224", "20101225", "20101226",
647 "20101227", "20101228", "20101229",
648 "20101230", "20101231", "20110101",
649 "20110102", "20110103", "20110108",
650 "20110109", "20110111", "20110112",
651 "20110113", "20110114", "20110115",
652 "20110119", "20110120", "20110121"),
653 "2011A":("20110207", "20110208", "20110220",
654 "20110222", "20110223", "20110224",
655 "20110225", "20110226", "20110305",
656 "20110306", "20110308", "20110328",
657 "20110406", "20110407", "20110408",
658 "20110409", "20110410", "20110411",
659 "20110422", "20110423", "20110424",
660 "20110425", "20110502", "20110503",
661 "20110505", "20110506", "20110507",
662 "20110508", "20110509", "20110510",
663 "20110511", "20110512", "20110514",
664 "20110605", "20110613", "20110628",
665 "20110629", "20110630", "20110701",
666 "20110702", "20110703", "20110704",
667 "20110705", "20110706", "20110707",
668 "20110708", "20110709", "20110710",
669 "20110711", "20110712", "20110713",
670 "20110714", "20110715", "20110716",
671 "20110717", "20110718", "20110719",
672 "20110720", "20110721", "20110722"),
673 "2011B":("20111102", "20111103", "20111104",
674 "20111105", "20111108", "20111110",
675 "20111112", "20111117", "20111118",
676 "20111119", "20111120", "20111121",
677 "20111122", "20111203", "20111211",
678 "20111212", "20111213", "20111214",
679 "20111220", "20111221", "20111222",
680 "20111223", "20111225", "20111226"),
681 "2012A":("20120205", "20120206", "20120208",
682 "20120219", "20120220", "20120221",
683 "20120222", "20120223", "20120224",
684 "20120303", "20120304", "20120305",
685 "20120307", "20120308", "20120309",
686 "20120325", "20120414", "20120503",
687 "20120524", "20120525", "20120526",
688 "20120527", "20120528", "20120529",
689 "20120530", "20120531", "20120601",
690 "20120602", "20120603", "20120604",
691 "20120605", "20120606", "20120607",
692 "20120608", "20120609", "20120610",
693 "20120611", "20120612", "20120613",
694 "20120617", "20120618", "20120717",
695 "20120731"),
696 "2012B":("20120801", "20120820", "20121025",
697 "20121030", "20121031", "20121114",
698 "20121115", "20121119", "20121120",
699 "20121122", "20121123", "20121124",
700 "20121203", "20121206", "20121220",
701 "20121225", "20121226", "20130115",
702 "20130119", "20130129", "20130130"),
703 "2013A":("20130219", "20130221", "20130222",
704 "20130223", "20130224", "20130225",
705 "20130226", "20130312", "20130316",
706 "20130317", "20130318", "20130319",
707 "20130323", "20130325", "20130328",
708 "20130331", "20130401", "20130402",
709 "20130414", "20130423", "20130506",
710 "20130526", "20130528", "20130531",
711 "20130615", "20130616", "20130617",
712 "20130618", "20130619", "20130630",
713 "20130701", "20130702", "20130719",
714 "20130720", "20130721", "20130722",
715 "20130723", "20130724", "20130730",
716 "20130803", "20130804", "20130805",
717 "20130806", "20130807", "20130808",
718 "20130809", "20130810", "20130811",
719 "20130812", "20130813", "20130814",
720 "20130815", "20130816", "20130817",
721 "20130818", "20130819", "20130820",
722 "20130821", "20131010", "20131011",
723 "20131012", "20131015", "20131018",
724 "20131108", "20131109", "20131110",
725 "20131111", "20131112", "20131113",
726 "20131114", "20131201", "20131202",
727 "20131203", "20131204", "20131208",
728 "20131209", "20131210", "20131219",
729 "20131220", "20131225", "20131226",
730 "20131228", "20131230", "20131231"),
731 "2014A":()
732 }
733
734
735