DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Generating timestamps

Solved!
Go to solution

Hi,

 

I have encountered a problem with generating a timestamp channel out of integer channels for years, months, days, hrs, min and sec. All these channels have been read from a binary file as eI32s. Now, I would like to genereate a timestamp from them in the order #YYYY.MM.DD hh:mm:ss but nothing I have tried works. Ideally, I do not want to loop through the channel values, much preferring to call a calculate function like:

 

Call Calculate("Ch(""[1]/date"")=DateSerial(Ch(""[1]/year""),Ch(""[1]/month""),Ch(""[1]/day""),""#YYYY.MM.DD"")",NULL,NULL,"")

 

Should I convert the integer channels I have to text? If so how does one do that? A command like: Call Calculate("Ch(""[1]/textyear"")=str(Ch(""[1]/intyear"")),NULL,NULL,"") doesn't work on channels.

Should I have read the values as strings from the binary channel with the plugin in the first place?

 

Is there a better way of generating a channel of time stamps bits of which are spread across other channels?

 

Thanks in advance.

 

 

0 Kudos
Message 1 of 4
(6,146 Views)

Hi tspc,

 

Your best bet is to do the conversion inside the DataPlugin you used to load the channels, employing a ProcessedChannel to add values and each DirectAccessChannel's Channel.Factor property to scale each to seconds.

 

If for some reason it doesn't work to have this happen automatically when you drag the file into the Data Portal, you're best bet is to perform the scaling to seconds and adding with the Channel Calculator, as you have already suggested.  There is no benefit to converting the channels from integers to strings, and you'll be better off with multiplying and adding the integers together numerically than using date/time functions.

 

Could you submit an example of your data?

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 2 of 4
(6,143 Views)

Hi Brad,

 

The data read with the plugin from the binary file looks like the following:

 

111 2 14 12 10 28
111 2 14 12 10 29
111 2 14 12 10 31
111 2 14 12 10 32
111 2 14 12 10 33
111 2 14 12 10 34
etc

 

corresponding to YYYY since 1900, Months since January, Hrs(24hrs format), minutes and seconds.

As it happens they are at the head of each block of binary data so the plugin code to read thems is simply:

 

Dim oBlock : Set oBlock = File.GetBinaryBlock()
oBlock.BlockWidth = 3424
Dim oChannelGroup : Set oChannelGroup = Root.ChannelGroups.Add(File.Info.FileName)

Dim oChannel1 : Set oChannel1 = oBlock.Channels.Add("sec", eI32) '4 bytes
Dim oChannel2 : Set oChannel2 = oBlock.Channels.Add("min", eI32)
Dim oChannel3 : Set oChannel3 = oBlock.Channels.Add("hrs", eI32)
Dim oChannel4 : Set oChannel4 = oBlock.Channels.Add("day", eI32)
Dim oChannel5 : Set oChannel5 = oBlock.Channels.Add("month", eI32)
Dim oChannel6 : Set oChannel6 = oBlock.Channels.Add("year", eI32)

Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel1)
Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel2)
Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel3)
Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel4)
Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel5)
Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel6)

 

From there on I am yet to figure out how to best use the ProcessedChannel and the DirectAccessChannel's Channel.Factor.

 

Best Regards,

0 Kudos
Message 3 of 4
(6,138 Views)
Solution
Accepted by topic author tspc

Hi tspc,

 

I spoke too soon and/or withhout thinking deeply enough.  Below you will find a DataPlugin which indeed uses the Channel.Factor property and a ProcessedChannel to combine only the hours, minutes, and seconds channels, but the date is not linear because the days per month and days per year vary:

 

Sub ReadStore(File)
  Dim oBlock : Set oBlock = File.GetBinaryBlock()
  oBlock.BlockWidth = 3424
  Dim oChannelGroup : Set oChannelGroup = Root.ChannelGroups.Add(File.Info.FileName) 
  Dim oChannel0 : Set oChannel0 = oChannelGroup.Channels.AddProcessedChannel("DateTime", eR64, eAddProcessor)
  Dim oChannel1 : Set oChannel1 = oBlock.Channels.Add("year", eI32) : oChannel1.Offset = 1900
  Dim oChannel2 : Set oChannel2 = oBlock.Channels.Add("month", eI32)
  Dim oChannel3 : Set oChannel3 = oBlock.Channels.Add("day", eI32)
  Dim oChannel4 : Set oChannel4 = oBlock.Channels.Add("hrs", eI32) : oChannel4.Factor = 3600
  Dim oChannel5 : Set oChannel5 = oBlock.Channels.Add("min", eI32) : oChannel5.Factor = 60
  Dim oChannel6 : Set oChannel6 = oBlock.Channels.Add("sec", eI32)
  Call oChannel0.Channels.Add(oChannel4)
  Call oChannel0.Channels.Add(oChannel5)
  Call oChannel0.Channels.Add(oChannel6)
  oChannel0.Properties.Add "DisplayType", "Time"
  Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel1)
  Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel2)
  Call oChannelGroup.Channels.AddDirectAccessChannel(oChannel3)
End Sub

 

You need to call this DataPlugin to load the data files and then run a channel calculation to turn the year, month, and day channels into a real number of elapsed seconds since 0 AD and add them to the time channel, like this:

 

Call Data.Root.Clear
DataFilePath = "C:\NICS\Discussion Forums\tspc\DataFile.abc"
Call DataFileLoad(DataFilePath, "tspc_abc")

Set Channels = Data.Root.ActiveChannelGroup.Channels
Symbols  = Array("dt", "yr", "mon", "day")
ChanRefs = Array(Channels(1), Channels(2), Channels(3), Channels(4))
Equation = "dt = dt + TTR(yr & ""/"" & Right(""00"" & mon, 2) & ""/"" & Right(""00"" & day, 2), ""#yyyy/mm/dd"")"
Call Calculate(Equation, Symbols, ChanRefs)
Call Channels.Remove(Channels(4).Name)
Call Channels.Remove(Channels(3).Name)
Call Channels.Remove(Channels(2).Name)

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 4 of 4
(6,119 Views)