Exemplo n.º 1
0
def test_nefis_putels_strings():
    #-------------------------------------------------------------------------------
    error, version = nefis.getnfv()
    print('')
    print('Library version: %s' % version[4:])
    print('')
    #-------------------------------------------------------------------------------
    dat_file = 'putels.dat'
    def_file = 'putels.def'
    coding = ' '
    ac_type = 'c'
    fp = -1
    print("------------")
    print(dat_file)
    print(def_file)
    print(coding)
    print(ac_type)
    print("------------")
    #-------------------------------------------------------------------------------
    error, fp = nefis.crenef(dat_file, def_file, coding, ac_type)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---defelm---')
    elm_name = 'Element 1'
    elm_type = 'character'
    elm_single_byte = 20
    elm_quantity = 'names'
    elm_unit = '[-]'
    elm_description = 'Discharge station names'
    elm_count = 2
    #elm_data = np.arange(15).reshape(5,3)
    elm_dimensions = np.arange(elm_count).reshape(elm_count)
    elm_dimensions[0] = 2
    elm_dimensions[1] = 3

    error = nefis.defelm(fp, elm_name, elm_type, elm_single_byte, elm_quantity, elm_unit, elm_description, elm_count, elm_dimensions)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---defcel---')
    cel_name = 'Cell 1'
    cel_names_count = 1
    elm_names = ['Element 1']

    error = nefis.defcel(fp, cel_name, cel_names_count, elm_names)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---defgrp---')
    grp_defined = 'Grp 1'
    cel_name = 'Cell 1'
    grp_count = 1
    grp_dimensions = np.arange(5).reshape(5)
    grp_dimensions[0] = 11
    grp_dimensions[1] = 0
    grp_dimensions[2] = 0
    grp_dimensions[3] = 0
    grp_dimensions[4] = 0
    grp_order = np.arange(5).reshape(5)
    grp_order[0] = 1
    grp_order[1] = 2
    grp_order[2] = 3
    grp_order[3] = 4
    grp_order[4] = 5

    error = nefis.defgrp(fp, grp_defined, cel_name, grp_count, grp_dimensions, grp_order)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---credat---')
    grp_name = 'Group 1'
    grp_defined = 'Grp 1'

    error = nefis.credat(fp, grp_name, grp_defined)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')





    #-------------------------------------------------------------------------------
    print('---putels---')
    usr_index = np.arange(15).reshape(5,3)
    usr_index[0,0] = 11
    usr_index[0,1] = 11
    usr_index[0,2] = 1
    usr_index[1,0] = 0
    usr_index[1,1] = 0
    usr_index[1,2] = 0
    usr_index[2,0] = 0
    usr_index[2,1] = 0
    usr_index[2,2] = 0
    usr_index[3,0] = 0
    usr_index[3,1] = 0
    usr_index[3,2] = 0
    usr_index[4,0] = 0
    usr_index[4,1] = 0
    usr_index[4,2] = 0
    np.ascontiguousarray(usr_index, dtype=np.int32)
    usr_order = np.arange(5).reshape(5)
    usr_order[0] = 1
    usr_order[1] = 2
    usr_order[2] = 3
    usr_order[3] = 4
    usr_order[4] = 5
    np.ascontiguousarray(usr_order, dtype=np.int32)
    grp_name = 'Group 1'
    elm_name = 'Element 1'
    #names = ['Name 11', 'Name 21', 'Name 12', 'Name 22', 'Name 13', 'Name 23']
    names = ('Name 11', 'Name 21', 'Name 12', 'Name 22', 'Name 13', 'Name 23')
    #names = [['Name 11', 'Name 21', 'Name 12'], [ 'Name 22', 'Name 13', 'Name 23']]
    error = nefis.putels(fp, grp_name, elm_name, usr_index, usr_order, names)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---clsnef---')
    error = nefis.clsnef(fp)
    print('------------')
Exemplo n.º 2
0
def test_nefis_putelt_integers():
    #-------------------------------------------------------------------------------
    error, version = nefis.getnfv()
    print('')
    print('Library version: %s' % version[4:])
    print('')
    #-------------------------------------------------------------------------------
    dat_file = 'putelt.dat'
    def_file = 'putelt.def'
    coding = ' '
    ac_type = 'c'
    fp = -1
    print("------------")
    print(dat_file)
    print(def_file)
    print(coding)
    print(ac_type)
    print("------------")
    #-------------------------------------------------------------------------------
    error, fp = nefis.crenef(dat_file, def_file, coding, ac_type)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    elm_name = 'Elm 3'
    elm_type = 'INTEGER'
    elm_single_byte = 4
    elm_quantity = 'integers'
    elm_unit = '[-]'
    elm_description = 'Just integers'
    elm_count = 2
    #elm_data = np.arange(15).reshape(5,3)
    elm_dimensions = np.arange(elm_count).reshape(elm_count)
    elm_dimensions[0] = 20
    elm_dimensions[1] = 5
    error = nefis.defelm(fp, elm_name, elm_type, elm_single_byte, elm_quantity, elm_unit, elm_description, elm_count, elm_dimensions)
    print('NEFIS error code (defelm): %d' % error)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : %s' % err_string)
        print("=========")
    #-------------------------------------------------------------------------------
    print('---defcel---')
    cel_name = 'Cell 1'
    cel_names_count = 1
    elm_names = ['Elm 3']

    error = nefis.defcel(fp, cel_name, cel_names_count, elm_names)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---defgrp---')
    grp_defined = 'Grp 1'
    cel_name = 'Cell 1'
    grp_count = 1
    grp_dimensions = np.arange(5).reshape(5)
    grp_dimensions[0] = 11
    grp_dimensions[1] = 0
    grp_dimensions[2] = 0
    grp_dimensions[3] = 0
    grp_dimensions[4] = 0
    grp_order = np.arange(5).reshape(5)
    grp_order[0] = 1
    grp_order[1] = 2
    grp_order[2] = 3
    grp_order[3] = 4
    grp_order[4] = 5

    error = nefis.defgrp(fp, grp_defined, cel_name, grp_count, grp_dimensions, grp_order)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---credat---')
    grp_name = 'Group 1'
    grp_defined = 'Grp 1'

    error = nefis.credat(fp, grp_name, grp_defined)
    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')





    #-------------------------------------------------------------------------------
    print('---putelt---')
    usr_index = np.arange(15).reshape(5,3)
    usr_index[0,0] = 11
    usr_index[0,1] = 11
    usr_index[0,2] = 1
    usr_index[1,0] = 0
    usr_index[1,1] = 0
    usr_index[1,2] = 0
    usr_index[2,0] = 0
    usr_index[2,1] = 0
    usr_index[2,2] = 0
    usr_index[3,0] = 0
    usr_index[3,1] = 0
    usr_index[3,2] = 0
    usr_index[4,0] = 0
    usr_index[4,1] = 0
    usr_index[4,2] = 0
    np.ascontiguousarray(usr_index, dtype=np.int32)
    usr_order = np.arange(5).reshape(5)
    usr_order[0] = 1
    usr_order[1] = 2
    usr_order[2] = 3
    usr_order[3] = 4
    usr_order[4] = 5
    np.ascontiguousarray(usr_order, dtype=np.int32)
    grp_name = 'Group 1'
    elm_name = 'Elm 3'
    floot = np.zeros(100, dtype=int, order='C')
    #np.ascontiguousarray(floats, dtype=np.float32)
    floats = np.zeros((20,5), dtype=int, order='C')
    for i in range(20):
        for j in range(5):
            k = j*20+i
            floats[i,j] = (i+1)*(j+1)
            floot[k] = floats[i,j]
    print(floats)
    print('')

    fmt = "%di" % (len(floot))
    numbers = struct.pack(fmt, *floot)

    #numbers= array.array('L', floot)

    #numbers = bytes()
    #numbers = numbers.join((struct.pack('i', val) for val in floot))

    #numbers = floot

    error = nefis.putelt(fp, grp_name, elm_name, usr_index, usr_order, numbers)

    if not error == 0:
        error, err_string = nefis.neferr()
        print('    NEFIS error string       : "%s"' % err_string)
        print('    =========')
    #-------------------------------------------------------------------------------
    print('---clsnef---')
    error = nefis.clsnef(fp)
    print('------------')
Exemplo n.º 3
0
    def __init__(self, fileheader, ac_type='r', coding=' ', debug=False):
        """
        NefData class is a python wrapper for the Nefis c-lib.
        It permits to read or write Nefis type files.

        :param fileheader: Nefis file name header, string
        :param ac_type: access type, character.
                        'w’ create a NEFIS file set, existing NEFIS file will be deleted
                        ’r’ read only access of the NEFIS file set
                        ’r+’ or 'w+' update of the NEFIS file set, write and read access of the NEFIS file set.
        :param coding: Coding type, character.
                       ’B’ big-endian representation of the data
                       ’L’ little-endian representation of the data
                       ' ' Endianess-representation is taken from the machine
        :param debug: debug flag, boolean
        """
        # ---Attributes definition---
        # groups
        self.grplist = []
        self.grp = {}
        # elements
        self.elmlist=[]
        self.elm={}
        # file names
        self.dat_file = fileheader+'.dat'
        self.def_file = fileheader+'.def'
        # access type
        if ac_type == 'w':
            self.ac_type = 'c'
        elif ac_type in ['r+', 'w+']:
            self.ac_type = 'u'
        else:
            self.ac_type = 'r'
        # Coding aka endianess-representation
        if coding in ['L', 'l']:
            self.coding = 'L'
        elif coding in ['B', 'b']:
            self.coding = 'B'
        else:
            self.coding = 'automatic'
        # debug flag
        if debug:
            self.debug=True
        else:
            self.debug=False

        # read/write/update delft output files
        self.fp = -1
        error, self.fp = nefis.crenef(self.dat_file, self.def_file, coding, ac_type)
        self._print_error(error)

        # populate a list of all groups and elements
        self._grp_list()
        self._elm_list()

        # debug message
        if self.debug:
            print("---------")
            print(self.dat_file)
            print(self.def_file)
            print(self.coding)
            print(self.ac_type)
            print("---------")