Index: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java
===================================================================
--- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	(revision 1863)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java	(revision 1864)
@@ -1,489 +1,489 @@
 package cedar.hepdata.formats;
 
 import cedar.hepdata.model.*;
 import cedar.hepdata.util.*;
 import cedar.hepdata.xml.*;
 import cedar.hepdata.db.*;
 import cedar.hepdata.webapp.components.*;
 
 
 import java.util.*;
 import java.text.*;
 import java.io.File;
 import java.io.*;
 
 import org.antlr.stringtemplate.*;
 import com.Ostermiller.util.SignificantFigures;
 
 
 public class YamlFormatter {
 
     public static String format(Paper p) {
 	    StringBuffer s = new StringBuffer();
         s.append("---\n"); 
         String s4="    ";
         String sp="  - ";
         if(p == null) return null;
         
 // _header contains all the bibliographic type stuff at the beginning
   
         s.append(_headerYAML(p));
 
 // here we deal with the extra resource are description/line files
 
         String[] descs = { "description1" , "description2" , "description3" , "description4" };
         String[] links = { "link1" , "link2" , "link3" , "link4" };
         String[] ids = new String[3];
         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/whalley/resource/" +ids[j] + "/" + descs[i];
-                String linkfile = "/home/whalley/resource/" +ids[j] + "/" + links[i];
+                String descfile = "/home/hepdata/resource/" +ids[j] + "/" + descs[i];
+                String linkfile = "/home/hepdata/resource/" +ids[j] + "/" + links[i];
                 File testdesc = new File(descfile);
                 File testlink = new File(linkfile);
                 String desc="";
                 String link="";
                 if(testdesc.exists() && testlink.exists() ){
                     try {
                         BufferedReader in = new BufferedReader(new FileReader(descfile));
                         desc=in.readLine();	    
                     } 
                     catch (IOException e){}
                     try {
                         BufferedReader in = new BufferedReader(new FileReader(linkfile));
                         link=in.readLine();	    
                     } 
                     catch (IOException e){}
                     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");
                 } 
             }
         }
 
 
 // next the insert.html file in the resource area
        first=true;
        for (int j=0; j<ids.length; j++){
-           String insertfile = "/home/whalley/resource/" +ids[j] + "/insert.html";
+           String insertfile = "/home/hepdata/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()){
             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(_footerYAML());
  
 // then  write it all out.
         return s.toString();
     }
  
     public static String _getTimestamp(){
         DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
         Date date = new Date();
         return dateFormat.format(date);
     }
 
 
     public static String _headerYAML(Paper p) {
         String s4="    ";
         String sp="  - ";
         StringBuffer s = new StringBuffer();
         if(p.getRedId()!=null) { s.append("durham: "+p.getRedId()+"\n"); }
         s.append("dateupdated: '"+p.getDateUpdated()+"'\n");
         if(p.getReferences().size()>0){
             s.append("references: # additional references (e.g. experiment TWiki page for analysis)\n");
             for (Reference r : p.getReferences()){
                 if(r.getDescription().startsWith("http")){
                     s.append(sp+"'"+r.getDescription());
                     s.append("'\n");
                 }
             }
         }
         if(p.getModifications().size()>0){
             s.append("modifications: # what, by whom and when encoded or changed\n");
             for (Modification m : p.getModifications()){
                 s.append(sp+"'"+m.getAction()+" by ");
                 s.append(m.getModifier()+" on ");
                 s.append(m.getComment());
                 s.append("'\n");
             }
         }
 
         int count=0;
         if(p.getComments().size()>0){
             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 _footerYAML() {
         StringBuffer s = new StringBuffer();
         return s.toString();
     }
 
     public static String _metadataYAML(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("name: 'Table "+ds.getId()+"'\n");
         for (String ct : ds.getComments()){
             if(ct.startsWith("Location:")) { 
                 location = ct.replaceFirst("Location:","");
             //    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("description: |\n");
                 if(first){
                     s.append(s4+"'"+ct.replaceFirst("VERBATIM","").replaceAll(">","&gt;").replaceAll("<","&lt;").replaceAll("\n","\n    "));
                     first=false;
                 } else {
                     s.append(s4+ct.replaceFirst("VERBATIM","").replaceAll(">","&gt;").replaceAll("<","&lt;").replaceAll("\n","\n    "));
                 }
                 if(count==ds.getComments().size()-1)  { s.append("'"); }
                 s.append("\n");
             }
             else{++count;}
         }
         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(", ");}  
             }
         }
 
         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()){
             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()){
                 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("yaxes:\n");
 
         int npoints=ds.getNumPoints();
         
         for (YAxis yax : ds.getYAxes()){
             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+"qualifiers:\n"); }
                 first=false;
 
                 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+"qualifiers:\n"); }
                 first=false;
                 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");
                 }
             }
             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;
    }
 }