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 plotVerticalSection(filename,varIdentifier,vertSection,coordVal,tIndex): """Takes five aruguments i.e. a string representing location of NetCDF file, variable identifier, string indicating whether vertical section is North-South (NS) or East-West (EW), the value of latitude/longitude depending on EW or NS, and the index along the time axis for which to get the data. Uses the extractVerticalSection() function from the extract module to extract data. Plots using the displayVertSectionPlot() function from the plotting module.""" import netCDF4 import netcdfUtils import plotting nc=netCDF4.Dataset(filename) #A netCDF4 dataset Object dataVar=nc.variables[varIdentifier] # A varible object representing a data variable ('lat' or 'latitude') # Check that longitude,latitude, vertical and time variables are present lonVar = netcdfUtils.findLongitudeVar(nc, dataVar) latVar = netcdfUtils.findLatitudeVar(nc, dataVar) zVar=netcdfUtils.findVerticalVar(nc,dataVar) #check if string that indicate vertical section is EW or NS else raise error if vertSection!='NS'and vertSection!='EW': raise ValueError("Only NS or EW can be used to indicate Vertical section") # if the vertical section identifier is North-South get latitudes if vertSection=='NS': #check if the slicing longitude is within the range of values if coordVal<-180 or coordVal>180: raise ValueError("The Longitude values range from -180(180W) to 180(180E)") #get the latitude values for the x axis coordValues=latVar[:] # if the vertical section identifier is East-West get longitudes elif vertSection=='EW': #check if the slicing latitude is within the range of values if coordVal<-90 or coordVal>90: raise ValueError("The Latitude values range from -90(90S) to +90(90N)") #get the longitude values for the x axis coordValues=lonVar[:] #check if the latitude, longitude and vertical dimesions are present in file if lonVar is None or latVar is None : raise ValueError("Cannot extract map data if longitude and latitude are not present") if zVar is None: raise ValueError("Cannot extract map data if Vertical dimension is not present") #extract data as well as the vertical and longitude/latitude coordinates data=extract.extractVerticalSection(nc,dataVar,vertSection,tIndex,coordVal) #get the title of the plot title=netcdfUtils.getTitle(dataVar) #plot the exrated data using the displayVertSectionPlot() from plotting module plotting.displayVertSectionPlot(data,zVar,coordValues,title,vertSection,coordVal) return
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