import sys
print(sys.version)
sys.dont_write_bytecode = True


#
# initial stuff

from adcploader import *

p_raw = RawProfileObj('../testfiles/demodata.txt')

startingpoint = dict(start=Vector(0,0,0), offset=Vector(0,0))
processing_settings = dict(proj_method=3)

p0 = ProcessedProfileObj(p_raw, processing_settings, startingpoint)

#plot_profile_2d(p0)

plot_2d_cfg = dict(saveas='../testfiles/tut_demo1.pdf')
# which is the same as 
plot_2d_cfg = {'saveas':'../testfiles/tut_demo1.pdf'}
#plot_profile_2d(p0)



import math
startingpoint_1a = dict(start=Vector(0,0,0), dir=0.5*math.pi)
startingpoint_1b = dict(start=Vector(0,0,0), end=Vector(0,1,0))
processing_settings_1a = dict(proj_method=1)
processing_settings_1b = dict(proj_method=3)
p1a = ProcessedProfileObj(p_raw, processing_settings_1a, startingpoint_1a)
p1b = ProcessedProfileObj(p_raw, processing_settings_1b, startingpoint_1b)
#plot_profile_2d(thin_out(p1a, {'keep_ensemble':5}), {'saveas':'../testfiles/tut_demo1a.pdf', 'title':'demo 1a'})
#plot_profile_2d(thin_out(p1b, {'keep_ensemble':15}), {'saveas':'../testfiles/tut_demo1b.pdf', 'title':'demo 1b'})


#plot_profile_3d(p0, cfg={'saveas':'../testfiles/tut_demo0_3d.pdf'})

#
# outliers

p2 = interpolate_outliers(p0, cfg={'limit':2.5, 'radius_h':10})
#plot_profile_3d(p2, cfg={'saveas':'../testfiles/tut_demo2.pdf'})

#
# averaging
p3 = get_averaged_profile(p2, cfg={'order':21})
#plot_profile_3d(p3, cfg={'saveas':'../testfiles/tut_demo3.pdf'})
 

#
# roughness
cfg_logfit = {'logheight':0.30, 'component':3}
p4 = logfit_profile(p3, cfg_logfit)
#plot_logfit_profile(p4, cfg=cfg_logfit)
# save as tut_demo4.pdf manually

#
# extrapolation
p5 = extrapolate_profile(p4, cfg={'topcells':5, 'forcepowerlaw':True})
#plot_profile_3d(p5, cfg={'saveas':'../testfiles/tut_demo5.pdf'})

# runs into numerical problems:
#p5a = extrapolate_profile(p4, cfg={'topcells':5, 'forcepowerlaw':False})
#plot_profile_3d(p5a, cfg={'saveas':'../testfiles/tut_demo_p5a.pdf'})


#
# visualization

#plot_profile_3d(p3, cfg=dict(style='contour', saveas='../testfiles/tut_demo6a.pdf', title='contour demo'))

#
# visualization: secondary flow
p0_sf = ProcessedProfileObj(p_raw, dict(proj_method=3, uv_rot=1), startingpoint)
p1_sf = get_averaged_profile(p2, cfg={'order':5})
#plot_profile_3d(thin_out(p1_sf, dict(keep_ensemble=5)), cfg=dict(style='vector', components=['y','z'], saveas='../testfiles/tut_demo6b.pdf', title='vector demo'))


#
# DXF output
writeDXF3D(p5, '../testfiles/tut_demo7_p5_3d.dxf', vel_scale=40)
writeDXF2D(p5, '../testfiles/tut_demo7_p5_2d.dxf', vel_scale=40)


#
# ASCII output
writeAscii3D(p5, '{x} {y} {z} {vx} {vy} {vz}', '../testfiles/tut_demo8_velocities_simple.txt')
import datetime
header='# generated on: {} \n# x      y     z      v_x       v_y       v_z\n'.format(datetime.datetime.now().strftime('%x %X'))
writeAscii3D(p5, '{x:6.2f} {y:6.2f} {z:6.2f} {vx:9.2e} {vy:9.2e} {vz:9.2e}', '../testfiles/tut_demo8_velocities_awesome.txt', header=header)


header2d='# generated on: {} \n# x      y     ks        tau_shear  v_shear\n'.format(datetime.datetime.now().strftime('%x %X'))
writeAscii2D(p4, '{x:6.2f} {y:6.2f} {ks:9.2e} {tau_shear:9.2e} {v_shear:9.2e}', '../testfiles/tut_demo8_rouhgness.txt', header=header2d, voidtext=-999)



print('end.')