1
2 """
3 Class that implements common functionality for selecting timesteps
4 """
5
6 from optparse import OptionGroup
7
9 """
10 This class compiles a list of timesteps that should be processed
11 """
14
16 """Add the necessary options
17 @param defaultUnique: whether timesteps are unique by default"""
18
19 time=OptionGroup(self.parser,
20 "Time Specification",
21 "Which times should be processed")
22 time.add_option("--time",
23 type="float",
24 dest="time",
25 default=[],
26 action="append",
27 help="Timestep that should be processed. Can be used more than once")
28 time.add_option("--latest-time",
29 dest="latest",
30 action="store_true",
31 default=False,
32 help="Use the latest time")
33 time.add_option("--all-times",
34 dest="all",
35 action="store_true",
36 default=False,
37 help="Process all times")
38 time.add_option("--after-time",
39 type="float",
40 dest="afterTime",
41 action="store",
42 default=None,
43 help="Process all after this time")
44 time.add_option("--before-time",
45 type="float",
46 dest="beforeTime",
47 action="store",
48 default=None,
49 help="Process all before this time")
50
51 if defaultUnique:
52 time.add_option("--duplicate-times",
53 dest="unique",
54 action="store_false",
55 default=True,
56 help="Allow using a time-directory onlymore than once")
57 else:
58 time.add_option("--unique-times",
59 dest="unique",
60 action="store_true",
61 default=False,
62 help="Use each time-directory only once")
63
64 time.add_option("--show-times",
65 dest="showTimes",
66 action="store_true",
67 default=False,
68 help="Show the times in the case and the times that will be used")
69
70 time.add_option("--parallel-times",
71 dest="parallelTimes",
72 action="store_true",
73 default=False,
74 help="Use the information from 'processor0' to determine the available times")
75
76 self.parser.add_option_group(time)
77
80 """Process the options
81 @param sol: the solution-directory that is to be worked with"""
82
83 if self.opts.parallelTimes:
84 sol.setToParallel()
85
86 if self.opts.latest:
87 self.opts.time.append(float(sol.getLast()))
88 if self.opts.all:
89 for t in sol.getTimes():
90 self.opts.time.append(float(t))
91 if self.opts.beforeTime or self.opts.afterTime:
92 start=float(sol.getFirst())
93 end=float(sol.getLast())
94 if self.opts.beforeTime:
95 end=self.opts.beforeTime
96 if self.opts.afterTime:
97 start=self.opts.afterTime
98 for t in sol.getTimes():
99 tVal=float(t)
100 if tVal>=start and tVal<=end:
101 self.opts.time.append(tVal)
102
103 self.opts.time.sort()
104
105 times=[]
106
107 for s in self.opts.time:
108 times.append(sol.timeName(s,minTime=True))
109
110 if self.opts.unique:
111 tmp=[]
112 last=None
113 cnt=0
114 for s in times:
115 if last!=s:
116 tmp.append(s)
117 else:
118 cnt+=1
119 last=s
120 if cnt>0:
121 self.warning("Removed",cnt,"duplicate times")
122 times=tmp
123
124 if len(times)==0:
125 self.warning("No valid times specified")
126
127 if self.opts.showTimes:
128 print "Times in case:",sol.getTimes()
129 print "Used times:",times
130
131 return times
132