[HepData-svn] r1863 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Wed Jun 10 13:49:11 BST 2015


Author: whalley
Date: Wed Jun 10 13:49:11 2015
New Revision: 1863

Log:
almost complete rewrite

Modified:
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java

Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java
==============================================================================
--- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	Wed Jun  3 18:34:31 2015	(r1862)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	Wed Jun 10 13:49:11 2015	(r1863)
@@ -20,13 +20,14 @@
 
     public static String format(Paper p) {
 	    StringBuffer s = new StringBuffer();
+        s.append("---\n"); 
         String s4="    ";
         String sp="  - ";
         if(p == null) return null;
         
-// _headerXML contains all the bibliographic type stuff at the beginning
+// _header contains all the bibliographic type stuff at the beginning
   
-        s.append(_headerXML(p));
+        s.append(_headerYAML(p));
 
 // here we deal with the extra resource are description/line files
 
@@ -36,12 +37,12 @@
         if(p.getSpiresId() != null ) { ids[0]=p.getSpiresId().toString(); } else { ids[0]=""; } 
         if(p.getInspireId() != null ) { ids[1]=p.getInspireId().toString(); } else { ids[1]=""; } 
         if(p.getRedId() != null ) { ids[2]=p.getRedId().toString(); } else { ids[2]=""; } 
-        
+    
         boolean first=true;
         for (int i=0; i<descs.length; i++){
             for(int j=0; j<ids.length; j++){
-                String descfile = "/home/hepdata/resource/" +ids[j] + "/" + descs[i];
-                String linkfile = "/home/hepdata/resource/" +ids[j] + "/" + links[i];
+                String descfile = "/home/whalley/resource/" +ids[j] + "/" + descs[i];
+                String linkfile = "/home/whalley/resource/" +ids[j] + "/" + links[i];
                 File testdesc = new File(descfile);
                 File testlink = new File(linkfile);
                 String desc="";
@@ -57,39 +58,60 @@
                         link=in.readLine();	    
                     } 
                     catch (IOException e){}
-                    if(first){ s.append("extras:\n"); }
+                    if(first){ 
+                        s.append("\n---\n");
+                  //      s.append("#This is Table 0\n");
+                        s.append("name: 'Table 0'\n");
+                        s.append("label: 'Additional resources for the whole record'\n");
+                        s.append("additional_resources:\n");
+                    }
                     first=false;
-                    s.append(sp+"description : "+desc+"\n");
-                    s.append(s4+"link : "+link+"\n");
+                    s.append(sp+"description : '"+desc+"'\n");
+                    s.append(s4+"link : '"+link+"'\n");
                 } 
             }
         }
 
 
 // next the insert.html file in the resource area
-        first=true;
-        for (int j=0; j<ids.length; j++){
-            String insertfile = "/home/hepdata/resource/" +ids[j] + "/insert.html";
-            File testinsert = new File(insertfile);
-            String insert="";
-            if(testinsert.exists()){
-                if(first) { s.append("inserts:\n"); }
-                first=false;
-                s.append(sp+"inserthtml: /resource/"+ids[j]+"insert.html\n");
-            }
-        }
+       first=true;
+       for (int j=0; j<ids.length; j++){
+           String insertfile = "/home/whalley/resource/" +ids[j] + "/insert.html";
+           File testinsert = new File(insertfile);
+           String insert="";
+           if(testinsert.exists()){
+               if(first) {
+                        s.append("---\n");
+                       // s.append("#This is Table 0\n");
+                        s.append("name: 'Table 0'\n");
+                        s.append("label: 'Additional resources for the whole record'\n");
+                        s.append("additional_resources:\n");
+               }
+               first=false;
+               s.append(sp+"inserthtml: '/resource/"+ids[j]+"/insert.html'\n");
+           }
+       }
+
 
 // and now the datasets
      
-        for (Dataset ds : p.getDatasets()){
-            if(ds.getId() == 1) {  s.append("datasets:\n"); } // first dataset, start the array
 
-            s.append(_metadataXML(ds));  // dataset information in _metadataXML
 
+
+        for (Dataset ds : p.getDatasets()){
+            s.append("\n---\n"); 
+            s.append(_metadataYAML(ds));  // dataset information in _metadata
+            s.append("data_file: data"+ds.getId()+".yaml\n");
+            s.append("additional_resources:\n");
+        }
+        for (Dataset ds : p.getDatasets()){
+            s.append("---\n"); 
+            s.append("#This is Data "+ds.getId()+"\n");
+            s.append(_dataYAML(ds));  // dataset information in _data
         }
  
 // finally finish off (at present does nothing!
-        s.append(_footerXML());
+        s.append(_footerYAML());
  
 // then  write it all out.
         return s.toString();
@@ -102,60 +124,55 @@
     }
 
 
-    public static String _headerXML(Paper p) {
+    public static String _headerYAML(Paper p) {
         String s4="    ";
         String sp="  - ";
         StringBuffer s = new StringBuffer();
-        s.append("inspireid: "+p.getInspireId()+"\n");
-        if(p.getSpiresId()!=null) { s.append("spires: "+p.getSpiresId()+"\n"); }
-        if(p.getArchive()!=null) { s.append("archive: "+p.getArchive().replaceAll("ARXIV","arXiv")+"\n"); }
         if(p.getRedId()!=null) { s.append("durham: "+p.getRedId()+"\n"); }
-        if(p.getCdsId()!=null) { s.append("cds: "+p.getCdsId()+"\n"); }
-        s.append("dateupdated: "+p.getDateUpdated()+"\n");
-        if(p.getTitle()!=null) { s.append("title: "+p.getTitle()+"\n"); }
-        if(p.getAuthors().size()>0){
-            s.append("authors:\n");
-            for (String author : p.getAuthors()){
-                s.append(sp+"author: "+author+"\n");
-            }
-        }
+        s.append("dateupdated: '"+p.getDateUpdated()+"'\n");
         if(p.getReferences().size()>0){
-            s.append("references:\n");
+            s.append("references: # additional references (e.g. experiment TWiki page for analysis)\n");
             for (Reference r : p.getReferences()){
-                s.append(sp+"{description: "+r.getDescription()+", ");
-                s.append("date: "+r.getDate()+", ");
-                s.append("type: "+r.getType()+"}");
-                s.append("\n");
+                if(r.getDescription().startsWith("http")){
+                    s.append(sp+"'"+r.getDescription());
+                    s.append("'\n");
+                }
             }
         }
         if(p.getModifications().size()>0){
-            s.append("modifications:\n");
+            s.append("modifications: # what, by whom and when encoded or changed\n");
             for (Modification m : p.getModifications()){
-                s.append(sp+"{action: "+m.getAction()+", ");
-                s.append("modifier: "+m.getModifier()+", ");
-                s.append("date: "+m.getComment()+"}");
-                s.append("\n");
+                s.append(sp+"'"+m.getAction()+" by ");
+                s.append(m.getModifier()+" on ");
+                s.append(m.getComment());
+                s.append("'\n");
             }
         }
 
-
-        s.append("collaboration: "+p.getInformalName()+"\n");
-        s.append("experiment: "+p.getExperimentName()+"\n");
+        int count=0;
         if(p.getComments().size()>0){
-            s.append("comments:\n");
-            for (String comment : p.getComments()){ 
-                s.append(sp+"comment: "+comment+"\n");
+            s.append("comment: | # preserve newlines\n");
+            boolean first=true;
+            for (String comment : p.getComments()){
+                if(first){ 
+                    s.append(s4+"'"+comment.replaceAll("\n","\n    "));
+                    first=false;
+                } else {
+                    s.append(s4+comment.replaceAll("\n","\n    "));
+                }
+                if(++count==p.getComments().size())  { s.append("'"); }
+                s.append("\n");
             }
         }
         return s.toString();
     }
 
-    public static String _footerXML() {
+    public static String _footerYAML() {
         StringBuffer s = new StringBuffer();
         return s.toString();
     }
 
-    public static String _metadataXML(Dataset ds){  
+    public static String _metadataYAML(Dataset ds){  
         String s4="    ";
         String sp="  - ";
         StringBuffer s = new StringBuffer();
@@ -164,76 +181,309 @@
         String location = "";
         Paper p = ds.getPaper();
        
+        s.append("name: 'Table "+ds.getId()+"'\n");
         for (String ct : ds.getComments()){
             if(ct.startsWith("Location:")) { 
                 location = ct.replaceFirst("Location:","");
-                s.append(sp+"label: "+ct.replaceFirst("Location:","Data from")+" from: "+p.getTitle()+"\n"); 
+            //    s.append("label: "+ct.replaceFirst("Location:","Data from")+" from: "+p.getTitle()+"\n"); 
+                s.append("label: '"+ct.replaceFirst("Location:","Data from")+"'\n"); 
             }
         }
 
+        boolean first=true;
+        int count=0;
         for (String ct : ds.getComments()){
+            count++;
             if(!ct.startsWith("Location:")) {
-                s.append(s4+"comment: "+ct.replaceFirst("VERBATIM","").replaceAll(">",">").replaceAll("<","<")+"\n");
+                s.append("description: |\n");
+                if(first){
+                    s.append(s4+"'"+ct.replaceFirst("VERBATIM","").replaceAll(">",">").replaceAll("<","<").replaceAll("\n","\n    "));
+                    first=false;
+                } else {
+                    s.append(s4+ct.replaceFirst("VERBATIM","").replaceAll(">",">").replaceAll("<","<").replaceAll("\n","\n    "));
+                }
+                if(count==ds.getComments().size()-1)  { s.append("'"); }
+                s.append("\n");
             }
+            else{++count;}
         }
-
-        for (String dsreac : ds.getDsReactions()){
-            s.append(s4+"reackey: "+dsreac+"\n");
-        }
-        for (String dsobs : ds.getDsObservables()){
-            s.append(s4+"obskey: "+dsobs+"\n");
-        }
-        for (String dsplab : ds.getDsPlabs()){
-            s.append(s4+"plabkey: \""+dsplab+"\n");
+        s.append("keywords:\n");
+        if(ds.getDsReactions().size() > 0) {
+            s.append(sp+"{ name: reactions, values: [");
+            int nr=0;
+            for (String dsreac : ds.getDsReactions()){
+                s.append("'"+dsreac+"'");
+                if(++nr==ds.getDsReactions().size()){s.append("] }\n"); } 
+                else {s.append(", ");}  
+            }
+        }
+        if(ds.getDsObservables().size() > 0) {
+            s.append(sp+"{ name: observables, values: [");
+            int no=0;
+            for (String dsobs : ds.getDsObservables()){
+                s.append("'"+dsobs+"'");
+                if(++no==ds.getDsObservables().size()){s.append("] }\n"); } 
+                else {s.append(", ");}  
+           }
+       }
+        if(ds.getDsPlabs().size() > 0) {
+            s.append(sp+"{ name: plabs, values: [");
+            int np=0;
+            for (String dsplab : ds.getDsPlabs()){
+                s.append("'"+dsplab+"'");
+                if(++np==ds.getDsPlabs().size()){s.append("] }\n"); } 
+                else {s.append(", ");}  
+            }
         }
 
-        s.append(s4+"xaxes:\n");
+        return s.toString();
+    }
+
+    public static String _dataYAML(Dataset ds){  
+        String s4="    ";
+        String sp="  - ";
+        StringBuffer s = new StringBuffer();
+        int idbase=9000000;
+        int id=idbase+ds.getId();
+        String location = "";
+        Paper p = ds.getPaper();
+       
+        s.append("xaxes:\n");
         for (XAxis xax : ds.getXAxes()){
-            s.append(s4+sp+"xheader: "+xax.getHeader()+"\n");
-            s.append(s4+s4+"bins:\n");
+            String name=xax.getHeader();
+            String unit="";
+            if(xax.getHeader().contains(" IN ")){
+                name=xax.getHeader().substring(0,xax.getHeader().indexOf(" IN ")).trim();
+                unit=xax.getHeader().substring(xax.getHeader().indexOf(" IN ")+4).trim();
+            }
+            s.append(sp+"header: {name: '"+name+"'");
+            if(!unit.equals("")){ s.append(", unit: '"+unit+"'"); }
+            s.append("}\n");
+            s.append(s4+"bins:\n");
             for (Bin bin : xax.getBins()){
-                s.append(s4+s4+sp+"{id: "+bin.getId()+", ");
-                if(bin.getDescription() != null ) { s.append("description: "+bin.getDescription()+", "); }
-                s.append("relation: "+bin.getRelation()+", ");
-                s.append("focus: "+bin.getFocus()+", ");
-                s.append("high: "+bin.getHighValue()+", ");
-                s.append("low: "+bin.getLowValue()+"");
-                if(bin.getId() < xax.getBins().size()) {s.append("}");}
+                boolean first=true;
+                s.append(s4+sp+"{");
+       //         if(bin.getId() != null ) { 
+       //             s.append("id: "+bin.getId()); 
+       //             first=false; 
+       //         }
+                
+                boolean asymmfocus = false;
+                if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) {
+                    double diff = bin.getFocus() - (bin.getLowValue() + bin.getHighValue()) / 2.0;
+                    if (Math.abs(diff/bin.getFocus()) > 1E-6) { asymmfocus = true; }
+                }
+                boolean haswidth = false;
+                if (bin.getLowValue() != null && bin.getHighValue() != null) {
+                    double diff = bin.getHighValue() - bin.getLowValue();
+                    double mean = (bin.getLowValue() + bin.getHighValue()) / 2.0;
+                    if((Math.abs(diff/mean) > 1E-6) || (mean == 0.0 && Math.abs(diff) > 1E-6)){ haswidth = true;}
+                }
+
+                if(bin.getFocus() != null) {
+                    if(asymmfocus || !haswidth) { 
+                        if(!first) {s.append(", "); } 
+                        if(bin.getFocusLength()!=null) { 
+                            s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); 
+                        } else {
+                             s.append("value: "+bin.getFocus()); 
+                        }
+                       first=false; 
+                    }
+               }
+               if(bin.getLowValue() != null && haswidth ) { 
+                    if(!first) { s.append(", "); } 
+                     if(bin.getLowValueLength()!=null) { 
+                        s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); 
+                    } else {
+                        s.append("high: "+bin.getLowValue()); 
+                    }
+                    first=false; 
+                }
+               if(bin.getHighValue() != null && haswidth ) { 
+                    if(!first) {s.append(", "); } 
+                    if(bin.getHighValueLength()!=null) { 
+                        s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); 
+                    } else {
+                        s.append("high: "+bin.getHighValue()); 
+                    }
+                    first=false; 
+                }
+                if(bin.getRelation() != null && bin.getRelation() != Relation.EQUALS ) { 
+                    if(!first) { s.append(", "); } 
+                    s.append("relation: "+bin.getRelation()); 
+                    first=false; 
+                }
+                if(bin.getDescription() != null ) {  
+                    if(!first) { s.append(", "); } 
+                    s.append("description: '"+bin.getDescription()+"'");  
+                    first=false;
+                }
+                if(bin.getId() < xax.getBins().size()) {
+                    s.append("}");
+                }
                 else{s.append("}");}  
                 s.append("\n");
             }
         }
 
-        s.append(s4+"yaxes:\n");
+        s.append("yaxes:\n");
+
+        int npoints=ds.getNumPoints();
+        
         for (YAxis yax : ds.getYAxes()){
-            s.append(s4+sp+"yheader: "+yax.getHeader()+"\n");
+            String name=yax.getHeader();
+            String unit="";
+            if(yax.getHeader().contains(" IN ")){
+                name=yax.getHeader().substring(0,yax.getHeader().indexOf(" IN ")).trim();
+                unit=yax.getHeader().substring(yax.getHeader().indexOf(" IN ")+4).trim();
+            }
+            s.append(sp+"header: {name: '"+name+"'");
+            if(!unit.equals("")){ s.append(", unit: '"+unit+"'"); }
+            s.append("}\n");
             boolean first=true;
             for (Property prop : yax.getProperties()){
-                if(first) { s.append(s4+s4+"qualifiers:\n"); }
+                if(first) { s.append(s4+"qualifiers:\n"); }
                 first=false;
-                s.append(s4+s4+sp+"qualifier: "+prop.getFull()+"\n");
+
+                s.append(s4+sp+"{type: "+prop.getName());
+
+                if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
+                    s.append(", value: '");
+                }
+                if (prop.getFocus() != null){
+                    s.append(prop.getFocus());
+                }
+                
+                if (prop.getLowValue() != null && prop.getHighValue() != null &&
+                    prop.getLowValue().equals(prop.getHighValue())) {
+                    s.append(prop.getLowValue());
+                } else {
+                    if(prop.getFocus() != null) { s.append(" ("); }
+                    s.append(prop.getLowValue());
+                    s.append("-");
+                    s.append(prop.getHighValue());
+                    if(prop.getFocus() != null) { s.append(")"); }
+                }
+                if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){
+                    s.append("'");
+                }
+                    
+                if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) {
+                   s.append(", unit: '");
+                   s.append(prop.getUnit().toString()+"'");
+                }
+                  
+                s.append("}\n");
             }
             for (String comment : yax.getComments()){
-                if(first) { s.append(s4+s4+"qualifiers:\n"); }
+                if(first) { s.append(s4+"qualifiers:\n"); }
                 first=false;
-                s.append(s4+s4+sp+"qualifier: "+comment+"\n");
+                unit="";
+                String type=comment.substring(0,comment.indexOf(":")-1).trim();
+                String value=comment.substring(comment.indexOf(":")+1).trim();
+                if(type.contains(" IN ")){
+                    unit=type.substring(type.indexOf(" IN ")+4).trim();
+                    type=type.substring(0,type.indexOf(" IN ")).trim();
+                }
+                s.append(s4+sp+"{type: "+type);
+                s.append(", value: '"+ value+"'");
+                if(!unit.equals("")){
+                    s.append(", unit: '"+ unit+"'");
+                }
+                s.append("}\n");
+            }
+
+            for(Uncertainty error : ds.getErrors()){
+                if(error.getPlus()==0.0){
+                    s.append(s4+sp+"{type: sys, value: '"+error.getComment()+"'}\n");
+                }
             }
-            s.append(s4+s4+"points:\n");
-            for (Point point : yax.getPoints()){
-                s.append(s4+s4+sp+"id: "+point.getId()+"\n");
-                s.append(s4+s4+s4+"value: "+point.getValue()+"\n");
-                if(point.getErrors().size()>0){
-                   s.append(s4+s4+s4+"errors:\n");
-                   for(PointError error : point.getErrors()){
-                        s.append(s4+s4+s4+sp+"{plus: "+error.getPlus()+", ");
-                        s.append("minus: \""+error.getMinus()+", ");
-                        s.append("norm: "+error.getNormType()+", ");
-                        s.append("type: "+error.getSourceType()+", ");
-                        s.append("comment: "+error.getComment()+"}\n");
+            for(Uncertainty error : yax.getErrors()){
+                if(error.getPlus()==0.0){
+                    s.append(s4+sp+"{type: sys, value: '"+error.getComment()+"'}\n");
+                }
+            }
+
+            s.append(s4+"points:\n");
+
+            for (int ip=1; ip<npoints+1; ip++){
+                Point point = yax.getPoint(ip);
+                if(point==null){
+                 //   s.append(s4+sp+"id: "+ip+"\n");
+                 //   s.append(s4+s4+"value: '-'\n");
+                    s.append(s4+sp+"value: '-'\n");
+                }
+                else{            
+                //    s.append(s4+sp+"id: "+point.getId()+"\n");
+                //    s.append(s4+s4+"value: ");
+                    s.append(s4+sp+"value: ");
+                    if(point.getRelation() != Relation.EQUALS){
+                        s.append(point.getRelation());
                     }
+//                    s.append(point.getValue()+" "+point.getValueLength()+"\n");
+                     s.append(point.getValue()+"\n");
+                   if(point.getErrors().size()>0){
+                        s.append(s4+s4+"errors:\n");
+                        for(Uncertainty error : point.getErrors()){
+                           s.append(_formatError(error,"point"));
+                       }
+                    }
+                    if(yax.getErrors().size()>0){
+                        for(Uncertainty error : yax.getErrors()){
+                           if(error.getPlus()!=0.0) s.append(_formatError(error,"axis"));
+                       }
+                   }
+                   if(ds.getErrors().size()>0){
+                        for(Uncertainty error : ds.getErrors()){
+                           if(error.getPlus()!=0.0) s.append(_formatError(error,"dataset"));
+                       }
+                   }
                 }
             }
         }
         return s.toString();
     }
+    
+    public static String _formatError(Uncertainty error, String which) {
+        String s4="    ";
+        String sp="  - ";
+        StringBuffer s = new StringBuffer();
+        Double test = error.getMinus();
+        if(which.equals("point")) test=-test;
+        if(error.getPlus()>=0.0 && (error.getPlus().equals(test))){
+            s.append(s4+s4+sp+"{symerror: "); 
+            s.append(error.getPlus());
+            if(error.getNormType()==ErrorNorm.PCT){  s.append("%"); }
+         }
+         else{
+             s.append(s4+s4+sp+"{asymerror: ");
+             s.append("{plus: "+error.getPlus());
+             if(error.getNormType()==ErrorNorm.PCT){  s.append("%"); }
+             s.append(", minus: "+error.getMinus());
+             if(error.getNormType()==ErrorNorm.PCT){  s.append("%"); }
+             s.append("}");
+         }
+         if(error.getSourceType()==ErrorSource.STAT) s.append(", label: stat");
+         else if(error.getSourceType()==ErrorSource.SYS) s.append(", label: sys");
+         else if(error.getSourceType()==ErrorSource.TOTAL) s.append(", label: total");
+         if(!error.getComment().equals("")){ s.append(","+error.getComment()); }
+         s.append("}\n");
+         return s.toString();
+   }
+
+   public static String forms(Double value, int after) {
+   
+       String form = "0.";
+       for(int i=0;  i<after; ++i){
+           form=form.concat("0");
+       }
+       DecimalFormat myFormatter = new DecimalFormat(form);
+           String sval = "Cannot format this";
+           if(value instanceof Double) {
+               sval = myFormatter.format(value);
+           }    
+           if(sval.endsWith(".")) { sval=sval.substring(0,sval.length()-1); }
+           return sval;
+   }
 }


More information about the HepData-svn mailing list