LISTSERV mailing list manager LISTSERV 16.5

Help for HPS-SVN Archives


HPS-SVN Archives

HPS-SVN Archives


HPS-SVN@LISTSERV.SLAC.STANFORD.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

HPS-SVN Home

HPS-SVN Home

HPS-SVN  May 2015

HPS-SVN May 2015

Subject:

r3048 - /java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtTimingConstantsLoader.java

From:

[log in to unmask]

Reply-To:

Notification of commits to the hps svn repository <[log in to unmask]>

Date:

Thu, 28 May 2015 02:05:38 -0000

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (184 lines)

Author: [log in to unmask]
Date: Wed May 27 19:05:29 2015
New Revision: 3048

Log:
Add command line tool for importing SVT timing constants from run spreadsheet CSV file.

Added:
    java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtTimingConstantsLoader.java

Added: java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtTimingConstantsLoader.java
 =============================================================================
--- java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtTimingConstantsLoader.java	(added)
+++ java/trunk/conditions/src/main/java/org/hps/conditions/svt/SvtTimingConstantsLoader.java	Wed May 27 19:05:29 2015
@@ -0,0 +1,168 @@
+package org.    hps.conditions.svt;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import org.hps.conditions.api.ConditionsRecord;
+import org.hps.conditions.database.DatabaseConditionsManager;
+import org.hps.conditions.run.RunRange;
+import org.hps.conditions.run.RunSpreadsheet;
+import org.hps.conditions.svt.SvtTimingConstants.SvtTimingConstantsCollection;
+
+/**
+ * Load SVT timing constant data from the run spreadsheet and insert into the conditions database.
+ * <p>
+ * Be very careful about running this, because it will create many new conditions records that may 
+ * already be present in the database.  In fact, don't run this at all without talking to me first. :-) 
+ * 
+ * @author Jeremy McCormick
+ */
+public final class SvtTimingConstantsLoader {
+    
+    /**
+     * Setup conditions.     
+     */
+    private static final DatabaseConditionsManager MANAGER = DatabaseConditionsManager.getInstance();
+    
+    /**
+     * The fields from the run spreadsheet for SVT timing constants.
+     */
+    private static final Set<String> FIELDS = new HashSet<String>();
+    static {
+        FIELDS.add("svt_offset_phase");
+        FIELDS.add("svt_offset_time");
+    }
+    
+    /**
+     * Load the SVT timing constants for time and phase offsets into the conditions database.
+     * 
+     * @param args the command line arguments (requires one argument which is CSV file name)
+     */
+    public static void main(String[] args) {
+        
+        // Load in CSV records from the exported run spreadsheet.
+        String path = args[0];
+        RunSpreadsheet runSheet = new RunSpreadsheet(new File(path));
+        
+        // Find the run ranges that have the same fields values.
+        List<RunRange> ranges = RunRange.findRunRanges(runSheet, FIELDS);
+        
+        // Get the unique field values for inserting new conditions collections.
+        List<Collection<String>> uniqueValues = RunRange.getUniqueValues(ranges);
+        
+        /*
+        System.out.println("unique values ...");
+        for (Collection<String> collection : uniqueValues) {
+            for (String value : collection) {
+                System.out.print(value + " ");                
+            }
+            System.out.println();
+        } 
+        */      
+               
+        // Create a new collection for each unique combination set of timing constants.
+        List<SvtTimingConstantsCollection> collections = createCollections(uniqueValues); 
+        
+        // Create a new collection for each of the unique combinations of values.
+        for (SvtTimingConstantsCollection collection : collections) {
+            int collectionId = 0;
+            try {
+                collectionId = MANAGER.addCollection(
+                        "svt_timing_constants",
+                        "SVT timing constants added by " + System.getProperty("user.name"),
+                        "timing constants from run spreadsheet");
+                collection.setCollectionId(collectionId);
+                collection.insert();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }            
+        } 
+                
+        // Create the conditions records for the run ranges.
+        for (RunRange range : ranges) {
+            
+            System.out.println(range);
+            
+            // Get the data values from the run range.
+            int offsetPhase = Integer.parseInt(range.getValue("svt_offset_phase"));
+            int offsetTime = Integer.parseInt(range.getValue("svt_offset_time"));
+
+            // Find the matching timing constants collection to use.
+            SvtTimingConstantsCollection collection = findCollection(collections, offsetPhase, offsetTime);
+            if (collection != null) {
+                System.out.println("offset_phase : " + collection.get(0).getOffsetPhase() + ", offset_time: " + collection.get(0).getOffsetTime());
+            }
+                        
+            // Create a new conditions record with the run range.
+            ConditionsRecord condi = new ConditionsRecord();
+            condi.setFieldValue("run_start", range.getRunStart());
+            condi.setFieldValue("run_end", range.getRunEnd());
+            condi.setFieldValue("name", "svt_timing_constants");
+            condi.setFieldValue("table_name", "svt_timing_constants");
+            condi.setFieldValue("notes", "timing constants from run spreadsheet");
+            condi.setFieldValue("created", new Date());
+            condi.setFieldValue("created_by", System.getProperty("user.name"));
+            
+            condi.setFieldValue("collection_id", collection.getCollectionId());
+
+            try {
+                condi.insert();
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }            
+            
+            System.out.print(condi);            
+            System.out.println();
+        }
+    }
+    
+    /**
+     * Create the conditions collections from the unique values found in the spreadsheet.
+     * 
+     * @param uniqueValues the list of unique raw values
+     * @return the new conditions collections
+     */
+    private static List<SvtTimingConstantsCollection> createCollections(List<Collection<String>> uniqueValues) {
+        List<SvtTimingConstantsCollection> collections = new ArrayList<SvtTimingConstantsCollection>();        
+        for (Collection<String> values : uniqueValues) {
+            
+            SvtTimingConstants timing = new SvtTimingConstants();
+            Iterator<String> it = values.iterator();            
+            Integer offsetPhase = Integer.parseInt(it.next());
+            Integer offsetTime = Integer.parseInt(it.next());
+            
+            timing.setFieldValue("offset_phase", offsetPhase);
+            timing.setFieldValue("offset_time", offsetTime);
+            
+            SvtTimingConstantsCollection collection = new SvtTimingConstantsCollection();
+            collection.add(timing);
+            collections.add(collection);
+        }
+        return collections;
+    }
+     
+    /**
+     * Find a timing constants collection from offset phase and time. 
+     * <p>
+     * Each collection has a single object in it.
+     * 
+     * @param timingConstantsList the list of collections
+     * @param offsetPhase the offset phase
+     * @param offsetTime the offset time
+     * @return the matching collection or <code>null</code> if not found
+     */
+    private static SvtTimingConstantsCollection findCollection(List<SvtTimingConstantsCollection> timingConstantsList, int offsetPhase, int offsetTime) {
+        for (SvtTimingConstantsCollection collection : timingConstantsList) {
+            if (collection.find(offsetPhase, offsetTime) != null) {
+                return collection;
+            }
+        }
+        return null;
+    }
+}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

November 2017
August 2017
July 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013

ATOM RSS1 RSS2



LISTSERV.SLAC.STANFORD.EDU

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager

Privacy Notice, Security Notice and Terms of Use