<filename>prms/text_to_netcdf/parameterToNetcdf.py<gh_stars>0
import gdal
import netCDF4
import numpy
import osr      
import sys
import time
import os

start_time = time.time()

def find_dimensions(fileHandle):

    """

    Returns the names and lengths of the variables in the file. Two lists, 
    variableNames and variableLengths are created to append the names and 
    lengths respectively. 
    
    """
    
    dimensionNames = []
    dimensionLengths = []  
    
    for line in fileHandle:
        if 'Dimensions' in line:
	    nextLine = fileHandle.next()
	    while 'Parameters' not in nextLine:
                dimensionNames.append(fileHandle.next().strip())
		dimensionLengths.append(int(fileHandle.next().strip()))
		nextLine = fileHandle.next()
    return dimensionNames, dimensionLengths

def copyParameterSectionFromInputFile(fileHandle):
    
    """

    copyParameterSectionFromInputFile function copies the parameter section from the input file to a new file 'values.param'.

    Args:
        fileHandle: The input file
        
    """
    
    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    temporaryFileHandle = open(fileLocation, 'w')
    foundParameterSection = False
    lines = fileHandle.readlines()
    for line in lines:
        if foundParameterSection or 'Parameters' in line:
            temporaryFileHandle.write(line)
            foundParameterSection = True

def find_parameters(fileHandle, numberOfHruCells):
    
    spaceRelatedParameterDimensions = []
    spaceRelatedParameterNames = []
    spaceRelatedParameterTypes = []

    spaceAndTimeRelatedParameterDimensions = []
    spaceAndTimeRelatedParameterNames = []
    spaceAndTimeRelatedParameterTypes = []

    otherParameterDimensions = []
    otherParameterNames = []
    otherParameterTypes = []
    otherParameterDimensionValues = []

    for line in fileHandle:
        if '####' in line:
            name = fileHandle.next().strip().split()[0]
	    numberOfDimensions = int(fileHandle.next().strip())
   	    
            if numberOfDimensions == 1:
	        dimension = fileHandle.next().strip()
		numberOfValues = int(fileHandle.next().strip())
		typeOfValues = int(fileHandle.next().strip())

                if numberOfValues == numberOfHruCells:
		    spaceRelatedParameterDimensions.append(dimension)	
		    spaceRelatedParameterNames.append(name)	
		    spaceRelatedParameterTypes.append(typeOfValues)
		else:
		    if name != 'jh_coef' and name != 'basin_tsta' \
		        and name != 'reach_segment':
		        otherParameterDimensions.append(dimension)
		        otherParameterNames.append(name)
		        otherParameterTypes.append(typeOfValues)
		        otherParameterDimensionValues.append(numberOfValues)	

	    elif numberOfDimensions == 2:
	        dimension = fileHandle.next().strip()
                fileHandle.next()
		numberOfValues = int(fileHandle.next().strip())
                if numberOfValues == numberOfHruCells * 12:
		    spaceAndTimeRelatedParameterDimensions.append(dimension)	
		    spaceAndTimeRelatedParameterNames.append(name)	
		    typeOfValues = int(fileHandle.next().strip())
		    spaceAndTimeRelatedParameterTypes.append(typeOfValues)
	
    return spaceRelatedParameterNames, spaceRelatedParameterTypes, \
	   spaceAndTimeRelatedParameterNames, spaceAndTimeRelatedParameterTypes, \
	   spaceRelatedParameterDimensions, spaceAndTimeRelatedParameterDimensions, \
	   otherParameterDimensions, otherParameterNames, otherParameterTypes, \
	   otherParameterDimensionValues

def find_space_dependent_parameter_values(fileHandle, spaceRelatedParameterName, numberOfHruCells):
    
    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """
    values = []

    for line in fileHandle:
	if spaceRelatedParameterName in line:
            for i in range(4):
		fileHandle.next()
	    for j in range(numberOfHruCells):
		values.append(fileHandle.next().strip())
    return values

def find_space_and_time_dependent_parameter_values(fileHandle, spaceAndTimeRelatedParameterName, numberOfHruCells, position):
    
    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """
    values = []

    for line in fileHandle:
	if spaceAndTimeRelatedParameterName in line:
            for i in range(5):
		fileHandle.next()
	    for j in range(numberOfHruCells * position):
		fileHandle.next()
	    for k in range(numberOfHruCells):
		values.append(fileHandle.next().strip())
    return values

def find_other_parameter_values(fileHandle, otherParameterName, numberOfValues):

    values = []

    for line in fileHandle:
	if otherParameterName in line:
            for i in range(4):
		fileHandle.next()
	    for j in range(numberOfValues):
		values.append(fileHandle.next().strip())
    
    return values

def find_average_resolution(fileHandle, numberOfHruCells, numberOfRows, numberOfColumns):

    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """

    latitudeValues = []
    longitudeValues = []
   
    for i in range(numberOfHruCells):
	valuesInLine = fileHandle.next().strip().split()
        longitudeValues.append(float(valuesInLine[1]))
	latitudeValues.append(float(valuesInLine[2]))

    minimumLatitudeValue = min(latitudeValues)
    maximumLatitudeValue = max(latitudeValues)

    minimumLongitudeValue = min(longitudeValues)
    maximumLongitudeValue = max(longitudeValues)

    averageOfLatitudeValues = (maximumLatitudeValue-minimumLatitudeValue)/numberOfRows
    averageOfLongitudeValues = (maximumLongitudeValue-minimumLongitudeValue)/numberOfColumns
     
    latitudeOfFirstHru = latitudeValues[0]
    longitudeOfFirstHru = longitudeValues[0]

    return averageOfLatitudeValues, averageOfLongitudeValues, latitudeOfFirstHru, longitudeOfFirstHru

def add_metadata(parameterName):

    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'variableDetails/parameterDetails.txt')
    fileHandle = open(fileLocation, 'r')
    for line in fileHandle:
        if parameterName in line:
	    if 'Name' in line:
	        parameterNameFromFile = line.strip()
	        lengthOfParameterName = len(parameterNameFromFile)
	        positionOfNameStart = parameterNameFromFile.index(':') + 2
 	        parameterName = parameterNameFromFile[positionOfNameStart:lengthOfParameterName]
            		
	        parameterDescriptionFromFile = fileHandle.next().strip()
	        lengthOfParameterDescription = len(parameterDescriptionFromFile)
	        positionOfDescriptionStart = parameterDescriptionFromFile.index(':') + 2
	        parameterDescription = parameterDescriptionFromFile[positionOfDescriptionStart:lengthOfParameterDescription]
		
	        parameterUnitFromFile = fileHandle.next().strip()
	        lengthOfParameterUnit = len(parameterUnitFromFile)
	        positionOfUnitStart = parameterUnitFromFile.index(':') + 2
	        parameterUnit = parameterUnitFromFile[positionOfUnitStart:lengthOfParameterUnit]
		
	        break

	else:
	    parameterName = parameterName
	    parameterDescription = 'None'
	    parameterUnit = 'None'

    return parameterName, parameterDescription, parameterUnit

def find_variable_type(parameterType):

    if parameterType == 1:
	value = 'i4'
    elif parameterType == 2:
	value = 'f8'

    return value


def parameter_to_netcdf(parameterFile, locationFile, numberOfHruCells, numberOfRows, numberOfColumns, outputFileName, event_emitter=None, **kwargs):
   
    fileHandle = open(parameterFile, 'r')
    dimensions = find_dimensions(fileHandle)
    dimensionNames = dimensions[0]
    dimensionLengths = dimensions[1]
   
    fileHandle = open(parameterFile, 'r')
    copyParameterSectionFromInputFile(fileHandle)

    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    fileHandle = open(fileLocation, 'r')
    parameters = find_parameters(fileHandle, numberOfHruCells)
    spaceRelatedParameterNames = parameters[0]
    spaceRelatedParameterTypes = parameters[1]
    spaceAndTimeRelatedParameterNames = parameters[2]
    spaceAndTimeRelatedParameterTypes = parameters[3]
    spaceRelatedParameterDimensions = parameters[4]
    spaceAndTimeRelatedParameterDimensions = parameters[5]
    otherParameterDimensions = parameters[6]
    otherParameterNames = parameters[7]
    otherParameterTypes = parameters[8]
    otherParameterDimensionValues = parameters[9]

    if spaceAndTimeRelatedParameterNames:
	monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
       
    fileHandle = open(locationFile, 'r')
    values = find_average_resolution(fileHandle, numberOfHruCells, numberOfRows, numberOfColumns)
    averageOfLatitudeValues = values[0]
    averageOfLongitudeValues = values[1]
    latitudeOfFirstHru = values[2]
    longitudeOfFirstHru = values[3]

    # Initialize new dataset
    ncfile = netCDF4.Dataset(outputFileName, mode='w')

    # Initialize dimensions
    lat_dim = ncfile.createDimension('lat', numberOfRows)
    lon_dim = ncfile.createDimension('lon', numberOfColumns)

    for index in range(len(dimensionNames)):
	dimensionNames[index] = ncfile.createDimension(dimensionNames[index], dimensionLengths[index])

    latList = []
    latList.append(latitudeOfFirstHru)
    previousValue = latitudeOfFirstHru
    lat = ncfile.createVariable('lat', 'f8', ('lat',))
    lat.long_name = 'latitude'  
    lat.units = 'degrees_north'
    for i in range(numberOfRows - 1):
	newValue = previousValue - averageOfLatitudeValues
	latList.append(newValue)
	previousValue = newValue
    lat[:] = latList

    lonList = []
    lonList.append(longitudeOfFirstHru)
    previousValue = longitudeOfFirstHru
    lon = ncfile.createVariable('lon', 'f8', ('lon',))
    lon.long_name = 'longitude'  
    lon.units = 'degrees_east'
    for i in range(numberOfColumns - 1):
	newValue = previousValue + averageOfLongitudeValues
	lonList.append(newValue)
	previousValue = newValue
    lon[:] = lonList

    sr = osr.SpatialReference()
    sr.ImportFromEPSG(4326)
    crs = ncfile.createVariable('crs', 'S1',)
    crs.spatial_ref = sr.ExportToWkt()
    			   
    kwargs['event_name'] = 'parameter_to_nc'
    kwargs['event_description'] = 'creating netcdf file from input parameter file'
    kwargs['progress_value'] = 0.00
    if event_emitter:
        event_emitter.emit('progress',**kwargs)

    prg = 0.10
    i = 0
    length = len(spaceRelatedParameterNames)
    numberOfParameters = length / 5

    for index in range(length):
        value = find_variable_type(spaceRelatedParameterTypes[index])
	metadata = add_metadata(spaceRelatedParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

        var = ncfile.createVariable(spaceRelatedParameterNames[index], value, ('lat', 'lon')) 
	var.layer_name = parameterName
	var.dimension = spaceRelatedParameterDimensions[index]
	var.layer_desc = parameterDescription
	var.layer_units = parameterUnit
        var.grid_mapping = "crs" 

        projectRoot = os.path.dirname(os.path.dirname(__file__))
    	fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    	fileHandle = open(fileLocation, 'r')
        values = find_space_dependent_parameter_values(fileHandle, spaceRelatedParameterNames[index], numberOfHruCells)	
	var[:] = values
	#temp = numpy.transpose(numpy.array(var))
	#var[:] = temp

        if i % numberOfParameters == 0:	
            progress_value = prg/length * 100
            kwargs['event_name'] = 'parameter_to_nc'
            kwargs['event_description'] = 'creating netcdf file from input parameter file'
            kwargs['progress_value'] = format(progress_value, '.2f')
	    if event_emitter:
                event_emitter.emit('progress',**kwargs)
	i += 1
	prg += 1
 
    for index in range(len(spaceAndTimeRelatedParameterNames)):
	value = find_variable_type(spaceAndTimeRelatedParameterTypes[index])
        metadata = add_metadata(spaceAndTimeRelatedParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

	for monthIndex in range(len(monthNames)):
	    var = ncfile.createVariable(spaceAndTimeRelatedParameterNames[index]+'_'+monthNames[monthIndex], value, ('lat', 'lon'))
	    var.layer_name = parameterName+'_'+monthNames[monthIndex]
	    var.dimension = spaceAndTimeRelatedParameterDimensions[index]+', '+'nmonths'
	    var.layer_desc = parameterDescription
            var.layer_units = parameterUnit
	    var.grid_mapping = "crs" 

	    projectRoot = os.path.dirname(os.path.dirname(__file__))
    	    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    	    fileHandle = open(fileLocation, 'r')
            values = find_space_and_time_dependent_parameter_values(fileHandle, spaceAndTimeRelatedParameterNames[index], numberOfHruCells, monthIndex)		
	    var[:] = values
	    #temp = numpy.transpose(numpy.array(var))
	    #var[:] = temp

    for index in range(len(otherParameterNames)):
        value = find_variable_type(otherParameterTypes[index])
	metadata = add_metadata(otherParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

        var = ncfile.createVariable(otherParameterNames[index], value, (otherParameterDimensions[index])) 
        var.layer_name = parameterName
	var.dimension = otherParameterDimensions[index]
	var.layer_desc = parameterDescription
	var.layer_units = parameterUnit
        var.grid_mapping = "crs" 

        fileHandle = open(parameterFile, 'r')
        values = find_other_parameter_values(fileHandle, otherParameterNames[index], otherParameterDimensionValues[index])
        var[:] = values
	#temp = numpy.transpose(numpy.array(var))
	#var[:] = temp
   
    # Global attributes
    fileHandle = open(parameterFile, 'r')
    ncfile.title = fileHandle.next().strip()
    ncfile.version = fileHandle.next().strip()
    ncfile.nsteps = 1
    ncfile.bands_name = 'nsteps'
    ncfile.bands_desc = 'Parameter information for ' + parameterFile
    ncfile.number_of_hrus = numberOfHruCells
    ncfile.number_of_rows = numberOfRows
    ncfile.number_of_columns = numberOfColumns

    # Close the 'ncfile' object
    ncfile.close()

    kwargs['event_name'] = 'parameter_to_nc'
    kwargs['event_description'] = 'creating netcdf file from input parameter file'
    kwargs['progress_value'] = 100
    if event_emitter:
        event_emitter.emit('progress',**kwargs)
  


<filename>prms/text_to_netcdf/parameterToNetcdf.py<gh_stars>0
import gdal
import netCDF4
import numpy
import osr      
import sys
import time
import os

start_time = time.time()

def find_dimensions(fileHandle):

    """

    Returns the names and lengths of the variables in the file. Two lists, 
    variableNames and variableLengths are created to append the names and 
    lengths respectively. 
    
    """
    
    dimensionNames = []
    dimensionLengths = []  
    
    for line in fileHandle:
        if 'Dimensions' in line:
	    nextLine = fileHandle.next()
	    while 'Parameters' not in nextLine:
                dimensionNames.append(fileHandle.next().strip())
		dimensionLengths.append(int(fileHandle.next().strip()))
		nextLine = fileHandle.next()
    return dimensionNames, dimensionLengths

def copyParameterSectionFromInputFile(fileHandle):
    
    """

    copyParameterSectionFromInputFile function copies the parameter section from the input file to a new file 'values.param'.

    Args:
        fileHandle: The input file
        
    """
    
    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    temporaryFileHandle = open(fileLocation, 'w')
    foundParameterSection = False
    lines = fileHandle.readlines()
    for line in lines:
        if foundParameterSection or 'Parameters' in line:
            temporaryFileHandle.write(line)
            foundParameterSection = True

def find_parameters(fileHandle, numberOfHruCells):
    
    spaceRelatedParameterDimensions = []
    spaceRelatedParameterNames = []
    spaceRelatedParameterTypes = []

    spaceAndTimeRelatedParameterDimensions = []
    spaceAndTimeRelatedParameterNames = []
    spaceAndTimeRelatedParameterTypes = []

    otherParameterDimensions = []
    otherParameterNames = []
    otherParameterTypes = []
    otherParameterDimensionValues = []

    for line in fileHandle:
        if '####' in line:
            name = fileHandle.next().strip().split()[0]
	    numberOfDimensions = int(fileHandle.next().strip())
   	    
            if numberOfDimensions == 1:
	        dimension = fileHandle.next().strip()
		numberOfValues = int(fileHandle.next().strip())
		typeOfValues = int(fileHandle.next().strip())

                if numberOfValues == numberOfHruCells:
		    spaceRelatedParameterDimensions.append(dimension)	
		    spaceRelatedParameterNames.append(name)	
		    spaceRelatedParameterTypes.append(typeOfValues)
		else:
		    if name != 'jh_coef' and name != 'basin_tsta' \
		        and name != 'reach_segment':
		        otherParameterDimensions.append(dimension)
		        otherParameterNames.append(name)
		        otherParameterTypes.append(typeOfValues)
		        otherParameterDimensionValues.append(numberOfValues)	

	    elif numberOfDimensions == 2:
	        dimension = fileHandle.next().strip()
                fileHandle.next()
		numberOfValues = int(fileHandle.next().strip())
                if numberOfValues == numberOfHruCells * 12:
		    spaceAndTimeRelatedParameterDimensions.append(dimension)	
		    spaceAndTimeRelatedParameterNames.append(name)	
		    typeOfValues = int(fileHandle.next().strip())
		    spaceAndTimeRelatedParameterTypes.append(typeOfValues)
	
    return spaceRelatedParameterNames, spaceRelatedParameterTypes, \
	   spaceAndTimeRelatedParameterNames, spaceAndTimeRelatedParameterTypes, \
	   spaceRelatedParameterDimensions, spaceAndTimeRelatedParameterDimensions, \
	   otherParameterDimensions, otherParameterNames, otherParameterTypes, \
	   otherParameterDimensionValues

def find_space_dependent_parameter_values(fileHandle, spaceRelatedParameterName, numberOfHruCells):
    
    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """
    values = []

    for line in fileHandle:
	if spaceRelatedParameterName in line:
            for i in range(4):
		fileHandle.next()
	    for j in range(numberOfHruCells):
		values.append(fileHandle.next().strip())
    return values

def find_space_and_time_dependent_parameter_values(fileHandle, spaceAndTimeRelatedParameterName, numberOfHruCells, position):
    
    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """
    values = []

    for line in fileHandle:
	if spaceAndTimeRelatedParameterName in line:
            for i in range(5):
		fileHandle.next()
	    for j in range(numberOfHruCells * position):
		fileHandle.next()
	    for k in range(numberOfHruCells):
		values.append(fileHandle.next().strip())
    return values

def find_other_parameter_values(fileHandle, otherParameterName, numberOfValues):

    values = []

    for line in fileHandle:
	if otherParameterName in line:
            for i in range(4):
		fileHandle.next()
	    for j in range(numberOfValues):
		values.append(fileHandle.next().strip())
    
    return values

def find_average_resolution(fileHandle, numberOfHruCells, numberOfRows, numberOfColumns):

    """
    
    Returns the values of variables in the file. 

    Args:
        numberOfDays (int): is the total number of values for the variable
	position (int): is the column position from where the values can be 
        retrieved
    
    """

    latitudeValues = []
    longitudeValues = []
   
    for i in range(numberOfHruCells):
	valuesInLine = fileHandle.next().strip().split()
        longitudeValues.append(float(valuesInLine[1]))
	latitudeValues.append(float(valuesInLine[2]))

    minimumLatitudeValue = min(latitudeValues)
    maximumLatitudeValue = max(latitudeValues)

    minimumLongitudeValue = min(longitudeValues)
    maximumLongitudeValue = max(longitudeValues)

    averageOfLatitudeValues = (maximumLatitudeValue-minimumLatitudeValue)/numberOfRows
    averageOfLongitudeValues = (maximumLongitudeValue-minimumLongitudeValue)/numberOfColumns
     
    latitudeOfFirstHru = latitudeValues[0]
    longitudeOfFirstHru = longitudeValues[0]

    return averageOfLatitudeValues, averageOfLongitudeValues, latitudeOfFirstHru, longitudeOfFirstHru

def add_metadata(parameterName):

    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'variableDetails/parameterDetails.txt')
    fileHandle = open(fileLocation, 'r')
    for line in fileHandle:
        if parameterName in line:
	    if 'Name' in line:
	        parameterNameFromFile = line.strip()
	        lengthOfParameterName = len(parameterNameFromFile)
	        positionOfNameStart = parameterNameFromFile.index(':') + 2
 	        parameterName = parameterNameFromFile[positionOfNameStart:lengthOfParameterName]
            		
	        parameterDescriptionFromFile = fileHandle.next().strip()
	        lengthOfParameterDescription = len(parameterDescriptionFromFile)
	        positionOfDescriptionStart = parameterDescriptionFromFile.index(':') + 2
	        parameterDescription = parameterDescriptionFromFile[positionOfDescriptionStart:lengthOfParameterDescription]
		
	        parameterUnitFromFile = fileHandle.next().strip()
	        lengthOfParameterUnit = len(parameterUnitFromFile)
	        positionOfUnitStart = parameterUnitFromFile.index(':') + 2
	        parameterUnit = parameterUnitFromFile[positionOfUnitStart:lengthOfParameterUnit]
		
	        break

	else:
	    parameterName = parameterName
	    parameterDescription = 'None'
	    parameterUnit = 'None'

    return parameterName, parameterDescription, parameterUnit

def find_variable_type(parameterType):

    if parameterType == 1:
	value = 'i4'
    elif parameterType == 2:
	value = 'f8'

    return value


def parameter_to_netcdf(parameterFile, locationFile, numberOfHruCells, numberOfRows, numberOfColumns, outputFileName, event_emitter=None, **kwargs):
   
    fileHandle = open(parameterFile, 'r')
    dimensions = find_dimensions(fileHandle)
    dimensionNames = dimensions[0]
    dimensionLengths = dimensions[1]
   
    fileHandle = open(parameterFile, 'r')
    copyParameterSectionFromInputFile(fileHandle)

    projectRoot = os.path.dirname(os.path.dirname(__file__))
    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    fileHandle = open(fileLocation, 'r')
    parameters = find_parameters(fileHandle, numberOfHruCells)
    spaceRelatedParameterNames = parameters[0]
    spaceRelatedParameterTypes = parameters[1]
    spaceAndTimeRelatedParameterNames = parameters[2]
    spaceAndTimeRelatedParameterTypes = parameters[3]
    spaceRelatedParameterDimensions = parameters[4]
    spaceAndTimeRelatedParameterDimensions = parameters[5]
    otherParameterDimensions = parameters[6]
    otherParameterNames = parameters[7]
    otherParameterTypes = parameters[8]
    otherParameterDimensionValues = parameters[9]

    if spaceAndTimeRelatedParameterNames:
	monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
       
    fileHandle = open(locationFile, 'r')
    values = find_average_resolution(fileHandle, numberOfHruCells, numberOfRows, numberOfColumns)
    averageOfLatitudeValues = values[0]
    averageOfLongitudeValues = values[1]
    latitudeOfFirstHru = values[2]
    longitudeOfFirstHru = values[3]

    # Initialize new dataset
    ncfile = netCDF4.Dataset(outputFileName, mode='w')

    # Initialize dimensions
    lat_dim = ncfile.createDimension('lat', numberOfRows)
    lon_dim = ncfile.createDimension('lon', numberOfColumns)

    for index in range(len(dimensionNames)):
	dimensionNames[index] = ncfile.createDimension(dimensionNames[index], dimensionLengths[index])

    latList = []
    latList.append(latitudeOfFirstHru)
    previousValue = latitudeOfFirstHru
    lat = ncfile.createVariable('lat', 'f8', ('lat',))
    lat.long_name = 'latitude'  
    lat.units = 'degrees_north'
    for i in range(numberOfRows - 1):
	newValue = previousValue - averageOfLatitudeValues
	latList.append(newValue)
	previousValue = newValue
    lat[:] = latList

    lonList = []
    lonList.append(longitudeOfFirstHru)
    previousValue = longitudeOfFirstHru
    lon = ncfile.createVariable('lon', 'f8', ('lon',))
    lon.long_name = 'longitude'  
    lon.units = 'degrees_east'
    for i in range(numberOfColumns - 1):
	newValue = previousValue + averageOfLongitudeValues
	lonList.append(newValue)
	previousValue = newValue
    lon[:] = lonList

    sr = osr.SpatialReference()
    sr.ImportFromEPSG(4326)
    crs = ncfile.createVariable('crs', 'S1',)
    crs.spatial_ref = sr.ExportToWkt()
    			   
    kwargs['event_name'] = 'parameter_to_nc'
    kwargs['event_description'] = 'creating netcdf file from input parameter file'
    kwargs['progress_value'] = 0.00
    if event_emitter:
        event_emitter.emit('progress',**kwargs)

    prg = 0.10
    i = 0
    length = len(spaceRelatedParameterNames)
    numberOfParameters = length / 5

    for index in range(length):
        value = find_variable_type(spaceRelatedParameterTypes[index])
	metadata = add_metadata(spaceRelatedParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

        var = ncfile.createVariable(spaceRelatedParameterNames[index], value, ('lat', 'lon')) 
	var.layer_name = parameterName
	var.dimension = spaceRelatedParameterDimensions[index]
	var.layer_desc = parameterDescription
	var.layer_units = parameterUnit
        var.grid_mapping = "crs" 

        projectRoot = os.path.dirname(os.path.dirname(__file__))
    	fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    	fileHandle = open(fileLocation, 'r')
        values = find_space_dependent_parameter_values(fileHandle, spaceRelatedParameterNames[index], numberOfHruCells)	
	var[:] = values
	#temp = numpy.transpose(numpy.array(var))
	#var[:] = temp

        if i % numberOfParameters == 0:	
            progress_value = prg/length * 100
            kwargs['event_name'] = 'parameter_to_nc'
            kwargs['event_description'] = 'creating netcdf file from input parameter file'
            kwargs['progress_value'] = format(progress_value, '.2f')
	    if event_emitter:
                event_emitter.emit('progress',**kwargs)
	i += 1
	prg += 1
 
    for index in range(len(spaceAndTimeRelatedParameterNames)):
	value = find_variable_type(spaceAndTimeRelatedParameterTypes[index])
        metadata = add_metadata(spaceAndTimeRelatedParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

	for monthIndex in range(len(monthNames)):
	    var = ncfile.createVariable(spaceAndTimeRelatedParameterNames[index]+'_'+monthNames[monthIndex], value, ('lat', 'lon'))
	    var.layer_name = parameterName+'_'+monthNames[monthIndex]
	    var.dimension = spaceAndTimeRelatedParameterDimensions[index]+', '+'nmonths'
	    var.layer_desc = parameterDescription
            var.layer_units = parameterUnit
	    var.grid_mapping = "crs" 

	    projectRoot = os.path.dirname(os.path.dirname(__file__))
    	    fileLocation = os.path.join(projectRoot, 'input_files/values.param')
    	    fileHandle = open(fileLocation, 'r')
            values = find_space_and_time_dependent_parameter_values(fileHandle, spaceAndTimeRelatedParameterNames[index], numberOfHruCells, monthIndex)		
	    var[:] = values
	    #temp = numpy.transpose(numpy.array(var))
	    #var[:] = temp

    for index in range(len(otherParameterNames)):
        value = find_variable_type(otherParameterTypes[index])
	metadata = add_metadata(otherParameterNames[index])
        parameterName = metadata[0]
	parameterDescription = metadata[1]
	parameterUnit = metadata[2]

        var = ncfile.createVariable(otherParameterNames[index], value, (otherParameterDimensions[index])) 
        var.layer_name = parameterName
	var.dimension = otherParameterDimensions[index]
	var.layer_desc = parameterDescription
	var.layer_units = parameterUnit
        var.grid_mapping = "crs" 

        fileHandle = open(parameterFile, 'r')
        values = find_other_parameter_values(fileHandle, otherParameterNames[index], otherParameterDimensionValues[index])
        var[:] = values
	#temp = numpy.transpose(numpy.array(var))
	#var[:] = temp
   
    # Global attributes
    fileHandle = open(parameterFile, 'r')
    ncfile.title = fileHandle.next().strip()
    ncfile.version = fileHandle.next().strip()
    ncfile.nsteps = 1
    ncfile.bands_name = 'nsteps'
    ncfile.bands_desc = 'Parameter information for ' + parameterFile
    ncfile.number_of_hrus = numberOfHruCells
    ncfile.number_of_rows = numberOfRows
    ncfile.number_of_columns = numberOfColumns

    # Close the 'ncfile' object
    ncfile.close()

    kwargs['event_name'] = 'parameter_to_nc'
    kwargs['event_description'] = 'creating netcdf file from input parameter file'
    kwargs['progress_value'] = 100
    if event_emitter:
        event_emitter.emit('progress',**kwargs)
  


Returns the names and lengths of the variables in the file. Two lists,      variableNames and variableLengths are created to append the names and      lengths respectively. copyParameterSectionFromInputFile function copies the parameter section from the input file to a new file 'values.param'.      Args:         fileHandle: The input file ###' in line: Returns the values of variables in the file.       Args:         numberOfDays (int): is the total number of values for the variable 	position (int): is the column position from where the values can be          retrieved Returns the values of variables in the file.       Args:         numberOfDays (int): is the total number of values for the variable 	position (int): is the column position from where the values can be          retrieved Returns the values of variables in the file.       Args:         numberOfDays (int): is the total number of values for the variable 	position (int): is the column position from where the values can be          retrieved # Initialize new dataset # Initialize dimensions #temp = numpy.transpose(numpy.array(var)) #var[:] = temp #temp = numpy.transpose(numpy.array(var)) #var[:] = temp #temp = numpy.transpose(numpy.array(var)) #var[:] = temp # Global attributes # Close the 'ncfile' object