Subversion Repositories lagranto.ecmwf

Rev

Rev 3 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 9
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
 
2
 
3
# --------------------------------------------------------
3
# --------------------------------------------------------
4
# Separate different commands out (delimiter & and |)
4
# Separate different commands out (delimiter & and |)
5
# --------------------------------------------------------
5
# --------------------------------------------------------
6
 
6
 
7
# Get input command and remove all spaces
7
# Get input command and remove all spaces
8
$cmd = $ARGV[0];
8
$cmd = $ARGV[0];
9
$_   = $cmd;s/\s+//g;$cmd=$_;
9
$_   = $cmd;s/\s+//g;$cmd=$_;
10
$len = length $cmd; 
10
$len = length $cmd; 
11
 
11
 
12
# Split the command string according to logical operators
12
# Split the command string according to logical operators
13
$nline = 0;
13
$nline = 0;
14
while ( $len > 0 )
14
while ( $len > 0 )
15
{
15
{
16
  # Get the length of the (remaining) command string
16
  # Get the length of the (remaining) command string
17
  $len = length $cmd; 
17
  $len = length $cmd; 
18
 
18
 
19
  # Get the position of the next command separator
19
  # Get the position of the next command separator
20
  $and  = index($cmd,'&');
20
  $and  = index($cmd,'&');
21
  $or   = index($cmd,'|' );
21
  $or   = index($cmd,'|' );
22
  if ( ($and == -1) && ($or == -1) )
22
  if ( ($and == -1) && ($or == -1) )
23
     { $next = $len+1; }
23
     { $next = $len+1; }
24
  elsif ( ($and == -1) && ($or >= 0) )
24
  elsif ( ($and == -1) && ($or >= 0) )
25
     { $next = $or;  @logop[$nline] = 'OR'; }
25
     { $next = $or;  @logop[$nline] = 'OR'; }
26
  elsif ( ($and >= 0) && ($or == -1) )
26
  elsif ( ($and >= 0) && ($or == -1) )
27
     { $next = $and; @logop[$nline] = 'AND'; }
27
     { $next = $and; @logop[$nline] = 'AND'; }
28
  elsif ($and > $or)
28
  elsif ($and > $or)
29
     { $next = $or; @logop[$nline] = 'OR';  }
29
     { $next = $or; @logop[$nline] = 'OR';  }
30
  else
30
  else
31
      { $next = $and; @logop[$nline] = 'AND'; }
31
      { $next = $and; @logop[$nline] = 'AND'; }
32
  
32
  
33
  # A logical operator is not allowed to be at position 0
33
  # A logical operator is not allowed to be at position 0
34
  if ( $next == 0) 
34
  if ( $next == 0) 
35
   {    
35
   {    
36
      die('Invalid expression... Check logical operators & and |');
36
      die('Invalid expression... Check logical operators & and |');
37
   }   
37
   }   
38
 
38
 
39
  # Extract the next substring
39
  # Extract the next substring
40
  $sub = substr($cmd,0,$next);
40
  $sub = substr($cmd,0,$next);
41
  $cmd = substr($cmd,$next+1,$len-$next-1);
41
  $cmd = substr($cmd,$next+1,$len-$next-1);
42
  $len = length $cmd; 
42
  $len = length $cmd; 
43
 
43
 
44
  # Save the command in a new line
44
  # Save the command in a new line
45
  @field[$nline] = $sub;
45
  @field[$nline] = $sub;
46
  $nline = $nline + 1;
46
  $nline = $nline + 1;
47
}
47
}
48
 
48
 
49
# --------------------------------------------------------
49
# --------------------------------------------------------
50
# Handle each command line separately
50
# Handle each command line separately
51
# --------------------------------------------------------
51
# --------------------------------------------------------
52
 
52
 
53
# Write start marker
53
# Write start marker
54
print "BEGIN \n";
54
print "BEGIN \n";
55
 
55
 
56
foreach ( $i = 0; $i < $nline; $i++  )
56
foreach ( $i = 0; $i < $nline; $i++  )
57
{
57
{
58
    # Split the command into its four components
58
    # Split the command into its four components
59
    @entry  = split /:/, @field[$i];
59
    @entry  = split /:/, @field[$i];
60
    $nentry = @entry;
60
    $nentry = @entry;
61
 
61
 
62
    # Either four or three elements are needed
62
    # Either four or three elements are needed
63
    if ( ($nentry < 3) | ($nentry > 4) )     
63
    if ( ($nentry < 3) | ($nentry > 4) )     
64
     {    
64
     {    
65
      die('Each expression needs either 3 or 4 fields...');
65
      die('Each expression needs either 3 or 4 fields...');
66
     }   
66
     }   
67
 
67
 
68
    # Write the command
68
    # Write the command
69
    print "@entry[0] \n";
69
    print "@entry[0] \n";
70
 
70
 
71
    # Get the variable and the 'mode' for this variable
71
    # Get the variable and the 'mode' for this variable
72
    $left  = index(@entry[1],'(');
72
    $left  = index(@entry[1],'(');
73
    $right = index(@entry[1],')');
73
    $right = index(@entry[1],')');
74
    if ( ($left == -1) && ($right == -1) ) 
74
    if ( ($left == -1) && ($right == -1) ) 
75
      { 
75
      { 
76
       $var  = @entry[1];
76
       $var  = @entry[1];
77
       $mode = 'VALUE';
77
       $mode = 'VALUE';
78
      }
78
      }
79
    elsif ( ($left > 0) && ($right > $left) ) 
79
    elsif ( ($left > 0) && ($right > $left) ) 
80
      { 
80
      { 
81
       $var  = substr(@entry[1],0,$left);  
81
       $var  = substr(@entry[1],0,$left);  
82
       $mode = substr(@entry[1],$left+1,$right-$left-1);
82
       $mode = substr(@entry[1],$left+1,$right-$left-1);
83
      }
83
      }
84
    else
84
    else
85
      {    
85
      {    
86
       die('Invalid variable specification...');
86
       die('Invalid variable specification...');
87
      }   
87
      }   
88
    print "$var  $mode \n";
88
    print "$var  $mode \n";
89
 
89
 
90
    # Get the parameter list for this command
90
    # Get the parameter list for this command
91
    @param  = split /,/, @entry[2];
91
    @param  = split /,/, @entry[2];
92
    $nparam = @param;
92
    $nparam = @param;
93
    print "$nparam \n";
93
    print "$nparam \n";
94
    print "@param \n";
94
    print "@param \n";
95
 
95
 
96
    # If only three parameters are given, the time is assumed to be the first one
96
    # If only three parameters are given, the time is assumed to be the first one
97
    if ( $nentry == 3 )
97
    if ( $nentry == 3 )
98
    {
98
    {
99
	@entry[3]='FIRST';
99
	@entry[3]='FIRST';
100
    }    
100
    }    
101
 
101
 
102
    # Get the variable and the 'mode' for this variable
102
    # Get the variable and the 'mode' for this variable
103
    $left  = index(@entry[3],'(');
103
    $left  = index(@entry[3],'(');
104
    $right = index(@entry[3],')');
104
    $right = index(@entry[3],')');
105
    if ( ($left == -1) && ($right == -1) ) 
105
    if ( ($left == -1) && ($right == -1) ) 
106
      { 
106
      { 
107
       $time = @entry[3];
107
       $time = @entry[3];
108
       $mode = 'ALL';
108
       $mode = 'ALL';
109
      }
109
      }
110
    elsif ( ($left > 0) && ($right > $left) ) 
110
    elsif ( ($left > 0) && ($right > $left) ) 
111
      { 
111
      { 
112
       $time = substr(@entry[3],0,$left);  
112
       $time = substr(@entry[3],0,$left);  
113
       $mode = substr(@entry[3],$left+1,$right-$left-1);
113
       $mode = substr(@entry[3],$left+1,$right-$left-1);
114
      }
114
      }
115
    else
115
    else
116
      {    
116
      {    
117
       die('Invalid time specification...');
117
       die('Invalid time specification...');
118
      }   
118
      }   
119
 
119
 
120
    # Get the time list for this command
120
    # Get the time list for this command
121
    if ( $time eq 'ALL' )
121
    if ( $time eq 'ALL' )
122
    {
122
    {
123
	$time = -999;
123
	$time = -999;
124
        print "1 \n";
124
        print "1 \n";
125
        print "$time \n";
125
        print "$time \n";
126
    }
126
    }
127
    elsif ( $time eq 'FIRST' )
127
    elsif ( $time eq 'FIRST' )
128
    {
128
    {
129
        $time = -996;
129
        $time = -996;
130
        print "1 \n";
130
        print "1 \n";
131
	print "$time \n";
131
	    print "$time \n";
132
    }
132
    }
133
    elsif ( $time eq 'LAST' )
133
    elsif ( $time eq 'LAST' )
134
    {
134
    {
135
        $time = -995;
135
        $time = -995;
136
        print "1 \n";
136
        print "1 \n";
137
	print "$time \n";
137
	    print "$time \n";
-
 
138
	}
-
 
139
	elsif ( $time eq 'TRIGGER' )
-
 
140
    {
-
 
141
        print "-993 \n";
138
    }
142
    }
139
    else
143
    else
140
    {
144
    {
141
        @times  = split /,/, $time;
145
        @times  = split /,/, $time;
142
        $outstr = "";
146
        $outstr = "";
143
	$outlen = 0;
147
	    $outlen = 0;
144
	foreach $j ( @times )
148
	    foreach $j ( @times )
145
        {
149
        {
146
	    $to = index($j,'to');
150
	    $to = index($j,'to');
147
            if ( $to == -1 )
151
            if ( $to == -1 )
148
               { 
152
               { 
149
		if ( $j eq 'FIRST' )
153
		if ( $j eq 'FIRST' )
150
		  {
154
		  {
151
                      $outlen=$outlen+1;
155
                      $outlen=$outlen+1;
152
		      $outstr=$outstr . " -996 ";
156
		      $outstr=$outstr . " -996 ";
153
                  }
157
                  }
154
                elsif ( $j eq 'LAST' )
158
                elsif ( $j eq 'LAST' )
155
		  {
159
		  {
156
                      $outlen=$outlen+1;
160
                      $outlen=$outlen+1;
157
		      $outstr=$outstr . " -995 ";
161
		      $outstr=$outstr . " -995 ";
158
                  }
162
                  }
159
                else
163
                else
160
		  {
164
		  {
161
                   $outlen=$outlen+1;
165
                   $outlen=$outlen+1;
162
                   $outstr=$outstr . "$j "; 
166
                   $outstr=$outstr . "$j "; 
163
	          }
167
	          }
164
               }
168
               }
165
            else
169
            else
166
	       { 
170
	       { 
167
                $outlen = $outlen+3;
171
                $outlen = $outlen+3;
168
                $t1 = substr($j,0,$to);
172
                $t1 = substr($j,0,$to);
169
		$t2 = substr($j,$to+2,length($j)-$to+1);
173
		$t2 = substr($j,$to+2,length($j)-$to+1);
170
		if ( $t1 eq 'FIRST' ) 
174
		if ( $t1 eq 'FIRST' ) 
171
		    { $t1='-996'; };
175
		    { $t1='-996'; };
172
		if ( $t2 eq 'FIRST' ) 
176
		if ( $t2 eq 'FIRST' ) 
173
		    { $t2='-996'; };
177
		    { $t2='-996'; };
174
		if ( $t1 eq 'LAST'  ) 
178
		if ( $t1 eq 'LAST'  ) 
175
		    { $t1='-995'; };
179
		    { $t1='-995'; };
176
		if ( $t2 eq 'LAST'  ) 
180
		if ( $t2 eq 'LAST'  ) 
177
		    { $t2='-995'; };
181
		    { $t2='-995'; };
178
	        $outstr=$outstr . $t1 . " -994 " . $t2 . " "; 
182
	        $outstr=$outstr . $t1 . " -994 " . $t2 . " "; 
179
	       }
183
	       }
180
        }
184
        }
181
	print "$outlen \n";
185
	print "$outlen \n";
182
        print "$outstr \n";
186
        print "$outstr \n";
183
    }
187
    }
184
 
188
 
185
    # Write the time mode
189
    # Write the time mode
186
    if ( $mode eq 'ALL' )
190
    if ( $mode eq 'ALL' )
187
    {
191
    {
188
        print "$mode \n";
192
        print "$mode \n";
189
    }
193
    }
190
    elsif ( $mode eq 'ANY' )
194
    elsif ( $mode eq 'ANY' )
191
    {
195
    {
192
        print "$mode \n";
196
        print "$mode \n";
193
    }
197
    }
194
    elsif ( $mode eq 'NONE' )
198
    elsif ( $mode eq 'NONE' )
195
    {
199
    {
196
        print "$mode \n";
200
        print "$mode \n";
197
    }
201
    }
198
    elsif ( $mode eq 'TRIGGER' )
202
    elsif ( $mode eq 'TRIGGER' )
199
    {
203
    {
200
        print "$mode \n";
204
        print "$mode \n";
201
    }
205
    }
202
    else
206
    else
203
    {    
207
    {    
204
       die('Invalid time mode...');
208
       die('Invalid time mode...');
205
    }   
209
    }   
206
 
210
 
207
	
211
	
208
    # Write the logical operator
212
    # Write the logical operator
209
    if ( $i < $nline-1 )
213
    if ( $i < $nline-1 )
210
    {
214
    {
211
      print "@logop[$i] \n";
215
      print "@logop[$i] \n";
212
    } 
216
    } 
213
}
217
}
214
 
218
 
215
# Write end marker
219
# Write end marker
216
print "END \n";
220
print "END \n";