import h5py
import numpy as np


def getData(cfileName):
    """
    Given a name of a *.cfile, this function extracts the interleaved
    Inphase-Quadrature data samples and convert it into a numpy array of complex
    data elements. *.cfile format has interleaved I and Q samples where each sample
    is a float32 type. GNURadio Companion (GRC) scripts output data into a file
    though a file sink block in this format.
    Read more in SDR data types: https://github.com/miek/inspectrum
    """

    # Read the *.cfile which has each element in float32 format.
    data = np.fromfile(cfileName, dtype="float32")

    # Take each consecutive interleaved I sample and Q sample to create a single complex element.
    data = data[0::2] + 1j*data[1::2]
    #print("data type=", type(data))
    # Return the complex numpy array.
    return data


dataset_folder = "/home/simonb/tharindu/data"


# creating the HDF5 file
hf = h5py.File('em-test-dataset-2023.h5', 'w')
# setting the file attributes
hf.attrs[u'dataset_version'] = u'1'
hf.attrs[u'dataset_creater'] = u'Tharindu L. Yasarathna <tharindu.yasarathna@ucdconnect.ie>'
hf.attrs[u'dataset_date'] = u'2023'


#########################################################################
#########################################################################
# creating internet of things group
iot = hf.create_group('internet-of-things')
# setting iot group attributes
iot.attrs[u'description'] = u'The EM radiation data of Internet of Things'

#------------------------------------------------------------------------
# creating Amazon Echo Dot group 
echodot = iot.create_group('amazon-echo-dot')
# setting Amazon Echo Dot attributes 
echodot.attrs[u'device_name'] = u'Amazon Echo Dot'
echodot.attrs[u'sampling_frequency'] = u'20MHz'
echodot.attrs[u'centre_frequency'] = u'1.2GHz'


# creating datasets with their attributes
iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echodot/time.cfile")
dataset = echodot.create_dataset('asking-for-time', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Dot was asked for time'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echodot/radio.cfile")
dataset = echodot.create_dataset('asking-to-play-radio', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Dot was asked to play the radio'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echodot/playmusic.cfile")
dataset = echodot.create_dataset('asking-to-play-music', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Dot was asked to play the music'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echodot/weather.cfile")
dataset = echodot.create_dataset('asking-to-weather', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Dot was asked to weather'
del iqdata

#------------------------------------------------------------------------
# creating Amazon Echo Show 5 group 
echoshow5 = iot.create_group('amazon-echo-show5')
# setting Amazon Echo Show 5 attributes 
echoshow5.attrs[u'device_name'] = u'Amazon Echo Show 5'
echoshow5.attrs[u'sampling_frequency'] = u'20MHz'
echoshow5.attrs[u'centre_frequency'] = u'1.2GHz'


# creating datasets with their attributes
iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echoshow/time.cfile")
dataset = echoshow5.create_dataset('asking-for-time', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Show 5 was asked to provide the time'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echoshow/radio.cfile")
dataset = echoshow5.create_dataset('asking-to-play-radio', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Show 5 was asked to play the radio'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echoshow/playmusic.cfile")
dataset = echoshow5.create_dataset('asking-to-play-music', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Show 5 was asked to play the music'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/echoshow/weather.cfile")
dataset = echoshow5.create_dataset('asking-to-weather', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Amazon Echo Show 5 was asked to weather'
del iqdata

#------------------------------------------------------------------------
# creating Smart Camera group 
smartcamera = iot.create_group('smart-camera')
# setting Google Home attributes 
smartcamera.attrs[u'device_name'] = u'Smart Camera'
smartcamera.attrs[u'sampling_frequency'] = u'20MHz'
smartcamera.attrs[u'centre_frequency'] = u'1.2GHz'


# creating datasets with their attributes
iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_AlarmOn.cfile")
dataset = smartcamera.create_dataset('alarm-on', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Smart Camera Alarm turned on'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_discussion.cfile")
dataset = smartcamera.create_dataset('talk-through-camera', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Discussion through Smart Camera'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_Move.cfile")
dataset = smartcamera.create_dataset('move-camera', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Move Smart Camera with phone'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_normalState.cfile")
dataset = smartcamera.create_dataset('camera-idle', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Smart Camera was idle'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_normalStateSomething.cfile")
dataset = smartcamera.create_dataset('camera-film-something', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Smart Camera was filming something'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_privacyOn.cfile")
dataset = smartcamera.create_dataset('camera-privacy-mode-on', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Privacy mode truned on in Smart Camera'
del iqdata

iqdata = getData(dataset_folder + "/iot-device-EM-Test-dataset-2023/smartcamera/Camera_VoiceCall.cfile")
dataset = smartcamera.create_dataset('voice-call-through-camera', data=iqdata)
dataset.attrs[u'dataset_description'] = u'Voice call through Smart Camera'
del iqdata



hf.close()

