# test for regressions in write methods for each plugin.
# See also exportToRich.jy
#

reset()

ds1= ripplesTimeSeries(60)
ds2= ripplesTimeSeries(60) + randomn(3554,60)
ds3= ripplesWaveformTimeSeries(60)
ds4= ripplesSpectrogramTimeSeries(60)
ds5= ripplesVectorTimeSeries(60)

print 'Line11: ', ds1
print 'Line12: ', ds2
print 'Line13: ', ds3  #TODO: formatting
print 'Line14: ', ds4
print 'Line15: ', ds5

from java.io import File
pwd= File('.').getAbsolutePath()
pwd= pwd[:-1] # remove the . in /.

print pwd

formatDataSet(ds1,'test_format_004.cdf?ds1')
formatDataSet(ds2,'test_format_004.cdf?ds2&append=T')
formatDataSet(ds4,'test_format_004.cdf?ds4&append=T')
formatDataSet(ds5,'test_format_004.cdf?ds5&append=T')
ds= getDataSet(pwd+'test_format_004.cdf?ds5')
print 'Line26: should be the same: ', ds5, ds

#formatDataSet(ds1,'test_format_006.hdf5?metadata=istp')  # TODO see https://sourceforge.net/p/autoplot/bugs/2538/
#formatDataSet(ds2,'test_format_006.hdf5?ds2&append=T')
#formatDataSet(ds2,'test_format_006.hdf5?ds4&append=T')
#ds= getDataSet(pwd+'test_format_006.hdf5')
#print 'Line32: should be the same: ', ds1, ds

formatDataSet(ds1,'test_format_008.dat')

formatDataSet(ds3,'test_format_011.wav')
ds= getDataSet(pwd+'test_format_011.wav')
print 'should be the same number of data points: ', ds3, ds

formatDataSet(ds2,'test_format_012.idlsav')
formatDataSet(bundle(ds1,ds2),'test_format_013.xls')
formatDataSet(ds1,'test_format_014.xls')
formatDataSet(ds4,'test_format_014.xls?append=T&sheet=mysheet2&cell=B3')

#formatDataSet(bundle(ds1,ds2),'test_format_017.dat')  # can't do bundle
formatDataSet(ds5,'test_format_021.dat')
formatDataSet(ds1,'test_format_015.dat')
formatDataSet(ds1,'test_format_016.bin')
#formatDataSet(ds4,'test_format_017.bin')  # can't do rank 2
formatDataSet(ds4,'test_format_017.dat')

# demo dataset schemes supported by export to rich ascii (This was exportToRich.jy)

ds= ds5
formatDataSet( ds,'test037_rich_vector.txt?header=rich')
print 'Line56: test037_rich_vector.txt'

ds= ds2
formatDataSet( ds,'test037_rich_scalar.txt?header=rich')
print 'Line60: test037_rich_scalar.txt'

ds= ds4
formatDataSet( ds,'test037_rich_spectrogram.txt?header=rich')
print 'Line64: test037_rich_spectrogram.txt'

nn= getDataSet('vap+inline:ripplesTimeSeries(200)&NAME=Density')
tt= getDataSet('vap+inline:ripplesTimeSeries(200)*2&NAME=Temperature')
bvec= getDataSet('vap+inline:ripplesVectorTimeSeries(200)&NAME=Bfield')
bmag= magnitude(bvec)
ds= bundle( nn, tt, bmag ) 
ds= putProperty( ds, QDataSet.DEPEND_0, nn.property( QDataSet.DEPEND_0 ) )
formatDataSet( ds,'test037_rich_simple_bundle.txt?header=rich')
print 'Line73: test037_rich_simple_bundle.txt'

#ds= bundle( nn, tt, bmag, bvec ) 
#formatDataSet( ds,'test037_rich_bundle.txt?header=rich')
#print 'test037_rich_bundle.txt'

# end, exportToRich.

ff= listDirectory(pwd+'test_format*')
for f in ff:
    print 'Line83: %9d %s' % ( File(f).length(), f )
    
#setCanvasSize(750,750)
setLayout(4,3)  # Note setLayout should be done after setCanvasSize for predictable result.

dom.getElementById('plot_9').zaxis.visible=True
waitUntilIdle()
dom.options.autolayout= False

dom.canvases[0].marginRow.top='2em'
dom.canvases[0].marginRow.bottom='100.00%-1.0em'

print 'marginRow: ', dom.canvases[0].marginRow.top, ', ', dom.canvases[0].marginRow.bottom
print 'rows[0]: ', dom.canvases[0].rows[0].top, ', ', dom.canvases[0].rows[0].bottom

plot( 0, pwd + 'test_format_004.cdf?ds1', title='cdf' )
plot( 1, pwd + 'test_format_004.cdf?ds4', title='cdf' )
plot( 2, pwd + 'test_format_004.cdf?ds5', title='cdf' )
plot( 3, pwd + 'test_format_015.dat', title='dat' )
#plot( 4, pwd + 'test_format_014.xls?ds5&depend0=A&column=1:10&sheet=mysheet2', title='xls' )
plot( 5, pwd + 'test_format_006.hdf5?Variable_1', title='hdf' )
plot( 6, pwd + 'test_format_011.wav', title='wav' )
plot( 8, pwd + 'test037_rich_spectrogram.txt?Flux', title='rich ascii' )
plot( 9, pwd + 'test037_rich_scalar.txt?data', title='rich ascii' )
plot( 10, pwd + 'test037_rich_simple_bundle.txt?Density', title='rich ascii' )
plot( 11, pwd + 'test037_rich_vector.txt?data', title='rich ascii' )

dom.canvases[0].marginRow.top='2em'
dom.canvases[0].marginRow.bottom='100.00%-1.0em'

print 'Line109: ',pwd
print 'marginRow: ', dom.canvases[0].marginRow.top, ', ', dom.canvases[0].marginRow.bottom
print 'rows[0]: ', dom.canvases[0].rows[0].top, ', ', dom.canvases[0].rows[0].bottom

writeToPng('test037_formatDataSet.png')
save('test037_formatDataSet.vap')