Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

getDBfiles.java

00001 package ProdDatabase;
00002 // getDBfiles.java
00003 // @Author Dave Robinson
00004 //
00005 // 21052003 Introduce controlFlag - bit 1 set means create upload files but don't upload
00006 //                                  bit 2 set means don't show gui and immediately create upload files
00007 
00008 
00009 import java.io.*;
00010 import java.util.Vector;
00011 import java.util.regex.*;
00012 
00013 public class getDBfiles {
00014 
00015         static String outputDirectory;
00016         static java.util.Map serialNoMap = null;
00017         static boolean dontOverwrite = false;
00018         static String firstSerialNo="";
00019         static String lastSerialNo="";
00020         static String testLocation="Anywhere";
00021         static String runNo=null;
00022         static String scanNo=null;
00023         static int temperature=0;
00024         static int testType=-1;
00025         static int testRequest=-1;
00026         
00027         public static void main(String[] args) {
00028           Pattern serialNoPattern = Pattern.compile("(\\d{14})");
00029           Pattern testRequestPattern = Pattern.compile("-(\\d{1,2})");
00030            Matcher matcher;
00031                 if(args.length<2) showOptions("Syntax is java ProdDatabase/getDBfiles followed by: ");
00032                         
00033                 outputDirectory=null;
00034                 boolean nextArgIsTestLocation = false;
00035                 boolean nextArgIsOutputDirectory=false;
00036                 boolean nextArgIsRunNo=false;
00037                 boolean nextArgIsScanNo=false;          
00038 
00039                 for(int i=0;i<args.length;i++) {
00040                   if(nextArgIsTestLocation) {
00041                       testLocation = args[i];
00042                       try {
00043                                    if(!GeneralUtilities.confirmLocation(testLocation)) {
00044                                       System.out.println("** Test location '"+testLocation+"' not recognised. Spelling? (Case-sensitive)");
00045                                       System.exit(0);
00046                                       }
00047                       } catch(Exception ee) {System.out.println("Failed to connect to database."); System.exit(0);}
00048                       nextArgIsTestLocation=false;
00049                       continue;
00050                       }
00051                   if(nextArgIsOutputDirectory) {
00052                       outputDirectory = args[i];
00053                       nextArgIsOutputDirectory=false;
00054                       File temp = new File(outputDirectory);
00055                       if(!temp.exists()) {
00056                         System.out.println("** Directory "+outputDirectory+" does not exist!");
00057                         System.exit(0);
00058                         }
00059                       continue;
00060                       }
00061                   if(nextArgIsRunNo) {
00062                       nextArgIsRunNo=false;
00063                       runNo = args[i];
00064                       continue;
00065                       }
00066                   if(nextArgIsScanNo) {
00067                       nextArgIsScanNo=false;
00068                       scanNo = args[i];
00069                       continue;
00070                       }
00071                   if(args[i].equals("-iv") || args[i].equals("-IV")) {
00072                         testType=SCTDBInfo.SCTDB_TEST_IV;
00073                         continue;
00074                         }
00075                   if(args[i].equals("-rc") || args[i].equals("-RC")) {
00076                         testType=SCTDBInfo.SCTDB_TEST_NPTGAIN;
00077                         continue;
00078                         }
00079                   if(args[i].equals("-xml") || args[i].equals("-XML")) {
00080                         testType=SCTDBInfo.SCTDB_TEST_TIMEWALK;
00081                         continue;
00082                         }
00083                   if(args[i].equals("-trim") || args[i].equals("-TRIM")) {
00084                         testType=SCTDBInfo.SCTDB_TEST_TRIM;
00085                         continue;
00086                         }
00087                   if(args[i].equals("-3ptgain") || args[i].equals("-3PTGAIN")) {
00088                         testType=SCTDBInfo.SCTDB_TEST_3PTGAIN;
00089                         continue;
00090                         }
00091                   if(args[i].equals("-warm") || args[i].equals("-WARM")) {
00092                         temperature=2;
00093                         continue;
00094                         }
00095                   if(args[i].equals("-cold") || args[i].equals("-COLD")) {
00096                         temperature=1;
00097                         continue;
00098                         }
00099 
00100                   if(args[i].equals("-t")) {
00101                         nextArgIsTestLocation=true;
00102                         continue;
00103                         }
00104                   if(args[i].equals("-o")) {
00105                         nextArgIsOutputDirectory=true;
00106                         continue;
00107                         }
00108                   if(args[i].equals("-r")) {
00109                         nextArgIsRunNo=true;
00110                         continue;
00111                         }
00112                   if(args[i].equals("-s")) {
00113                         nextArgIsScanNo=true;
00114                         continue;
00115                         }
00116                   if(args[i].equals("-d")) {
00117                         dontOverwrite=true;
00118                         continue;
00119                         }
00120                   matcher = testRequestPattern.matcher(args[i]);
00121                   if(matcher.matches()) {
00122                      testRequest = Integer.parseInt(args[i].substring(matcher.start(1),matcher.end(1)));
00123                      continue;
00124                      }
00125 // check for 14-digit no.
00126 // assume first serial no, otherwise 2nd serial no
00127                   matcher = serialNoPattern.matcher(args[i]);
00128                   if(matcher.matches()) {
00129                       String thisSerialNo = args[i].substring(matcher.start(1),matcher.end(1));
00130                       if(!firstSerialNo.equals("")) lastSerialNo=thisSerialNo;
00131                       else firstSerialNo = thisSerialNo;
00132                       continue;
00133                       }
00134                      System.out.println("Unrecognised argument: "+args[i]);
00135                      System.exit(0);
00136                    }
00137 
00138 // check validity of arguments
00139                 if(testType==-1 && testRequest==-1) showOptions("You must specify a test type: ");
00140                 if(firstSerialNo.equals("")) showOptions("You must specify a serial number. ");
00141                 if(!lastSerialNo.equals("")) {
00142                     String firstPart = firstSerialNo.substring(0,9);
00143                     String lastPart = lastSerialNo.substring(0,9);
00144                     if(!firstPart.equals(lastPart)) showOptions("Second serial no is not the same devicetype as the first serial no. Try again: ");
00145                     }
00146                 else lastSerialNo=firstSerialNo;
00147 // Run and Scan
00148                 if(scanNo!=null && runNo!=null) runNo+="-"+scanNo;
00149                 else if(runNo!=null) runNo+="%";
00150 
00151                 if(testRequest!=-1 && testType!=-1) {
00152                    System.err.println("Cant request more than one type of download at the same time.");
00153                    System.exit(0);
00154                    }                  
00155                 if(testRequest>SCTDBInfo.SCTDB_TEST_IV) {
00156                    System.err.println(testRequest+" is not a known test index.");
00157                    System.exit(0);
00158                    }
00159 // now download
00160                 String baseSN = firstSerialNo.substring(0,9);
00161                 String firstSN = firstSerialNo.substring(9);
00162                 String lastSN = lastSerialNo.substring(9);
00163                 int i1 = Integer.parseInt(firstSN);
00164                 int i2 = Integer.parseInt(lastSN);
00165                 
00166                 
00167                 long l1 = Long.parseLong(firstSerialNo);
00168                 long l2 = Long.parseLong(lastSerialNo);
00169                 for(long l=l1;l<=l2;l++) {
00170                     try {
00171                         String sn = Long.toString(l);
00172                         if(testRequest==-1) printRawData(sn, l);
00173                         else doTestRequest(sn, l);
00174                     }catch(Exception e) {
00175                         System.out.println("Failed to connect to database: "+e.toString());
00176                     }
00177                 }
00178             }
00179         
00180         public static java.util.Map getSerialMap() throws Exception {
00181             if (serialNoMap == null) serialNoMap = GeneralUtilities.getRawDataLists(firstSerialNo, lastSerialNo, testLocation, testType, temperature, runNo, testRequest);
00182             return serialNoMap;
00183         }
00184         
00185         public static void doTestRequest(String sn, long l) throws Exception {
00186             String fileName = sn+"_"+SCTDBInfo.sctdaqNormalTestNames[testRequest]+".dat";
00187             //Don't do anything more if the file already exists and we've been told not to overwrite
00188             if (dontOverwrite && new File(fileName).exists()) return;
00189             
00190             SCTDBTestInfoHolder testInfo = (SCTDBTestInfoHolder)getSerialMap().get(new Long(l));
00191             if(testInfo==null) {
00192                 System.out.println("**No data available for "+sn);
00193                 return;
00194             }           
00195             String result = GeneralUtilities.getTestString(testRequest,testInfo.get(SCTDBTestInfoHolder.TESTNUMBER));
00196             saveFile(fileName,result,testInfo);
00197         }
00198         
00199         public static void showOptions(String text) {
00200             System.out.println(text+"\n");
00201             System.out.println("**Compulsory** parameters:");
00202             System.out.println(" -n to retrieve test results for test index n (see list below)  OR");
00203             System.out.println("'-iv' to retrieve iv data files  OR");
00204             System.out.println("'-rc' to retieve RC data files  OR");
00205             System.out.println("'-xml' to retrieve module configuration XML files  OR");
00206             System.out.println("'-trim' to retrieve trim files (mask and trim settings)  OR");
00207             System.out.println("'-3ptgain' to retrieve RC data files from 3ptgain tests");
00208             System.out.println("- one (or two) 14 digit serial numbers to retrieve data for the");
00209             System.out.println("  specified serial number or range of serial numbers\n");
00210             System.out.println("**Optional** parameters:");  
00211             System.out.println("'-warm' to retrieve only warm data");
00212             System.out.println("'-cold' to retrieve only cold data");        
00213             System.out.println("'-o outputDirectory' to write data files to outputDirectory");
00214             System.out.println("'-t testlocation' to download only data taken at testlocation");
00215             System.out.println("'-r RunNumber' to download only data for the given RunNumber");
00216             System.out.println("'-s ScanNumber' to download only data for the given ScanNumber (if RunNumber also requested)");
00217             System.out.println("\nTest Index vs test type:");
00218             for(int i=0;i<12;i++) System.out.println(i+" - "+SCTDBInfo.sctdaqNormalTestNames[i]);
00219             System.exit(0);
00220         }
00221 
00222   private static void saveFile(String fname, String data, SCTDBTestInfoHolder testInfo) {
00223      try { 
00224        File output = (outputDirectory!=null) ? new File(outputDirectory,fname) : new File(fname);
00225        BufferedWriter out = new BufferedWriter(new FileWriter(output));
00226        out.write(data);
00227        out.close();
00228        System.out.println("Saved "+fname+" uploaded by "+testInfo.get(SCTDBTestInfoHolder.LOCATION)+" on "+testInfo.get(SCTDBTestInfoHolder.DATE));
00229      }catch(Exception e){System.err.println("**ERROR saving file - "+e.toString());}
00230 }
00231 
00232 
00233 
00234   private static void printRawData(String sn, long l) throws Exception {
00235       String fname = sn;
00236       switch(testType) {
00237       case SCTDBInfo.SCTDB_TEST_IV:
00238           fname ="_iv.dat";
00239           break;
00240       case SCTDBInfo.SCTDB_TEST_NPTGAIN:
00241           fname+="_rc.dat";
00242           break;
00243       case SCTDBInfo.SCTDB_TEST_TIMEWALK:
00244           fname+=".xml";
00245           break;
00246           
00247       case SCTDBInfo.SCTDB_TEST_3PTGAIN:
00248           fname+="_rc3.dat";
00249           break;
00250       case SCTDBInfo.SCTDB_TEST_TRIM:   //Note this is slightly dodgy - 2 files for trim
00251           fname+=".trim";
00252           break;
00253       default:
00254           System.out.println("Unrecognized testType: " + testType);
00255           return;
00256       }
00257       
00258       //Don't do anything more if the file already exists and we've been told not to overwrite
00259       if (dontOverwrite && new File(fname).exists()) return;
00260       
00261       SCTDBTestInfoHolder testInfo = (SCTDBTestInfoHolder)getSerialMap().get(new Long(l));
00262       if(testInfo==null) {
00263           System.out.println("**No data available for "+sn);
00264           return;
00265       }
00266       
00267       Vector theData = GeneralUtilities.getRawData(testInfo.get(SCTDBTestInfoHolder.TESTNUMBER));
00268       sn =testInfo.get(SCTDBTestInfoHolder.SN);
00269       if(theData.size()!=2 && theData.size()!=4) {
00270           System.err.println("Strange data size for "+sn+" : "+theData.size());
00271           return;
00272       }
00273       File output;
00274       BufferedWriter out;
00275       if(theData.size()==2) {     
00276           saveFile(fname,(String)theData.elementAt(1),testInfo);
00277       } else if(theData.size()==4 && testType==SCTDBInfo.SCTDB_TEST_TRIM) {
00278           fname = sn+".trim";
00279           saveFile(fname,(String)theData.elementAt(1),testInfo); 
00280           fname = sn+".mask";
00281           saveFile(fname,(String)theData.elementAt(3),testInfo);
00282       }
00283   }
00284   
00285   
00286 
00287 
00288 }

Generated on Thu Jul 15 09:55:42 2004 for SCT DAQ/DCS Software - Java by doxygen 1.3.5