|
[HepData-svn] r1872 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formatsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgFri Jun 19 17:09:27 BST 2015
Author: watt Date: Fri Jun 19 17:09:27 2015 New Revision: 1872 Log: Changes to field names in YAML formatter discussed by Mike and Eamonn 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 17 19:35:30 2015 (r1871) +++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java Fri Jun 19 17:09:27 2015 (r1872) @@ -6,7 +6,6 @@ import cedar.hepdata.db.*; import cedar.hepdata.webapp.components.*; - import java.util.*; import java.text.*; import java.io.File; @@ -18,85 +17,19 @@ public class YamlFormatter { + public static String format(Paper p) { - StringBuffer s = new StringBuffer(); + 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 + // _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/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/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 - - - + // and now the datasets for (Dataset ds : p.getDatasets()){ s.append("\n---\n"); @@ -105,17 +38,18 @@ s.append("additional_resources:\n"); } for (Dataset ds : p.getDatasets()){ - s.append("---\n"); + s.append("\n---\n"); s.append("# This is Data "+ds.getId()+"\n"); s.append(_dataYAML(ds)); // dataset information in _data } -// finally finish off (at present does nothing! + // finally finish off (at present does nothing! s.append(_footerYAML()); -// then write it all out. + // then write it all out. return s.toString(); } + public static String _getTimestamp(){ DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); @@ -128,30 +62,22 @@ String s4=" "; String sp=" - "; StringBuffer s = new StringBuffer(); - if(p.getRedId()!=null) { s.append("durham: "+p.getRedId()+"\n"); } + s.append("record_ids: # optional\n"); + if(p.getInspireId()!=null) { s.append(sp+"{type: inspire, id: "+p.getInspireId()+"}\n"); } + if(p.getRedId()!=null) { s.append(sp+"{type: red, id: "+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.getHaveRivet()) s.append(sp+"'http://rivet.hepforge.org/analyses#"+p.getRivetName()+"'\n"); - } if(p.getModifications().size()>0){ s.append("modifications: # what, by whom and when encoded or changed\n"); int mcount=0; for (Modification m : p.getModifications()){ mcount++; if (m.getAction()!=null) { - s.append(sp+"{ action: '"+m.getAction()+"'"); + s.append(sp+"{action: '"+m.getAction()+"'"); } else { - if (mcount==1) { s.append(sp+"{ action: 'Encoded'"); } - else { s.append(sp+"{ action: 'Modified'"); } + if (mcount==1) { s.append(sp+"{action: 'Encoded'"); } + else { s.append(sp+"{action: 'Modified'"); } } - s.append(", date: '"+m.getComment()+"', who: '"+m.getModifier()+"' }\n"); + s.append(", date: '"+m.getComment()+"', who: '"+m.getModifier()+"'}\n"); } } @@ -168,14 +94,70 @@ s.append("\n"); } } + + s.append("\nadditional_resources:\n"); + if(p.getReferences().size()>0){ + for (Reference r : p.getReferences()){ + if(r.getDescription().startsWith("http")){ + s.append(sp+"{location: '"+r.getDescription()+"', description: \"experiment TWiki page for analysis\"}\n"); + } + } + if (p.getHaveRivet()) { s.append(sp+"{location: 'http://rivet.hepforge.org/analyses#"+p.getRivetName()+"', description: \"Rivet analysis\"}\n"); } + } + + // 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]=""; } + + 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]; + 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="http://hepdata.cedar.ac.uk"+in.readLine(); + } + catch (IOException e){} + s.append(sp+"{location: '"+link+"', description: '"+desc+"'\n"); + } + } + } + + // next the insert.html file in the resource area + 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()){ + s.append(sp+"{location: 'http://hepdata.cedar.ac.uk/resource/"+ids[j]+"/insert.html', description: \"insert.html\"\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=" - "; @@ -189,7 +171,7 @@ 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")+" from: "+p.getTitle()+"\n"); s.append("label: '"+ct.replaceFirst("Location:","Data from")+"'\n"); } } @@ -209,34 +191,34 @@ } s.append("keywords:\n"); if(ds.getDsReactions().size() > 0) { - s.append(sp+"{ name: reactions, values: ["); + 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"); } + if(++nr==ds.getDsReactions().size()){s.append("]}\n"); } else {s.append(", ");} } } if(ds.getDsObservables().size() > 0) { - s.append(sp+"{ name: observables, values: ["); + 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"); } + if(++no==ds.getDsObservables().size()){s.append("]}\n"); } else {s.append(", ");} - } - } + } + } if(ds.getDsPlabs().size() > 0) { - s.append(sp+"{ name: plabs, values: ["); + 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"); } + if(++np==ds.getDsPlabs().size()){s.append("]}\n"); } else {s.append(", ");} } } - - + + Double ecmlow=99999.0; Double ecmhigh=-99999.0; boolean haveecm=false; @@ -256,7 +238,7 @@ if(prop.getHighValue()>ecmhigh) ecmhigh=prop.getHighValue(); haveecm=true; } - } + } } } if(!haveecm){ @@ -282,14 +264,15 @@ } if(haveecm){ - s.append(sp+"{ name: cmenergies, values: [" + ecmlow); + s.append(sp+"{name: cmenergies, values: [" + ecmlow); if(!ecmlow.equals(ecmhigh)) s.append("-"+ecmhigh); - s.append("] }\n"); + s.append("]}\n"); } return s.toString(); } + public static String _dataYAML(Dataset ds){ String s4=" "; String sp=" - "; @@ -299,7 +282,7 @@ String location = ""; Paper p = ds.getPaper(); - s.append("xaxes:\n"); + s.append("independent_variables:\n"); for (XAxis xax : ds.getXAxes()){ String name=xax.getHeader(); String unit=""; @@ -310,14 +293,14 @@ s.append(sp+"header: {name: '"+name+"'"); if(!unit.equals("")){ s.append(", units: '"+unit+"'"); } s.append("}\n"); - s.append(s4+"bins:\n"); + s.append(s4+"values:\n"); for (Bin bin : xax.getBins()){ boolean first=true; s.append(s4+sp+"{"); - // if(bin.getId() != null ) { - // s.append("id: "+bin.getId()); - // first=false; - // } + // if(bin.getId() != null ) { + // s.append("id: "+bin.getId()); + // first=false; + // } boolean asymmfocus = false; if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) { @@ -337,21 +320,21 @@ if(bin.getFocusLength()!=null) { s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); } else { - s.append("value: "+bin.getFocus()); + s.append("value: "+bin.getFocus()); } - first=false; + first=false; } - } - if(bin.getLowValue() != null && haswidth ) { + } + if(bin.getLowValue() != null && haswidth ) { if(!first) { s.append(", "); } - if(bin.getLowValueLength()!=null) { + if(bin.getLowValueLength()!=null) { s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); } else { s.append("low: "+bin.getLowValue()); } first=false; } - if(bin.getHighValue() != null && haswidth ) { + if(bin.getHighValue() != null && haswidth ) { if(!first) {s.append(", "); } if(bin.getHighValueLength()!=null) { s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); @@ -378,7 +361,7 @@ } } - s.append("yaxes:\n"); + s.append("dependent_variables:\n"); int npoints=ds.getNumPoints(); @@ -421,8 +404,8 @@ } if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) { - s.append(", units: '"); - s.append(prop.getUnit().toString()+"'"); + s.append(", units: '"); + s.append(prop.getUnit().toString()+"'"); } s.append("}\n"); @@ -456,46 +439,47 @@ } } - s.append(s4+"points:\n"); + s.append(s4+"values:\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+"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+"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(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")); - } + 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){ + 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")); - } - } + 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=" - "; @@ -506,38 +490,41 @@ 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.getSourceType()==ErrorSource.STAT || error.getSourceType()==ErrorSource.SYS || error.getSourceType()==ErrorSource.TOTAL) { - if(!error.getComment().equals("")) s.append(","+error.getComment()); - s.append("'"); - } - s.append("}\n"); - return s.toString(); - } + } + 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.getSourceType()==ErrorSource.STAT || error.getSourceType()==ErrorSource.SYS || error.getSourceType()==ErrorSource.TOTAL) { + if(!error.getComment().equals("")) s.append(","+error.getComment()); + s.append("'"); + } + s.append("}\n"); + return s.toString(); + } + - public static String forms(Double value, int after) { + 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; - } + 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 |