# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# $HeadURL$
# $Id$


from Sloppy.Base.plugin import PluginRegistry

import logging
logger = logging.getLogger('plugin')

try:
    import pygsl
except ImportError:
    logger.error("Could not find pygsl. Plugin skipped.")
else:
    logger.info("pygsl successfully loaded.")
    pygsl.import_all()

    class Plugin:        
        def __init__(self, app):
            self.app = app
            self.pygsl = pygsl
        
    PluginRegistry.register("pygsl", Plugin)
        ds2 = Dataset()
        ds2.key = pdict.unique_key(project.datasets, "exp_ds2")
        ds2.data = Table(colcount=2, rowcount=4)
        ds2.data[0] = [10,17,3,8]
        ds2.data[1] = [1,89,48,1]

        ulist.append( project.datasets, ds, undolist=ul )
        ulist.append( project.datasets, ds2, undolist=ul )                

        plot = Plot()
        plot.key = pdict.unique_key(project.plots, "exp_plot")
        layer1 = Layer(type="line2d",
                       lines=[Line(source=ds,cx=0,cy=1), Line(source=ds2)],
                       x=0.0, y=0.0, width=1.0, height=0.5)
        layer2 = Layer(type="line2d",
                       lines=[Line(source=ds2,cx=0,cy=1)],
                       x=0.0, y=0.5, width=1.0, height=0.5)
        plot.layers = [layer1, layer2]
#        plot.layers.arrange(rowcount=1, colcount=2)
        
        ulist.append( project.plots, plot, undolist=ul )

        uwrap.emit_last(project.datasets, "changed")
        undolist.append(ul)
        
        



PluginRegistry.register("Default", Plugin)
            undolist = project.journal
            
        table = dataset.data   # TODO: check for Table
            
        p = Plot( key = pdict.unique_key(project.plots, "Spectrum %s" % dataset.key),
                  layers = [Layer(type='line2d',
                                  lines=[Line( label=dataset.key,
                                               source=dataset,
                                               cx=1,
                                               cy=2 )
                                         ],
                                  axes={'y' : Axis(scale="log",
                                                   label='SIMS intensity (cts/sec)',
                                                   start=10,
                                                   format='%2.1e'),
                                        'x' : Axis(scale="linear",
                                                   label='mass (amu)')},                                  
                                  title=u"SIMS mass spectrum of %s" % dataset.key,
                                  )
                            ]
                  
                  )

        project.add_plot(p, undolist=undolist)
        



#------------------------------------------------------------------------------
PluginRegistry.register("SIMS", Plugin)