def extractVerticalSection(nc,dataVar,vertSection,tIndex,coordVal):
    """ Takes five arguments; the NetCDF dataset, the variable object, the 
    string reprenting verticle setion, the index along the time axis and the 
    index along the latitude/longitude. Using the arguments it extracts returns
    the data, from a NetCDF file"""
    
    import netcdfUtils
    # Find the number of dimensions for the data variable
    numDims = len(dataVar.dimensions)
    
    # check number of dimensions and raise error if they are not 2,or 3,or 4  
    if numDims < 3 or numDims > 4:
        raise ValueError("Cannot extract data from variable with %d dimensions" % numDims)
    # Check if the longitude, latitude and vertical dimensions are present
    lonVar = netcdfUtils.findLongitudeVar(nc, dataVar)
    latVar = netcdfUtils.findLatitudeVar(nc, dataVar)
    zVar=netcdfUtils.findVerticalVar(nc,dataVar)
    #raise error if the longitude, latitude and vertical dimensions are abscent
    if lonVar is None or latVar is None or zVar is None:
        raise ValueError("Cannot extract map data if longitude, latitude and Vertical dimensios are abscent")
    # change longitutude value from range (-180 to 180) into (0 to 360)
    if vertSection=='NS':
        #Map the slicing longitude from -180-to-180 to 0-360
        if min(lonVar[:])>=0:
            if coordVal<=0:
                coordVal=coordVal+180
            else:
                coordVal=coordVal+180        
     #the dimensions present are time, vertical, latitude or longitude
    if vertSection=='NS'and numDims == 4:
        coordVar=netcdfUtils.findLongitudeVar(nc, dataVar) 
        coordIndex=utils.findNearestIndex(coordVar,coordVal)
        return dataVar[tIndex,:,:,coordIndex]
    # the dimensions present must be vertical, latitude or longitude
    elif vertSection=='NS'and numDims == 3: 
        coordVar=netcdfUtils.findLongitudeVar(nc, dataVar) 
        coordIndex=utils.findNearestIndex(coordVar,coordVal)          
        return dataVar[:,:,coordIndex]
    # the dimensions present are time, vertical, latitude & longitude in that order
    if vertSection=='EW'and numDims == 4:
        coordVar=netcdfUtils.findLatitudeVar(nc, dataVar) 
        coordIndex=utils.findNearestIndex(coordVar,coordVal)       
        return dataVar[tIndex,:,coordIndex,:]
    # the dimensions present must be vertical, latitude & longitude in that order
    elif vertSection=='EW'and numDims == 3:
        coordVar=netcdfUtils.findLatitudeVar(nc, dataVar) 
        coordIndex=utils.findNearestIndex(coordVar,coordVal)  
        return dataVar[:,coordIndex,:]
def findNearestLonIndex(nc,dataVar,lonval):
    """ Searches through longitude values for the target value, returning the index
    of the longitude value that is closest numerically to the target value.
    If more than one longitude value is equally close to the target, the index
    of the first value will be returned. """
    import utils
    import netcdfUtils
    # map the longitutude value from range (-180 to 180) onto (0 to 360) in the data file
    coordval=netcdfUtils.findLongitudeVar(nc,dataVar)
    if min(coordval[:])>=0:
        #check if the latitude value is within the latitude range
       if lonval<=0:
           lonval=lonval+180
       else:
           lonval=lonval+180  
    # check if the Variable object representing the latitude axis exists in the netcdf file  
    if coordval is None:
        raise ValueError("No Variable object for the longitude axis exists in the netcdf file")
    #create an array of longitude values from where to search
    lonvals=coordval[:]
    #find the index of the given longitudevalue and check if it exists
    lonIndex=utils.findNearestIndex(lonvals,lonval) 
    #if no index is found
    if lonIndex is None:
        raise ValueError("No index value found for the longitude value entered")
    #longitude index value   
    return lonIndex 
def extractTimeseries(nc,dataVar,lonVal,latVal,zVal):
    """ Takes five arguments; the NetCDF dataset, the variable object, the 
    longitude value, the latitude value and the vertical value and use them to 
    extracts data and time values at the point described. It returns the data of 
    the  and variable represented by the variable object the time values for 
    each point described by the longitude, latitude and vertical values in the
    NetCDF file"""
    # Find the number of dimensions for the data variable
    numDims = len(dataVar.dimensions)
    
    # Check if the number of dimensions and raise error if they are not 3 or 4  
    if numDims < 3 or numDims > 4:
        raise ValueError("Cannot extract data from variable with %d dimensions" % numDims)
    # Check if longitude, latitude and vertical dimensions are present
    lonVar = netcdfUtils.findLongitudeVar(nc, dataVar)
    latVar = netcdfUtils.findLatitudeVar(nc, dataVar)
    timeVar=netcdfUtils.findTimeVar(nc,dataVar)
    zVar=netcdfUtils.findVerticalVar(nc,dataVar)
    timeVal=timeVar[:]
    # check if the longitude, latitude or the time dimension is abscent and raise error
    if lonVar is None or latVar is None or timeVar is None:
        raise ValueError("Cannot extract map data if longitude, latitude and time dimensios are abscent")
    
   # map the longitutude value from range (-180 to 180) onto (0 to 360) in the data file
    #lonVal=lonVal+180 
    if min(lonVar[:])>=0:
        if lonVal<0:
            lonVal=lonVal+180
    # if the dimensions are 4 i.e. time,vertical,latitude and longitude 
    if numDims == 4:
         #get the vertical,longitude and latitude indices
        lonIndex=utils.findNearestIndex(lonVar,lonVal)
        latIndex=utils.findNearestIndex(latVar,latVal)
        zIndex=utils.findNearestIndex(zVar,zVal)
        return dataVar[:,zIndex,latIndex,lonIndex],timeVal
    # if the dimensions are 3 ie time, latitude and longitude
    elif numDims == 3: 
        #get the longitude and latitude indices
        lonIndex=utils.findNearestIndex(lonVar,lonVal)
        latIndex=utils.findNearestIndex(latVar,latVal)         
        return dataVar[:,latIndex,lonIndex],timeVal
def findNearestLatIndex(nc,dataVar,latVal):
    """ Searches through latitude values for the target value,using the findLatitudeVar() in 
    netcdfUtils module and the findNearestIndex() function from the utils module, returning
    the index of the latitude value that is closest numerically to the target value. If more
    than one latitude value is equally close to the target, the index of the first value will
    be returned. If the Latitude dimension does not exit a error is raised """
    import utils
    #check if the latitude value is within the latitude range
    if latVal>90 or latVal<-90:
        raise ValueError("The value entered is ouside latitude range of values +90 to -90")
    # check if the Variable object representing the latitude axis exists in the netcdf file
    coordVar=findLatitudeVar(nc,dataVar)#returns Variable object representing latitude axis for the data variable
    if coordVar is None:
        raise ValueError("No Variable object for the latitude axis exists in the netcdf file")
    else:
    #get an array of latitude values from where to search for the given latitude value   
        latVals=coordVar[:] 
    #find the index of the given latitude value
    latIndex=utils.findNearestIndex(latVals,latVal) 
    return latIndex #latitude index value
def findNearestZIndex(nc,dataVar,zVal): 
    """ Searches through vertical axis values for the target value,using the findVerticalVar() 
    in netcdfUtils module and the findNearestIndex() function from the utils module, returning 
    the index of the vertical axis value that is closest numerically to the target value. If 
    more than one latitude value is equally close to the target, the index of the first value 
    will be returned.If the vertical dimension does not exit an error is raised """
    import utils  
    # check if the Variable object representing the vertical axis exists in the netcdf file
    coordVal=findVerticalVar(nc,dataVar)#returns Variable object representing vertical axis for the data variable
    if coordVal is None:
        raise ValueError("No Variable object for the vertical axis exists in the netcdf file")
    else:
    #get an array of vertical values from where to search for the given vertical value  
        zVals=coordVal[:]
    #check if the vertical value given is within the vertical axis range of values    
    if zVal<zVals[0] or zVal>max(zVals):
      raise ValueError("The value entered is ouside vertical coordinate range of values")
   #find the index of the given vertical axis value
    zIndex=utils.findNearestIndex(zVals,zVal)
    #return the index of the vertical value
    return zIndex