Skip to content

API libmagcdf

createCdata(xarr, att, listVar)

Creates a tuple with convertes data for c-library libmagcdf

Parameters:

Name Type Description Default
xarr Dataset

cdf data

required
att dict

attributes of data variables

required
listVar list

data variables to be written to the cdf

required

Returns:

Type Description
tuple

data variables in ctypes format

Source code in leocdf/libmagcdf/readlibmagcdf.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def createCdata(xarr: xr.Dataset, att: dict, listVar: list)  -> tuple:
    '''Creates a tuple with convertes data for c-library libmagcdf

    Parameters
    ----------
    xarr : xr.Dataset
        cdf data
    att : dict
        attributes of data variables
    listVar : list
        data variables to be written to the cdf

    Returns
    -------
    tuple
        data variables in ctypes format
    '''
    emDoub = np.ctypeslib.as_ctypes( np.zeros(1).astype(np.float64) )
    emInt = np.ctypeslib.as_ctypes( np.zeros(1).astype(np.int8) )
    emUint = np.ctypeslib.as_ctypes( np.zeros(1).astype(np.uint16) )

    listData = []
    for var in listVar:
        cAttr = createCattr(var, att)
        typeArr = bytes(att[var]['TYPE'], 'utf-8')
        if att[var]['TYPE'] == 'CDF_DOUBLE':
            arrayc = np.ctypeslib.as_ctypes( xarr[var].data.astype(np.float64) )
            VarClient = zVariable( bytes(var, 'utf-8'), typeArr, cAttr, att[var]['COMPRESS'], att[var]['BLOCKING'], xarr[var].data.size, arrayc , emInt, emUint )
        elif att[var]['TYPE'] == 'CDF_INT1':
            arrayc = np.ctypeslib.as_ctypes( xarr[var].data.astype(np.byte) )
            VarClient = zVariable( bytes(var, 'utf-8'), typeArr, cAttr, att[var]['COMPRESS'], att[var]['BLOCKING'], xarr[var].data.size, emDoub, arrayc, emUint )
        elif att[var]['TYPE'] == 'CDF_UINT2':
            arrayc = np.ctypeslib.as_ctypes( xarr[var].data.astype(np.uint16) )
            VarClient = zVariable( bytes(var, 'utf-8'), typeArr, cAttr, att[var]['COMPRESS'], att[var]['BLOCKING'], xarr[var].data.size, emDoub, emInt, arrayc )
        else:
            print("Error")
        listData.append( VarClient )
    # NAME, TYPE, zVarAttr, COMPRESS, BLOCK, LENGTH, DATADouble, DATAInt, DATAUint
    return tuple(listData)