示例#1
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)
示例#2
0
def preProcess_IMERGD():
    inPath="F:/Test/GraduationWork/Data/IMERG/"
    ncPath=inPath+"Daily_nc_V06/"
    outTifPath=inPath+"Daily_tif_0.1/"
    outHBALPath = inPath + "Daily_HBAL/"
    outResampledPath=inPath+"Daily_HBAL_0.05/"
    workspace=inPath + "0/"
    mask="F:/Test/GraduationWork/Data/HB/"+"HubeiAreaLarge.shp"
    for f in os.listdir(ncPath):
        env.workspace=workspace
        ncfile=os.path.join(ncPath,f)
        date=f.split('-')[1].split('.')[-1]

        layer = 'nc_' + date
        arcpy.MakeNetCDFRasterLayer_md(ncfile, "precipitationCal", "lon", "lat", layer)  # "nc制作图层"

        outTif=outTifPath+"IMERGD_"+date+".tif"
        arcpy.Resample_management(layer, outTif, "0.1", "BILINEAR")

        outExtractByMask = arcpy.sa.ExtractByMask(outTif, mask)
        outHBAL=outHBALPath+"IMERGD_"+date+"_HBAL"+".tif"
        outExtractByMask.save(outHBAL)

        outTif = outResampledPath + "Resampled_IMERGD_" + date + "_HBAL"+".tif"
        arcpy.Resample_management(outHBAL, outTif, "0.05", "BILINEAR")
        print("{} is done!".format(date))
        # break

    return 0
示例#3
0
def TRMM_NetCDF(filelist, outdir):
    """
    Function converts NetCDFs to tiffs. Designed to work with TRMM data
    downloaded from GLOVIS

    :param filelist:            list of '.nc' files to convert to tifs.
    :param outdir:              directory to which tif files should be saved

    :return output_filelist:    list of local filepaths of extracted data.
    """

    # Set up initial parameters.
    arcpy.env.workspace = outdir
    filelist = core.enf_list(filelist)
    output_filelist = []

    # convert every file in the list "filelist"
    for infile in filelist:

        # use arcpy module to make raster layer from netcdf
        arcpy.MakeNetCDFRasterLayer_md(infile, "r", "longitude", "latitude",
                                       "r", "", "", "BY_VALUE")
        arcpy.CopyRaster_management("r", infile[:-3] + ".tif", "", "", "",
                                    "NONE", "NONE", "")
        output_filelist.append(infile[:-3] + ".tif")
        print('Converted netCDF file ' + infile + ' to Raster')

    return output_filelist
def createRaster(folder, netcdffile, tiffolder):
    arcpy.env.workspace = tiffolder
    filename = os.path.join(folder, netcdffile)
    newfilename = netcdffile + ".tif"
    tiffile = os.path.join(tiffolder, newfilename)
    if not os.path.exists(os.path.join(tiffolder, newfilename)):
        arcpy.MakeNetCDFRasterLayer_md(in_netCDF_file=filename,
                                       variable="precipitationCal",
                                       x_dimension="lon",
                                       y_dimension="lat",
                                       out_raster_layer=newfilename,
                                       band_dimension="",
                                       dimension_values="",
                                       value_selection_method="BY_VALUE")
        arcpy.CopyRaster_management(newfilename, tiffile, "", "", "", "NONE",
                                    "NONE", "")
        if not os.path.exists(tiffile):
            print("Failed to create " + newfilename)
        if os.path.exists(tiffile):
            print(newfilename + " is successfully created")
    else:
        print(newfilename + " already exists")


# folder = 'D:\\IDN_GIS\\01_Data\\01_Global\\Rasters\\Climate\\Precipitation\\IMERG\\daily\\NC4'
# tiffolder = 'D:\\IDN_GIS\\01_Data\\01_Global\\Rasters\\Climate\\Precipitation\\IMERG\\daily\\tif'
# for i in os.listdir(folder):
#     if i.endswith(".nc4"):
#         print(i)
#         createRaster(folder, i, tiffolder)
示例#5
0
def f1():

    ##########
    arcpy.env.workspace = ""
    rasters = arcpy.ListRasters()
    mask=""
    for raster in rasters:
        out = ExtractByMask(raster, mask)   #"按掩膜提取"
        out.save("xxx/_34.tif")

        arcpy.Resample_management(raster, out, "xres yres", "BILINEAR")  # "NEAREST ","BILINEAR","CUBIC","MAJORITY" #"重采样"

        arcpy.ExtractSubDataset_management("xxx.hdf", "outfile.tif", "2")   #"提取子数据集,第三个参数是选择提取第几个子数据集(波段)"

        layer=""
        arcpy.MakeNetCDFRasterLayer_md(raster, "precipitation", "lon", "lat", layer)      # "nc制作图层"
        arcpy.CopyRaster_management(layer, out, format="TIFF")  # "图层保存为栅格"

        ExtractValuesToPoints(mask, raster,out, "INTERPOLATE","VALUE_ONLY") # "值提取到点"/"NONE","INTERPOLATE"/"VALUE_ONLY","ALL"

        out= SetNull(raster, raster, "Value=-3000") # "将满足条件的像元值设为Nodata"

        out=CellStatistics(rasters, out, "SUM", "NODATA")   # "像元统计" "MEAN/MAJORITY/MAXIMUM/MEDIAN/MINIMUM/MINORITY/RANGE/STD/SUM/VARIETY "    "NODATA"/"DATA"忽略nodata像元
        out.save("xxx.img")

        arcpy.Delete_management(raster) # "删除文件"

        rasters = arcpy.ListRasters()   # "数据的重命名"
        for raster in rasters:
            raster.save("xxx.tif")

        arcpy.TableToExcel_conversion(mask, "xxx.xls")# "表转Excel"

        arcpy.DirectionalDistribution_stats(raster, out, "1_STANDARD_DEVIATION", "xxx", "#")# "标准差椭圆"
        arcpy.MeanCenter_stats(raster, out, "xxx", "#", "#")    # "中心"
示例#6
0
def makeRasterArray(files):
	#outputFolder = "D:/Users/cmarciniak/My Documents/GLDAS/rasters/"
	print("Making rasters...")
	rasters=[]
	for file in files:
		print("Making raster from "+file)
		tempFile = file[0:12]
		arcpy.MakeNetCDFRasterLayer_md( file , "AvgSurfT_GDS0_SFC", "g0_lon_1","g0_lat_0",tempFile)
		rasters.append(tempFile)
	return rasters
示例#7
0
def exportbands(timeband, inputproperties, inputfilepath, outputpath):
    '''
    Within the NetCDF processing loop we create raster files
    and save them to new folders.

    Take date arguments...

    Along with the dimension value, the input file, and the
    output file.
    '''

    # Grab a string value and the date value from current band.
    dimension_date, dimension_value = sub_getdatefromcurrentband(
        inputproperties, timeband)

    # Parse date variable of current file into logical chunks.
    # The string is in the Month/Day/Year format:
    filemonth, fileday, fileyear = sub_processdatesfromnetcdf(dimension_date)

    # Make filename from year, month, and day:
    arguments = [noaafile_type, fileyear, filemonth, fileday]
    outputrastername = '_'.join(map(str, arguments))
    print('Exporting raster {}.tif'.format(outputrastername))

    # Take CURRENT dimensions from current NetCDF.
    # Pull current layer out as raster layer in memory named 'outfilename'.
    # rainfall (PRATE) is the value that will be mapped.

    makenetcdf = arcpy.MakeNetCDFRasterLayer_md(inputfilepath, "prate", "lon",
                                                "lat", "temporaryraster", "",
                                                dimension_value, "BY_VALUE")

    # Convert in-memory raster layer to a saved layer, also named 'outfilename'.
    outputrasterfile = os.path.join(outputpath,
                                    ''.join([outputrastername, '.tif']))

    arcpy.AddMessage("Writing " + outputrastername)

    # Resample raster such that resultant raster has half the X and Y dimensions
    # This will make each raster having second column perfectly aligned by prime meridian
    arcpy.Resample_management("temporaryraster", outputrasterfile,
                              "0.9375 0.94747340425532", "NEAREST")

    # Clip first column of each raster (the only column that lies in -X dimension)
    arcpy.Clip_management(outputrasterfile, "-0.9375 -90.054779 0 88.070221",
                          "clip", "", "", "NONE", "NO_MAINTAIN_EXTENT")

    # Mosaic the first column of each raster to the original raster
    # ArcGIS will automatically take care of converting -ve X values to +ve
    arcpy.Mosaic_management("clip", outputrasterfile, "LAST", "LAST", "0", "9",
                            "", "", "")

    print makenetcdf.getMessages()
    arcpy.Delete_management(outputrastername)
    arcpy.Delete_management("in_memory")
示例#8
0
def exportbands(timeband,
                inputproperties,
                inputfilepath,
                outputpath):
    '''
    Within the NetCDF processing loop we create raster files
    and save them to new folders.

    Take date arguments...

    Along with the dimension value, the input file, and the
    output file.
    '''

    # Grab a string value and the date value from current band.
    dimension_date, dimension_value = sub_getdatefromcurrentband(inputproperties,
                                                                 timeband)

    # Parse date variable of current file into logical chunks.
    # The string is in the Month/Day/Year format:
    filemonth, fileday, fileyear = sub_processdatesfromnetcdf(dimension_date)

    # Make filename from year, month, and day:
    arguments = [noaafile_type, fileyear, filemonth, fileday]
    outputrastername = '_'.join(map(str, arguments))
    print('Exporting raster {}.tif'.format(outputrastername))

    # Take CURRENT dimensions from current NetCDF.
    # Pull current layer out as raster layer in memory named 'outfilename'.
    # rainfall (PRATE) is the value that will be mapped.
    makenetcdf = arcpy.MakeNetCDFRasterLayer_md(inputfilepath,
                                                "prate",
                                                "lon",
                                                "lat",
                                                "temporaryraster",
                                                "",
                                                dimension_value,
                                                "BY_VALUE")

    # Convert in-memory raster layer to a saved layer, also named 'outfilename'.
    outputrasterfile = os.path.join(outputpath, ''.join([outputrastername, '.tif']))
    copyraster = arcpy.CopyRaster_management("temporaryraster",
                                             outputrasterfile,
                                             "",
                                             "")

    # Print errors and delete objects from ArcPy environment,
    # ... emptying the temporary memory of the environment.
    print copyraster.getMessages()
    print makenetcdf.getMessages()
    arcpy.Delete_management(outputrastername)
    arcpy.Delete_management("in_memory")
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)
示例#10
0
文件: main.py 项目: HNoorazar/Ag
def export_tiffs(rasters_path_out,
                 nc_path,
                 name_format,
                 export_hants_only=False):
    '''
    This function exports the output of the HANTS analysis.
    If 'export_hants_only' is False (default), the output rasters have the best
    value available. Therefore, the cells in the output rasters will have the
    original value for the cells that are not outliers and the hants values for
    the cells that are outliers or the cells where data is not available.
    If 'export_hants_only' is True, the exported rasters have the values
    obtained by the HANTS algorithm disregarding of the original values.
    '''
    # Print
    print 'Exporting...'

    # Create folders
    if not os.path.exists(rasters_path_out):
        os.makedirs(rasters_path_out)
    # Read time data
    nc_file = netCDF4.Dataset(nc_path, 'r')
    time_var = nc_file.variables['time'][:]
    nc_file.close()

    # Output type
    if export_hants_only:
        variable_selected = 'hants_values'
    else:
        variable_selected = 'combined_values'

    # Loop through netcdf file
    for yyyymmdd in time_var:
        print '\t{0}'.format(yyyymmdd)
        output_name = rasters_path_out + os.sep + name_format.format(yyyymmdd)

        temp_lyr_name = 'ras_{0}'.format(yyyymmdd)
        arcpy.MakeNetCDFRasterLayer_md(nc_path, variable_selected, 'longitude',
                                       'latitude', temp_lyr_name, '#',
                                       'time {0}'.format(yyyymmdd), 'BY_VALUE')
        output_ras = arcpy.Raster(temp_lyr_name)
        output_ras.save(output_name)
        arcpy.management.Delete(temp_lyr_name)

    # Return
    print 'Done'
    return rasters_path_out
示例#11
0
def nc_make_raster(ncinfo):
    # 设置日期
    date_start = datetime.date(ncinfo['yrstart'], 1, 1)
    date_end = datetime.date(ncinfo['yrend'], 12, 31)
    date_add = datetime.timedelta(1)

    # make netcdf to rasters loop
    date = date_start
    while date <= date_end:
        try:
            # Set local variables
            inNetCDFFile = ncinfo['name']
            variable = ncinfo['type']
            XDimension = 'lon'
            YDimension = 'lat'
            # 注意保存文件名的时候,arcgis保存入数据库是不允许出现‘-’符合的,所以要去除
            outRasterLayer = arcpy.env.workspace + os.sep + ncinfo[
                'type'] + '_' + ncinfo['rcp'] + '_' + date.isoformat().replace(
                    '-', '') + '_nc'
            bandDimmension = ''
            dimensionValues = 'time %s' % date.isoformat()
            valueSelectionMethod = 'BY_VALUE'

            # Execute MakeNetCDFRasterLayer
            arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension,
                                           YDimension, outRasterLayer,
                                           bandDimmension, dimensionValues,
                                           valueSelectionMethod)

            # save raster dataset
            temp_save = arcpy.env.workspace + '\\' + ncinfo[
                'type'] + '_' + ncinfo['rcp'] + '_' + date.isoformat().replace(
                    '-', '')
            Raster(outRasterLayer).save(temp_save)
            print 'Read netCDF successed: %s at %s' % (ncinfo['name'],
                                                       date.isoformat())

        except:
            files_fail['nc'].append(ncinfo['name'] + ' at ' + date.isoformat())
            print arcpy.GetMessages()
            print 'Read netCDF failed: %s at %s' % (ncinfo['name'],
                                                    date.isoformat())

        # 时间+1,完成万物的循环~~~
        date += date_add
示例#12
0
def NetCDF2Tiff(inNetCDFFile, toTIFFFile):

    variable = "rain"
    XDimension = "longitude"
    YDimension = "latitude"
    outRasterLayer = "rain_Layer"
    bandDimmension = ""
    dimensionValues = ""
    valueSelectionMethod = "BY_VALUE"

    print "start"
    arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension,
                                   YDimension, outRasterLayer, bandDimmension,
                                   dimensionValues, valueSelectionMethod)
    print "done"
    arcpy.CopyRaster_management(outRasterLayer, toTIFFFile)

    print "finish"
示例#13
0
    def convert_netcdf(self):
        """Convert NetCDF to a different file type"""

        for source_path in self.get_dir_paths():
            for file in os.listdir(source_path):
                if file.startswith("3B43") & file.endswith(".nc"):
                    file_path = os.path.join(source_path,
                                             file).replace("\\", "/")
                    out_ras = source_path + "/TIFF_" + file[-23:-7] + ".tif"
                    out_pcp = file[-23:-7]
                    print("CONVERTING ...... " + file + " TO TIFF")
                    arcpy.MakeNetCDFRasterLayer_md(
                        file_path, "pcp", "longitude", "latitude", out_pcp, "",
                        "", "BY_VALUE")  # Make NetCDF Raster Layer
                    arcpy.CopyRaster_management(out_pcp, out_ras, "", "",
                                                "-9999.90039063", "NONE",
                                                "NONE", "32_BIT_FLOAT", "NONE",
                                                "NONE")  # Copy Raster
        print("ALL FILES CONVERTED SUCCESSFULLY!!!!!")
示例#14
0
def f1():
    pathIn = 'F:/Test/Data/IMERG/' + 'IMERGM_nc_201501_201805_HB91_51/'
    pathOut = 'F:/Test/Data/IMERG/' + 'IMERGM_tif_201501_201805_HB91_51/'
    arcpy.env.workspace = pathIn
    for nc_file in arcpy.ListFiles("*.nc"):
        time = nc_file[20:28]
        y, m, d = int(time[0:4]), int(time[4:6]), int(time[6:8])
        layer = 'nc_' + time
        arcpy.MakeNetCDFRasterLayer_md(nc_file, "precipitation", "lon", "lat",
                                       layer)  # "nc制作图层"
        if (m == 2):
            if (y == 2016):
                times = 29 * 24
            else:
                times = 28 * 24
        elif (m == 4 or m == 6 or m == 9 or m == 11):
            times = 30 * 24
        else:
            times = 31 * 24
        print y, m, d
        outTimes = Times(layer, times)
        outTimes.save(pathOut + 'I' + time[0:6] + '.tif')
def createRaster(folder, netcdffile, tiffolder):
    arcpy.env.workspace = tiffolder
    filename = os.path.join(folder, netcdffile)
    newfilename = netcdffile + ".tif"
    tiffile = os.path.join(tiffolder, newfilename)
    if not os.path.exists(os.path.join(tiffolder, newfilename)):
        arcpy.MakeNetCDFRasterLayer_md(in_netCDF_file=filename,
                                       variable="precipitationCal",
                                       x_dimension="lon",
                                       y_dimension="lat",
                                       out_raster_layer=newfilename,
                                       band_dimension="",
                                       dimension_values="",
                                       value_selection_method="BY_VALUE")
        arcpy.CopyRaster_management(newfilename, tiffile, "", "", "", "NONE",
                                    "NONE", "")
        if not os.path.exists(tiffile):
            print("Failed to create " + newfilename)
        if os.path.exists(tiffile):
            print(newfilename + " is successfully created")
    else:
        print(newfilename + " already exists")
示例#16
0
def func_2():  #nc转tif,裁剪,值提取到点
    inPath = "F:/Test/Data/IMERG/IMERGDV6_nc_20180601_20180731"
    outPath = "F:/Test/Data/IMERG/IMERGDV6_tif_20180601_20180731"
    Points323 = "F:/Test/Paper180614/Data/Points/" + "Points323.shp"
    shpPath = "F:/Test/Paper180614/Data/IEMRG/RGS323_DAY"
    Excel = "F:/Test/Paper180614/Data/PreExtreme/" + "IMERGPreExtreme.csv"

    # arcpy.env.workspace = outPath
    Df = pd.DataFrame()

    for file in os.listdir(inPath):
        # 3B-DAY.MS.MRG.3IMERG.20180609-S000000-E235959.V06.nc4.nc
        fileName = file[21:29]
        print(fileName)
        ncFile = os.path.join(inPath, file)
        arcpy.MakeNetCDFRasterLayer_md(ncFile,
                                       "precipitationCal",
                                       "lon",
                                       "lat",
                                       out_raster_layer=fileName)
        tifFile = os.path.join(outPath, 'I' + fileName + '.tif')
        arcpy.CopyRaster_management(fileName,
                                    out_rasterdataset=tifFile,
                                    format="TIFF")

        outshp = os.path.join(shpPath, 'RGS323_I' + fileName + '.shp')
        arcpy.sa.ExtractValuesToPoints(Points323, tifFile, outshp,
                                       'INTERPOLATE', "VALUE_ONLY")

        tempList = arcpy.da.FeatureClassToNumPyArray(outshp, ('RASTERVALU'))
        tempDf = pd.DataFrame(data=tempList['RASTERVALU'], columns=[fileName])
        Df = pd.concat((Df, tempDf), axis=1)
        # print(Df)
        if fileName == '20180602':
            # break
            continue
    Df.to_csv(Excel)
    return 0
示例#17
0
def TRMM_NetCDF(filelist, outdir):
    """
     Function converts NetCDFs to tiffs. Designed to work with TRMM data.

     inputs:
       filelist    list of '.nc' files to conver to tifs.
       outdir      directory to which tif files should be saved
    """

    # Set up initial parameters.
    arcpy.env.workspace = outdir
    filelist = core.enforce_list(filelist)

    # convert every file in the list "filelist"
    for infile in filelist:

        # use arcpy module to make raster layer from netcdf
        arcpy.MakeNetCDFRasterLayer_md(infile, "r", "longitude", "latitude",
                                       "r", "", "", "BY_VALUE")
        arcpy.CopyRaster_management("r", infile[:-3] + ".tif", "", "", "",
                                    "NONE", "NONE", "")
        print('{NetCDF} Converted netCDF file ' + infile + ' to Raster')

    return
示例#18
0
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
示例#19
0
    rain_raster = os.path.join(arcpy.env.scratchFolder,
                               'rain_raster_' + filenameSuffix + '.tif')
    pet_raster = os.path.join(arcpy.env.scratchFolder,
                              'pet_raster_' + filenameSuffix + '.tif')
    out_raster_layer = "out_raster_layer"
    print(rain_raster)
    print(dimension_values)

    # arcpy.MakeNetCDFRasterLayer_md(in_netCDF_file="C:\\LUCI_data\\Aparima\\VCSN\\rain_vclim_clidb_1972010100_2018101500_south-island_p05_daily_Aparima.nc", variable="rain", x_dimension="longitude", y_dimension="latitude", out_raster_layer=out_raster_layer, band_dimension="", dimension_values=dimension_values, value_selection_method="BY_VALUE")

    # Create raster layers from netCDF files
    arcpy.MakeNetCDFRasterLayer_md(in_netCDF_file=rain_netCDF,
                                   variable="rain",
                                   x_dimension="longitude",
                                   y_dimension="latitude",
                                   out_raster_layer=out_raster_layer,
                                   dimension_values=dimension_values,
                                   value_selection_method="BY_VALUE")

    print('Created raster layer')
    arcpy.CopyRaster_management(out_raster_layer, rain_raster)
    print('Saved tiff')

sys.exit()

# Define Strahler 1 shp
reaches_shp = r'C:\LUCI_data\Aparima\GIS\NIWA\Strahler1_Watershed_DN3_Aparima.shp'

# Project rasters to same coordinate system as reaches shapefile
spat_ref = arcpy.Describe(reaches_shp).spatialReference
    def mpprocess(output_directory, variable_name, input_woa_netcdf,
                  interpolation_procedure, interpolation_resolution,
                  coordinate_system, extraction_extent, createxyz,
                  depth_range):
        try:
            i = depth_range

            status = "Started"

            arcpy.env.extent = extraction_extent

            arcpy.AddMessage("Processing depth: " + str(int(i)))

            out_temp_layer = variable_name[0:4] + str(int(i)) + ".shp"
            dimensionValues = "depth " + str(int(i))

            output_dir_geographic = os.path.join(
                output_directory, "temp", "Geographic",
                variable_name[0:4] + str(int(i)))
            output_geographic = os.path.join(output_dir_geographic,
                                             variable_name[0:4] + str(int(i)))

            output_dir_projected = os.path.join(
                output_directory, "temp", "Projected",
                variable_name[0:4] + str(int(i)))
            output_projected = os.path.join(output_dir_projected,
                                            variable_name[0:4] + str(int(i)))

            if not os.path.exists(
                    os.path.join(output_dir_projected,
                                 "xy_coords.yxz")) or os.path.exists(
                                     os.path.join(output_dir_geographic,
                                                  "xy_coords.yxz")):

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

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

                env.workspace = os.path.join(output_directory, "temp",
                                             variable_name[0:4] + str(int(i)))

                # 1 Extract layer to a temporary feature class
                arcpy.MakeNetCDFFeatureLayer_md(
                    in_netCDF_file=input_woa_netcdf,
                    variable=variable_name,
                    x_variable="lon",
                    y_variable="lat",
                    out_feature_layer=out_temp_layer,
                    row_dimension="lat;lon",
                    z_variable="",
                    m_variable="",
                    dimension_values=dimensionValues,
                    value_selection_method="BY_VALUE")

                # 2 Interpolate to higher resolution and 3 save to output directory
                if interpolation_procedure == "IDW":
                    status = "Interpolating " + str(int(i)) + " using IDW"
                    arcpy.gp.Idw_sa(out_temp_layer, variable_name,
                                    output_geographic,
                                    interpolation_resolution, "2",
                                    "VARIABLE 10", "")
                elif interpolation_procedure == "Spline":
                    status = "Interpolating " + str(int(i)) + " using Spline"
                    arcpy.CopyFeatures_management(
                        out_temp_layer,
                        os.path.join(output_dir_geographic, "out.shp"))
                    arcpy.gp.Spline_sa(out_temp_layer, variable_name,
                                       output_geographic,
                                       interpolation_resolution, "TENSION",
                                       "0.1", "10")
                    arcpy.Delete_management(
                        os.path.join(output_directory, "temp", "Geographic",
                                     variable_name[0:4] + str(int(i)),
                                     "out.shp"))
                elif interpolation_procedure == "Kriging":
                    status = "Interpolating " + str(
                        int(i)) + " using Ordinary Kriging"
                    arcpy.gp.Kriging_sa(
                        out_temp_layer, variable_name, output_geographic,
                        "Spherical " + str(interpolation_resolution),
                        interpolation_resolution, "VARIABLE 10", "")

                elif interpolation_procedure == "None":
                    status = "Making a raster for " + str(int(i))
                    arcpy.MakeNetCDFRasterLayer_md(
                        in_netCDF_file=input_woa_netcdf,
                        variable=variable_name,
                        x_dimension="lon",
                        y_dimension="lat",
                        out_raster_layer=output_geographic,
                        band_dimension="",
                        dimension_values="",
                        value_selection_method="BY_VALUE")

                if len(coordinate_system) > 1:
                    status = "Reprojecting " + variable_name[0:4] + str(
                        int(i)) + "."
                    arcpy.ProjectRaster_management(output_geographic,
                                                   output_projected,
                                                   coordinate_system,
                                                   "NEAREST", "#", "#", "#",
                                                   "#")

                if createxyz == "Only Geographic" or createxyz == "Both":
                    status = "Building geographic xy coords for " + variable_name[
                        0:4] + str(int(i)) + "."
                    raster_to_xyz(output_geographic,
                                  variable_name[0:4] + str(int(i)),
                                  output_dir_geographic, 349000000.0)

                    df = pd.read_csv(os.path.join(
                        output_dir_geographic,
                        variable_name[0:4] + str(int(i)) + ".yxz"),
                                     header=0,
                                     names=["y", "x", "z"],
                                     sep=" ",
                                     dtype={
                                         "y": np.float32,
                                         "x": np.float32,
                                         "z": np.float32
                                     })
                    master = df[["x", "y"]].copy()
                    master.columns = ["x", "y"]
                    master = np.round(master, 4)
                    master.to_pickle(
                        os.path.join(output_dir_geographic, "xy_coords.yxz"))
                    del master, df
                    gc.collect()

                if createxyz == "Only Projected" or createxyz == "Both":
                    status = "Building projected xy coords for " + variable_name[
                        0:4] + str(int(i)) + "."
                    raster_to_xyz(output_projected,
                                  variable_name[0:4] + str(int(i)),
                                  output_dir_projected, 349000000.0)

                    df = pd.read_csv(os.path.join(
                        output_dir_projected,
                        variable_name[0:4] + str(int(i)) + ".yxz"),
                                     header=0,
                                     names=["y", "x", "z"],
                                     sep=" ",
                                     dtype={
                                         "y": np.float32,
                                         "x": np.float32,
                                         "z": np.float32
                                     })
                    master = df[["x", "y"]].copy()
                    master.columns = ["x", "y"]
                    master = np.round(master, 4)
                    master.to_pickle(
                        os.path.join(output_dir_projected, "xy_coords.yxz"))
                    del master, df
                    gc.collect()
            else:
                arcpy.AddMessage("Skipping " + str(int(i)) + ".")
        except:
            arcpy.AddMessage("Failed on " + str(int(i)) + ",at status: " +
                             str(status))
            arcpy.AddMessage(arcpy.GetMessages())
示例#21
0
    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Set log configuration
        logPath = os.path.join(parameters[0].valueAsText, "logs")
        if not os.path.exists(logPath):
            os.makedirs(logPath)
        logFile = os.path.join(logPath, "chloro.log")
        logging.basicConfig(filename=logFile,
                            format='%(asctime)s -- %(message)s',
                            datefmt='%d/%m/%Y %H:%M:%S',
                            level=logging.INFO)
        arcpy.AddMessage(
            "\nApplying available chlorophyll_a values to dark targets...")
        logging.info("Starting applyChloro.py script...")

        arcpy.CheckOutExtension("Spatial")
        logging.info(
            "Check Out Extension: Spatial Analyst extension checked out\n")

        # Define variables from parameters
        working_folder = parameters[0].valueAsText
        chloro_folder = os.path.join(os.path.dirname(working_folder),
                                     "Auxiliary", "Chlorophyll")
        if not os.path.exists(chloro_folder):
            os.makedirs(chloro_folder)
        cell_size = parameters[1].value
        focal_field = "chlor_a_" + str(cell_size) + "x" + str(cell_size)

        # Determine list of yearly GDBs in workspace
        arcpy.env.workspace = working_folder
        gdbList = arcpy.ListWorkspaces("*", "FileGDB")
        arcpy.AddMessage("Workspace contains the following " +
                         str(len(gdbList)) + " GDBs: " + str(gdbList))

        # Iterate through yearly GDBs
        for gdb in gdbList:
            arcpy.AddMessage("\nProcessing " + str(gdb))
            logging.info("Processing '%s' geodatabase\n", gdb)
            gdbDesc = arcpy.Describe(gdb)
            gdbYear = gdbDesc.baseName

            # Determine list of .nc files in corresponding yearly chlorophyll folder
            chloro_year = os.path.join(chloro_folder, gdbYear)
            arcpy.env.workspace = chloro_year
            ncList = arcpy.ListFiles('*.nc')

            # Determine list of feature classes in current GDB
            arcpy.env.workspace = gdb
            fcList = arcpy.ListFeatureClasses()
            arcpy.AddMessage("\nGDB contains the following " +
                             str(len(fcList)) + " feature classes: " +
                             str(fcList))

            # Iterate through feature classes in GDB
            for fc in fcList:

                # Check if chlorophyll_a has already been added to current feature class
                arcpy.AddMessage("\nVerifying " + fc + "...")
                logging.info("Processing '%s' feature class", fc)
                fldList = arcpy.ListFields(fc)
                fldNames = []
                for fld in fldList:
                    fldNames.append(fld.name)

                # If no chlorophyll_a data already in feature class, proceed with applying values
                if not focal_field in fldNames:

                    # Create points feature class from current feature class to extract chlorophyll raster values
                    arcpy.AddMessage("Creating points feature class...")
                    targetLyr = "targetLyr"
                    arcpy.MakeFeatureLayer_management(fc, targetLyr)
                    logging.info(
                        "Make Feature Layer: '%s' layer created from '%s' feature class",
                        targetLyr, fc)
                    pointFC = fc + "_point"
                    arcpy.FeatureToPoint_management(targetLyr, pointFC,
                                                    "CENTROID")
                    logging.info(
                        "Feature To Point: '%s' points feature class created from centroid of features in '%s' layer",
                        pointFC, targetLyr)

                    # Determine year and day of year to load appropriate .nc file as raster
                    yDay = self.yearDay(fc.split("_")[1])
                    chloro_file = "A" + gdbYear + yDay

                    # Iterate through list of year's .nc files to find corresponding file to current feature class
                    for ncFile in ncList:

                        # Check for .nc file and feature class match
                        if ncFile.startswith(chloro_file):

                            # Make NetCDF raster layer from .nc file
                            arcpy.AddMessage(
                                "Preparing chlorophyll_a raster layer...")
                            ncFilePath = os.path.join(chloro_year, ncFile)
                            arcpy.MakeNetCDFRasterLayer_md(
                                ncFilePath, "chlor_a", "lon", "lat",
                                chloro_file)
                            logging.info(
                                "Make NetCDF Raster Layer: '%s' raster layer created from '%s'",
                                chloro_file, ncFilePath)

                            # Apply extent to raster layer (to limit processing to pertinent region)
                            chloro_extent = arcpy.Extent(
                                -160.0, 40.0, -40.0, 89.989002)
                            chloro_rectExtract = arcpy.sa.ExtractByRectangle(
                                chloro_file, chloro_extent, "INSIDE")
                            logging.info(
                                "Extract By Rectangle: Extent (-160 (W), 40 (S), -40 (E), 89.989002 (N)) applied to '%s'",
                                chloro_file)

                            # Calculate focal statistics (mean value of focal window)
                            arcpy.AddMessage("Calculating focal statistics...")
                            neighborhood = arcpy.sa.NbrRectangle(
                                cell_size, cell_size, "CELL")
                            chloro_focal = arcpy.sa.FocalStatistics(
                                chloro_rectExtract, neighborhood, "MEAN",
                                "DATA")
                            logging.info(
                                "Focal Statistics: '%s' raster created by calculating mean value of '%s'x'%s' neighbourhood calculated for cells from '%s'",
                                chloro_focal, str(cell_size), str(cell_size),
                                chloro_file)

                            if not "chlor_a" in fldNames:
                                # Extract point values from raster
                                arcpy.AddMessage(
                                    "Extracting raster chlorophyll_a values to points..."
                                )
                                extractFC = fc + "_extract"
                                arcpy.sa.ExtractValuesToPoints(
                                    pointFC, chloro_rectExtract, extractFC)
                                arcpy.AlterField_management(
                                    extractFC, "RASTERVALU", "chlor_a")
                                logging.info(
                                    "Extract Values to Points: '%s' feature class created with point values calculated from '%s' raster layer with '%s' feature class",
                                    extractFC, chloro_file, pointFC)

                            # Extract focal values from raster
                            arcpy.AddMessage(
                                "Extracting raster chlorophyll_a mean values to points..."
                            )
                            finalExtractFC = fc + "_final_extract"
                            arcpy.sa.ExtractValuesToPoints(
                                extractFC, chloro_focal, finalExtractFC)
                            ##                            focal_field = "chlor_a_" + str(cell_size) + "x" + str(cell_size)
                            arcpy.AlterField_management(
                                finalExtractFC, "RASTERVALU", focal_field)
                            logging.info(
                                "Extract Values to Points: '%s' feature class created with point values calculated from '%s' raster layer with '%s' feature class",
                                finalExtractFC, chloro_focal, extractFC)

                            # Join point and focal values to feature class
                            arcpy.AddMessage(
                                "Joining values to feature class...")
                            self.join_field(fc, "OBJECTID", finalExtractFC,
                                            "ORIG_FID",
                                            "chlor_a;" + focal_field)
                            logging.info(
                                "Join Field: chlor_a and chlor_a focal values joined to '%s' feature class from '%s' table",
                                fc, finalExtractFC)

                            # Break iteration through .nc files once processing with corresponding .nc file and feature class is complete
                            break

                # If chlorophyll_a values found in feature class, no further processing required for current feature class
                else:
                    arcpy.AddMessage(
                        "Chlorophyll_a values already applied to feature class. Continuing..."
                    )
                    logging.info("Values already applied")

                logging.info("Processing for '%s' feature class complete\n",
                             fc)

            # Delete extra feature classes used during geoprocessing
            self.cleanWorkspace(gdb)

        arcpy.CheckInExtension("Spatial")
        logging.info(
            "Check In Extension: Spatial Analyst extension checked back in")
        logging.info("applyChloro.py script finished\n\n")

        return
 #name2=name2[0].split('-')
 #outfile2=name2[0]+'_'+name2[1]+'_'+name2[2]+'_18'
 inNetCDFFile = 'G:/NCEP/v_windspeed/vwnd.10m.gauss.2015.nc'
 variable = 'vwnd'
 XDimension = "lon"
 YDimension = "lat"
 outRasterLayer_1 = 'vwnd_%s' % outfile1
 #outRasterLayer_2 = 'pres_%s'%outfile2
 bandDimmension = ''
 date_1 = ['time', date1]
 #date_2=['time',date2]
 dimensionValues_1 = [date_1]
 #dimensionValues_2=[date_2]
 valueSelectionMethod = 'BY_VALUE'
 arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension,
                                YDimension, outRasterLayer_1,
                                bandDimmension, dimensionValues_1,
                                valueSelectionMethod)
 #arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable,XDimension, YDimension,outRasterLayer_2, bandDimmension, dimensionValues_2,valueSelectionMethod)
 #提取点
 inPointFeatures = "78_BJ_TJ_HB_PM2.5.shp"
 inRaster_1 = outRasterLayer_1
 #inRaster_2= outRasterLayer_2
 outPointFeatures_1 = "G:/NCEP_V2.0/2015vwnd.gdb/%s" % outRasterLayer_1
 #outPointFeatures_2="G:/NCEP/work_2017_pres.gdb/%s"%outRasterLayer_2
 # Check out the ArcGIS Spatial Analyst extension license
 arcpy.CheckOutExtension("Spatial")
 # Execute ExtractValuesToPoints
 ExtractValuesToPoints(inPointFeatures, inRaster_1, outPointFeatures_1,
                       "INTERPOLATE", "VALUE_ONLY")
 #ExtractValuesToPoints(inPointFeatures, inRaster_2, outPointFeatures_2,"INTERPOLATE", "VALUE_ONLY")
 arcpy.Delete_management(outRasterLayer_1)
示例#23
0
dimensionValues = ""
valueSelectionMethod = ""

# Finds netCDF files in a folder
for root, dirs, files in os.walk(arcpy.env.workspace):
    for name in files:
        if ".nc" in name:
            # Removes the RGB version of ACOLITE output
            if "RGB" not in name:
                netCDF = (os.path.join(root, name))
                netCDFs.append(name)

            for net in netCDFs:
                print net
                for var in variableList:
                    # Split the layer name from the extension
                    lyrName = (os.path.join(root, net))[:-3] + ".lyr"

                    # Split the raster from extension
                    RasterLayer = ((netCDF)[:-3] + "_" + var + ".tif")
                    # Make the netCDF Raster
                    arcpy.MakeNetCDFRasterLayer_md(netCDF, var, XDimension,
                                                   YDimension, lyrName,
                                                   bandDimmension,
                                                   dimensionValues,
                                                   valueSelectionMethod)
                    # Copy the Raster Layer to a Raster file
                    arcpy.CopyRaster_management(lyrName, RasterLayer,
                                                "DEFAULTS", "", "", "", "",
                                                "32_BIT_FLOAT")
    '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997',
    '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006',
    '2007', '2008', '2009', '2010', '2011', '2012', '2013', '2014'
]

for year in years:
    folder_Input = "Y:\\ERA_Interim_35yr_data\\era_data\\erai_runoff_1980to2014.tar.gz\\{0}_erai_runoff".format(
        year)
    for month in months:
        for day in days:

            # NetCDF Raster layer
            fileInput = folder_Input + "\\" + year + month + day + "_erai_runoff.grib.nc"
            fileOutput = year + "-" + month + "-" + day
            tifOutput = folder_Output + "\\" + year + "-" + month + "-" + day + ".tif"
            arcpy.MakeNetCDFRasterLayer_md(fileInput, "RO", "lon", "lat",
                                           fileOutput, "time", "#", "BY_VALUE")
            # Export to GeoTiFF
            arcpy.CopyRaster_management(fileOutput, tifOutput, "#", "#",
                                        "-9.999900e+003", "NONE", "NONE", "#",
                                        "NONE", "NONE")
            # Define Projection
            # arcpy.DefineProjection_management(tifOutput, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
            # borrar map aux
            arcpy.Delete_management(year + "-" + month + "-" + day)
            # cerrar raster
            # arcpy.Delete_management(tifOutput)

            FECHA = year + "-" + month + "-" + day
            print(FECHA)

# Finaliza
示例#25
0
# at http://www.engr.scu.edu/~emaurer/gridded_obs/index_gridded_obs.htm

import netCDF4 as nc
import numpy as np
import os
from netCDF4 import *
import glob
import gc

pr1949_0101 = 'water_Scarcity\Precipitation\pr1949'
unzipped_nc = 'water_Scarcity\Precipitation\unzipped_data\\'

#Convert January 1st 1949  from NetCDF to raster layer to get extent and coordinate system
arcpy.MakeNetCDFRasterLayer_md(
    in_netCDF_file='nldas_met_update.obs.daily.pr.1949.nc',
    variable='pr',
    x_dimension='longitude',
    y_dimension='latitude',
    out_raster_layer=pr1949_0101)
arcpy.env.outputCoordinateSystem = pr1949_0101
myRaster = arcpy.Raster(pr1949_0101)
mx = myRaster.extent.XMin
my = myRaster.extent.YMin

#Read multifile dataset, read out structure and dimensions
nc1949 = unzipped_nc + 'nldas_met_update.obs.daily.pr.1949.nc'
f1949 = nc.Dataset(nc1949)
# print variables
f1949.variables.keys()
aprec1949 = f1949.variables['pr']
print aprec1949
#Try summing across days to get annual rainfall in each pixel
示例#26
0
########################################################################################################################################
#Set working directory for ArcPy
arcpy.env.workspace = wd
del wd

#Activate Spatial Analyst extension
arcpy.CheckOutExtension("Spatial")

#Allowing outputs to be overwritten
arcpy.env.overwriteOutput = True

#Upload only layers of interest using Arcpy
for i in range(0, len(dates)):
    arcpy.MakeNetCDFRasterLayer_md("erdMBsstdmday_LonPM180_b102_bcb5_c347.nc",
                                   "sst", "longitude", "latitude", indT[i], "",
                                   dates[i])

#Create a composite raster with all bands previously identified
SST = arcpy.CompositeBands_management(indT, "SST_compMOI.TIF")

#Names of shapefiles containing limits for Machalilla and GSF Marine Reserves
AOIs = [r"LimitesRMCM.shp", r"LimitesRMGSF.shp"]
#Names for output files in the same order as shapefiles
outname = ["RMCM", "RMGSF"]


def ExtractVals(nc, aoi, out):
    i = 0
    for j in aoi:
        #Mask out raster with Marine Reserve limits and save it
from arcpy import env
#The folder_path is the name and location of the folder containing the NetCDF files, and where the rasters will be created.
Input_folder = arcpy.GetParameterAsText(0)
arcpy.env.workspace = Input_folder

#This is the variable to display from the NetCDF file. To assign the default value, leave as NetCDF_Var= "".
Variable = arcpy.GetParameterAsText(1)

#This is the X dimension from the NetCDF file. To assign the default value, leave as NetCDF_X= "".
X_Value = arcpy.GetParameterAsText(2)

#This is the Y dimension from the NetCDF file. To assign the default value, leave as NetCDF_Y= "".
Y_Value = arcpy.GetParameterAsText(3)

#This is the name and location where the rasters will be generated.
Output_Location = arcpy.GetParameterAsText(4)

#This for loop will search through the folder designated by folder_path and if a file ends in ".nc", then this script will convert it to a raster.
for NetCDFs in os.listdir(Input_folder):
    if NetCDFs[-3:] == ".nc":
        NetCDF_Name = NetCDFs[5:13]
        print NetCDF_Name
        print Output_Location + "/" + NetCDF_Name
        arcpy.MakeNetCDFRasterLayer_md(NetCDFs, Variable, X_Value, Y_Value,
                                       NetCDF_Name)
        Raster_Name = "x" + str(NetCDF_Name)
        arcpy.CopyRaster_management(NetCDF_Name,
                                    Output_Location + "/" + Raster_Name)

arcpy.AddMessage("The rasters have been successfully generated.")
示例#28
0
                    stdvar = var
                    vartype = "variable name"
            if stdvar.strip() == "":
                stdvar = var
                vartype = "variable name"
            if ncdfVar == stdvar:
                arcpy.AddMessage("...matched with " + vartype)
                fileVar = var
                break
    if fileVar != None:
        # We've found a weather variable of interest to us within the NetCDF file, so...
        # now call Make NetCDF Raster Layer, passing lat,lon and the var, and append it to a list of rasters that will form our raster bands
        arcpy.AddMessage("Making NetCDF Raster Layer with " + inNetCDF + ", " +
                         var + ", " + longVar + ", " + latVar + ", " + maowVar)
        rasterBands.append(
            arcpy.MakeNetCDFRasterLayer_md(inNetCDF, var, longVar, latVar,
                                           maowVar))
        # keep track of the weather variables that are available for generating derivatives
        wim.WeatherVarAvailableForDerivatives(maowVar)
##    else:
##        if nullRaster == None:
##            # create a null raster to fill the gap
##            nullRaster = wim.CreateEmptyRaster()
##        suffix = ""
##        if ncdfVar != None:
##            suffix = " for " + ncdfVar
##        if maowVar != None:
##            if suffix == "":
##                suffix = " for"
##            suffix += " (" + maowVar + ")"
##        arcpy.AddMessage("Setting empty raster" + suffix)
##        rasterBands.append(nullRaster)
s_avg_tif = "H:\\ESM267\\wind\\out\\s_avg.tif"
s_countries = "H:\\ESM267\\wind\\wind.gdb\\s_countries"

############################################
# generate s layers, globally
############################################

# generate s layers from u and v vectors
for j in range(0, 365, 30):
    u = "u_%03d" % j
    v = "v_%03d" % j
    s = "in_memory\\s_%03d" % j
    s_tif = "h:\\ESM267\\wind\\out\\s_%03d.tif" % j

    # make raster layers from u and v
    arcpy.MakeNetCDFRasterLayer_md(u_nc, "uwnd", "lon", "lat", u, "",
                                   "time %d" % j, "BY_INDEX")
    arcpy.MakeNetCDFRasterLayer_md(v_nc, "vwnd", "lon", "lat", v, "",
                                   "time %d" % j, "BY_INDEX")

    # calculate wind vector strength
    arcpy.gp.RasterCalculator_sa(
        "SquareRoot( Square('%s') + Square('%s') )" % (u, v), s)

    # resampling
    arcpy.Resample_management(s, s_tif, "0.25 0.25", "BILINEAR")

############################################
# summarize by country
############################################

# copy country features
示例#30
0
def Process_TRMM(Package, Basin, Year, zoneField):

    # assembling the list of file addresses
    TRMM_path = Package + '/Datos/NASA_Datos/TRMM/' + Year
    inZoneData = Package + '/Datos/Cuencas/' + Basin + '/Parametros/Shapefile/HighShape.shp'
    OutputTRMM = Package + '/Datos/Cuencas/' + Basin + '/Datos_Intermedia/TRMM/TRMM_Precip' + Year + '.dbf'
    OutputTRMMpath = Package + '/Datos/Cuencas/' + Basin + '/Datos_Intermedia/TRMM'

    #import modules and manage temporary folders
    import arcpy, arcgisscripting, sys, os, csv, string, shutil
    from arcpy import env
    from arcpy.sa import *
    from dbfpy import dbf

    arcpy.env.overwriteOutput = True

    IntermediateOutput = TRMM_path + '/temp'

    # make sure the TRMM output path is there
    if not os.path.exists(OutputTRMMpath):
        os.makedirs(OutputTRMMpath)

    # If a temp data folder already exists, delete its contents and recreate it.
    if os.path.exists(IntermediateOutput):
        shutil.rmtree(IntermediateOutput)
        os.makedirs(IntermediateOutput)
    else:

        # If no temporary folder exists, NetCDFs are converted to tiffs for the first time.
        os.makedirs(IntermediateOutput)
        arcpy.env.workspace = TRMM_path
        arcpy.env.overwriteOutput = True

        NCfiles = arcpy.ListFiles("*.nc")

        for filename in NCfiles:
            print 'Process_TRMM: Converting netCDF file ' + filename + ' to Raster'
            inNCfiles = arcpy.env.workspace + "/" + filename
            fileroot = filename[0:(len(filename) - 3)]
            outRasterLayer = TRMM_path + "/" + fileroot
            arcpy.MakeNetCDFRasterLayer_md(inNCfiles, "r", "longitude",
                                           "latitude", "r", "", "", "BY_VALUE")
            arcpy.CopyRaster_management("r", outRasterLayer + ".tif", "", "",
                                        "", "NONE", "NONE", "")

        print 'Process_TRMM: Finished creating TIFs!'

    # Execute zonal statistics functions on selected basin and store output as a dbf.
    arcpy.env.workspace = TRMM_path
    TIFfiles = arcpy.ListFiles("*.tif")

    try:
        for filename in TIFfiles:
            fileroot = filename[0:(len(filename) - 4)]
            print "Process_TRMM: Calculating zonal statistics on " + filename

            inValueRaster = arcpy.env.workspace + "/" + filename
            outTable = IntermediateOutput + "/" + fileroot + '.dbf'
            arcpy.CheckOutExtension("Spatial")
            outZstat = ZonalStatisticsAsTable(inZoneData, zoneField,
                                              inValueRaster, outTable,
                                              "NODATA", "ALL")

        print 'Process_TRMM: Finished calculating zonal statistics!'

    except:
        print 'Error: Process_TRMM: Error encountered while calculating zonal statistics!'
        print 'Ensure that shapefile HighShape.shp is in the WSGS 1984 geographic projection'
        print 'And is stored in the correct directory under basin parameters.'

    # Create csvs from DBFs
    arcpy.env.workspace = IntermediateOutput
    DBFfiles = arcpy.ListFiles("*.dbf")

    try:
        for filename in DBFfiles:
            fileroot = filename[0:(len(filename) - 4)]

            # replace the '.' with '_', because the merge function is apparently very finicky with inputs.
            csv_fn = IntermediateOutput + '/' + string.replace(
                fileroot, '.', '_') + '.csv'

            inDBFfiles = arcpy.env.workspace + '/' + filename
            with open(csv_fn, 'wb') as csvfile:
                in_db = dbf.Dbf(inDBFfiles)
                out_csv = csv.writer(csvfile)
                names = []
                for field in in_db.header.fields:
                    names.append(field.name)
                out_csv.writerow(names)
                for rec in in_db:
                    out_csv.writerow(rec.fieldData)
                in_db.close()

        print 'Process_TRMM: Finished conversion to CSVs!'
    except:
        print 'Error: Process_TRMM: Error encountered while creating CSVs from dbfs!'

    # Merge CSVs together and print a bunch of progress items.
    arcpy.env.workspace = IntermediateOutput
    CSVfiles = arcpy.ListFiles("*.csv")

    print 'Process_TRMM: Creating Output file at ' + OutputTRMM
    print 'Process_TRMM: This may take up to 15 minutes'
    arcpy.env.workspace = IntermediateOutput
    CSVfiles = arcpy.ListFiles("*.csv")
    arcpy.Merge_management(CSVfiles, OutputTRMM)

    print 'Process_TRMM: Output file created for year ' + Year