source: trunk/htdocs/perl/find_archives.pl @ 203

Last change on this file since 203 was 203, checked in by MatthewWhiting, 17 years ago

Adding some code to give a nice front page for times when the server is down.

  • Property svn:executable set to *
File size: 23.4 KB
Line 
1#! /usr/bin/perl -w
2
3# find_archives.pl
4# Author: Albert Teoh
5# Date: December 2005
6# ATNF, CSIRO
7
8# Crawls through data archive directories (given as an input parameter),
9# searches for all observation and/or calibration files, enquires about
10# their pulsar parameters and inputs these details as well as their
11# location into a database
12
13
14use strict;
15use File::Find ();
16use DBI qw(:sql_types);
17use POSIX;
18
19# Set the variable $File::Find::dont_use_nlink if you're using AFS,
20# since AFS cheats.
21
22# for the convenience of &wanted calls, including -eval statements:
23use vars qw/*name *dir *prune @archive_dirs $archive_dir @files
24    $vap_cmd $vap_out $params $num_vap_params $db_handle
25    $filename $raj $decj $dm $period $bw $cnfg
26    $freq $inst $mjdint $mjdfrac $npol $nchan $nbin $nsub
27    $rcvr $site $length $obsrvr $data_loc $data_type
28    $file_size $MJD $raH $raM $raS $decD $decM $decS $rajd $decjd $decstring
29    $pi $pion180 $NGP_RA $NGP_DEC $ASC_NODE $draR $decR
30    $sinb $sinl $cosl $gb $gl $gl_raw
31    $bmaj $bmin $bpa $hdrver $survey $nbeam
32    $output $vap $archive_extensions $ext       
33    $default
34    @result_params $FILE_SIZE_INDEX $FB $DFB $WBC $CPSR2
35    $params_NAME   
36    $params_PROJID 
37    $params_RAJ     
38    $params_DECJ   
39    $params_FREQ   
40    $params_BW     
41    $params_LENGTH 
42    $params_DATE   
43    $params_TIME   
44    $params_MJDINT 
45    $params_MJDFRAC
46    $params_BMAJ   
47    $params_BMIN   
48    $params_BPA     
49    $params_DM     
50    $params_PERIOD 
51    $params_NCHAN   
52    $params_NPOL   
53    $params_NBIN   
54    $params_NSUB   
55    $params_NBITS   
56    $params_TSAMP   
57    $params_NBEAM
58    $params_CNFG   
59    $params_INST   
60    $params_RCVR   
61    $params_TELESCOP
62    $params_SITE   
63    $params_OBSRVR 
64    /;  # $machine
65                                               
66*name   = *File::Find::name;
67*dir    = *File::Find::dir;
68*prune  = *File::Find::prune;
69
70# The data types
71$FB = "FB";
72$WBC = "WBC";
73$DFB = "DFB";
74$CPSR2 = "CPSR2";
75
76# Pulsar parameters (listed in "vap -H")
77#$params  = "name raj decj dm period bw cnfg freq inst intmjd fracmjd npol nchan nbin nsub rcvr site length obsrvr";
78#$params  = "name projid ra dec freq bw length stt_date stt_time intmjd fracmjd bmaj bmin bpa dm period nchan npol nbin nsub nbits tsamp nbeam cnfg backend rcvr telescop site obsrvr";
79$params  = "name projid ra dec freq bw length stt_date stt_time intmjd fracmjd mjd bmaj bmin bpa dm period nchan npol nbin nsub nbits tsamp nbeam cnfg backend rcvr telescop site obsrvr";
80
81# vap result parameter indices. Used to locate the resulting
82# parameter's location
83$params_NAME    = 0;
84$params_PROJID  = 1;
85$params_RAJ     = 2;
86$params_DECJ    = 3;
87$params_FREQ    = 4;
88$params_BW      = 5;
89$params_LENGTH  = 6;
90$params_DATE    = 7;
91$params_TIME    = 8;
92$params_MJDINT  = 9;
93$params_MJDFRAC = 10;
94$params_BMAJ    = 11;
95$params_BMIN    = 12;
96$params_BPA     = 13;
97$params_DM      = 14;
98$params_PERIOD  = 15;
99$params_NCHAN   = 16;
100$params_NPOL    = 17;
101$params_NBIN    = 18;
102$params_NSUB    = 19;
103$params_NBITS   = 20;
104$params_TSAMP   = 21;
105$params_NBEAM   = 22;
106$params_CNFG    = 23;
107$params_INST    = 24;
108$params_RCVR    = 25;
109$params_TELESCOP= 26;
110$params_SITE    = 27;
111$params_OBSRVR  = 28;
112
113# filename extensions
114$archive_extensions = "ar rf cf cfb fb";
115
116# The machine to do all the processing on
117#$machine = "tycho";
118
119# The location of vap
120$vap = "/pulsar/psr/linux/bin/vap";
121
122#####################
123# Begin main method
124#
125my @vap_params = split(/ /, $params);
126$num_vap_params = scalar(@vap_params);
127
128# The column number (starting from 0) of the file
129# size when calling ls -l
130$FILE_SIZE_INDEX = 4;
131
132# Use the default operation which is to firstly delete any
133# stale records, then crawl through and insert new records
134$default = 1;
135
136print "ARGV = @ARGV\n";
137
138if (scalar(@ARGV) == 0) {
139        print "find_archives.pl: A crawler that populates a relational database\n
140                                with indexes to observational data along with their respective\n
141                                cal files.\n\n
142                                Usage: find_archives.pl [options] directory1 [directory2 ...]\n\n
143                                If no options are provided, it will default to do both -d and -p\n
144                                Options:\n
145                                \t-d Delete stale records\n
146                                \t-p Populate database with new records\n";
147       
148}
149
150# Connect to the database
151$db_handle = connectdb();
152
153#########################################################
154# Step 1. First remove any stale entries in the database
155#         (remove file locations that no longer exist)
156#########################################################
157
158if ($ARGV[0] eq "-d") {
159        deleteStaleRecords();
160        $default = 0;
161        shift @ARGV;
162}
163
164
165#########################################################
166# Step 2. Crawl through each input directory and insert
167#         any new archives
168
169if ($ARGV[0] eq "-p") {
170        shift @ARGV;
171        @archive_dirs = @ARGV;
172
173        foreach $archive_dir (@archive_dirs) {
174
175                # Traverse desired filesystems
176                File::Find::find({wanted => \&wanted}, $archive_dir);
177        }
178        $default = 0;
179}
180
181# Else do both
182if ($default) {
183
184        deleteStaleRecords();
185
186        @archive_dirs = @ARGV;
187
188        foreach $archive_dir (@archive_dirs) {
189               
190                # Check that the input directory is legit
191               
192                # Traverse desired filesystems
193                File::Find::find({wanted => \&wanted}, $archive_dir);
194        }
195
196}
197if ($db_handle->disconnect()) {
198        print "Successfully Disconnected from database\n";
199}
200else {
201        print "Error: Failed to disconnect from the database\n";
202        exit;
203}
204
205#######################################################
206# Subroutine definitions
207#
208sub wanted {
209 
210        my @files;
211        my @all_files;
212        my @cpsr2_obs_dirs;
213        my $file;
214        my $cpsr2_obs_dir;
215        my $search_str = "";
216        my $num_dumps = 0;
217       
218        if (-d and /^[a-zA-Z]{0,1}[0-9]{4}[+-][0-9]{4}.*/s ) {
219
220                #print "In directory $_\n";
221               
222                foreach $ext (split (/\s+/, $archive_extensions) ) {
223                        $search_str .= $File::Find::name . "/*.$ext "
224                }
225
226                @all_files = glob($search_str);
227               
228                if (scalar(@all_files) == 0) {
229               
230                        #print "This is a CPSR2 directory: $_\n";
231                       
232                        # Foreach observation
233                        @cpsr2_obs_dirs = glob($File::Find::name . "/????-??-??-??:??:??");
234                       
235                        foreach $cpsr2_obs_dir (@cpsr2_obs_dirs) {
236                                #print "dir is $cpsr2_obs_dir\n";
237                               
238                                # Need to check if this entry already exists
239                                $cpsr2_obs_dir =~ s/\s//g;
240
241                                if (isDuplicate($cpsr2_obs_dir) == 1) {
242                                        print "$cpsr2_obs_dir is a duplicate entry. Skipping....\n\n";
243                                        next;
244                                }                               
245                                #else {
246                                        #print "$cpsr2_obs_dir NOT a duplicate. Inserting into database\n";
247                                #}
248                               
249                               
250                                @all_files = glob($cpsr2_obs_dir . "/m*fb " . $cpsr2_obs_dir . "/m*ar");
251                               
252                                if (scalar(@all_files) == 0) {
253                                        next;
254                                }
255                               
256                                #print "first file is " . $all_files[0] . "\n";
257                               
258                                # Work out the length of each dump
259                                # $vap_cmd = "ssh $machine $vap -n -c \\\"$params\\\" ".$all_files[0]. " |";
260                                $vap_cmd = "$vap -n -c \"$params\" ".$all_files[0]. " | grep -v filename | grep -v -x \"\" |";
261
262                                print "Calling vap...$vap_cmd\n";
263
264                                open(VAP,$vap_cmd);
265                               
266                                while ($vap_out = <VAP>) {
267                                   
268                                    print "vap_out = $vap_out\n";
269
270                                    #@result_params = split(/\s+/, $vap_out);
271                                    @result_params = split(" ", $vap_out);
272                                   
273                                    $length = $result_params[$params_LENGTH+1];
274                                    $result_params[$params_BW+1] *= 2;
275                                       
276                                    #print "length = $length secs\n";
277                                   
278                                    $num_dumps = `ls -1 $cpsr2_obs_dir/m*fb $cpsr2_obs_dir/m*ar | wc -l`;
279                                   
280                                    #print "num dumps = $num_dumps\n";
281                                   
282                                    $result_params[$params_LENGTH+1] = $length*$num_dumps;
283                                   
284                                    #print "total length = " . $result_params[$params_LENGTH+1] . "\n";
285                                   
286                                    #print "new bw = " . $result_params[$params_BW+1] . "\n";
287                                   
288                                    # +1 because first column is filename
289                                    if (scalar(@result_params) == ($num_vap_params + 1)) {
290                                        print "@result_params\n";
291                                       
292                                        populate_observations($cpsr2_obs_dir, $num_dumps);
293                                    }
294                                    else {
295                                        print "Incorrect number of cols = ".scalar(@result_params).". Expected " . ($num_vap_params+1) ."\n";
296                                    }
297                                   
298                                }
299                                print "\n";
300                               
301
302                                # Count the number of files
303                        }
304                }
305                else {
306                        foreach $file (@all_files) {
307
308                                # Need to check if this entry already exists
309                                $_ =~ s/\s//g;
310                                $file =~ s/\s//g;
311
312                                if (isDuplicate($file) == 1) {
313                                        #print "$file is a duplicate entry. Skipping....\n\n";
314                                        next;
315                                }                               
316                                else {
317                                        #print "$file NOT a duplicate. Inserting into database\n";
318                                }
319
320                        #       $vap_cmd = "ssh $machine $vap -n -c \\\"$params\\\" $file |";
321                                $vap_cmd = "$vap -n -c \"$params\" $file |";
322
323                                print "Calling vap...$vap_cmd\n";
324
325                                open(VAP,$vap_cmd);
326
327                                while ($vap_out = <VAP>) {
328                                        @result_params = split(/\s+/, $vap_out);
329
330                                        # +1 because first column is filename
331                                        if (scalar(@result_params) == ($num_vap_params + 1)) {
332                                                print "@result_params\n";
333
334                                                populate_observations($File::Find::name);
335                                        }
336                                        else {
337                                                print "Incorrect number of cols = ".scalar(@result_params).". Expected " . $num_vap_params+1 ."\n";
338                                        }
339                                }
340                                print "\n";
341                        }
342                }
343                # don't traverse into this directory
344                $File::Find::prune = 1;
345        }
346}
347
348sub isDuplicate {
349
350#DEBUGGING
351    return 0;
352
353        my $filename = $_[0];
354        my $search_data_loc;
355        my $search_filename;
356        my $pulsar_name;
357       
358        if ( $filename =~ m/(.*(J[0-9]{4}[+-][0-9]{1,4}.*))[\/]+([a-z][0-9]{6}_[0-9]{6}\.([a-z]{2,}))$/i ) {
359                $search_data_loc = $1;
360                $search_filename = $3;
361                $pulsar_name = $2;
362        }
363       
364        elsif ($filename =~ m/(.*(J[0-9]{4}[+-][0-9]{1,4}.*))[\/]+([0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}:[0-9]{2}:[0-9]{2})$/i ) {
365                $search_data_loc = $1;
366                $search_filename = $3;
367                $pulsar_name = $2;
368        }
369        else {
370                return 1;
371        }
372       
373       
374        my $sql;
375       
376        if ($pulsar_name =~ m/.*_R$/) {
377               
378                $sql = qq{ SELECT * from cals
379                                                          WHERE data_loc LIKE ? AND
380                                                                      filename    = ?
381                                                };
382        }
383        else {
384                print "This is an observation\n";
385                $sql = qq{ SELECT * from observations
386                                                          WHERE data_loc LIKE ? AND
387                                                                      filename    = ?
388                                                };
389        }
390       
391        my $sth = $db_handle->prepare( $sql );
392
393  eval {
394    print "like $search_data_loc and filename = $search_filename \n";
395   
396    $sth->bind_param( 1, $search_data_loc, SQL_VARCHAR );
397    $sth->bind_param( 2, $search_filename, SQL_VARCHAR );
398    $sth->execute();
399
400        };
401
402  if( $@ ) {
403    warn "Database error: $DBI::errstr\n";
404    $db_handle->rollback(); #just die if rollback is failing
405  }
406       
407        # read the records
408        my @data = $sth->fetchrow_array();
409               
410        $sth->finish();
411       
412        if (scalar(@data) > 0) {
413                return 1;
414        }
415       
416        return 0;
417}
418
419sub populate_observations {
420
421
422        print "populate_obs(".$_[0].")\n";
423        $data_loc = $_[0];
424        my $display_data_loc = $data_loc;
425       
426        if ($data_loc =~ m/(.*)\/[0-9]{4}-[0-9]{2}-[0-9]{2}-[0-9]{2}:[0-9]{2}:[0-9]{2}$/i) {
427                print "Actual data loc is $1\n";
428                $display_data_loc = $1;
429        }
430               
431        $filename = shift(@result_params);
432       
433        # remove leading and trailing whitespace
434        $filename =~ s/ //g;
435       
436        my $display_filename = $filename;
437
438        my $ls = `ls -l $data_loc/$filename`;
439       
440        my @file_details = split(/\s+/, $ls);
441       
442        $file_size = $file_details[$FILE_SIZE_INDEX];
443       
444        # Get the total filesize
445        if (scalar (@_) > 1) {
446                my $num_dumps = $_[1];
447       
448                $file_size *= $num_dumps*2; # *2 because of other band
449                print "Multiplying filesize by number files = $num_dumps with total $file_size\n";
450        }
451
452        # Try to find the data type
453        if ($filename =~ /\.ar$/) {
454                $data_type = $FB;
455        }
456        elsif ($filename =~ /\.rf/ || $filename =~ /fb/) {
457                if ($filename =~ /^a/) { # DFB file
458                        $data_type = $DFB;
459                }
460               
461                elsif ($filename =~ /^w/) { # Wide Band Correlator file
462                        $data_type = $WBC;
463                }
464                elsif ($filename =~ /^m/ || $filename =~ /^n/ || $filename =~ /^r/) {
465                        $data_type = $CPSR2;
466                       
467                        # Use the directory of observation instead of filename instance
468                        my @dirs = split(/\//, $data_loc);
469                       
470                        print "The new filename will be " . $dirs[$#dirs] . " from $data_loc\n";
471                       
472                        $display_filename = $dirs[$#dirs];
473                       
474                        # For some reason, the name of the pulsar cals for CPSR2 files are all "CAL"
475                        if ($data_loc =~ /\_R/) {
476                                my @jname_parts = split(/J/i, $dirs[$#dirs-1]);
477                                $result_params[$params_NAME] = $jname_parts[1];
478                        }
479
480                }
481                else {
482                        $data_type = undef;
483                }
484        }
485       
486        # Check that N/A values are set to NULL
487
488        # Get the total MJD
489        if ( $result_params[$params_MJDINT] eq "N/A" &&
490             $result_params[$params_MJDFRAC] eq "N/A" ) {
491                $MJD = undef;
492        }
493        else {
494                $MJD = $result_params[$params_MJDINT] + $result_params[$params_MJDFRAC];
495        }
496       
497        print "MJD = $MJD\n";
498
499        #### Calculate the RA and Dec in decimal degrees.
500
501        ($raH,$raM,$raS) = split(':', $result_params[$params_RAJ]);
502        $rajd = ($raH + $raM/60. + $raS/3600.) * 15.;
503
504        ($decD,$decM,$decS) = split(':', $result_params[$params_DECJ]);
505        $decjd = (abs($decD) + $decM/60. + $decS/3600.);
506        my @decstring = split(/ */,$result_params[$params_DECJ]);
507        if($decstring[0] eq '-'){
508            $decjd = -1. * $decjd;
509        }
510
511        ### Calculate the Galactic Longitude and latitude
512        $pi=asin(1) * 2.;
513        $pion180 = $pi/180.;
514        $NGP_RA = 192.859508 * $pion180; # location of NGP
515        $NGP_DEC= 27.128336 * $pion180;
516        $ASC_NODE=32.932;
517       
518        $draR = $rajd*$pion180 - $NGP_RA;
519        $decR = $decjd*$pion180;
520        $sinb = cos($decR) * cos($NGP_DEC) * cos($draR) + sin($decR) * sin($NGP_DEC);
521        $gb = asin($sinb); # this is the latitude, but in radians.
522       
523        $sinl = (sin($decR) * cos($NGP_DEC) - cos($decR) * cos($draR) * sin($NGP_DEC)) / cos($gb);
524        $cosl = cos($decR) * sin($draR) / cos($gb);
525       
526        # Need to get the correct quadrant, as this isn't preserved by
527        # atan, which returns angle between -90 and 90.
528        $gl_raw = atan($sinl/$cosl);
529        if($sinl > 0){
530            if($cosl > 0 ){ $gl = $gl_raw; }
531            else { $gl = $gl_raw + $pi; }
532        }
533        else {
534            if($cosl > 0){ $gl = $gl_raw + 2.*$pi; }
535            else{ $gl = $gl_raw + $pi; }
536        }
537        # Now put them into degrees.
538        $gb = $gb / $pion180;
539        $gl = ($gl / $pion180) + $ASC_NODE;
540
541        print "rajd = $rajd, decjd=$decjd, gl=$gl, gb=$gb\n";
542
543        if($result_params[$params_BMAJ] eq "UNDEF"){
544            $bmaj = (1.2*(299792458./($result_params[$params_FREQ] *1.e6))/64.) / $pion180; }
545        if($result_params[$params_BMAJ] eq "UNDEF"){
546            $bmin = (1.2*(299792458./($result_params[$params_FREQ] *1.e6))/64.) / $pion180; }
547        if($result_params[$params_BMAJ] eq "UNDEF"){
548            $bpa = 0.;  }
549
550        # Dud things that aren't required for this set of data.
551        $hdrver = NULL;
552        $survey = NULL;
553        $nbeam = 1;
554       
555        my $i; 
556        for ($i = 0; $i <= $#result_params; $i++) {
557                if ($result_params[$i] eq "N/A") {
558                        $result_params[$i] = undef;
559                }
560        }
561       
562        print "filename = $display_filename\n";
563        print "src_name = " . $result_params[$params_NAME] . "\n";
564        print "projid = " . $result_params[$params_PROJID] . "\n";
565        print "raj = " . $result_params[$params_RAJ] . "\n";
566        print "dec = " . $result_params[$params_DECJ] . "\n";
567        print "data_type = $data_type\n";
568        print "freq = " . $result_params[$params_FREQ] . "\n";
569        print "bw = " . $result_params[$params_BW] . "\n";
570        print "scanlen = " . $result_params[$params_LENGTH] . "\n";
571        print "date = " . $result_params[$params_DATE] ."\n";
572        print "ut = " . $result_params[$params_TIME] ."\n";
573        print "MJD = " . $MJD ."\n";
574        print "rajd = " . $rajd ."\n";
575        print "decjd = " . $decjd ."\n";
576        print "gl = " . $gl ."\n";
577        print "gb = " . $gb ."\n";
578        print "bmaj = " . $bmaj ."\n";
579        print "bmin = " . $bmin ."\n";
580        print "bpa = " . $bpa ."\n";
581        print "dm = " . $result_params[$params_DM] . "\n";
582        print "period = " . $result_params[$params_PERIOD] . "\n";
583        print "nchan = " . $result_params[$params_NCHAN] . "\n";
584        print "npol = " . $result_params[$params_NPOL] . "\n";
585        print "nbin = " . $result_params[$params_NBIN] . "\n";
586        print "nsub = " . $result_params[$params_NSUB] . "\n";
587        print "tsamp = " . $result_params[$params_TSAMP] . "\n";
588        print "nbits = " . $result_params[$params_NBITS] . "\n";
589        print "nbeam = " . $result_params[$params_NBEAM] . "\n";
590        print "cnfg = " . $result_params[$params_CNFG] . "\n";
591        print "inst = " . $result_params[$params_INST] . "\n";
592        print "rcvr = " . $result_params[$params_RCVR] . "\n";
593        print "hdrver = " . $hdrver . "\n";
594        print "survey = " . $survey . "\n";
595        print "telescope = " . $result_params[$params_TELESCOP] . "\n";
596        print "site = " . $result_params[$params_SITE] . "\n";
597        print "obsrvr = " . $result_params[$params_OBSRVR] . "\n";
598        print "data_loc = $display_data_loc\n";
599        print "file_size = $file_size bytes\n";
600
601        my $sql;
602       
603        if ($data_loc =~ /\_R/) {
604            print "Inserting cal file $display_filename\n";
605               
606            $sql = qq{ INSERT INTO cals
607                           (filename, src_name, project_id,
608                            raj, decj, data_type, obsfreq, bw,
609                            scanlen, date, ut, MJD,
610                            rajd, decjd, gl, gb, bmaj, bmin, bpa,
611                            dm, period, nchan, npol, nbin, nsub,
612                            tsamp, nbits, nbeam,
613                            cnfg, inst, rcvr, hdrver, survey,
614                            telescope, site, obsrvr,
615                            data_loc, file_size_bytes
616                            )
617                           
618                           VALUES
619                           ( ?, ?, ?
620                             ?, ?, ?, ?, ?,
621                             ?, ?, ?, ?,
622                             ?, ?, ?, ?, ?, ?, ?,
623                             ?, ?, ?, ?, ?, ?,
624                             ?, ?, ?,
625                             ?, ?, ?, ?, ?,
626                             ?, ?, ?,
627                             ?, ?,
628                             )
629                       };
630        }
631        else {
632           
633            print "Inserting obs file $display_filename\n";
634           
635            $sql = qq{ INSERT INTO observations
636                           (filename, src_name, project_id,
637                            raj, decj, data_type, obsfreq, bw,
638                            scanlen, date, ut, MJD,
639                            rajd, decjd, gl, gb, bmaj, bmin, bpa,
640                            dm, period, nchan, npol, nbin, nsub,
641                            tsamp, nbits, nbeam,
642                            cnfg, inst, rcvr, hdrver, survey,
643                            telescope, site, obsrvr,
644                            data_loc, file_size_bytes
645                            )
646                           
647                           VALUES
648                           ( ?, ?, ?
649                             ?, ?, ?, ?, ?,
650                             ?, ?, ?, ?,
651                             ?, ?, ?, ?, ?, ?, ?,
652                             ?, ?, ?, ?, ?, ?,
653                             ?, ?, ?,
654                             ?, ?, ?, ?, ?,
655                             ?, ?, ?,
656                             ?, ?,
657                             )
658                       };
659        }
660#       my $sth = $db_handle->prepare( $sql );
661#DEBUGGING     
662        my $sth;
663
664        #for( @records ) {
665  eval {
666
667    $sth->bind_param( 1,  $display_filename,                SQL_VARCHAR );
668    $sth->bind_param( 2,  $result_params[$params_NAME],     SQL_VARCHAR );
669    $sth->bind_param( 3,  $result_params[$params_RAJ],      SQL_VARCHAR );
670    $sth->bind_param( 4,  $result_params[$params_DECJ],     SQL_VARCHAR );
671    $sth->bind_param( 5,  $data_type,                       SQL_VARCHAR );
672    $sth->bind_param( 6,  $result_params[$params_FREQ],     SQL_NUMERIC );
673    $sth->bind_param( 7,  $result_params[$params_BW],       SQL_NUMERIC );
674    $sth->bind_param( 8,  $result_params[$params_LENGTH],   SQL_NUMERIC );
675    $sth->bind_param( 9,  $result_params[$params_DATE],     SQL_NUMERIC );
676    $sth->bind_param( 10, $result_params[$params_TIME],     SQL_NUMERIC );
677    $sth->bind_param( 11, $MJD,                             SQL_NUMERIC );
678    $sth->bind_param( 12, $rajd,                            SQL_NUMERIC );
679    $sth->bind_param( 13, $decjd,                           SQL_NUMERIC );
680    $sth->bind_param( 14, $gl,                              SQL_NUMERIC );
681    $sth->bind_param( 15, $gb,                              SQL_NUMERIC );
682    $sth->bind_param( 16, $bmaj,                            SQL_NUMERIC );
683    $sth->bind_param( 17, $bmin,                            SQL_NUMERIC );
684    $sth->bind_param( 18, $bpa,                             SQL_NUMERIC );
685    $sth->bind_param( 19, $result_params[$params_DM],       SQL_NUMERIC );
686    $sth->bind_param( 20, $result_params[$params_PERIOD],   SQL_NUMERIC );   
687    $sth->bind_param( 21, $result_params[$params_NCHAN],    SQL_INTEGER );
688    $sth->bind_param( 22, $result_params[$params_NPOL],     SQL_INTEGER );
689    $sth->bind_param( 23, $result_params[$params_NBIN],     SQL_INTEGER );   
690    $sth->bind_param( 24, $result_params[$params_NSUB],     SQL_INTEGER );
691    $sth->bind_param( 25, $result_params[$params_NBITS],    SQL_INTEGER );
692    $sth->bind_param( 26, $result_params[$params_TSAMP],    SQL_INTEGER );
693    $sth->bind_param( 27, $nbeam,                           SQL_INTEGER );
694    $sth->bind_param( 28, $result_params[$params_CNFG],     SQL_VARCHAR );
695    $sth->bind_param( 29, $result_params[$params_INST],     SQL_VARCHAR );
696    $sth->bind_param( 30, $result_params[$params_RCVR],     SQL_VARCHAR );
697    $sth->bind_param( 31, $hdrver,                          SQL_VARCHAR );
698    $sth->bind_param( 32, $survey,                          SQL_VARCHAR );
699    $sth->bind_param( 33, $result_params[$params_TELESCOP], SQL_VARCHAR );
700    $sth->bind_param( 34, $result_params[$params_SITE],     SQL_INTEGER );
701    $sth->bind_param( 35, $result_params[$params_OBSRVR],   SQL_VARCHAR );
702    $sth->bind_param( 13, $display_data_loc,                SQL_VARCHAR );
703    $sth->bind_param( 34, $file_size,                       SQL_INTEGER );
704
705#    $sth->execute();
706#DEBUGGING
707    DBI::dump_results($sth);
708
709    # $db_handle->commit(); # Autocommit is already ON so no need for a manual commit
710  };
711
712  if( $@ ) {
713      print "warning statement\n";
714#DEBUGGING
715#    warn "Database error: $DBI::errstr\n";
716#    $db_handle->rollback(); #just die if rollback is failing
717  }
718       
719        #}
720
721#DEBUGGING
722#       $sth->finish();
723}
724
725# Removes
726sub deleteStaleRecords {
727
728        my $sql, my $sth, my @data, my $file;
729       
730        $sql = qq{ SELECT data_loc, filename from cals };
731        $sth = $db_handle->prepare( $sql );
732
733  eval {
734
735    $sth->execute();
736
737        };
738       
739  if( $@ ) {
740    warn "Database error: $DBI::errstr\n";
741    $db_handle->rollback(); #just die if rollback is failing
742  }
743
744        my @cals2delete;
745       
746        while (@data = $sth->fetchrow_array()) {
747               
748                if (scalar(@data) == 2) {
749                        $file = $data[0]."/".$data[1];
750                       
751                        if (!-e $file) {
752                                 print "$file does NOT exist\n\n";
753                               
754                                # Push the row into the cals to delete
755                                # So array will look like:
756                                # {/full/path/to/dir, filename.cf, /full/path/to/dir, filename2.cf, etc..}
757                                push (@cals2delete, @data);
758                        }
759                }
760                       
761  }
762
763        $sql = qq{ SELECT data_loc, filename from observations };
764       
765        $sth = $db_handle->prepare( $sql );
766  eval {
767
768    $sth->execute();
769
770        };
771       
772  if( $@ ) {
773    warn "Database error: $DBI::errstr\n";
774    $db_handle->rollback(); #just die if rollback is failing
775  }
776       
777        my @obs2delete;
778       
779        while (@data = $sth->fetchrow_array()) {
780       
781                if (scalar(@data) == 2) {
782                        $file = $data[0]."/".$data[1];
783                       
784                        if (!-e $file) {
785                                print "$file does NOT exist\n\n";
786                               
787                                # Push the row into the cals to delete
788                                # So array will look like:
789                                # {/full/path/to/dir, filename.cf, /full/path/to/dir, filename2.cf, etc..}
790                                push (@obs2delete, @data);
791                        }
792                }                 
793  }
794
795        print "There are " . scalar(@cals2delete)/2 . " stale cals (" .
796              scalar(@cals2delete) . "/2) and " .
797              scalar(@obs2delete)/2 . " stale obs to delete\n";
798
799        # Delete the rows
800        my $data_loc, my $filename;
801       
802        while (@cals2delete) {
803                $data_loc = shift @cals2delete;
804                $filename = shift @cals2delete;
805               
806                $sql = qq{ DELETE FROM cals WHERE data_loc = ? AND filename = ?};
807
808                $sth = $db_handle->prepare( $sql );
809        eval {
810
811        $sth->bind_param( 1, $data_loc, SQL_VARCHAR );
812        $sth->bind_param( 2, $filename, SQL_VARCHAR );
813        $sth->execute();
814
815                };
816               
817                if( $@ ) {
818        warn "Database error: $DBI::errstr\n";
819        $db_handle->rollback(); #just die if rollback is failing
820        }
821                else {
822                        print "Successfully deleted $data_loc/$filename\n";
823                }
824
825        }
826       
827        while (@obs2delete) {
828                $data_loc = shift @obs2delete;
829                $filename = shift @obs2delete;
830               
831                $sql = qq{ DELETE FROM observations WHERE data_loc = ? AND filename = ?};
832
833                $sth = $db_handle->prepare( $sql );
834               
835        eval {
836
837        $sth->bind_param( 1, $data_loc, SQL_VARCHAR );
838        $sth->bind_param( 2, $filename, SQL_VARCHAR );
839        $sth->execute();
840
841                };
842               
843                if( $@ ) {
844        warn "Database error: $DBI::errstr\n";
845        $db_handle->rollback(); #just die if rollback is failing
846        }
847                else {
848                        print "Successfully deleted $data_loc/$filename\n";
849                }
850        }
851
852        $sth->finish();
853       
854}
855
856sub connectdb {
857       
858        # Connect to the MySQL server
859#       my $dbh = DBI->connect("dbi:mysql:database=psrchive;host=localhost", "psrdba", "lighthouse")
860#       or die "Couldn't connect to database: $DBI::errstr\n";
861
862#       print "Successfully connected to db\n" if $dbh;
863
864# DEBUGGING     
865    print "We would normally connect to the DB here\n";
866    return 1;
867#       return $dbh;
868}
Note: See TracBrowser for help on using the repository browser.