def netcdf_to_dir_and_speed(nds_filenames, nds_datatype, nds_time):
    for loop_filename in nds_filenames:
        loop_filename_ext = loop_filename + ".nc"
        print(loop_filename + ": Making NETCDF points")

        # create points from netcdf
        nds_lon = arcpy.NetCDFFileProperties(
            r"netcdf/" + loop_filename_ext).getVariables()[1]
        nds_lat = arcpy.NetCDFFileProperties(
            r"netcdf/" + loop_filename_ext).getVariables()[0]
        nds_variable = arcpy.NetCDFFileProperties(
            r"netcdf/" + loop_filename_ext).getVariables()[3]
        row_dimensions = nds_lon + ";" + nds_lat
        arcpy.md.MakeNetCDFFeatureLayer(r"netcdf/" + loop_filename_ext,
                                        nds_variable, nds_lon, nds_lat,
                                        r"memory/" + loop_filename,
                                        row_dimensions, '', '', nds_time,
                                        "BY_VALUE")

        # reproject from WGS84 to Asia South Equidistant Conic (ESRI:102029)
        print(loop_filename + ": Reprojecting")
        arcpy.management.Project(
            r"memory/" + loop_filename, r"temp/proj_" + loop_filename + ".shp",
            "PROJCS['Asia_South_Equidistant_Conic',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Equidistant_Conic'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',125.0],PARAMETER['Standard_Parallel_1',7.0],PARAMETER['Standard_Parallel_2',-32.0],PARAMETER['Latitude_Of_Origin',-15.0],UNIT['Meter',1.0]]",
            None, None, "NO_PRESERVE_SHAPE", None, "NO_VERTICAL")

        # interpolate
        print(loop_filename + ": Interpolating")
        arcpy.ddd.NaturalNeighbor(r"temp/proj_" + loop_filename + ".shp",
                                  nds_variable, r"memory/nat_" + loop_filename,
                                  landraster_filename)

        # clip
        print(loop_filename + ": Clipping")
        arcpy.management.Clip(
            r"memory/nat_" + loop_filename,
            "-2892045,90595181 1129654,52986545 -202045,905951815 4590154,52986545",
            r"memory/clip_" + loop_filename, landraster_filename,
            "-3,402823e+38", "NONE", "NO_MAINTAIN_EXTENT")

    # calculate direction
    print(nds_datatype + ": Calculating direction")
    direction_temp = arcpy.sa.RasterCalculator(
        [r"memory/clip_wind_u", r"memory/clip_wind_v"], ["u", "v"],
        "(180 / 3.14159265358979323846) * ATan2(u, v)")
    direction_raster = arcpy.sa.Con(direction_temp < 0, direction_temp + 360,
                                    direction_temp)
    direction_nullraster = arcpy.ia.SetNull(landraster_filename,
                                            direction_raster, "Value = 1")
    direction_nullraster.save(r"wind/direction/wd" + savename_time + r".tif")

    # calculate speed
    print(nds_datatype + ": Calculating speed")
    speed_raster = arcpy.sa.RasterCalculator(
        [r"memory/clip_wind_u", r"memory/clip_wind_v"], ["u", "v"],
        "sqrt(u * u  + v * v)")
    speed_nullraster = arcpy.ia.SetNull(landraster_filename, speed_raster,
                                        "Value = 1")
    speed_nullraster.save(r"wind/speed/ws" + savename_time + r".tif")
示例#2
0
def main(in_netcdf, out_dir):
    try:
        nc_fp = arcpy.NetCDFFileProperties(in_netcdf)
        nc_dim = 'time'
        in_netcdf_layer = os.path.basename(in_netcdf).split('.')[0]
        out_layer = "{0}_layer".format(in_netcdf_layer)
        arcpy.MakeNetCDFRasterLayer_md(in_netcdf, "r", "lon", "lat", out_layer,
                                       "", "", "")
        print "Created NetCDF Layer for " + out_layer

        for i in range(0, nc_fp.getDimensionSize(nc_dim)):
            nc_dim_value = nc_fp.getDimensionValue(nc_dim, i)
            print("\tDimension value: {0}".format(nc_dim_value))
            print("\tDimension index: {0}".format(
                nc_fp.getDimensionIndex(nc_dim, nc_dim_value)))
            dmy = nc_dim_value.split('/')
            date_str = "{2}.{1}.{0}".format(dmy[0], dmy[1], dmy[2])
            out_layer_file = "{0}{1}.{2}.tif".format(out_dir, in_netcdf_layer,
                                                     date_str)

            # Execute SelectByDimension tool
            valueSelect = ["time", i]
            net_layer = arcpy.SelectByDimension_md(out_layer, [valueSelect],
                                                   "BY_INDEX")
            #define output tif name
            arcpy.CopyRaster_management(net_layer, out_layer_file)
            arcpy.AddMessage(out_layer_file + " "
                             "exported" + " " + "successfully")
    except Exception as err:
        print(err)
示例#3
0
def make_iterators(listofnoaapaths, listofnewouputpaths):
    '''
    Creates tuples of NetCDF files AND top bands for the main loop
    of the code.

    I'm using a generator since the NetCDF files can be large.

    :param listofnoaafiles
    :return: tuples of TOP bands and NetCDF files
    '''

    netcdffiles = [
        arcpy.NetCDFFileProperties(file) for file in listofnoaapaths
    ]

    # Get number of bands in the time dimension_type.
    # Do it directly with the getDim... function!
    topbands = [
        netcdffile.getDimensionSize('time') for netcdffile in netcdffiles
    ]

    # Convert list of output path to generator:
    outputpaths = [path for path in listofnewouputpaths]

    # Chain together three lists: the NetCDF files, bands, and output path:
    return topbands, netcdffiles, listofnoaapaths, outputpaths
    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        # Create list of possible variables for second parameter.
        if parameters[0].value and arcpy.Exists(parameters[0].valueAsText):
            ncFP = arcpy.NetCDFFileProperties(parameters[0].valueAsText)
            parameters[1].filter.list = ncFP.getVariables()
        # Populate the output layer name parameter with a reasonable default
        # (i.e. the name of the first variable chosen with _layer appended
        # to it.)
        if parameters[1].value and not parameters[2].altered:
            variable_name_list = str(parameters[1].valueAsText).split(';')
            out_layer_name = variable_name_list[0] + "_Layer"
        # Generate a unique layer name
            i = 1
            temp_name = out_layer_name
            while arcpy.Exists(temp_name):
                temp_name = out_layer_name + str(i)
                i += 1
            out_layer_name = temp_name
            parameters[2].value = out_layer_name

        return
示例#5
0
    def __init__(self, netCDFloc):
        '''
         Defines the Class Properties based off the the NetCDF File Properties inputed
        '''
        ncFileProp = arcpy.NetCDFFileProperties(netCDFloc)
        self.__ncFileProperties = ncFileProp

        self.__sourceLocation = netCDFloc
        self.__determineDimensions()
        self.__determineVariables()
def processFile(netcdf_file, input_fc, input_fc_10, input_fc_1, output_fc, output_fc_10, output_fc_1):

    print(config)
    logging.info("Processing NetCDF :: " + netcdf_file)

    # Get the number of time indexes (normally 66 hours) 
    nc_FP = arcpy.NetCDFFileProperties(netcdf_file)  
    top = nc_FP.getDimensionSize(config["nc_time_dimension"])
    logging.info(str(top) + " time slices...")
    
    time_from_nc = nc_FP.getDimensionValue(config["nc_time_dimension"], 0)
    if len(time_from_nc) == 19:
        start_date = datetime.datetime.strptime(time_from_nc, "%d.%m.%Y %H:%M:%S")
    else:
        start_date = datetime.datetime.strptime(time_from_nc, "%d.%m.%Y")


    # For each time slice
    for i in range(0, top):
        # New time dimension
        dimension_value = config["nc_time_dimension"] + " " + str(i)
        new_time = start_date + datetime.timedelta(hours=i)
        
        logging.info("Processing date: " + new_time.strftime("%d.%m.%Y %H.%M") + " - index " + str(i))

        # Update the time-field on the input data
        date_expr = "datetime.datetime(%s,%s,%s,%s,%s)" % (new_time.year, new_time.month, new_time.day, new_time.hour, new_time.minute)
        arcpy.CalculateField_management(input_fc, "time", date_expr)
        arcpy.CalculateField_management(input_fc_10, "time", date_expr)
        arcpy.CalculateField_management(input_fc_1, "time", date_expr)

        # For each variable
        for variable in ["air_temperature_2m", "cloud_area_fraction", "relative_humidity_2m", "low_type_cloud_area_fraction", "high_type_cloud_area_fraction", "precipitation_amount_acc"]:
            temp_raster = "temp_raster_%s_%s" % (i, variable)
            logging.info("Processing variable %s for date %s " % (variable, new_time))

            # Create the raster for the given time/variable
            arcpy.MakeNetCDFRasterLayer_md(netcdf_file, variable, "x", "y", temp_raster, "#", dimension_value, "BY_INDEX")
            # Calculate the value on the input
            arcpy.AddSurfaceInformation_3d(input_fc, temp_raster, "Z_MEAN", "LINEAR")
            arcpy.AddSurfaceInformation_3d(input_fc_10, temp_raster, "Z_MEAN", "LINEAR")
            arcpy.AddSurfaceInformation_3d(input_fc_1, temp_raster, "Z_MEAN", "LINEAR")
            # Update the correct field
            arcpy.CalculateField_management(input_fc, variable, "!Z_MEAN!", "PYTHON_9.3")
            arcpy.CalculateField_management(input_fc_10, variable, "!Z_MEAN!", "PYTHON_9.3")
            arcpy.CalculateField_management(input_fc_1, variable, "!Z_MEAN!", "PYTHON_9.3")
            
        # Copy features to output fc
        logging.info("Copying output data for date %s" % new_time)
        arcpy.arcpy.Append_management([input_fc], output_fc)
        arcpy.arcpy.Append_management([input_fc_10], output_fc_10)
        arcpy.arcpy.Append_management([input_fc_1], output_fc_1)
    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
    # Multivalue parameter 1 contains variable names. Split them into a list.
        selectedVars = []
        if parameters[1].value:
            ncFP = arcpy.NetCDFFileProperties(parameters[0].valueAsText)
            selectedVars = []
            selectedVars = str(parameters[1].valueAsText).split(';')

        # For each selected variable, get its dimension.  Build a list
        dimList = []
        [dimList.append(ncFP.getDimensionsByVariable(v.replace("'", ""))) \
            for v in selectedVars]

        # Verify the all variables have the same exact dimensions
        if not all(x == dimList[0] for x in dimList):
            parameters[1].setErrorMessage("All selected variables must share " +
                "the same dimensions.")
        return
def run_etl_for(which):
    arcpy.env.overwriteOutput = True
    print "Starting " + which
    variable = myConfig[which + 'Var']
    gdb = myConfig['gdb']
    mosaic_ds = gdb + myConfig[which + 'MDS']
    extract = myConfig['extract_Folder']
    name = "\\" + myConfig[which + 'Name']
    extension = myConfig[which + 'Extension']

    time_var = myConfig[which + 'TimeVar']
    time_dim = myConfig[which + 'TimeDim']

    the_start_date = get_start_date_from_db(mosaic_ds)
    while the_start_date.date() < datetime.date.today():
        the_start_date += datetime.timedelta(days=1)
        temp_date = the_start_date.strftime(myConfig[which + 'NameDateFormat'])
        in_net_cdf = extract + name + temp_date + extension
        if os.path.isfile(in_net_cdf):
            print(in_net_cdf + " exist")
            nc_fp = arcpy.NetCDFFileProperties(in_net_cdf)
            nc_dim = nc_fp.getDimensions()
            the_date = ''
            if len(time_var) + len(time_dim) > 0:
                the_date = nc_fp.getAttributeValue(time_var, time_dim)
                if the_date.find(" UTC") > 0:
                    the_date = the_date.split(' ')[0].replace('.', '-')
                elif the_date.find("Z") > 0:
                    print str(the_date)
                    the_date = the_date.split("T")[0]

            for dimension in nc_dim:
                top = nc_fp.getDimensionSize(dimension)
                for i in range(0, top):
                    if dimension == "time":
                        dimension_values = nc_fp.getDimensionValue(
                            dimension, i)
                        if the_date == '':
                            the_date = str(dimension_values)

            rast = the_date.replace('-', '')
            arcpy.MakeNetCDFRasterLayer_md(in_net_cdf, variable,
                                           myConfig[which + 'Lon'],
                                           myConfig[which + 'Lat'], rast, "",
                                           "", "BY_VALUE", "CENTER")

            # convert the layer to a raster
            arcpy.CopyRaster_management(rast, mosaic_ds + rast, "", "",
                                        "-3,402823e+038", "NONE", "NONE", "",
                                        "NONE", "NONE")

            # Process: Add Rasters To Mosaic Dataset
            arcpy.AddRastersToMosaicDataset_management(
                mosaic_ds, "Raster Dataset", mosaic_ds + rast,
                "UPDATE_CELL_SIZES", "NO_BOUNDARY", "NO_OVERVIEWS", "", "", "",
                "", "", "SUBFOLDERS", "ALLOW_DUPLICATES", "true", "true",
                "NO_THUMBNAILS", "")

            the_name = myConfig[which + 'MDS'].replace('\\', '').replace(
                '/', '') + rast

            expression = "Name= '" + the_name + "'"
            rows = arcpy.UpdateCursor(
                mosaic_ds, expression
            )  # Establish r/w access to data in the query expression.
            if the_date.find("-") < 0:
                year = the_date[0:4]
                month = the_date[4:6]
                day = the_date[6:8]
            else:
                year = the_date[0:4]
                month = the_date[5:7]
                day = the_date[8:10]

            dt_obj = year + "/" + month + "/" + day
            for r in rows:
                r.dateObtained = dt_obj  # here the value is being set in the proper field
                rows.updateRow(r)  # update the values

            del rows
示例#9
0
jing_netCDF = r'C:\LUCI_data\Aparima\netCDF\streamq_5440662.nc'
root_grp_jing = Dataset(jing_netCDF, "r", format="NETCDF4")
reaches = root_grp_jing.variables['rchid']
reach_ids = list(reaches[:])
root_grp_jing.close()
'''

# Set time bounds
start_date = date(2005, 2, 10)
end_date = date(2005, 2, 13)

# Define rainfall and pet netCDF files
rain_netCDF = r'C:\LUCI_data\Aparima\VCSN\vcsn_rain_aparima_19900101_20181015.nc'
pet_netCDF = r'C:\LUCI_data\Aparima\VCSN\vcsn_pet_aparima_19900101_20181015.nc'

nc_FP = arcpy.NetCDFFileProperties(rain_netCDF)
print(nc_FP)

nc_Dim = nc_FP.getDimensions()
print(nc_Dim)

print(arcpy.env.scratchFolder)
'''
for dimension in nc_Dim:

    if dimension == "time":
        top = nc_FP.getDimensionSize(dimension)
        for i in range(int(start_date - epoch_date), int(end_date - epoch_date) + 1):

            dimension_values = nc_FP.getDimensionValue(dimension, i)
            currentDate = date(GetYear(dimension_values),
    #inNetCDFBndFc = netcdflilePath + os.sep + inNetCDFBndFc

    # arcpy environment settings
    env.workspace = scratchPath
    env.overwriteOutput = 1

    # Check out the ArcGIS Spatial Analyst extension license
    arcpy.CheckOutExtension("Spatial")

    ##==============================================================================
    #Loop through files from here
    ##==============================================================================
    # Find out all variables that share dimension ncX, ncY, and ncTimeDim
    # and the size of the time dimension

    ncFileProp = arcpy.NetCDFFileProperties(inNetCDFFile)
    ncVarsXDim = ncFileProp.getVariablesByDimension(ncXDim)
    ncVarsYDim = ncFileProp.getVariablesByDimension(ncYDim)
    ncVarsTimeDim = ncFileProp.getVariablesByDimension(ncTimeDim)
    ncVarsXYTimeDim = list(
        set(ncVarsXDim) & set(ncVarsYDim) & set(ncVarsTimeDim))

    print ncVarsXYTimeDim  #NSN:REMOVE
    #[u'T2_AVE_INTERANNUAL_VARIANCE', u'T2_MIN', u'T2_MAX_INTERANNUAL_VARIANCE',
    # u'T2_AVE', u'T2_MIN_DAILY_VARIANCE', u'T2_MAX_DAILY_VARIANCE', u'T2_MAX_STDERR',
    # u'T2_AVE_STDERR', u'T2_MIN_INTERANNUAL_VARIANCE', u'T2_MAX',
    # u'T2_AVE_DAILY_VARIANCE', u'T2_MIN_STDERR']

    #ncVarsXYTimeDim = ['T2_MIN', 'T2_MAX'] #NSN:REMOVE
    #print ncVarsXYTimeDim #NSN:REMOVE
    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Describe the supplied netCDF File.
        ncFP = arcpy.NetCDFFileProperties(parameters[0].valueAsText)
        selectedVars = parameters[1].valueAsText.split(';')

        # Coordinate variables can be identified via values of their standard
        # name attribute or values of their untis attribute.

        XCoordNamesList = [
            "longitude", "projection_x_coordinate", "grid_longitude"
        ]
        YCoordNamesList = [
            "latitude", "projection_y_coordinate", "grid_longitude"
        ]
        XUnitNamesList = [
            "degrees_east", "degree_east", "degree_E", "degrees_E", "degreeE",
            "degreesE"
        ]
        YUnitNamesList = [
            "degrees_north", "degree_north", "degree_N", "degrees_N",
            "degreeN", "degreesN"
        ]
        XAxisNameList = ["X"]
        YAxisNameList = ["Y"]

        # Case #1: Trajectory variable can be identified under two conditions;
        # the presence of a attribute name of 'positive' on a variable or
        # the presence of the units variable with a units of pressure (pascal).

        x_variable = ''
        y_variable = ''
        ZVariable = ''

        if (not x_variable) or (not y_variable):
            try:
                coordAttributeValue = ncFP.getAttributeValue(
                    str(selectedVars[0]), "coordinates")
                coordVariables = coordAttributeValue.split(' ')
                for element in coordVariables:
                    try:
                        ZAttribute = ncFP.getAttributeValue(
                            element, "positive")
                        if ZAttribute.upper() == 'UP' or ZAttribute.upper() \
                        == 'DOWN':
                            ZVariable = element
                    except:
                        pass

                    try:
                        SNattributeValue = ncFP.getAttributeValue(
                            element, "standard_name")
                    except:
                        SNattributeValue = "missing"

                    try:
                        UNattributeValue = ncFP.getAttributeValue(
                            element, "units")
                        if UNattributeValue.upper() == 'PASCAL':
                            ZVariable = element
                    except:
                        UNattributeValue = "missing"

                    try:
                        AXattributeValue = ncFP.getAttributeValue(
                            element, "axis")
                    except:
                        AXattributeValue = "missing"

                    if SNattributeValue in XCoordNamesList:
                        x_variable = element
                    if SNattributeValue in YCoordNamesList:
                        y_variable = element
                    if UNattributeValue in XUnitNamesList:
                        x_variable = element
                    if UNattributeValue in YUnitNamesList:
                        y_variable = element
                    if AXattributeValue in XAxisNameList:
                        x_variable = element
                    if AXattributeValue in YAxisNameList:
                        y_variable = element

            except:
                CoordAttributeValue = "missing"

        # Convert the python list of selected variable into a single
        # (comma delimited) string if necessary.

        if selectedVars.count > 1:
            variable_list = ','.join([str(x) for x in selectedVars])
        else:
            variable_list = selectedVars[0]

        # Set the row dimensions parameter of the Make NetCDF Feature Layer tool
        # to the trajectory dimension name.

        try:
            dimNames = ncFP.getDimensionsByVariable(selectedVars[0])
            row_dimension = str(dimNames[-1])
        except:
            row_dimension = ""

        if (x_variable) and (y_variable) and (row_dimension):
            if (debug):
                arcpy.AddWarning("netCDFFile Name: %s" % \
                    parameters[0].valueAsText)
                arcpy.AddWarning("Variable List: %s" % variable_list)
                arcpy.AddWarning("x_variable: %s" % x_variable)
                arcpy.AddWarning("y_variable: %s" % y_variable)
                arcpy.AddWarning("Output Feature Layer: %s " % \
                    parameters[2].valueAsText)
                arcpy.AddWarning("Row Dimensions: %s " % row_dimension)
                arcpy.AddWarning("Z Variable: %s " % ZVariable)

            result1 = arcpy.MakeNetCDFFeatureLayer_md(
                parameters[0].valueAsText, variable_list, x_variable,
                y_variable, parameters[2].valueAsText, row_dimension,
                ZVariable)

            # Force the netCDF Feature Layer to be added to the display
            arcpy.SetParameter(3, result1)
        else:
            if (not x_variable) or (not y_variable):
                msg1 = "Unable to automatically determine x and y variables " \
                   + "from the netCDF file. Use Make NetCDF Feature Layer tool."
                arcpy.AddError(msg1)
            if (not row_dimension):
                msg1 = "Unable to automatically determine row dimension " \
                     + "variable(s) from the netCDF file. Use Make NetCDF " \
                    + "Feature Layer tool."
                arcpy.AddError(msg1)
        return
示例#12
0
inputMD = arcpy.GetParameterAsText(1)

# Populate the key variables needed by this script
wim.PopulateVariables("NetCDF", inputMD)

# Empty the existing contents of the forecast GDB
wim.EmptyGDB(wim.forecastGDBPath)

# Load the config tables into dictionaries
wim.LoadConfigTables("NetCDF")

# set the locale to the user's environment
locale.setlocale(locale.LC_TIME, '')

# access the properties of the NetCDF file
ncFP = arcpy.NetCDFFileProperties(inNetCDF)

timeVar = "time"
latVar = "latitude"
longVar = "longitude"
vars = ncFP.getVariables()
# First, loop through the variables to get the latitude, longitude and time variables
for var in vars:
    stdvar = ""
    try:
        stdvar = ncFP.getAttributeValue(var, "standard_name")
        arcpy.AddMessage("Standard name for variable " + var + " is " + stdvar)
    except:
        arcpy.AddMessage("No standard name attribute for " + var)
        try:
            stdvar = ncFP.getAttributeValue(var, "long_name")
示例#13
0
def GCMO_NetCDF(netcdf_list, variable, outdir):
    """
    Extracts all time layers from a "Global Climate Model Output" NetCDF layer

    Inputs:
        netcdf_list     list of netcdfs from CORDEX climate distribution
        varaible        the climate variable of interest (tsmax, tsmin, etc)
        outdir          output directory to save files.
    """

    if not os.path.exists(outdir):
        os.makedirs(outdir)

    netcdf_list = core.enf_list(netcdf_list)

    for netcdf in netcdf_list:
        # get net cdf properties object
        props = arcpy.NetCDFFileProperties(netcdf)

        print("finding dimensions")
        dims = props.getDimensions()
        for dim in dims:
            print dim, props.getDimensionSize(dim)

        # make sure the variable is in this netcdf
        if variable:
            if not variable in props.getVariables():
                print("Valid variables for this file include {0}".format(
                    props.getVariables()))
                raise Exception(
                    "Variable '{0}' is not in this netcdf!".format(variable))

        for dim in dims:
            if dim == "time":

                # set other dimensions
                x_dim = "lon"
                y_dim = "lat"
                band_dim = ""
                valueSelectionMethod = "BY_VALUE"

                size = props.getDimensionSize(dim)
                for i in range(size):

                    # sanitize the dimname for invalid characters
                    dimname = props.getDimensionValue(dim, i).replace(
                        " 12:00:00 PM", "")
                    dimname = dimname.replace("/", "-").replace(" ", "_")

                    dim_value = [["time", props.getDimensionValue(dim, i)]]
                    print("extracting '{0}' from '{1}'".format(
                        variable, dim_value))

                    outname = core.create_outname(outdir, netcdf, dimname,
                                                  'tif')

                    arcpy.MakeNetCDFRasterLayer_md(netcdf, variable, x_dim,
                                                   y_dim, "temp", band_dim,
                                                   dim_value,
                                                   valueSelectionMethod)
                    arcpy.CopyRaster_management("temp", outname, "", "", "",
                                                "NONE", "NONE", "")

    return
示例#14
0
import arcpy
from arcpy.sa import *  #引入模块
outLoc = "E:/seaice/osisaf_conc/testtif_type"  #输出路径
inNetCDF = "E:/seaice/osisaf_conc/type/ice_type_nh_polstere-100_multi_201908021200.nc"  #输入路径
variable = 'ice_type'  #此处是.nc数据中的变量名(海冰类型)
x_dimension = "xc"
y_dimension = "yc"
band_dimension = ""
valueSelectionMethod = "BY_VALUE"  #以上五个变量为第一个函数会用到的变量,提前定义好
nc_FP = arcpy.NetCDFFileProperties(inNetCDF)  #读取netCDF文件
nc_Dim = nc_FP.getDimensions()  #获取维度信息,返回一个维度列表 ['lon','lat','time']
'''
在一个.nc文件中有400个时间,每天有一个海冰密集度数据,所以导出有400个tiff图像
为了给导出图像方便命名,要使用 dimension_values ,每一个输出的变量值都是使用该维度的值
'''

for dimension in nc_Dim:

    if dimension == "time":
        top = nc_FP.getDimensionSize(dimension)  #获取维度的大小
        for i in range(0, 1):
            dimension_values = nc_FP.getDimensionValue(dimension, i)
            nowFile = str(dimension_values[0:7])
            nowFile = nowFile.translate(None, '/')
            print('doing....' + str(i + 1))
            dv1 = ["time", dimension_values]
            dimension_values = [dv1]
            #nowFile=str(i+1)
            outpath = outLoc + "\\" + nowFile + ".tif"

            arcpy.MakeNetCDFRasterLayer_md(inNetCDF, variable, x_dimension,
    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Describe the supplied netCDF File.
        ncFP = arcpy.NetCDFFileProperties(parameters[0].valueAsText)
        selectedVars = parameters[1].valueAsText.split(';')

        # Coordinate variables can be identified via values of their standard
        # name attribute or values of their untis attribute.

        XCoordNamesList = [
            "longitude", "projection_x_coordinate", "grid_longitude"
        ]
        YCoordNamesList = [
            "latitude", "projection_y_coordinate", "grid_longitude"
        ]
        XUnitNamesList = [
            "degrees_east", "degree_east", "degree_E", "degrees_E", "degreeE",
            "degreesE"
        ]
        YUnitNamesList = [
            "degrees_north", "degree_north", "degree_N", "degrees_N",
            "degreeN", "degreesN"
        ]
        XAxisNameList = ["X"]
        YAxisNameList = ["Y"]

        # Case 1: Data are organized by a single station dimension and there
        # are spatial variables also organized by that station dimension.

        dimNames = ncFP.getDimensionsByVariable(str(selectedVars[0]))
        # Must assume that the station dimension is the right most dimension.
        station_dimension = dimNames[-1]
        varNames = ncFP.getVariablesByDimension(station_dimension)
        x_variable = ""
        y_variable = ""
        row_dimension = ""
        for var in varNames:
            if (debug):
                arcpy.AddMessage(var)
            # Identify the coordinate variable by its standard name, units, or
            # axis attribute.

            try:
                SNattributeValue = ncFP.getAttributeValue(var, "standard_name")
            except:
                SNattributeValue = "missing"

            try:
                UNattributeValue = ncFP.getAttributeValue(var, "units")
            except:
                UNattributeValue = "missing"

            try:
                AXattributeValue = ncFP.getAttributeValue(var, "axis")
            except:
                AXattributeValue = "missing"

            if SNattributeValue in XCoordNamesList:
                x_variable = var
            if SNattributeValue in YCoordNamesList:
                y_variable = var
            if UNattributeValue in XUnitNamesList:
                x_variable = var
            if UNattributeValue in YUnitNamesList:
                y_variable = var
            if AXattributeValue in XAxisNameList:
                x_variable = var
            if AXattributeValue in YAxisNameList:
                y_variable = var

        # Case #2: Two dimensional lat/long coordinate variable
        # If unsuccessful in locating variable for x and y coordinates based on
        # station dimension, check to see if variable of interest is
        # georeferenced via a 2 dimensional lat/long coordinate variable

        # Coordinate Variable Method will only work if CDL conforms to the
        # CF 1.6 convention (section 2.4) that "All other dimensions should,
        # whenever possible, be placed to the left of the spatiotemporal
        # dimensions."

        if (not x_variable) or (not y_variable):
            try:
                coordAttributeValue = ncFP.getAttributeValue(
                    str(selectedVars[0]), "coordinates")
                coordVariables = coordAttributeValue.split(' ')
                for element in coordVariables:
                    try:
                        SNattributeValue = ncFP.getAttributeValue(
                            element, "standard_name")
                    except:
                        SNattributeValue = "missing"

                    try:
                        UNattributeValue = ncFP.getAttributeValue(
                            element, "units")
                    except:
                        UNattributeValue = "missing"

                    try:
                        AXattributeValue = ncFP.getAttributeValue(
                            element, "axis")
                    except:
                        AXattributeValue = "missing"

                    if SNattributeValue in XCoordNamesList:
                        x_variable = element
                    if SNattributeValue in YCoordNamesList:
                        y_variable = element
                    if UNattributeValue in XUnitNamesList:
                        x_variable = element
                    if UNattributeValue in YUnitNamesList:
                        y_variable = element
                    if AXattributeValue in XAxisNameList:
                        x_variable = element
                    if AXattributeValue in YAxisNameList:
                        y_variable = element

            except:
                CoordAttributeValue = "missing"

        # Convert the python list of selected variable into a single
        # (comma delimited) string if necessary.
        if selectedVars.count > 1:
            variable_list = ','.join([str(x) for x in selectedVars])
        else:
            variable_list = selectedVars[0]

        # Set the row dimensions parameter of the Make NetCDF Feature Layer tool
        # to the right-most dimension name of the first variable (this
        # should be the name of the station dimension.
        try:
            dimNames = ncFP.getDimensionsByVariable(selectedVars[0])
            row_dimension = str(dimNames[-1])
        except:
            row_dimension = ""

        if (x_variable) and (y_variable) and (row_dimension):
            if (debug):
                arcpy.AddWarning("netCDFFile Name: %s" % \
                    parameters[0].valueAsText)
                arcpy.AddWarning("Variable List: %s" % variable_list)
                arcpy.AddWarning("x_variable: %s" % x_variable)
                arcpy.AddWarning("y_variable: %s" % y_variable)
                arcpy.AddWarning("Output Feature Layer: %s " % \
                    parameters[2].valueAsText)
                arcpy.AddWarning("Row Dimensions: %s " % row_dimension)

            result1 = arcpy.MakeNetCDFFeatureLayer_md(
                parameters[0].valueAsText, variable_list, x_variable,
                y_variable, parameters[2].valueAsText, row_dimension)
            # Force the netCDF Feature Layer to be added to the display
            arcpy.SetParameter(3, result1)
        else:
            if (not x_variable) or (not y_variable):
                msg1 = "Unable to automatically determine x and y variables " \
                   + "from the netCDF file. Use Make NetCDF Feature Layer tool."
                arcpy.AddError(msg1)
            if (not row_dimension):
                msg1 = "Unable to automatically determine row dimension " \
                     + "variable(s) from the netCDF file. Use Make NetCDF " \
                    + "Feature Layer tool."
                arcpy.AddError(msg1)
        return
    def execute(self, parameters, messages):
        """The source code of the tool."""

        # Describe the supplied netCDF File.
        ncFP = arcpy.NetCDFFileProperties(parameters[0].valueAsText)
        selectedVars = parameters[1].valueAsText.split(';')

        # Coordinate variables can be identified via values of their standard
        # name attribute or values of their untis attribute.

        XCoordNamesList = ["longitude", "projection_x_coordinate",
                            "grid_longitude"]
        YCoordNamesList = ["latitude", "projection_y_coordinate",
                           "grid_longitude"]
        XUnitNamesList = ["degrees_east", "degree_east", "degree_E",
                           "degrees_E", "degreeE", "degreesE" ]
        YUnitNamesList = ["degrees_north", "degree_north", "degree_N",
                              "degrees_N", "degreeN", "degreesN"]
        XAxisNameList = ["X"]
        YAxisNameList = ["Y"]

        # Case 1: Independent latitude, Longitude, Vertical and Time Axes
        # Get the dimensions associated with the first variable.  We can use a
        # single variable because tool validation guarentees that all the
        # selected variables have the same dimensions.
        dimNames = ncFP.getDimensionsByVariable(str(selectedVars[0]))
        x_variable = ""
        y_variable = ""
        for dimension in dimNames:
            # Per CF 1.6 (Chapter 5, preamble): "All of a variable's dimensions
            # that are latitude, longitude, vertical, or time dimensions must
            # have corresponding coordinate variables, i.e., one-dimensional
            # variables with the same name as the dimension."
            # Identify the coordinate variable by its standard name or units.

            try:
                SNattributeValue = ncFP.getAttributeValue(dimension,
                    "standard_name")
            except:
                SNattributeValue = "missing"

            try:
                UNattributeValue = ncFP.getAttributeValue(dimension, "units")
            except:
                UNattributeValue = "missing"

            try:
                AXattributeValue = ncFP.getAttributeValue(dimension,"axis")
            except:
                AXattributeValue = "missing"

            if SNattributeValue in XCoordNamesList:
                x_variable = dimension
            if SNattributeValue in YCoordNamesList:
                y_variable = dimension
            if UNattributeValue in XUnitNamesList:
                x_variable = dimension
            if UNattributeValue in YUnitNamesList:
                y_variable = dimension
            if AXattributeValue in XAxisNameList:
                x_variable = dimension
            if AXattributeValue in YAxisNameList:
                y_variable = dimension

        # Set the row dimensions parameter of the Make NetCDF Feature Layer tool
        # to the names of the latitude and longitude dimensions
        rowDimensions = ""
        rowDimensions = x_variable + ";" + y_variable


        # Case #2: 2 dimensional lat/long coordinate variable
        # If unsuccessful in locating variable for x and y coordinates based on
        # latitude and longitude, check to see if variable of interest is
        # georeferenced via a 2 dimensional lat/long coordinate variable

        # Coordinate Variable Method will only work if CDL conforms to the
        # CF 1.6 convention (section 2.4) that "All other dimensions should,
        # whenever possible, be placed to the left of the spatiotemporal
        # dimensions."

        if (not x_variable) or (not y_variable):
            try:
                coordAttributeValue = ncFP.getAttributeValue(str(selectedVars[0]),
                    "coordinates")
                coordVariables = coordAttributeValue.split(' ')
                for element in coordVariables:
                    try:
                        SNattributeValue = ncFP.getAttributeValue(element,
                            "standard_name")
                    except:
                        SNattributeValue = "missing"

                    try:
                        UNattributeValue = ncFP.getAttributeValue(element,
                            "units")
                    except:
                        UNattributeValue = "missing"

                    try:
                        AXattributeValue = ncFP.getAttributeValue(element,
                            "axis")
                    except:
                        AXattributeValue = "missing"

                    if SNattributeValue in XCoordNamesList:
                        x_variable = element
                    if SNattributeValue in YCoordNamesList:
                        y_variable = element
                    if UNattributeValue in XUnitNamesList:
                        x_variable = element
                    if UNattributeValue in YUnitNamesList:
                        y_variable = element
                    if AXattributeValue in XAxisNameList:
                        x_variable = element
                    if AXattributeValue in YAxisNameList:
                        y_variable = element

                # Set the row dimensions parameter of the Make NetCDF Feature
                # Layer tool to the names of the coordinate variables
                dimNames = ncFP.getDimensionsByVariable(str(selectedVars[0]))
                rowDimensions = str(dimNames[-1]) + ";" + str(dimNames[-2])


            except:
                CoordAttributeValue = "missing"

        # Convert the python list of selected variables into a single
        # (comma delimited string if necessary.
        if selectedVars.count > 1:
            variable_list = ','.join([str(x) for x in selectedVars])
        else:
            variable_list = selectedVars[0]

        if (x_variable) and (y_variable):
            if (debug):
                arcpy.AddWarning("netCDFFile Name: %s" \
                    % parameters[0].valueAsText)
                arcpy.AddWarning("Variable List: %s" % variable_list)
                arcpy.AddWarning("x_variable: %s" % x_variable)
                arcpy.AddWarning("y_variable: %s" % y_variable)
                arcpy.AddWarning("Output Feature Layer: %s " \
                    % parameters[2].valueAsText)
                arcpy.AddWarning("Row Dimensions: %s " % rowDimensions)

            result1 = arcpy.MakeNetCDFFeatureLayer_md(parameters[0].valueAsText,
                                            variable_list,
                                            x_variable,
                                            y_variable,
                                            parameters[2].value,
                                            rowDimensions)
            # Force the netCDF Feature Layer to be added to the display
            arcpy.SetParameter(3, result1)
        else:
            arcpy.AddError("Unable to automatically determine x and y " +
                "variables from the netCDF file. Use Make NetCDF " +
                "Feature Layer tool.")
        return