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

Last change on this file since 207 was 207, checked in by MatthewWhiting, 16 years ago

Some minor changes to find_archives, but a major rewrite of it in the form of new_find_archives, to try to get things working properly for cpsr2 AND dfb/wbc.

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