def getWavelength(fileName):
	'''
	Acquire wavelength(s) from related HDR file
	'''
	fileHandler = open(fileName+'.hdr')
	wavelengthGroup = [float(x.strip('\r').strip('\n').strip(',')) for x in fileHandler.readlines() 
						if isDigit(x.strip('\r').strip('\n').strip(','))]

	return wavelengthGroup
def main(fileName):
    '''
	The main function, reading the data and exporting netCDF file
	'''
    newData = Dataset("WavelengthExp.nc", "w", format="NETCDF4")
    dimensionBand, dimensionX, dimensionY = getDimension(fileName)
    wavelength, hdrInfo = getWavelength(fileName), getHeaderInfo(fileName)

    newData.createDimension('band', dimensionBand)
    newData.createDimension('x', dimensionX)
    newData.createDimension('y', dimensionY)
    newData.createDimension('wavelength', len(wavelength))

    mainDataHandler, tempVariable = open('/Users/jeromemao/Desktop/terraref/data'),\
            newData.createVariable('exposure_2','f8',('band', 'x', 'y'))#('band', 'x', 'y')
    fileSize = os.path.getsize(fileName)
    dataNumber, dataType, dataSize = fileSize/DATATYPE[hdrInfo['data type']][-1], DATATYPE[hdrInfo['data type']][0],\
             DATATYPE[hdrInfo['data type']][-1]

    with TimeMeasurement("unpacking") as lineTiming:  #measuring the time
        value = struct.unpack(
            dataType * dataNumber, mainDataHandler.read(
                dataSize * dataNumber))  #reading the data from the file

    with TimeMeasurement("assigning value") as lineTiming:
        tempVariable[:, :, :] = value  #TODO need a better method to assign value to avoid "de-interleaving"

    nestedWavelength = newData.createVariable('wavelength', 'f8',
                                              ('wavelength', ))
    nestedWavelength[:] = wavelength
    headerInfo = newData.createGroup("HeaderInfo")

    for members in hdrInfo:
        setattr(headerInfo, members, hdrInfo[members])
        if isDigit(hdrInfo[members]):
            tempVariable = headerInfo.createVariable(members, 'i4')
            tempVariable.assignValue(int(hdrInfo[members]))

    newData.close()
def main(fileName):
	'''
	The main function, reading the data and exporting netCDF file
	'''
	newData = Dataset("WavelengthExp.nc","w",format="NETCDF4")
	dimensionBand, dimensionX, dimensionY = getDimension(fileName)
	wavelength, hdrInfo = getWavelength(fileName), getHeaderInfo(fileName)

	newData.createDimension('band',       dimensionBand)
	newData.createDimension('x',          dimensionX)
	newData.createDimension('y',          dimensionY)
	newData.createDimension('wavelength', len(wavelength))

	mainDataHandler, tempVariable = open('/Users/jeromemao/Desktop/terraref/data'),\
									newData.createVariable('exposure_2','f8',('band', 'x', 'y'))#('band', 'x', 'y')
	fileSize = os.path.getsize(fileName)
	dataNumber, dataType, dataSize = fileSize/DATATYPE[hdrInfo['data type']][-1], DATATYPE[hdrInfo['data type']][0],\
									 DATATYPE[hdrInfo['data type']][-1]

	with TimeMeasurement("unpacking") as lineTiming: #measuring the time
		value = struct.unpack(dataType*dataNumber,mainDataHandler.read(dataSize*dataNumber))#reading the data from the file

	with TimeMeasurement("assigning value") as lineTiming:
		tempVariable[:,:,:] = value #TODO need a better method to assign value to avoid "de-interleaving"

	nestedWavelength    = newData.createVariable('wavelength', 'f8',('wavelength',))
	nestedWavelength[:] = wavelength
	headerInfo          = newData.createGroup("HeaderInfo")

	for members in hdrInfo:
		setattr(headerInfo,members,hdrInfo[members])
		if isDigit(hdrInfo[members]):
			tempVariable = headerInfo.createVariable(members,'i4')
			tempVariable.assignValue(int(hdrInfo[members]))

	newData.close()