#!/usr/bin/env python

import numpy as np
import sys


lon1d, lat1d, bx1d, by1d, bz1d= np.loadtxt("BSharp-sm.txt", skiprows=4, unpack=True)
nlon=688
nlat=448
lon=lon1d.reshape((nlat,nlon))
lat=lat1d.reshape((nlat,nlon))
bx=bx1d.reshape((nlat,nlon))
by=by1d.reshape((nlat,nlon))
bz=bz1d.reshape((nlat,nlon))
dlon = lon[0,1]-lon[0,0]
dlat = lat[1,0]-lat[0,0]

left=lon1d[0]
right=lon1d[-1]
bottom=lat1d[0]
top=lat1d[-1]

vtk_header="""# vtk DataFile Version 2.0
File generated by picture_to_vtk.py script
ASCII
DATASET STRUCTURED_POINTS
DIMENSIONS {} {} 1
SPACING {} {} 1.0
ORIGIN {} {}  0
POINT_DATA {}""".format(nlon,nlat,dlon,dlat,left, bottom, nlon*nlat)
file1 = open("test.vtk", "w")
file1.write(vtk_header)




headerdataset="""
SCALARS bc_data float 1
LOOKUP_TABLE default
"""

##WRITE BX
file1.write(headerdataset)
#file1.write(" ".join(map(lambda x : str(x), bx.transpose().flatten())))
file1.write(" ".join(map(lambda x : str(x), bx1d)))

##WRITE BY
file1.write(headerdataset)
file1.write(" ".join(map(lambda x : str(x), by1d)))
#file1.write(" ".join(map(lambda x : str(x), by.transpose().flatten())))

##WRITE BZ
file1.write(headerdataset)
#file1.write(" ".join(map(lambda x : str(x), bz.transpose().flatten())))
file1.write(" ".join(map(lambda x : str(x), bz1d)))

file1.close()

