001    package cnslab.cnsnetwork;
002
003    import java.awt.BasicStroke;
004    import java.awt.Color;
005    import java.awt.Font;
006    import java.awt.Rectangle;
007    import java.awt.event.ActionEvent;
008    import java.awt.event.ActionListener;
009    import java.awt.geom.Rectangle2D;
010    import java.io.File;
011    import java.util.ArrayList;
012    import java.util.Properties;
013
014    import javax.swing.JButton;
015    import javax.swing.JCheckBox;
016    import javax.swing.JPanel;
017
018    import org.freehep.graphics2d.VectorGraphics;
019    import org.freehep.graphicsio.ps.PSGraphics2D;
020    import org.jfree.chart.ChartFactory;
021    import org.jfree.chart.ChartPanel;
022    import org.jfree.chart.JFreeChart;
023    import org.jfree.chart.axis.AxisLocation;
024    import org.jfree.chart.axis.CategoryAxis;
025    import org.jfree.chart.axis.NumberAxis;
026    import org.jfree.chart.block.BlockBorder;
027    import org.jfree.chart.plot.CategoryPlot;
028    import org.jfree.chart.plot.CombinedDomainXYPlot;
029    import org.jfree.chart.plot.PlotOrientation;
030    import org.jfree.chart.plot.XYPlot;
031    import org.jfree.chart.renderer.AbstractRenderer;
032    import org.jfree.chart.renderer.GrayPaintScale;
033    import org.jfree.chart.renderer.category.ScatterRenderer;
034    import org.jfree.chart.renderer.xy.VectorRenderer;
035    import org.jfree.chart.renderer.xy.XYBlockRenderer;
036    import org.jfree.chart.renderer.xy.XYErrorRenderer;
037    import org.jfree.chart.renderer.xy.XYItemRenderer;
038    import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
039    import org.jfree.chart.title.PaintScaleLegend;
040    import org.jfree.data.Range;
041    import org.jfree.data.statistics.DefaultMultiValueCategoryDataset;
042    import org.jfree.data.statistics.MultiValueCategoryDataset;
043    import org.jfree.data.xy.DefaultXYZDataset;
044    import org.jfree.data.xy.VectorSeries;
045    import org.jfree.data.xy.VectorSeriesCollection;
046    import org.jfree.data.xy.XYDataset;
047    import org.jfree.data.xy.XYSeries;
048    import org.jfree.data.xy.XYSeriesCollection;
049    import org.jfree.data.xy.YIntervalSeries;
050    import org.jfree.data.xy.YIntervalSeriesCollection;
051    import org.jfree.ui.ApplicationFrame;
052    import org.jfree.ui.RectangleEdge;
053    import org.jfree.ui.RectangleInsets;
054    import org.jfree.ui.RefineryUtilities;
055
056    import ucar.ma2.ArrayChar;
057    import ucar.ma2.ArrayDouble;
058    import ucar.nc2.NetcdfFile;
059    import ucar.nc2.Variable;
060    
061    // Remarked out when switched for Log4j to Logback for logging.
062    // This log4j log file might have been initialized but not used.      
063    // import org.apache.log4j.Layout;
064    // import org.apache.log4j.FileAppender;
065    // import org.apache.log4j.SimpleLayout;
066    // import org.apache.log4j.net.SyslogAppender;
067
068    /***********************************************************************
069    * Class to plot the recorded data by using Java Free chart.
070    *
071    * @version
072    *   $Date: 2012-08-04 13:43:22 -0500 (Sat, 04 Aug 2012) $
073    *   $Rev: 104 $
074    *   $Author: croft $
075    * @author
076    *   Yi Dong
077    * @author
078    *   David Wallace Croft, M.Sc.
079    * @author
080    *   Jeremy Cohen
081    ***********************************************************************/
082    public class  PlotResult
083      extends ApplicationFrame
084      implements ActionListener
085    ////////////////////////////////////////////////////////////////////////
086    ////////////////////////////////////////////////////////////////////////
087    {
088    
089    private static final long  serialVersionUID = 1L;
090    
091    private static final String SEPARATOR = "_"; // See RecWriter.
092    
093    public int nowValue=-1;
094
095    /**
096     * number of single unit ele
097     */
098    public int suSeries; //number of single unit ele
099//  public  static int muSeries; //number of mu unit ele 
100
101    /**
102     * number of sub exps
103     */
104    public  static int numExp; //number of sub exps
105    /**
106     * number of field electrode
107     */
108    public  static int numField; //number of field electrode 
109    /**
110     * number of vector electrode 
111     */
112    public  static int numVector; //number of vector electrode 
113    /**
114     * number of intracellular electrodes
115     */
116    public static int numIntra;
117    /**
118     * xEdge length
119     */
120    public  static int xEdge; //xEdge length;
121    /**
122     * yEdge length;
123     */
124    public  static int yEdge; //yEdge length;
125    /**
126     * the names of subexperiments 
127     */
128    public  static ArrayChar.D2 subexpNames;
129
130    public int picture=0;
131    public int currExp;
132    public int currField;
133    public int currIntra;
134
135    /**
136     * single unit names;
137     */
138    public  ArrayChar.D2 suNames; //single unit names;
139    /**
140     *  field electrodes names
141     */
142    public  ArrayChar.D2 fieldNames; //single unit names;
143    /**
144     *  vector electrodes names
145     */
146    public  ArrayChar.D2 vectorNames; //single unit names;
147    /**
148     * intracellular electrodes names
149     */
150    public ArrayChar.D2 intraNames;
151
152    /**
153     * bin, x, y
154     */
155    public double dataField [][][]; // bin, x, y
156    public DefaultXYZDataset show;
157    public double fieldMax;
158    public int currFrame=0;
159    public double fieldBinSize;
160    public AbstractRenderer suRender;
161    JFreeChart chartsave;
162    public String titleField;
163    public double xUnit=10.0;
164    public double yUnit=10.0;
165
166
167    // vector plots
168    private VectorSeries [] vectorDataField ;
169    private double vectorBinSize;
170    VectorSeriesCollection vectorxydataset;
171    
172    // intra plots
173    private double intraBinSize;
174    private XYSeriesCollection intraVoltageData;
175    private XYSeriesCollection intraCurrentData;
176    
177    public static NetcdfFile ncfile;
178
179//  private AbstractRenderer muRender;
180
181    ////////////////////////////////////////////////////////////////////////
182    ////////////////////////////////////////////////////////////////////////
183
184    public static void main(String [] args) throws Exception
185    ////////////////////////////////////////////////////////////////////////
186    {
187      if(args.length <1)
188      {
189        init();
190      }
191      else
192      {
193        init(args[0]);
194      }
195//    System.out.println(numField);
196//    ncfile.close();
197//    new PlotResult().suPlot();  
198//    new PlotResult().muPlot();  
199//    new PlotResult().exportFile();  
200      suPlot();
201      muPlot();
202      fieldPlot();
203      vectorPlot();
204      intraPlot();
205//    exportFile();
206      stop();
207
208    }
209
210    ////////////////////////////////////////////////////////////////////////
211    ////////////////////////////////////////////////////////////////////////
212    
213    /**
214     * {@inheritDoc}
215     * @see ActionListener#actionPerformed(ActionEvent)
216     */
217    public void actionPerformed(ActionEvent e)
218    ////////////////////////////////////////////////////////////////////////
219    {
220      if(e.getActionCommand().equals("F_next"))
221      {
222        show.removeSeries(new Integer(currFrame));
223        currFrame++;
224        if(currFrame == dataField.length) currFrame=0;
225        chartsave.setTitle(titleField+" "+String.format("%.0f",(fieldBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(fieldBinSize*(currFrame+1)*1000.0))+" ms");
226        show.addSeries(new Integer(currFrame),dataField[currFrame]);
227      }
228      else if (e.getActionCommand().equals("F_pre"))
229      {
230        show.removeSeries(new Integer(currFrame));
231        currFrame--;
232        if(currFrame == -1) currFrame=dataField.length-1;
233        chartsave.setTitle(titleField+" "+String.format("%.0f",(fieldBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(fieldBinSize*(currFrame+1)*1000.0))+" ms");
234        show.addSeries(new Integer(currFrame),dataField[currFrame]);
235      }
236      else if (e.getActionCommand().equals("V_pre"))
237      {
238        vectorxydataset.removeSeries(vectorDataField[currFrame]);
239        currFrame--;
240        if(currFrame == -1) currFrame=vectorDataField.length-1;
241        chartsave.setTitle(titleField+" "+String.format("%.0f",(vectorBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(vectorBinSize*(currFrame+1)*1000.0))+" ms");
242        vectorxydataset.addSeries(vectorDataField[currFrame]);
243      }
244      else if (e.getActionCommand().equals("V_next"))
245      {
246        vectorxydataset.removeSeries(vectorDataField[currFrame]);
247        currFrame++;
248        if(currFrame == vectorDataField.length) currFrame = 0;
249        chartsave.setTitle(titleField+" "+String.format("%.0f",(vectorBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(vectorBinSize*(currFrame+1)*1000.0))+" ms");
250        vectorxydataset.addSeries(vectorDataField[currFrame]);
251      }
252      else if (e.getActionCommand().equals("SUsave"))
253      {
254        Properties p = new Properties();
255        p.setProperty("PageSize","A5");
256        try{
257          VectorGraphics g = new PSGraphics2D(new File("pics/SU_"+subexpNames.getString(currExp)+"_Pic"+picture+".eps"), new java.awt.Dimension(800,800)); 
258          g.setProperties(p); 
259          g.startExport(); 
260          //jpanel.print(g); 
261          Rectangle2D r2D = new Rectangle2D.Double(0,0, 800,800);
262          chartsave.draw(g, r2D);
263          g.endExport();
264          picture++;
265        }catch(Exception x)
266        {
267          x.printStackTrace();
268          System.exit(-1);
269        }
270      }
271      else if (e.getActionCommand().equals("MUsave"))
272      {
273        Properties p = new Properties();
274        p.setProperty("PageSize","A5");
275        try{
276          VectorGraphics g = new PSGraphics2D(new File("pics/MU_"+subexpNames.getString(currExp)+"_Pic"+picture+".eps"), new java.awt.Dimension(800,800)); 
277          g.setProperties(p); 
278          g.startExport(); 
279          //jpanel.print(g); 
280          Rectangle2D r2D = new Rectangle2D.Double(0,0, 800,800);
281          chartsave.draw(g, r2D);
282          g.endExport();
283          picture++;
284        }catch(Exception x)
285        {
286          x.printStackTrace();
287          System.exit(-1);
288        }
289      }
290      else if (e.getActionCommand().equals("Fieldsave"))
291      {
292        Properties p = new Properties();
293        p.setProperty("PageSize","A5");
294        try{
295          VectorGraphics g = new PSGraphics2D(new File("pics/Field_"+subexpNames.getString(currExp)+"_ID"+currField+"_Pic"+picture+".eps"), new java.awt.Dimension(800,800)); 
296          g.setProperties(p); 
297          g.startExport(); 
298          //jpanel.print(g); 
299          Rectangle2D r2D = new Rectangle2D.Double(0,0, 800,800);
300          chartsave.draw(g, r2D);
301          g.endExport();
302          picture++;
303        }catch(Exception x)
304        {
305          x.printStackTrace();
306          System.exit(-1);
307        }
308      }
309      else if (e.getActionCommand().equals("Vectorsave"))
310      {
311        Properties p = new Properties();
312        p.setProperty("PageSize","A5");
313        try{
314          VectorGraphics g = new PSGraphics2D(new File("pics/Vector_"+subexpNames.getString(currExp)+"_ID"+currField+"_Pic"+picture+".eps"), new java.awt.Dimension(800,800)); 
315          g.setProperties(p); 
316          g.startExport(); 
317          //jpanel.print(g); 
318          Rectangle2D r2D = new Rectangle2D.Double(0,0, 800,800);
319          chartsave.draw(g, r2D);
320          g.endExport();
321          picture++;
322        }catch(Exception x)
323        {
324          x.printStackTrace();
325          System.exit(-1);
326        }
327      }
328      else if (e.getActionCommand().equals("Intrasave"))
329      {
330          Properties p = new Properties();
331          p.setProperty("PageSize","A5");
332          try{
333            VectorGraphics g = new PSGraphics2D(new File("pics/Intracellular_"+subexpNames.getString(currExp)+"_ID"+currIntra+"_Pic"+picture+".eps"), new java.awt.Dimension(800,800)); 
334            g.setProperties(p); 
335            g.startExport(); 
336            Rectangle2D r2D = new Rectangle2D.Double(0,0, 800,800);
337            chartsave.draw(g, r2D);
338            g.endExport();
339            picture++;
340          }catch(Exception x)
341          {
342            x.printStackTrace();
343            System.exit(-1);
344          }
345      }
346
347      int byte0 = -1;
348      
349      for(int iter=0 ; iter< suSeries; iter++)
350      {
351        if(e.getActionCommand().equals(new Integer(iter).toString())) byte0 = iter;
352      }
353      
354      if(byte0 >= 0)
355      {
356        boolean flag = suRender.getItemVisible(byte0, 0);
357        suRender.setSeriesVisible(byte0, new Boolean(!flag));
358      }
359    }
360
361    /**
362     * {@inheritDoc}
363     * @see javax.swing.JFrame#PlotResult()
364     */
365    public PlotResult() throws Exception
366    ////////////////////////////////////////////////////////////////////////
367    {
368      super("result");
369      init();
370//    org.apache.log4j.BasicConfigurator.configure();
371//    org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
372//
373//    String filename = "results/simExperiment.nc";
374//    NetcdfFile ncfile = NetcdfFile.open(filename);
375//    Variable exp = ncfile.findVariable("subExpNum");
376//    numExp = exp.readScalarInt();
377//    ncfile.close();
378    }
379
380    /**
381     * {@inheritDoc}
382     * @see ApplicationFrame#PlotResult(String)
383     */
384    public PlotResult(String st) throws Exception
385    ////////////////////////////////////////////////////////////////////////
386    {
387      super(st);
388//    org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
389      //org.apache.log4j.BasicConfigurator.configure();
390//    String filename = "results/simExperiment.nc";
391//    NetcdfFile ncfile = NetcdfFile.open(filename);
392//    Variable exp = ncfile.findVariable("subExpNum");
393//    numExp = exp.readScalarInt();
394//    ncfile.close();
395    }
396
397    /**
398     * 
399     * 
400     * @return single unit data structure
401     * 
402     * @throws Exception
403     */
404    public  DefaultMultiValueCategoryDataset readSu() throws Exception
405    ////////////////////////////////////////////////////////////////////////
406    {
407//    String filename = "results/simExperiment.nc";
408//    NetcdfFile ncfile = NetcdfFile.open(filename);
409      Variable v = ncfile.findVariable("suNames");
410      ArrayChar.D2 names = (ArrayChar.D2) v.read();
411//    System.out.println(names.getString(0));
412      int [] varShape = v.getShape();
413      suSeries=varShape[0];
414//    System.out.println(varShape[0]+" "+varShape[1]);
415      DefaultMultiValueCategoryDataset data = new DefaultMultiValueCategoryDataset();
416      Variable exp = ncfile.findVariable("subExpNum");
417      int numExp = exp.readScalarInt();
418      for( int i = 0 ; i < numExp; i ++)
419      {
420        Variable trial = ncfile.findVariable("SubExp" + i + SEPARATOR + "trialNum");
421        int numTri = trial.readScalarInt();
422        for(int j = 0 ; j < numTri ; j++)
423        {
424          for(int eleId=0; eleId < varShape[0]; eleId++)
425          {
426            Variable spike = ncfile.findVariable("SubExp" + i + SEPARATOR + "trial" + j + SEPARATOR + names.getString(eleId));
427            int [] spikeShape = spike.getShape();
428            ArrayDouble.D1 spiData = (ArrayDouble.D1) spike.read();
429
430            ArrayList arrlist = new ArrayList();
431            for( int temp=0 ; temp< spikeShape[0]; temp++)
432            {
433              arrlist.add(spiData.get(temp)*1000.0);
434            }
435            data.add(arrlist,names.getString(eleId),"Sub"+i+" Trial"+j);
436          }
437        }
438      }
439//    ncfile.close();
440      return data;
441    }
442
443    /**
444     * 
445     * 
446     * @param expId
447     * @return single unit data structure
448     * 
449     * @throws Exception
450     */
451    public  DefaultMultiValueCategoryDataset readSu(int expId) throws Exception
452    ////////////////////////////////////////////////////////////////////////
453    {
454//    String filename = "results/simExperiment.nc";
455//    NetcdfFile ncfile = NetcdfFile.open(filename);
456      Variable v = ncfile.findVariable("suNames");
457      suNames = (ArrayChar.D2) v.read();
458
459      //    System.out.println(names.getString(0));
460      int [] varShape = v.getShape();
461      suSeries=varShape[0];
462      //    System.out.println(varShape[0]+" "+varShape[1]);
463      DefaultMultiValueCategoryDataset data = new DefaultMultiValueCategoryDataset();
464      Variable trial = ncfile.findVariable("SubExp" + expId + SEPARATOR + "trialNum");
465      int numTri = trial.readScalarInt();
466      for(int j = 0 ; j < numTri ; j++)
467      {
468        for(int eleId=0; eleId < varShape[0]; eleId++)
469        {
470          Variable spike = ncfile.findVariable("SubExp" + expId + SEPARATOR + "trial" + j
471                          + SEPARATOR + suNames.getString(eleId));
472          if(spike!=null)
473          {
474            int [] spikeShape = spike.getShape();
475            ArrayDouble.D1 spiData = (ArrayDouble.D1) spike.read();
476
477            ArrayList arrlist = new ArrayList();
478            for( int temp=0 ; temp< spikeShape[0]; temp++)
479            {
480              arrlist.add(spiData.get(temp)*1000.0);
481            }
482            data.add(arrlist,suNames.getString(eleId),"Sub"+expId+" Trial"+j);
483          }
484        }
485      }
486//    ncfile.close();
487      return data;
488    }
489
490    /**
491     * 
492     *  populate the field electrode data structure.
493     * @param expId
494     * @param fieldId
495     * 
496     * @throws Exception
497     */
498    public  void readField(int expId, int fieldId) throws Exception
499    ////////////////////////////////////////////////////////////////////////
500    {
501//    String filename = "results/simExperiment.nc";
502//    NetcdfFile ncfile = NetcdfFile.open(filename);
503      Variable v = ncfile.findVariable("fieldNames");
504      fieldNames = (ArrayChar.D2) v.read();
505
506      Variable bin = ncfile.findVariable("fieldBinSize");
507      fieldBinSize = bin.readScalarDouble();
508
509
510      Variable ratedata = ncfile.findVariable("SubExp" + expId + SEPARATOR + fieldNames.getString(fieldId));
511      int [] rShape= ratedata.getShape(); //x,y,bins
512//    System.out.println(rShape[2]+" "+rShape[0]+" "+rShape[1]);
513      fieldMax=0.0;
514
515      int xStep,yStep,xReso,yReso;
516      double xMul,yMul;
517
518      if(xEdge < rShape[0])
519      {
520        xStep = 1;
521        xMul = (double)xEdge / (double)rShape[0] ;
522        xReso = rShape[0];
523        xUnit=1.0;
524      }
525      else
526      {
527        xStep = xEdge / rShape[0];
528        xReso = xEdge;
529        xMul = 1.0;
530        xUnit=(double)xStep;
531      }
532
533      if(yEdge < rShape[1])
534      {
535        yStep = 1;
536        yMul = (double)yEdge / (double)rShape[1] ;
537        yReso = rShape[1];
538        yUnit=1.0;
539      }
540      else
541      {
542        yStep = yEdge / rShape[1];
543        yReso = yEdge;
544        yMul = 1.0;
545        yUnit=(double)yStep;
546      }
547
548
549      int factorx=xEdge/rShape[0];
550      int factory=yEdge/rShape[1];
551      if(factorx <1) factorx=1;
552      if(factory <1) factory=1;
553
554//    dataField = new double [rShape[2]][3][rShape[0]*rShape[1]];
555      dataField = new double [rShape[2]][3][xReso*yReso];
556
557      ArrayDouble.D3 rate = (ArrayDouble.D3) ratedata.read();
558
559      for(int x=0; x < xReso; x++)
560      {
561        for(int y=0; y< yReso;y++)
562        {
563          for(int t=0; t< rShape[2]; t++)
564          {
565            dataField[t][0][x*yReso+y]=(double)x*(double)xMul- (double)(xStep-1)/2.0;
566            dataField[t][1][x*yReso+y]=(double)y*(double)yMul- (double)(yStep-1)/2.0;
567//          dataField[t][0][x*yReso+y]=(double)x*(double)xMul;
568//          dataField[t][1][x*yReso+y]=(double)y*(double)yMul;
569            dataField[t][2][x*yReso+y]=rate.get(x/xStep,y/yStep,t)/fieldBinSize;
570            if(dataField[t][2][x*yReso+y]>fieldMax) fieldMax=dataField[t][2][x*yReso+y];
571          }
572        }
573      }
574
575      /*
576    for(int x=0; x < rShape[0]; x++)
577    {
578      for(int y=0; y< rShape[1]; y++)
579      {
580        for(int t=0; t< rShape[2]; t++)
581        {
582          dataField[t][0][x*rShape[1]+y]=(double)x;
583          dataField[t][1][x*rShape[1]+y]=(double)y;
584          dataField[t][2][x*rShape[1]+y]=rate.get(x,y,t)/fieldBinSize;
585          if(dataField[t][2][x*rShape[1]+y]>fieldMax) fieldMax=dataField[t][2][x*rShape[1]+y];
586        }
587      }
588    }
589       */
590//    ncfile.close();
591    }
592
593    /**
594     * 
595     *  populate the vector electrode data structure.
596     * @param expId
597     * @param fieldId
598     * 
599     * @throws Exception
600     */
601    public  void readVector(int expId, int fieldId) throws Exception
602    ////////////////////////////////////////////////////////////////////////
603    {
604      Variable v = ncfile.findVariable("vectorNames");
605      vectorNames = (ArrayChar.D2) v.read();
606
607      Variable bin = ncfile.findVariable("vectorBinSize");
608      vectorBinSize = bin.readScalarDouble();
609
610
611      Variable rateX = ncfile.findVariable("SubExp" + expId + SEPARATOR + vectorNames.getString(fieldId) + SEPARATOR + "X");
612      Variable rateY = ncfile.findVariable("SubExp" + expId + SEPARATOR + vectorNames.getString(fieldId) + SEPARATOR + "Y");
613
614      ArrayDouble.D3 ratedataX = (ArrayDouble.D3) rateX.read();
615      ArrayDouble.D3 ratedataY = (ArrayDouble.D3) rateY.read();
616
617      int [] rShape= ratedataX.getShape(); //x,y,bins
618
619
620
621      vectorDataField = new VectorSeries [rShape[2]];
622
623      for(int t=0; t<rShape[2]; t++)
624      {
625        vectorDataField[t] = new VectorSeries("S"+t);
626      }
627
628      int xStep,yStep,xReso,yReso;
629      double xMul,yMul;
630
631      if(xEdge < rShape[0])
632      {
633        xStep = 1;
634        xMul = (double)xEdge / (double)rShape[0] ;
635        xReso = rShape[0];
636        xUnit=1.0;
637      }
638      else
639      {
640        xStep = xEdge / rShape[0];
641        xReso = xEdge;
642        xMul = 1.0;
643        xUnit=(double)xStep;
644      }
645
646      if(yEdge < rShape[1])
647      {
648        yStep = 1;
649        yMul = (double)yEdge / (double)rShape[1] ;
650        yReso = rShape[1];
651        yUnit=1.0;
652      }
653      else
654      {
655        yStep = yEdge / rShape[1];
656        yReso = yEdge;
657        yMul = 1.0;
658        yUnit=(double)yStep;
659      }
660
661
662
663      fieldMax=0.0;
664      for(int x=0; x < rShape[0]; x++)
665      {
666        for(int y=0; y< rShape[1]; y++)
667        {
668          for(int t=0; t<rShape[2]; t++)
669          {
670            double currL = Math.sqrt(ratedataX.get(x,y,t)*ratedataX.get(x,y,t) + ratedataY.get(x,y,t)*ratedataY.get(x,y,t));
671            if( currL >fieldMax ) fieldMax=currL;
672          }
673
674        }
675      }
676
677      for(int t=0; t<rShape[2]; t++)
678      {
679        for(int x=0; x < rShape[0]; x++)
680        {
681          for(int y=0; y< rShape[1]; y++)
682          {
683            vectorDataField[t].add(x*xStep,y*yStep, ratedataX.get(x,y,t) ,ratedataY.get(x,y,t));
684          }
685        }
686      }
687
688    }
689
690
691    /**
692     * 
693     * 
694     * @param expId
695     * @return multiunit electrod data
696     * 
697     * @throws Exception
698     */
699    public  XYDataset readMU(int expId) throws Exception
700    ////////////////////////////////////////////////////////////////////////
701    {
702//    String filename = "results/simExperiment.nc";
703//    NetcdfFile ncfile = NetcdfFile.open(filename);
704      Variable v = ncfile.findVariable("muNames");
705      suNames = (ArrayChar.D2) v.read();
706//    System.out.println(names.getString(0));
707      int [] varShape = v.getShape();
708      suSeries=varShape[0];
709//    System.out.println(varShape[0]+" "+varShape[1]);
710      YIntervalSeries [] eles = new YIntervalSeries[suSeries];
711      for(int i=0; i < suSeries; i++)
712      {
713        eles[i] = new YIntervalSeries(suNames.getString(i));
714      }
715
716
717      Variable bin = ncfile.findVariable("muBinSize");
718      double binSize = bin.readScalarDouble();
719
720
721      for(int eleId=0; eleId < suSeries; eleId++)
722      {
723        Variable ratedata = ncfile.findVariable("SubExp" + expId + SEPARATOR + suNames.getString(eleId)+ SEPARATOR + "ALL");
724        int [] rShape= ratedata.getShape();
725        ArrayDouble.D2 rate = (ArrayDouble.D2) ratedata.read();
726        double [] mean = new double [rShape[1]];
727        double [] sd = new double [rShape[1]];
728        for(int y=0; y< rShape[1]; y++)
729        {
730          double [] tmp = new double[rShape[0]];
731          for(int x=0; x< rShape[0]; x++)
732          {
733            tmp[x]=rate.get(x,y)/binSize;
734          }
735          mean[y]=FunUtil.mean(tmp);
736          sd[y] = FunUtil.sd(tmp);
737          eles[eleId].add((y+1)*binSize*1000.0,mean[y],mean[y]-sd[y],mean[y]+sd[y]);
738        }
739      }
740
741//    ncfile.close();
742      YIntervalSeriesCollection data = new YIntervalSeriesCollection();
743
744      for(int i=0; i < suSeries; i++)
745      {
746        data.addSeries(eles[i]);
747      }
748      return data;
749    }
750    
751    /**
752     * 
753     * 
754     * @param expId
755     * @return intracellular recorder data
756     * 
757     * @throws Exception
758     */
759    public  void readIntra ( int expId , int intraId ) throws Exception
760    ////////////////////////////////////////////////////////////////////////
761    {   
762      Variable intraNamesVar = ncfile.findVariable("intraNames");
763      intraNames = (ArrayChar.D2) intraNamesVar.read();
764
765      Variable intraBinSizeVar = ncfile.findVariable("intraBinSize");
766      intraBinSize = intraBinSizeVar.readScalarDouble();
767            
768          Variable voltageVar = ncfile.findVariable("SubExp" + expId + SEPARATOR + intraNames.getString(intraId) + SEPARATOR + "V");
769          Variable currentVar = ncfile.findVariable("SubExp" + expId + SEPARATOR + intraNames.getString(intraId) + SEPARATOR + "C");
770          
771      ArrayDouble.D1 voltageData = (ArrayDouble.D1) voltageVar.read();
772      ArrayDouble.D2 currentData = (ArrayDouble.D2) currentVar.read();
773      
774      int numVoltageBins = voltageData.getShape()[0];
775      int numCurrentBins = currentData.getShape()[1];
776      int numCurrentChannels = currentData.getShape()[0];
777                        
778      XYSeries voltage = new XYSeries("Voltage");
779      XYSeries[] current = new XYSeries[numCurrentChannels];
780      
781      for (int i = 0; i < numVoltageBins; i++)
782      {
783          double time = intraBinSize * i;
784          voltage.add(time, voltageData.get(i));
785      }
786         
787      for (int channel = 0; channel < numCurrentChannels; channel++)
788      {
789          current[channel] = new XYSeries("Current" + channel);
790          for (int i = 0; i < numCurrentBins; i++)
791          {
792                  double time = intraBinSize * i;
793                  current[channel].add(time, currentData.get(channel, i) * 10E9);
794          }
795      }
796           
797      intraVoltageData = new XYSeriesCollection(voltage);
798      
799      intraCurrentData = new XYSeriesCollection();
800      for (int i = 0; i  < current.length; i++)
801          intraCurrentData.addSeries(current[i]);
802    }
803
804    /**
805     * 
806     * 
807     * @param i
808     * @return Panel for singlue unit
809     * 
810     * @throws Exception
811     */
812    public  JPanel createSuPanel(int i) throws Exception
813    ////////////////////////////////////////////////////////////////////////
814    {
815      JFreeChart jfreechart = createSUChart(readSu(i));
816      return new ChartPanel(jfreechart);
817    }
818
819    /**
820     * 
821     * 
822     * @param multivaluecategorydataset
823     * @return Charte for single unit electrode
824     */
825    private  JFreeChart createSUChart(MultiValueCategoryDataset multivaluecategorydataset)
826    ////////////////////////////////////////////////////////////////////////
827    {
828      ScatterRenderer shape = new ScatterRenderer();
829      suRender = shape;
830      for(int i=0; i < suSeries; i++)
831      {
832        if(i==nowValue)
833        {
834          suRender.setSeriesVisible(i, new Boolean(true));
835        }
836        else
837        {
838          suRender.setSeriesVisible(i, new Boolean(false));
839        }
840        shape.setSeriesShape(i, new Rectangle(2, 4));
841        shape.setSeriesShape(i, new Rectangle(2, 4));
842      }
843
844      CategoryPlot categoryplot = new CategoryPlot(multivaluecategorydataset, new CategoryAxis("Trial"), new NumberAxis("Time"), shape);
845      categoryplot.setBackgroundPaint(Color.lightGray);
846      categoryplot.setOrientation(PlotOrientation.HORIZONTAL);
847      categoryplot.setDomainGridlinePaint(Color.white);
848      categoryplot.setRangeGridlinePaint(Color.white);
849      categoryplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
850      JFreeChart jfreechart = new JFreeChart(categoryplot);
851      jfreechart.setBackgroundPaint(Color.white);
852      chartsave = jfreechart;
853      return jfreechart;
854    }
855
856    /**
857     * 
858     * 
859     * @return chart for field electrode
860     */
861    private JFreeChart createFieldChart()
862    ////////////////////////////////////////////////////////////////////////
863    {
864      show = new DefaultXYZDataset();
865      show.addSeries(new Integer(currFrame),dataField[currFrame]);
866//    System.out.println(fieldMax);
867
868      NumberAxis numberaxis = new NumberAxis("X");
869//    numberaxis.setStandardTickUnits(NumberAxis.createStandardTickUnits());
870      numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
871      //numberaxis.setTickUnit( new NumberTickUnit(xUnit*5)); 
872      numberaxis.setLowerMargin(0.0D);
873      numberaxis.setUpperMargin(0.0D);
874      numberaxis.setAxisLinePaint(Color.white);
875      numberaxis.setTickMarkPaint(Color.white);
876      NumberAxis numberaxis1 = new NumberAxis("Y");
877      numberaxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
878//    numberaxis1.setStandardTickUnits(NumberAxis.createStandardTickUnits());
879      //numberaxis1.setTickUnit( new NumberTickUnit(yUnit*5)); 
880      numberaxis1.setLowerMargin(0.0D);
881      numberaxis1.setUpperMargin(0.0D);
882      numberaxis1.setAxisLinePaint(Color.white);
883      numberaxis1.setTickMarkPaint(Color.white);
884
885      XYBlockRenderer xyblockrenderer = new XYBlockRenderer();
886//    System.out.println("fieldMax="+fieldMax);
887//    System.out.println("fieldMax="+fieldMax);
888      if(fieldMax==0.0)fieldMax=1.0;
889
890      GrayPaintScale graypaintscale = new GrayPaintScale(0, fieldMax);
891      xyblockrenderer.setPaintScale(graypaintscale);
892      XYPlot xyplot = new XYPlot(show, numberaxis, numberaxis1, xyblockrenderer);
893
894      xyplot.setBackgroundPaint(Color.lightGray);
895      xyplot.setDomainGridlinesVisible(false);
896      xyplot.setRangeGridlinePaint(Color.white);
897      xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
898      xyplot.setOutlinePaint(Color.blue);
899      JFreeChart jfreechart = new JFreeChart(titleField+" "+String.format("%.0f",(fieldBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(fieldBinSize*(currFrame+1)*1000.0))+" ms", xyplot);
900      chartsave = jfreechart;
901//    jfreechart.setTitle("Field Plot "+(fieldBinSize*currFrame*1000.0)+"-"+(fieldBinSize*(currFrame+1)*1000.0));
902      jfreechart.removeLegend();
903
904      NumberAxis numberaxis2 = new NumberAxis("Scale");
905      numberaxis2.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
906      numberaxis2.setRange(new Range(0,fieldMax));
907      numberaxis2.setAxisLinePaint(Color.white);
908      numberaxis2.setTickMarkPaint(Color.white);
909      numberaxis2.setTickLabelFont(new Font("Dialog", 0, 7));
910      PaintScaleLegend paintscalelegend = new PaintScaleLegend(graypaintscale, numberaxis2);
911      paintscalelegend.setAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
912      paintscalelegend.setAxisOffset(5D);
913      paintscalelegend.setMargin(new RectangleInsets(5D, 5D, 5D, 5D));
914      paintscalelegend.setFrame(new BlockBorder(Color.red));
915      paintscalelegend.setPadding(new RectangleInsets(10D, 10D, 10D, 10D));
916      paintscalelegend.setStripWidth(10D);
917      paintscalelegend.setPosition(RectangleEdge.RIGHT);
918      paintscalelegend.setBackgroundPaint(new Color(120, 120, 180));
919      jfreechart.addSubtitle(paintscalelegend);
920      jfreechart.setBackgroundPaint(new Color(180, 180, 250));
921      return jfreechart;
922    }
923
924    /**
925     * 
926     * 
927     * @return Chart for vector electrode
928     */
929    private JFreeChart createVectorChart()
930    ////////////////////////////////////////////////////////////////////////
931    {
932      vectorxydataset = new VectorSeriesCollection();
933      vectorxydataset.addSeries(vectorDataField[currFrame]);
934
935      NumberAxis numberaxis = new NumberAxis("X");
936      numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
937      numberaxis.setLowerMargin(0.01D);
938      numberaxis.setUpperMargin(0.01D);
939      numberaxis.setAutoRangeIncludesZero(false);
940      NumberAxis numberaxis1 = new NumberAxis("Y");
941      numberaxis1.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
942      numberaxis1.setLowerMargin(0.01D);
943      numberaxis1.setUpperMargin(0.01D);
944      numberaxis1.setAutoRangeIncludesZero(false);
945      VectorRenderer vectorrenderer = new VectorRenderer();
946      vectorrenderer.setSeriesPaint(0, Color.blue);
947      XYPlot xyplot = new XYPlot(vectorxydataset , numberaxis, numberaxis1, vectorrenderer);
948      xyplot.setBackgroundPaint(Color.lightGray);
949      xyplot.setDomainGridlinePaint(Color.white);
950      xyplot.setRangeGridlinePaint(Color.white);
951      xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
952      xyplot.setOutlinePaint(Color.black);
953      JFreeChart jfreechart = new JFreeChart(titleField+" "+String.format("%.0f",(vectorBinSize*currFrame*1000.0))+"-"+String.format("%.0f",(vectorBinSize*(currFrame+1)*1000.0))+" ms", xyplot);                                            
954      jfreechart.setBackgroundPaint(Color.white);
955      chartsave = jfreechart;
956      return jfreechart;
957    }
958
959    /**
960     * 
961     * 
962     * @param xydataset
963     * @param i
964     * @return chart for multi unit electrode
965     */
966    private  JFreeChart createMUChart(XYDataset xydataset,int i)
967    ////////////////////////////////////////////////////////////////////////
968    {
969      JFreeChart jfreechart = ChartFactory.createScatterPlot("Multi Unit SubExp "+subexpNames.getString(i), "Time (ms)", "Rate (Hz)", xydataset, PlotOrientation.VERTICAL, true, true, false);
970      jfreechart.setBackgroundPaint(Color.white);
971      XYPlot xyplot = (XYPlot)jfreechart.getPlot();
972      xyplot.setBackgroundPaint(Color.lightGray);
973      xyplot.setDomainGridlinePaint(Color.white);
974      xyplot.setRangeGridlinePaint(Color.white);
975      xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
976      XYErrorRenderer render = new XYErrorRenderer();
977      suRender = render;
978      //  YIntervalRenderer render = new YIntervalRenderer();
979/*
980    render.setSeriesStroke(0, new BasicStroke(3F, 1, 1));                                                 
981    render.setSeriesStroke(1, new BasicStroke(3F, 1, 1));                                                 
982    render.setSeriesLinesVisible(0,true);
983    render.setSeriesLinesVisible(1,true);
984 */
985      //render.setSeriesShapesVisible(0,true);
986      //
987      for(int ii=0; ii < suSeries; ii++)
988      {
989        render.setSeriesStroke(ii, new BasicStroke(3F, 1, 1));                                                 
990        render.setSeriesLinesVisible(ii,true);
991        if(ii==nowValue)
992        {
993          suRender.setSeriesVisible(ii, new Boolean(true));
994        }
995        else
996        {
997          suRender.setSeriesVisible(ii, new Boolean(false));
998
999        }
1000      }
1001
1002
1003      xyplot.setRenderer(render);
1004      chartsave = jfreechart;
1005      return jfreechart;
1006      /*
1007    JFreeChart jfreechart = ChartFactory.createXYLineChart("Multi Unit", "Time (ms)", "Rate (Hz)", xydataset, PlotOrientation.VERTICAL, true, true, false);
1008    jfreechart.setBackgroundPaint(Color.white);
1009    XYPlot xyplot = (XYPlot)jfreechart.getPlot();
1010    xyplot.setBackgroundPaint(Color.lightGray);
1011    xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
1012    xyplot.setDomainGridlinePaint(Color.white);
1013    xyplot.setRangeGridlinePaint(Color.white);
1014    //  DeviationRenderer deviationrenderer = new DeviationRenderer(true, false);
1015    //  deviationrenderer.setSeriesStroke(0, new BasicStroke(3F, 1, 1));                                                 
1016    //  deviationrenderer.setSeriesStroke(1, new BasicStroke(3F, 1, 1));                                                 
1017    //  deviationrenderer.setSeriesFillPaint(0, new Color(255, 200, 200));
1018    //  deviationrenderer.setSeriesFillPaint(1, new Color(200, 200, 255));
1019    xyplot.setRenderer(deviationrenderer);
1020    NumberAxis numberaxis = (NumberAxis)xyplot.getRangeAxis();
1021    numberaxis.setAutoRangeIncludesZero(false);
1022    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
1023    return jfreechart;
1024       */
1025    }
1026    
1027    /**
1028     * 
1029     * 
1030     * @return Chart for intracellular electrodes
1031     */
1032    private JFreeChart createIntraChart()
1033    ////////////////////////////////////////////////////////////////////////
1034    {           
1035        NumberAxis timeAxis = new NumberAxis("Time (s)");
1036        CombinedDomainXYPlot plot = new CombinedDomainXYPlot(timeAxis);
1037        
1038        XYItemRenderer voltageRenderer = new XYLineAndShapeRenderer(true, false);
1039        NumberAxis voltageAxis = new NumberAxis("Voltage (v)");
1040        XYPlot voltagePlot = new XYPlot(intraVoltageData, timeAxis, voltageAxis, voltageRenderer);
1041        voltagePlot.setBackgroundPaint(Color.lightGray);
1042        voltagePlot.setDomainGridlinePaint(Color.white);
1043        voltagePlot.setRangeGridlinePaint(Color.white);
1044        voltagePlot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
1045        plot.add(voltagePlot);
1046
1047        XYItemRenderer currentRenderer = new XYLineAndShapeRenderer(true, false);
1048        NumberAxis currentAxis = new NumberAxis("Current (nA)");
1049        XYPlot currentPlot = new XYPlot(intraCurrentData, timeAxis, currentAxis, currentRenderer);
1050        currentPlot.setBackgroundPaint(Color.lightGray);
1051        currentPlot.setDomainGridlinePaint(Color.white);
1052        currentPlot.setRangeGridlinePaint(Color.white);
1053        currentPlot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
1054        plot.add(currentPlot);
1055
1056        JFreeChart jfreechart = new JFreeChart("SubExp " + subexpNames.getString(currExp) + " Intracellular Unit " + intraNames.getString(currIntra), plot);
1057
1058        chartsave = jfreechart; // For SaveEPS
1059        
1060        return jfreechart;
1061    }
1062    
1063
1064    /**
1065     * 
1066     *  create the panel
1067     * @param i
1068     * @return
1069     * 
1070     * @throws Exception
1071     */
1072    public  JPanel createMUPanel(int i) throws Exception
1073    ////////////////////////////////////////////////////////////////////////
1074    {
1075      JFreeChart jfreechart = createMUChart(readMU(i),i);
1076      return new ChartPanel(jfreechart);
1077    }
1078
1079    /**
1080     * 
1081     * create the panel
1082     * @return
1083     * 
1084     * @throws Exception
1085     */
1086    public  JPanel createFieldPanel() throws Exception
1087    ////////////////////////////////////////////////////////////////////////
1088    {
1089      JFreeChart jfreechart = createFieldChart();
1090      return new ChartPanel(jfreechart);
1091    }
1092    
1093    /**
1094     * 
1095     * create the intracellular recorder panel
1096     * @return
1097     * 
1098     * @throws Exception
1099     */
1100    public  JPanel createIntraPanel() throws Exception
1101    ////////////////////////////////////////////////////////////////////////
1102    {
1103      JFreeChart jfreechart = createIntraChart();
1104      return new ChartPanel(jfreechart);
1105    }
1106
1107    /**
1108     * create the panel
1109     * 
1110     * @return
1111     * 
1112     * @throws Exception
1113     */
1114    public  JPanel createVectorPanel() throws Exception
1115    ////////////////////////////////////////////////////////////////////////
1116    {
1117      JFreeChart jfreechart = createVectorChart();
1118      return new ChartPanel(jfreechart);
1119    }
1120    public static void suPlot() throws Exception
1121    ////////////////////////////////////////////////////////////////////////
1122    {
1123//    org.apache.log4j.BasicConfigurator.configure();
1124//    org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
1125      ucar.nc2.Dimension exp = ncfile.findDimension("suEleId");
1126      if(exp!=null)
1127      {
1128        for(int i = 0 ; i< numExp; i++)
1129        {
1130          PlotResult frame = new PlotResult("SU Raster "+subexpNames.getString(i));
1131          frame.currExp = i;
1132          JPanel jpanel = frame.createSuPanel(i);
1133          JPanel command = new JPanel();
1134          for(int j = 0 ; j< frame.suSeries; j++)
1135          {
1136            JCheckBox check = new JCheckBox(frame.suNames.getString(j));
1137            check.setActionCommand(new Integer(j).toString());
1138            check.addActionListener(frame);
1139            check.setSelected(false);
1140            command.add(check);
1141          }
1142
1143          JButton pre = new JButton("SaveEPS");
1144          pre.setActionCommand("SUsave");
1145          pre.addActionListener(frame);
1146          command.add(pre);
1147
1148          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1149          //frame.setContentPane(jpanel);
1150          frame.add(jpanel);
1151          frame.add(command,"South");
1152          frame.pack();
1153          RefineryUtilities.centerFrameOnScreen(frame);
1154          frame.setVisible(true);
1155        }
1156      }
1157    }
1158
1159    public static void muPlot() throws Exception
1160    ////////////////////////////////////////////////////////////////////////
1161    {
1162//    org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
1163      ucar.nc2.Dimension exp = ncfile.findDimension("muEleId");
1164      if(exp!=null)
1165      {
1166        for(int i = 0 ; i< numExp; i++)
1167        {
1168          PlotResult frame = new PlotResult("MU Rateplot "+subexpNames.getString(i));
1169          frame.currExp = i;
1170          JPanel jpanel = frame.createMUPanel(i);
1171
1172          JPanel command = new JPanel();
1173          for(int j = 0 ; j< frame.suSeries; j++)
1174          {
1175            JCheckBox check = new JCheckBox(frame.suNames.getString(j));
1176            check.setActionCommand(new Integer(j).toString());
1177            check.addActionListener(frame);
1178            check.setSelected(false);
1179            command.add(check);
1180          }
1181
1182          JButton pre = new JButton("SaveEPS");
1183          pre.setActionCommand("MUsave");
1184          pre.addActionListener(frame);
1185          command.add(pre);
1186
1187
1188
1189          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1190//    frame.setContentPane(jpanel);
1191          frame.add(jpanel);
1192          frame.add(command,"South");
1193          frame.pack();
1194          RefineryUtilities.centerFrameOnScreen(frame);
1195          frame.setVisible(true);
1196        }
1197      }
1198    }
1199
1200    public static void fieldPlot() throws Exception
1201    ////////////////////////////////////////////////////////////////////////
1202    {
1203//    org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
1204      for(int i = 0 ; i< numExp; i++)
1205      {
1206        for(int j=0; j< numField; j++)
1207        {
1208          PlotResult frame = new PlotResult("FieldPlot "+subexpNames.getString(i)+" Id "+j);
1209          frame.currExp = i;
1210          frame.currField = j;
1211          frame.readField(i,j);
1212          frame.titleField= frame.fieldNames.getString(j);
1213          JPanel jpanel = frame.createFieldPanel();
1214
1215          JPanel command = new JPanel();
1216
1217          JButton pre = new JButton("<-");
1218          pre.setActionCommand("F_pre");
1219          pre.addActionListener(frame);
1220          command.add(pre);
1221          JButton next = new JButton("->");
1222          next.setActionCommand("F_next");
1223          next.addActionListener(frame);
1224          command.add(next);
1225
1226          pre = new JButton("SaveEPS");
1227          pre.setActionCommand("Fieldsave");
1228          pre.addActionListener(frame);
1229          command.add(pre);
1230
1231
1232
1233          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1234//        frame.setContentPane(jpanel);
1235          frame.add(jpanel);
1236          frame.add(command,"South");
1237          frame.pack();
1238          RefineryUtilities.centerFrameOnScreen(frame);
1239          frame.setVisible(true);
1240        }
1241      }
1242    }
1243
1244    public static void vectorPlot() throws Exception
1245    ////////////////////////////////////////////////////////////////////////
1246    {
1247      for(int i = 0 ; i< numExp; i++)
1248      {
1249        for(int j=0; j< numVector; j++)
1250        {
1251          PlotResult frame = new PlotResult("VectorPlot "+subexpNames.getString(i)+" Id "+j);
1252          frame.currExp = i;
1253          frame.currField = j;
1254          frame.readVector(i,j);
1255          frame.titleField= frame.vectorNames.getString(j);
1256          JPanel jpanel = frame.createVectorPanel();
1257
1258          JPanel command = new JPanel();
1259
1260          JButton pre = new JButton("<-");
1261          pre.setActionCommand("V_pre");
1262          pre.addActionListener(frame);
1263          command.add(pre);
1264          JButton next = new JButton("->");
1265          next.setActionCommand("V_next");
1266          next.addActionListener(frame);
1267          command.add(next);
1268
1269          pre = new JButton("SaveEPS");
1270          pre.setActionCommand("Vectorsave");
1271          pre.addActionListener(frame);
1272          command.add(pre);
1273
1274
1275
1276
1277
1278          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1279//        frame.setContentPane(jpanel);
1280          frame.add(jpanel);
1281          frame.add(command,"South");
1282          frame.pack();
1283          RefineryUtilities.centerFrameOnScreen(frame);
1284          frame.setVisible(true);
1285        }
1286      }
1287    }
1288
1289    public static void intraPlot() throws Exception
1290    {
1291        for (int i = 0; i < numExp; i++)
1292        {
1293                for (int j = 0; j < numIntra; j++)
1294                {
1295                PlotResult frame = new PlotResult ( "Intracellular Plot " + subexpNames.getString( i ) + " Id "+ j );
1296                frame.currExp = i;
1297                frame.currIntra = j;
1298                frame.readIntra( i , j );
1299                frame.titleField = frame.intraNames.getString( j );
1300                
1301                JPanel intraPanel = frame.createIntraPanel();
1302                intraPanel.setPreferredSize ( new java.awt.Dimension( xEdge + 50, yEdge + 50 ));
1303                
1304                JPanel commandPanel = new JPanel();
1305                
1306                JButton saveButton = new JButton( "SaveEPS" );
1307                saveButton.setActionCommand("Intrasave");
1308                saveButton.addActionListener(frame);
1309                
1310                commandPanel.add( saveButton );
1311                
1312                frame.add( intraPanel );
1313                frame.add( commandPanel , "South" );
1314                frame.pack( );
1315                RefineryUtilities.centerFrameOnScreen(frame);
1316                frame.setVisible( true );
1317                }
1318        }
1319    }
1320
1321
1322    /**
1323     * 
1324     *  generate the EPS files
1325     * 
1326     * @throws Exception
1327     */
1328    public  static void exportFile() throws Exception
1329    ////////////////////////////////////////////////////////////////////////
1330    {
1331      Properties p = new Properties();
1332      p.setProperty("PageSize","A5");
1333
1334      //JPanel jpanel = createMUPanel(0);
1335      for(int i = 0 ; i < numExp; i++)
1336      {
1337        PlotResult frame = new PlotResult("SU Raster SubExp "+i);
1338        JFreeChart jfreechart = frame.createSUChart(frame.readSu(i));
1339        VectorGraphics g = new PSGraphics2D(new File("pics/SU_sub"+i+".eps"), new java.awt.Dimension(xEdge+50,yEdge+50)); 
1340        g.setProperties(p); 
1341        g.startExport(); 
1342        //jpanel.print(g); 
1343        Rectangle2D r2D = new Rectangle2D.Double(0,0, xEdge+50,yEdge+50);
1344        jfreechart.draw(g, r2D);
1345        g.endExport();
1346      }
1347
1348      for(int i = 0 ; i < numExp; i++)
1349      {
1350        PlotResult frame = new PlotResult("MU Rateplot SubExp "+i);
1351        JFreeChart jfreechart = frame.createMUChart(frame.readMU(i),i);
1352        VectorGraphics g = new PSGraphics2D(new File("pics/MU_sub"+i+".eps"), new java.awt.Dimension(xEdge+50,yEdge+50)); 
1353        g.setProperties(p); 
1354        g.startExport(); 
1355        //jpanel.print(g); 
1356        Rectangle2D r2D = new Rectangle2D.Double(0,0, xEdge+50,yEdge+50);
1357        jfreechart.draw(g, r2D);
1358        g.endExport();
1359      }
1360
1361      for(int i = 0 ; i< numExp; i++)
1362      {
1363        for(int j=0; j< numField; j++)
1364        {
1365          PlotResult frame = new PlotResult("FieldPlot SubExp "+i+" Id "+j);
1366          frame.readField(i,j);
1367          frame.titleField= frame.fieldNames.getString(j);
1368
1369          for(int k=0; k< frame.dataField.length; k++)
1370          {
1371            frame.currFrame = k;
1372            JFreeChart jfreechart = frame.createFieldChart();
1373            VectorGraphics g = new PSGraphics2D(new File("pics/field_sub"+i+"_id"+j+"_t"+k+".eps"), new java.awt.Dimension(xEdge+50,yEdge+50)); 
1374            g.setProperties(p); 
1375            g.startExport(); 
1376            //jpanel.print(g); 
1377            Rectangle2D r2D = new Rectangle2D.Double(0,0, xEdge+50,yEdge+50);
1378            jfreechart.draw(g, r2D);
1379            g.endExport();
1380          }
1381        }
1382      }
1383
1384
1385      for(int i = 0 ; i< numExp; i++)
1386      {
1387        for(int j=0; j< numVector; j++)
1388        {
1389          PlotResult frame = new PlotResult("VectorPlot SubExp "+i+" Id "+j);
1390          frame.readVector(i,j);
1391
1392          frame.titleField= frame.vectorNames.getString(j);
1393
1394          for(int k=0; k< frame.vectorDataField.length; k++)
1395          {
1396            frame.currFrame = k;
1397            JFreeChart jfreechart = frame.createVectorChart();
1398            VectorGraphics g = new PSGraphics2D(new File("pics/vector_sub"+i+"_id"+j+"_t"+k+".eps"), new java.awt.Dimension(xEdge+50,yEdge+50)); 
1399            g.setProperties(p); 
1400            g.startExport(); 
1401            //jpanel.print(g); 
1402            Rectangle2D r2D = new Rectangle2D.Double(0,0, xEdge+50,yEdge+50);
1403            jfreechart.draw(g, r2D);
1404            g.endExport();
1405          }
1406        }
1407      }
1408      
1409      for(int i = 0 ; i< numExp; i++)
1410      {
1411          for(int j=0; j< numIntra; j++)
1412          {
1413                  PlotResult frame = new PlotResult("IntraPlot SubExp "+i+" Id "+j);
1414                  frame.readIntra(i,j);
1415
1416                  frame.titleField= frame.intraNames.getString(j);
1417
1418                  JFreeChart jfreechart = frame.createIntraChart();
1419                  VectorGraphics g = new PSGraphics2D(new File("pics/intra_sub"+i+"_id"+j+".eps"), new java.awt.Dimension(xEdge+50,yEdge+50)); 
1420                  g.setProperties(p); 
1421                  g.startExport(); 
1422                  //jpanel.print(g); 
1423                  Rectangle2D r2D = new Rectangle2D.Double(0,0, xEdge+50,yEdge+50);
1424                  jfreechart.draw(g, r2D);
1425                  g.endExport();
1426          }
1427      }
1428
1429
1430    }
1431
1432    /**
1433     *  initialize the plotting class
1434     * 
1435     * 
1436     * @throws Exception
1437     */
1438    public static void init() throws Exception
1439    ////////////////////////////////////////////////////////////////////////
1440    {
1441// Remarked out when switched for Log4j to Logback for logging.
1442// This log4j log file might have been initialized but not used.      
1443//      org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
1444      String filename = "results/simExperiment.nc";
1445      ncfile = NetcdfFile.open(filename);
1446      Variable exp = ncfile.findVariable("subExpNum");
1447      numExp = exp.readScalarInt();
1448      int [] varShape;
1449      exp = ncfile.findVariable("fieldNames");
1450      if(exp!=null)
1451      {
1452        ArrayChar.D2 fieldNames   = (ArrayChar.D2) exp.read();
1453        varShape = exp.getShape();
1454        numField = varShape[0];
1455      }
1456      else
1457      {
1458        numField=0;
1459      }
1460
1461      exp = ncfile.findVariable("vectorNames");
1462      if(exp!=null)
1463      {
1464        ArrayChar.D2 vectorNames   = (ArrayChar.D2) exp.read();
1465        varShape = exp.getShape();
1466        numVector = varShape[0];
1467      }
1468      else
1469      {
1470        numVector=0;
1471      }
1472      
1473      exp = ncfile.findVariable("intraNames");
1474      if (exp != null)
1475      {
1476          varShape = exp.getShape();
1477          numIntra = varShape[0];
1478      }
1479      else
1480      {
1481          numIntra = 0;
1482      }
1483
1484      exp = ncfile.findVariable("subexpNames");
1485      subexpNames = (ArrayChar.D2) exp.read();
1486      exp = ncfile.findVariable("xEdgeLen");
1487      xEdge = exp.readScalarInt();
1488
1489      exp = ncfile.findVariable("yEdgeLen");
1490      yEdge = exp.readScalarInt();
1491
1492    }
1493
1494    public static void init(String fileName) throws Exception
1495    ////////////////////////////////////////////////////////////////////////
1496    {
1497      // Remarked out when switched for Log4j to Logback for logging.
1498      // This log4j log file might have been initialized but not used.      
1499      // org.apache.log4j.BasicConfigurator.configure(new FileAppender(new SimpleLayout(),"log/ncfile.log"));
1500      
1501//    String filename = "results/"+fileName;
1502      String filename = fileName;
1503      ncfile = NetcdfFile.open(filename);
1504      Variable exp = ncfile.findVariable("subExpNum");
1505      numExp = exp.readScalarInt();
1506      int [] varShape;
1507      exp = ncfile.findVariable("fieldNames");
1508      if(exp!=null)
1509      {
1510        ArrayChar.D2 fieldNames   = (ArrayChar.D2) exp.read();
1511        varShape = exp.getShape();
1512        numField = varShape[0];
1513      }
1514      else
1515      {
1516        numField=0;
1517      }
1518
1519      exp = ncfile.findVariable("vectorNames");
1520      if(exp!=null)
1521      {
1522        ArrayChar.D2 vectorNames   = (ArrayChar.D2) exp.read();
1523        varShape = exp.getShape();
1524        numVector = varShape[0];
1525      }
1526      else
1527      {
1528        numVector=0;
1529      }
1530      
1531      exp = ncfile.findVariable("intraNames");
1532      if (exp != null)
1533      {
1534          varShape = exp.getShape();
1535          numIntra = varShape[0];
1536      }
1537      else
1538      {
1539          numIntra = 0;
1540      }
1541
1542      exp = ncfile.findVariable("subexpNames");
1543      subexpNames = (ArrayChar.D2) exp.read();
1544
1545      exp = ncfile.findVariable("xEdgeLen");
1546      xEdge = exp.readScalarInt();
1547
1548      exp = ncfile.findVariable("yEdgeLen");
1549      yEdge = exp.readScalarInt();
1550
1551    }
1552    public static void stop() throws Exception
1553    ////////////////////////////////////////////////////////////////////////
1554    {
1555      ncfile.close();
1556    }
1557
1558    public void getFrame(int subExp,int recorderType, int recorderID) throws Exception
1559    ////////////////////////////////////////////////////////////////////////
1560    {
1561      if(recorderType==0) // Single-Unit
1562      {
1563        ucar.nc2.Dimension exp = ncfile.findDimension("suEleId");
1564        if(exp!=null)
1565        {
1566          PlotResult frame = new PlotResult("SU Raster "+subexpNames.getString(subExp));
1567          frame.nowValue=recorderID;
1568          frame.removeWindowListener(frame.getWindowListeners()[0]);
1569
1570          frame.currExp = subExp;
1571          JPanel jpanel = frame.createSuPanel(subExp);
1572          JPanel command = new JPanel();
1573          for(int jj = 0 ; jj< frame.suSeries; jj++)
1574          {
1575            JCheckBox check = new JCheckBox(frame.suNames.getString(jj));
1576            check.setActionCommand(new Integer(jj).toString());
1577            check.addActionListener(frame);
1578            if(jj==recorderID)
1579            {
1580              check.setSelected(true);
1581            }
1582            else
1583            {
1584              check.setSelected(false);
1585            }
1586            command.add(check);
1587          }
1588
1589          JButton pre = new JButton("SaveEPS");
1590          pre.setActionCommand("SUsave");
1591          pre.addActionListener(frame);
1592          command.add(pre);
1593
1594          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1595          //frame.setContentPane(jpanel);
1596          frame.add(jpanel);
1597          frame.add(command,"South");
1598          frame.pack();
1599          RefineryUtilities.centerFrameOnScreen(frame);
1600          frame.setVisible(true);
1601        }
1602
1603      }
1604      if(recorderType==1) // Multi-Unit
1605      {
1606        ucar.nc2.Dimension exp = ncfile.findDimension("muEleId");
1607        if(exp!=null)
1608        {
1609          PlotResult frame = new PlotResult("MU Rateplot "+subexpNames.getString(subExp));
1610          frame.nowValue=recorderID;
1611          frame.removeWindowListener(frame.getWindowListeners()[0]);
1612          frame.currExp = subExp;
1613          JPanel jpanel = frame.createMUPanel(subExp);
1614
1615          JPanel command = new JPanel();
1616          for(int jj = 0 ; jj< frame.suSeries; jj++)
1617          {
1618            JCheckBox check = new JCheckBox(frame.suNames.getString(jj));
1619            check.setActionCommand(new Integer(jj).toString());
1620            check.addActionListener(frame);
1621            if(jj==recorderID)
1622            {
1623              check.setSelected(true);
1624            }
1625            else
1626            {
1627              check.setSelected(false);
1628            }
1629
1630            command.add(check);
1631          }
1632
1633          JButton pre = new JButton("SaveEPS");
1634          pre.setActionCommand("MUsave");
1635          pre.addActionListener(frame);
1636          command.add(pre);
1637
1638
1639
1640          jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1641          //    frame.setContentPane(jpanel);
1642          frame.add(jpanel);
1643          frame.add(command,"South");
1644          frame.pack();
1645          RefineryUtilities.centerFrameOnScreen(frame);
1646          frame.setVisible(true);
1647        }
1648      }
1649      if(recorderType==2) // Field
1650      {
1651        PlotResult frame = new PlotResult("FieldPlot "+subexpNames.getString(subExp)+" Id "+recorderID);
1652
1653        //System.out.println(frame.getWindowListeners().length);
1654        frame.removeWindowListener(frame.getWindowListeners()[0]);
1655        frame.currExp = subExp;
1656        frame.currField = recorderID;
1657        frame.readField(subExp,recorderID);
1658        frame.titleField= frame.fieldNames.getString(recorderID);
1659        JPanel jpanel = frame.createFieldPanel();
1660
1661        JPanel command = new JPanel();
1662
1663        JButton pre = new JButton("<-");
1664        pre.setActionCommand("F_pre");
1665        pre.addActionListener(frame);
1666        command.add(pre);
1667        JButton next = new JButton("->");
1668        next.setActionCommand("F_next");
1669        next.addActionListener(frame);
1670        command.add(next);
1671
1672        pre = new JButton("SaveEPS");
1673        pre.setActionCommand("Fieldsave");
1674        pre.addActionListener(frame);
1675        command.add(pre);
1676
1677
1678
1679        jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1680        //        frame.setContentPane(jpanel);
1681        frame.add(jpanel);
1682        frame.add(command,"South");
1683        frame.pack();
1684        RefineryUtilities.centerFrameOnScreen(frame);
1685        frame.setVisible(true);
1686      }
1687      if(recorderType==3) // Vector
1688      {
1689        PlotResult frame = new PlotResult("VectorPlot "+subexpNames.getString(subExp)+" Id "+recorderID);
1690        frame.removeWindowListener(frame.getWindowListeners()[0]);
1691        frame.currExp = subExp;
1692        frame.currField = recorderID;
1693        frame.readVector(subExp,recorderID);
1694        frame.titleField= frame.vectorNames.getString(recorderID);
1695        JPanel jpanel = frame.createVectorPanel();
1696
1697        JPanel command = new JPanel();
1698
1699        JButton pre = new JButton("<-");
1700        pre.setActionCommand("V_pre");
1701        pre.addActionListener(frame);
1702        command.add(pre);
1703        JButton next = new JButton("->");
1704        next.setActionCommand("V_next");
1705        next.addActionListener(frame);
1706        command.add(next);
1707
1708        pre = new JButton("SaveEPS");
1709        pre.setActionCommand("Vectorsave");
1710        pre.addActionListener(frame);
1711        command.add(pre);
1712
1713        jpanel.setPreferredSize(new java.awt.Dimension(xEdge+50,yEdge+50));
1714//        frame.setContentPane(jpanel);
1715        frame.add(jpanel);
1716        frame.add(command,"South");
1717        frame.pack();
1718        RefineryUtilities.centerFrameOnScreen(frame);
1719        frame.setVisible(true);
1720      }
1721      if(recorderType==4) // Intracellular
1722      {
1723          PlotResult frame = new PlotResult ( "Intracellular Plot " + subexpNames.getString( subExp ) + " Id "+ recorderID );
1724          frame.removeWindowListener(frame.getWindowListeners()[0]);
1725          frame.currExp = subExp;
1726          frame.currIntra = recorderID;
1727          frame.readIntra( subExp , recorderID );
1728          frame.titleField = frame.intraNames.getString( recorderID );
1729
1730          JPanel intraPanel = frame.createIntraPanel();
1731          intraPanel.setPreferredSize ( new java.awt.Dimension( xEdge + 50, yEdge + 50 ));
1732
1733          JPanel commandPanel = new JPanel();
1734
1735          JButton saveButton = new JButton( "SaveEPS" );
1736          saveButton.setActionCommand("Intrasave");
1737          saveButton.addActionListener(frame);
1738
1739          commandPanel.add( saveButton );
1740
1741          frame.add( intraPanel );
1742          frame.add( commandPanel , "South" );
1743          frame.pack( );
1744          RefineryUtilities.centerFrameOnScreen(frame);
1745          frame.setVisible( true );
1746      }     
1747    }
1748
1749    ////////////////////////////////////////////////////////////////////////
1750    ////////////////////////////////////////////////////////////////////////
1751    }