#!/bin/env python
import PyTango
import json
from datetime import datetime
from pytz import timezone
## NeXus file name and timezone info
nxsfile = '/tmp/example1.nxs'
berlin = timezone('Europe/Berlin')
fmt = '%Y-%m-%dT%H:%M:%S.%f%z'
# Selector Server
selector = PyTango.DeviceProxy("p09/nxsrecselector/hastodt")
# NeXus Writer
writer = PyTango.DeviceProxy(selector.WriterDevice)
# selected components
components = list(selector.Components or [])
# user data
datarecord = selector.UserData
# configuration variables
confvars = selector.ConfigVariables
selector.updateConfigVariables()
# create configuration
finalXML = selector.CreateWriterConfiguration(components)
print finalXML
# open the file
writer.Init()
writer.FileName = nxsfile
writer.OpenFile()
writer.XMLSettings = finalXML
# get start_time
starttime = berlin.localize(datetime.now())
# open the entry
record = {}
record["data"] = json.loads(datarecord)
record["data"]["start_time"] = str(starttime.strftime(fmt))
writer.JSONRecord = json.dumps(record)
writer.OpenEntry()
# experimental loop
for i in range(10):
srecord = {}
## setting CLIENT data
srecord["data"] = {"point_nb": i}
writer.Record(json.dumps(srecord))
#close the entry
endtime = berlin.localize(datetime.now())
record["data"]["end_time"] = str(endtime.strftime(fmt))
writer.JSONRecord = json.dumps(record)
writer.CloseEntry()
#close the file
writer.CloseFile()