snippet.swim_io_sampleΒΆ

from irfpy.swim.swimio import *
from irfpy.swim import SwimSciCount
import urllib.request, urllib.parse, urllib.error
from Sadppac import *

def main():

    ## As a sample, you can read BMU format file.
    bmu = SwimSciCount.getBmuUri(939)
    fnam, inf=urllib.request.urlretrieve(bmu)

    reader = SwimBmuReader()
    reader.readFile(fnam)
    sarray=reader.getArray()

    ## The dump is applied to the array of the data set.
    datarr=[]

    ## Fill the array.
    for i in range(10):

        ## Each data is obtained by SwimPacket format.
        spac=sarray.getPacket(i)

        ## Instance to dump is SwimCounterDataV1 for version 1.
        s=io.SwimCounterDataV1()

        ## The SwimCounterDataV1 can be filled by setFromPacket method.
        ## If you want to store 'processed' data, you may need to
        ## use setData() method.
        s.setFromPacket(spac)

        ## Add the instance of SwimCounterData to an array.
        datarr.append(s)

    header = """This is raw data dumped to the SwimCounterData format.
Only the start counter is shown, and the data was no processing.
"""
    ## A sample to dump.
    f=open('dump1', 'w')
    dump(datarr, f, header=header)
    f.close()

    ## A sample to load.
    f=open('dump1', 'r')
    d2=load(f)
    f.close()

    ## The loaded data is again dumped.
    f=open('dump2', 'w')
    dump(d2, f, header=header)
    f.close()

    print('Please confirm the results by\n  diff dump1 dump2\nBoth should be the same')

if __name__ == "__main__":
    main()