1 |
#!/home/jsoc/bin/linux_x86_64/activeperl |
2 |
|
3 |
# Run like this: |
4 |
# fixsharp1.pl dbname=jsoc dbhost=hmidb dbport=5432 type1=hmi.sharp_720s:/home/mbobra/pros/swharp/cea_fix/output_ccd_definitive.txt,hmi.sharp_720s_nrt:/home/mbobra/pros/swharp/cea_fix/output_ccd_nrt.txt type2=hmi.sharp_cea_720s:/home/mbobra/pros/swharp/cea_fix/output_cea_definitive.txt,hmi.sharp_cea_720s_nrt:/home/mbobra/pros/swharp/cea_fix/output_cea_nrt.txt d=0 |
5 |
|
6 |
use warnings; |
7 |
use strict; |
8 |
|
9 |
use FindBin qw($Bin); |
10 |
use lib "$Bin/../../../base/libs/perl"; |
11 |
use DBI; |
12 |
use DBD::Pg; |
13 |
use FindBin qw($Bin); |
14 |
use drmsLocks; |
15 |
use drmsArgs; |
16 |
use drmsRunProg; |
17 |
|
18 |
use constant kRetSuccess => 0; |
19 |
use constant kRetInvalidArgs => 1; |
20 |
use constant kRetDbQuery => 2; |
21 |
|
22 |
# Required parameters |
23 |
use constant kArgDbname => "dbname"; |
24 |
use constant kArgDbhost => "dbhost"; |
25 |
use constant kArgDbport => "dbport"; |
26 |
# The next two arguments can optionally specify a file containing a comma-separated list of recnums. The format |
27 |
# in that case would look like: |
28 |
# typeX=hmi.blah:/path/to/listfile1,hmi.blahblah:/path/to/listfile2,hmi.blahblahblah:/path/to/listfile3 |
29 |
use constant kArgType1Series => "type1"; # comma-separted list of type-1 series (like hmi.sharp_720s) |
30 |
use constant kArgType2Series => "type2"; # comma-separted list of type-2 series (like hmi.sharp_cea_720s) |
31 |
|
32 |
# Optional parameters |
33 |
use constant kOptDo => "d"; |
34 |
|
35 |
my($argsinH); |
36 |
my($args); |
37 |
my($optsinH); |
38 |
my($opts); |
39 |
my($dbname); |
40 |
my($dbhost); |
41 |
my($dbport); |
42 |
my($dbuser); |
43 |
my($dsn); |
44 |
my($dbh); |
45 |
my($doit); |
46 |
my(@type1); |
47 |
my(@type2); |
48 |
my(@type1recnums); |
49 |
my(@type2recnums); |
50 |
my($listfile); |
51 |
my($fh); |
52 |
my($line); |
53 |
my($whereclz); |
54 |
my($stmnt); |
55 |
my($first); |
56 |
my($ns); |
57 |
my($serieslower); |
58 |
my($rv); |
59 |
|
60 |
$argsinH = |
61 |
{ |
62 |
&kArgDbname => 's', |
63 |
&kArgDbhost => 's', |
64 |
&kArgDbport => 'i', |
65 |
&kArgType1Series => 's', |
66 |
&kArgType2Series => 's' |
67 |
}; |
68 |
|
69 |
$optsinH = |
70 |
{ |
71 |
&kOptDo => 'noval' |
72 |
}; |
73 |
|
74 |
$args = new drmsArgs($argsinH, 1); |
75 |
$opts = new drmsArgs($optsinH, 0); |
76 |
|
77 |
if (!defined($args)) |
78 |
{ |
79 |
$rv = &kRetInvalidArgs; |
80 |
} |
81 |
else |
82 |
{ |
83 |
$dbname = $args->Get(&kArgDbname); |
84 |
$dbhost = $args->Get(&kArgDbhost); |
85 |
$dbport = $args->Get(&kArgDbport); |
86 |
$dbuser = $ENV{USER}; |
87 |
|
88 |
$doit = 0; |
89 |
if (defined($opts)) |
90 |
{ |
91 |
$doit = $opts->Get(&kOptDo); |
92 |
if (!defined($doit)) |
93 |
{ |
94 |
$doit = 0; |
95 |
} |
96 |
} |
97 |
|
98 |
$dsn = "dbi:Pg:dbname=$dbname;host=$dbhost;port=$dbport"; |
99 |
$dbh = DBI->connect($dsn, $dbuser, '', {AutoCommit => 0}); # will need to put pass in .pg_pass |
100 |
|
101 |
if (defined($dbh)) |
102 |
{ |
103 |
# Type1 series |
104 |
GetSeries($args, &kArgType1Series, \@type1, \@type1recnums); |
105 |
|
106 |
foreach my $series (@type1) |
107 |
{ |
108 |
$ns = ($series =~ /^\s*(\S+)\./)[0]; |
109 |
$serieslower = lc($series); |
110 |
|
111 |
$fh = undef; |
112 |
if (defined($listfile = shift(@type1recnums))) |
113 |
{ |
114 |
if (!defined(open($fh, $listfile))) |
115 |
{ |
116 |
$rv = &kRetFileIO; |
117 |
last; |
118 |
} |
119 |
} |
120 |
|
121 |
$first = 1; |
122 |
while (1) |
123 |
{ |
124 |
if (defined($fh)) |
125 |
{ |
126 |
$line = <$fh>; |
127 |
if (!defined($line)) |
128 |
{ |
129 |
$fh->close(); |
130 |
last; # done with records in THIS series; onto next series. |
131 |
} |
132 |
|
133 |
chomp($line); |
134 |
$whereclz = " WHERE recnum IN ($line)"; |
135 |
} |
136 |
else |
137 |
{ |
138 |
$whereclz = ""; |
139 |
} |
140 |
|
141 |
# Update the vaues of variable keywords CRPIX1, CRPIX2, CRVAL1, and CRVAL2 |
142 |
$stmnt = "UPDATE $series SET crpix1 = imcrpix1 - crpix1 + 1, crpix2 = imcrpix2 - crpix2 + 1, (crval1, crval2) = (0.0, 0.0)" . $whereclz; |
143 |
|
144 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type1 series update statement: $stmnt\n"); |
145 |
if ($rv != &kRetSuccess) |
146 |
{ |
147 |
last; |
148 |
} |
149 |
|
150 |
if ($first) |
151 |
{ |
152 |
# Update the descriptions of keywords CRPIX1, CRPIX2, CRVAL1, and CRVAL2 |
153 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'X coordinate of disk center with respect to lower-left corner of patch (in pixels)' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crpix1'"; |
154 |
|
155 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type1 series update statement: $stmnt\n"); |
156 |
if ($rv != &kRetSuccess) |
157 |
{ |
158 |
last; |
159 |
} |
160 |
|
161 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'Y coordinate of disk center with respect to lower-left corner of patch (in pixels)' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crpix2'"; |
162 |
|
163 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type1 series update statement: $stmnt\n"); |
164 |
if ($rv != &kRetSuccess) |
165 |
{ |
166 |
last; |
167 |
} |
168 |
|
169 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'X origin: (0,0) at disk center' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crval1'"; |
170 |
|
171 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type1 series update statement: $stmnt\n"); |
172 |
if ($rv != &kRetSuccess) |
173 |
{ |
174 |
last; |
175 |
} |
176 |
|
177 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'Y origin: (0,0) at disk center' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crval2'"; |
178 |
|
179 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type1 series update statement: $stmnt\n"); |
180 |
} |
181 |
|
182 |
if (!defined($fh)) |
183 |
{ |
184 |
last; |
185 |
} |
186 |
|
187 |
if ($rv != &kRetSuccess) |
188 |
{ |
189 |
last; |
190 |
} |
191 |
|
192 |
$first = 0; |
193 |
} # while |
194 |
|
195 |
if ($rv != &kRetSuccess) |
196 |
{ |
197 |
last; |
198 |
} |
199 |
} # foreach |
200 |
|
201 |
if ($rv == &kRetSuccess) |
202 |
{ |
203 |
# Type 2 series |
204 |
GetSeries($args, &kArgType2Series, \@type2, \@type2recnums); |
205 |
|
206 |
foreach my $series (@type2) |
207 |
{ |
208 |
$ns = ($series =~ /^\s*(\S+)\./)[0]; |
209 |
$serieslower = lc($series); |
210 |
|
211 |
$fh = undef; |
212 |
if (defined($listfile = shift(@type2recnums))) |
213 |
{ |
214 |
if (!defined(open($fh, $listfile))) |
215 |
{ |
216 |
$rv = &kRetFileIO; |
217 |
last; |
218 |
} |
219 |
} |
220 |
|
221 |
$first = 1; |
222 |
while (1) |
223 |
{ |
224 |
if (defined($fh)) |
225 |
{ |
226 |
$line = <$fh>; |
227 |
if (!defined($line)) |
228 |
{ |
229 |
$fh->close(); |
230 |
last; # done with records in THIS series; onto next series. |
231 |
} |
232 |
|
233 |
chomp($line); |
234 |
$whereclz = " WHERE recnum IN ($line)"; |
235 |
} |
236 |
else |
237 |
{ |
238 |
$whereclz = ""; |
239 |
} |
240 |
|
241 |
if ($first) |
242 |
{ |
243 |
# Update the values and descriptions of the constant keywords CTYPE1 and CTYPE2 |
244 |
$stmnt = "UPDATE $ns.drms_keyword SET defaultval = 'CRLN-CEA', description = 'CRLN-CEA' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'ctype1'"; |
245 |
|
246 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
247 |
if ($rv != &kRetSuccess) |
248 |
{ |
249 |
last; |
250 |
} |
251 |
|
252 |
$stmnt = "UPDATE $ns.drms_keyword SET defaultval = 'CRLT-CEA', description = 'CRLT-CEA' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'ctype2'"; |
253 |
|
254 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
255 |
if ($rv != &kRetSuccess) |
256 |
{ |
257 |
last; |
258 |
} |
259 |
|
260 |
# Update the values, units, and descriptions of the constant keywords CUNIT1 and CUNIT2 |
261 |
$stmnt = "UPDATE $ns.drms_keyword SET unit = 'none', defaultval = 'degree', description = 'Degree' WHERE lower(seriesname) = '$serieslower' AND (lower(keywordname) = 'cunit1' OR lower(keywordname) = 'cunit2')"; |
262 |
|
263 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
264 |
if ($rv != &kRetSuccess) |
265 |
{ |
266 |
last; |
267 |
} |
268 |
} |
269 |
|
270 |
# Update the values of variable keywords CDELT1, CDELT2, CROTA2, CRPIX1, CRPIX2, CRVAL1, CRVAL2 |
271 |
$stmnt = "UPDATE $series SET cdelt1 = 0.03, cdelt2 = 0.03, crota2 = 0.0, crpix1 = sg_000_axis000::real / 2 + 0.5, crpix2 = sg_000_axis001::real / 2 + 0.5, crval1 = crln_obs + (londtmax + londtmin) / 2, crval2 = (latdtmax + latdtmin) / 2" . $whereclz; |
272 |
|
273 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
274 |
if ($rv != &kRetSuccess) |
275 |
{ |
276 |
last; |
277 |
} |
278 |
|
279 |
if ($first) |
280 |
{ |
281 |
# Update the units and descriptions of the keywords CDELT1, CDELT2, CRPIX1, CRPIX2, CRVAL1, CRVAL2 |
282 |
$stmnt = "UPDATE $ns.drms_keyword SET unit = 'degree', description = 'Map scale in X direction' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'cdelt1'"; |
283 |
|
284 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
285 |
if ($rv != &kRetSuccess) |
286 |
{ |
287 |
last; |
288 |
} |
289 |
|
290 |
$stmnt = "UPDATE $ns.drms_keyword SET unit = 'degree', description = 'Map scale in Y direction' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'cdelt2'"; |
291 |
|
292 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
293 |
if ($rv != &kRetSuccess) |
294 |
{ |
295 |
last; |
296 |
} |
297 |
|
298 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'X coordinate of patch center with respect to lower-left corner (in pixels)' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crpix1'"; |
299 |
|
300 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
301 |
if ($rv != &kRetSuccess) |
302 |
{ |
303 |
last; |
304 |
} |
305 |
|
306 |
$stmnt = "UPDATE $ns.drms_keyword SET description = 'Y coordinate of patch center with respect to lower-left corner (in pixels)' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crpix2'"; |
307 |
|
308 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
309 |
if ($rv != &kRetSuccess) |
310 |
{ |
311 |
last; |
312 |
} |
313 |
|
314 |
$stmnt = "UPDATE $ns.drms_keyword SET unit = 'degree', description = 'Longitude at center of patch' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crval1'"; |
315 |
|
316 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
317 |
if ($rv != &kRetSuccess) |
318 |
{ |
319 |
last; |
320 |
} |
321 |
|
322 |
$stmnt = "UPDATE $ns.drms_keyword SET unit = 'degree', description = 'Latitude at center of patch' WHERE lower(seriesname) = '$serieslower' AND lower(keywordname) = 'crval2'"; |
323 |
|
324 |
$rv = ExeStmnt($dbh, $stmnt, $doit, "type2 series update statement: $stmnt\n"); |
325 |
} |
326 |
|
327 |
if (!defined($fh)) |
328 |
{ |
329 |
last; |
330 |
} |
331 |
|
332 |
if ($rv != &kRetSuccess) |
333 |
{ |
334 |
last; |
335 |
} |
336 |
|
337 |
$first = 0; |
338 |
} # while |
339 |
|
340 |
if ($rv != &kRetSuccess) |
341 |
{ |
342 |
last; |
343 |
} |
344 |
} # foreach |
345 |
} |
346 |
} |
347 |
|
348 |
if ($rv == &kRetSuccess && $doit) |
349 |
{ |
350 |
# Commit the db changes. |
351 |
print "Committing changes.\n"; |
352 |
$dbh->commit(); |
353 |
} |
354 |
else |
355 |
{ |
356 |
print "Rolling back changes, error code $rv.\n"; |
357 |
$dbh->rollback(); |
358 |
} |
359 |
|
360 |
exit($rv); |
361 |
} |
362 |
|
363 |
sub NoErr |
364 |
{ |
365 |
my($rv) = $_[0]; |
366 |
my($dbh) = $_[2]; |
367 |
my($stmnt) = $_[2]; |
368 |
my($ok) = 1; |
369 |
|
370 |
if (!defined($rv) || !$rv) |
371 |
{ |
372 |
if (defined($$dbh) && defined($$dbh->err)) |
373 |
{ |
374 |
print STDERR "Error " . $$dbh->errstr . ": Statement '$stmnt' failed.\n"; |
375 |
} |
376 |
|
377 |
$ok = 0; |
378 |
} |
379 |
|
380 |
return $ok; |
381 |
} |
382 |
|
383 |
sub ExeStmnt |
384 |
{ |
385 |
my($dbh, $stmnt, $doit, $diag) = @_; |
386 |
my($rsp); |
387 |
my($res); |
388 |
my($rv); |
389 |
|
390 |
$rv = &kRetSuccess; |
391 |
|
392 |
if ($doit) |
393 |
{ |
394 |
$res = $dbh->do($stmnt); |
395 |
if (!NoErr($res, $dbh, $stmnt)) |
396 |
{ |
397 |
$rv = &kRetDbQuery; |
398 |
} |
399 |
} |
400 |
|
401 |
print $diag; |
402 |
|
403 |
return $rv; |
404 |
} |
405 |
|
406 |
sub GetSeries |
407 |
{ |
408 |
my($args, $argname, $slistR, $rlistR) = @_; |
409 |
my(@list); |
410 |
|
411 |
@list = split(qr(,), $args->Get($argname)); |
412 |
foreach my $elem (@list) |
413 |
{ |
414 |
if ($elem =~ /([^:]+):(.+)/) |
415 |
{ |
416 |
push(@$slistR, $1); |
417 |
push(@$rlistR, $2); |
418 |
} |
419 |
else |
420 |
{ |
421 |
push(@$slistR, $elem); |
422 |
} |
423 |
} |
424 |
} |