⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getnetcdfvariableminmax.py

📁 得到效用路径中的最大和最小的数值,然后可以对其中的数据路径得到很好的应用!
💻 PY
字号:
'''----------------------------------------------------------------------------------
 Tool Name:     GetNetCDFVariableMinMax
 Source Name:   GetNetCDFVariableMinMax.py
 Version:       ArcGIS 9.2
 Author:        Environmental Systems Research Institute Inc.
 Required Argumuments:  A netCDF file.
                        A netCDF variable name.
 Description:   Determines the minimum and maximum values of a multidimensional variable.
----------------------------------------------------------------------------------'''

#Import required modules
import sys, os, arcgisscripting
from Numeric import *
from Scientific.IO.NetCDF import NetCDFFile

#Create the Geoprocessing Object
gp = arcgisscripting.create()

#Define message constants so they may be translated easily
msgVariable = "Variable does not exists."
msgInvalidParameters = "Invalid number of parameters."

try:
    #Check the number of parameters.
    if len(sys.argv) < 2:
        raise Exception, msgInvalidParameters
    
    #Set the input netCDF file
    ncFileName = gp.GetParameterAsText(0)

    #Set the variable name
    ncVarName = gp.GetParameterAsText(1)

    #Open the netCDF file
    ncFile = NetCDFFile(ncFileName, 'r')

    #Make sure that the variable exists
    ncVarNames = ncFile.variables.keys()

    ncVarExists = False    
    for ncVNm in ncVarNames:
       if ncVNm == ncVarName:
           ncVarExists = True
           ncVarName = ncVNm
           break
           
    if not ncVarExists:
        gp.AddError("NetCDF variable " + ncVarName + " does not exist.")
        gp.AddMessage("NetCDF variables are:")            
        for ncVNm in ncVarNames:
            gp.AddMessage(ncVNm)
        raise Exception, msgVariable
    else:
        gp.AddMessage("Variable" + " = " + ncVarName)
    
    #Get a netCDF variable object
    ncVar = ncFile.variables[ncVarName]

    #Get values of a netCDF variable 
    ncVarArray = ncVar.getValue()
    
    #Create a flat array  
    ncVarArray = ncVarArray.flat
    gp.AddMessage("Number of values " + " = " + str(len(ncVarArray)))
    
    #Check variable attributes and compress array if necessary
    ncVarAtts = dir(ncVar)
    for ncVarAtt in ncVarAtts:
        if ncVarAtt.lower() == "missing_value".lower():
            missingValue = getattr(ncVar,"missing_value")
            ncVarArray = compress(not_equal(ncVarArray, missingValue), ncVarArray)
        if ncVarAtt.lower() == "_FillValue".lower():
            fillValue = getattr(ncVar,"_FillValue")    
            ncVarArray = compress(not_equal(ncVarArray, fillValue), ncVarArray)

    #Get min and max        
    vMin = min(ncVarArray)
    vMax = max(ncVarArray)

    #Set parameter and return messages
    gp.SetParameterAsText(2, str(vMin))
    gp.SetParameterAsText(3, str(vMax))
    gp.AddMessage("Minimum" + " = " + str(vMin))
    gp.AddMessage("Maximum" + " = " + str(vMax))

except Exception, ErrorDesc:
    gp.AddError(str(ErrorDesc))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -