def extractRaster(rasterList): b1_List, b2_List, b3_List, b6_List = [], [], [], [] modisList = [] Rnum = 0 if not os.path.exists(raster_path): os.makedirs(raster_path) for in_raster in rasterList: raster_name_b1 = in_raster[8:16] + '_b1' + '.tif' raster_name_b2 = in_raster[8:16] + '_b2' + '.tif' raster_name_b3 = in_raster[8:16] + '_b3' + '.tif' raster_name_b6 = in_raster[8:16] + '_b6' + '.tif' out_raster_b1 = raster_path + '\\' + raster_name_b1 out_raster_b2 = raster_path + '\\' + raster_name_b2 out_raster_b3 = raster_path + '\\' + raster_name_b3 out_raster_b6 = raster_path + '\\' + raster_name_b6 arcpy.ExtractSubDataset_management(in_raster, out_raster_b1, 0) arcpy.ExtractSubDataset_management(in_raster, out_raster_b2, 1) arcpy.ExtractSubDataset_management(in_raster, out_raster_b3, 2) arcpy.ExtractSubDataset_management(in_raster, out_raster_b6, 5) b1_List.append(raster_name_b1) b2_List.append(raster_name_b2) b3_List.append(raster_name_b3) b6_List.append(raster_name_b6) Rnum += 1 print 'MODIS: ' + raster_name_b1, raster_name_b2, raster_name_b3, raster_name_b6 modisList.append(b1_List) modisList.append(b2_List) modisList.append(b3_List) modisList.append(b6_List) print 'Numbers: ', Rnum print '-' * 50 return modisList
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", "#", "#") # "中心"
def extractHDFToTif(num): HDFfiles = os.listdir(path + "Middle\\HDFs\\") file = path + "Middle\\HDFs\\" + str(HDFfiles[num]) arcpy.ExtractSubDataset_management( file, path + "Middle\\Rasters\\" + str(HDFfiles[num]).rstrip(".HDF") + ".tif", 0) pass
def mod13a3Process(folder, index, outputFolder, product): for filename in os.listdir(folder): if filename.endswith(".hdf"): print("processing "+filename) arcpy.env.workspace = outputFolder arcpy.CheckOutExtension("spatial") inputRaster = os.path.join(folder,filename) outputEVI = '{0}.{1}.tif'.format(filename, product) if arcpy.Exists(os.path.join(outputFolder, outputEVI)): print(outputEVI + " exists") else: EVIfile = arcpy.ExtractSubDataset_management(inputRaster,outputEVI,index) arcpy.CheckInExtension("spatial") else: continue
def rastersPreprocessing(dates_dictionary, directory): for keys, values in dates_dictionary.items(): arrays_withnames = [arrays for arrays in values[1]] for hdf_file in arrays_withnames: for folderPath, folderNames, fileNames in os.walk(directory): for modis_images in fileNames: if modis_images == hdf_file: workspace = env.workspace = folderPath raster_modis_images = arcpy.Raster(modis_images) try: # Extract ndvi layer from hdf multiband ndvi_name = modis_images.split( ".hdf")[0] + "_ndvi.tif" ndvi_path = os.path.join(workspace, ndvi_name) ndvi_image = arcpy.ExtractSubDataset_management( raster_modis_images, ndvi_path, "0") print("ndvi_image {}".format(ndvi_image)) # project ndvi images prj_name = modis_images.split( ".hdf")[0] + "_prj.tif" prj_path = os.path.join(workspace, prj_name) prj_ndvi = arcpy.ProjectRaster_management( ndvi_image, prj_path, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]", "NEAREST", "5.59954171587093E-03 5.59954171587093E-03", "", "", "PROJCS['Unknown_datum_based_upon_the_custom_spheroid_Sinusoidal',GEOGCS['GCS_Unknown_datum_based_upon_the_custom_spheroid',DATUM['D_Not_specified_based_on_custom_spheroid',SPHEROID['Custom_spheroid',6371007.181,0.0]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Sinusoidal'],PARAMETER['false_easting',0.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',0.0],UNIT['Meter',1.0]]", "NO_VERTICAL") print("prj_ndvi {}".format(prj_ndvi)) except arcpy.ExecuteError: print(arcpy.GetMessages()) continue
def lst_processing(mod11c3_file): lst_day_name = '{0}.day.tif'.format(mod11c3_file) lst_day = arcpy.ExtractSubDataset_management(mod11c3_file,outputEVI,0) lst_night = 2
inPath = "R:\\NorgeIsModelling\\Indice\\MODIS\\Monthly_actEva\\Ori\\" outPath = "R:\\NorgeIsModelling\\Indice\\MODIS\\Monthly_actEva\\Processed\\" Database = "R:\\NorgeIsModelling\\Data\\IsModelling.gdb" arcpy.env.scratchWorkspace = Database for iYear in range(2001, 2015): for iMon in range(1, 13): MonStr = "%02d" % (iMon) env.workspace = inPath + str(iYear) + "\\" + MonStr + "\\" hdfList = arcpy.ListRasters('*', 'hdf') for hdf in hdfList: eviName = hdf[0:41] #print("Subsetting EVI band from ....."+str(hdf)) tifFile = outPath + str( iYear) + "\\" + MonStr + "\\" + eviName + ".tif" #print(tifFile) data1 = arcpy.ExtractSubDataset_management(hdf, tifFile, "0") SetNullRaster = SetNull(tifFile, tifFile, "VALUE > 32760") NullOut = outPath + str( iYear) + "\\" + MonStr + "\\" + eviName + "SetNull" + ".tif" SetNullRaster.save(NullOut) InRaster = glob.glob(outPath + str(iYear) + "\\" + MonStr + "\\" + "*SetNull.tif") #print(InRaster) arcpy.MosaicToNewRaster_management(input_rasters = InRaster, output_location = outPath + str(iYear) + "\\" + MonStr + "\\", raster_dataset_name_with_extension="merge.tif",\ coordinate_system_for_the_raster="", pixel_type="16_BIT_UNSIGNED", cellsize="", number_of_bands="1", mosaic_method="MEAN", mosaic_colormap_mode="FIRST") print("Done" + str(iYear) + str(iMon)) print "Done"
rvals = [[0, 3], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 1], [7, 1], [8, 1], [9, 1], [10, 1], [11, 3], [12, 2], [13, 4], [14, 2], [15, 5], [16, 5]] remap = arcpy.sa.RemapRange(rvals) arcpy.env.workspace = inpath flist = arcpy.ListFiles('*.hdf') nfiles = len(flist) cnt = 0 for i in flist: cnt += 1 scene = i[:24] outfile = outpath + scene + 'LCReclass.TIF' extract = arcpy.ExtractSubDataset_management(i, subdataset_index='0') reclass = arcpy.sa.Reclassify(extract, 'VALUE', remap, "NODATA") reclass.save(outfile) del extract del reclass print "Done processing file {0} of {1}".format(cnt, nfiles)
# Description: Extract and Mask NDVI from HDF based on AOI # Purpose: Automated prediction system for vegetation cover based on MODIS- NDVI satellite data and neural networks # Author: Sohaib K. M. Abujayyab # Created: 11/02/2019 # Requirements: None # ------------------------------------------------------------------------------- # Import system modules import arcpy import os arcpy.env.overwriteOutput = True arcpy.env.workspace = arcpy.GetParameterAsText(0) rasterList = arcpy.ListRasters() inMaskData = arcpy.GetParameterAsText(1) OutputFolder = arcpy.GetParameterAsText(2) #Extract NDVI from HDF for raster in rasterList: arcpy.AddMessage(str(raster)) OutputFolderpath = os.path.abspath(OutputFolder) OutputFolderpath2 = os.path.join(OutputFolderpath, '') OutputTif = str(OutputFolderpath2) + str(raster[9:-29]) arcpy.AddMessage(str(OutputTif)) Output=arcpy.ExtractSubDataset_management(raster, "in_memory/Output", "0") # Execute ExtractByMask arcpy.gp.ExtractByMask_sa("in_memory/Output", inMaskData, OutputTif+ ".tif") del Output
import gc gc.disable() from arcpy import env from arcpy.sa import * arcpy.CheckOutExtension("Spatial") #Turns on Spatial Analyst Extension env.workspace = arcpy.GetParameterAsText(0) # TO BE PARAMETER env.overwriteOutput = True #If TRUE: when ran deletes data if it already exists. If FALSE: Does not delte data that already exists. study_area = arcpy.GetParameterAsText( 1) # MUST BE IN GEODATABASE # TO BE PARAMETER ## HDF > RASTER TIFF lst = arcpy.ListFiles("*.hdf") mosaiclist = [] for filez in lst: output_rast = filez[0:-4] + "_raster" + ".tif" extracted = arcpy.ExtractSubDataset_management(filez, output_rast, "0") OutExtractByMask = ExtractByMask(extracted, study_area) OutExtractByMask.save((filez[0:-4]) + "_sa.tif") ## MOSAIC RASTERS TOGETHER for dirname, dirnames, filenames in os.walk(env.workspace): for subdirname in dirnames: env.workspace = os.path.join(dirname, subdirname) mosaiclist = arcpy.ListRasters("*sa.tif") M_Out = env.workspace i = 0 for raster in mosaiclist: while i < len(lst): currentPattern = lst[i]
arcpy.env.workspace = working_dir #Make list of hdf files for that day wildcard = "*A" + yd + "*" hdfList = arcpy.ListRasters(wildcard, 'HDF') #Test if there are HDF files for that day if not (os.path.isfile(output_dir + "\\" + yd + 'MOD.bands.proj.tif')): print hdfList if len(hdfList) <> 0: #Loop through HDF files for hdf in hdfList: tifname = hdf[8:23] + ".tif" print "Extracting bands from..." + str(hdf) data1 = arcpy.ExtractSubDataset_management( hdf, output_dir + r'\bands.' + tifname, [11, 13, 14]) data1 = arcpy.ExtractSubDataset_management( hdf, output_dir + r'\state.' + tifname, [1]) #Make a list of all the new tifs arcpy.env.workspace = output_dir tiflist = arcpy.ListRasters('bands.*', 'TIF') tiflist_state = arcpy.ListRasters('state.*', 'TIF') #Mosaic tifs together print "Combining MODIS tiles..." arcpy.MosaicToNewRaster_management(tiflist,output_dir, yd + 'bands.tif', \ "PROJCS['World_Sinusoidal',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Sinusoidal'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],UNIT['Meter',1.0]]", '16_BIT_SIGNED', \ number_of_bands="3") arcpy.MosaicToNewRaster_management(tiflist_state,output_dir, yd + 'state.tif', \ "PROJCS['World_Sinusoidal',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Sinusoidal'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],UNIT['Meter',1.0]]", '16_BIT_SIGNED', \
for i in (0, ): # Set the current workspace (where the hdf files are located) env.workspace = "f:\\wtmp\hdf\\" # Get a list of HDF files from the workspace using #an internal GIS function "listRasters" rasterList = arcpy.ListRasters("*", "hdf") # Extract rasters from HDF files in the workspace # Change "0" to the hdf layer needed for raster in rasterList: output = "f:\\wtmp\\hdf\\output\\layer" + str(i) + "\\" + str( raster)[:-4] + ".tif" layer = str(i) arcpy.ExtractSubDataset_management(raster, output, layer) print "Layer" + str(i) + ": 1. Extractining rasters - Finished" ## 2. Mosaic each raster to one large dataset # glob (from iglob lib) is used to grab part of the filename (string). here we take the date part below from glob import iglob #set where the tif files are located env.workspace = "f:\\wtmp\\hdf\\output\\layer" + str(i) # Get a list of dates from file names DateList = list() #creates an empty list for date rasterList = arcpy.ListRasters("*", "tif") #list of all tif raster files for raster in rasterList: date = raster[1:8] DateList.append(date)
def extract_from_hdf(filelist, layerlist, layernames=False, outdir=None): """ Extracts tifs from MODIS extract_HDF_layer files, ensures proper projection. inputs: filelist list of '.hdf' files from which data should be extracted (or a directory) layerlist list of layer numbers to pull out as individual tifs should be integers such as [0,4] for the 0th and 4th layer respectively. layernames list of layer names to put more descriptive file suffixes to each layer outdir directory to which tif files should be saved if outdir is left as 'False', files are saved in the same directory as the input file was found. """ if outdir is not None: if not os.path.exists(outdir): os.makedirs(outdir) # enforce lists for iteration purposes and sanitize inputs filelist = core.enf_filelist(filelist) for filename in filelist: if '.xml' in filename or not '.hdf' in filename: filelist.remove(filename) layerlist = core.enf_list(layerlist) layernames = core.enf_list(layernames) # ignore user input layernames if they are invalid, but print warnings if layernames and not len(layernames) == len(layerlist): Warning('Layernames must be the same length as layerlist!') Warning('Ommiting user defined layernames!') layernames = False # create empty list to add failed file names into failed = [] # iterate through every file in the input filelist for infile in filelist: # pull the filename and path apart path, name = os.path.split(infile) arcpy.env.workspace = path for i, layer in enumerate(layerlist): # specify the layer names. if layernames: layername = layernames[i] else: layername = str(layer).zfill(3) # use the input output directory if the user input one, otherwise build one if outdir: outname = os.path.join( outdir, "{0}_{1}.tif".format(name[:-4], layername)) else: outname = os.path.join( path, "{0}_{1}.tif".format(name[:-4], layername)) # perform the extracting and projection definition try: # extract the subdataset arcpy.ExtractSubDataset_management(infile, outname, str(layer)) # define the projection as the MODIS Sinusoidal define_projection(outname) print("Extracted {0}".format(os.path.basename(outname))) except: print("Failed to extract {0} from {1}".format( os.path.basename(outname), os.path.basename(infile))) failed.append(infile) print("Finished extracting all hdfs! \n") return failed
print() # For Loop to go through all the files and extract Bands for hdf in hdf_list: # Extract Band Numbers (Variables of HDFs) MODIS_Product = os.path.basename(hdf).split(".")[0] A = os.path.basename(hdf).split(".")[1] modis_grid_code = os.path.basename(hdf).split(".")[2] version = os.path.basename(hdf).split(".")[3] B = os.path.basename(hdf).split(".")[4] name = MODIS_Product + "_" + A + "_" + modis_grid_code + "_" + version + ".tif" print() print("\t - Extracting NDVI from... " + MODIS_Product + " " + B ) arcpy.ExtractSubDataset_management( hdf, outws + name , "0") print("\t\t - Successful") print() print("HDF DATA EXTRACTING SUCCESSFUL...") print() # Rasters to same day mosaics print("Commencing Stiching of MOODIS tiles...") print() extracted_NDVI = r'E:\NDVI\EXTRACTED_NDVI\\' outws1 = r'E:\NDVI\MODIS_stiched\\'
import arcpy arcpy.CheckOutExtension("spatial") arcpy.gp.overwriteOutput=1 arcpy.env.workspace = "G:\\migration\\landuse\\2001" raster_sets = arcpy.ListRasters("*", "hdf") #ref= "G:\\pheasant\\1.maxent\\2.rasters\\LIM\\TIF\\dem1.tif" #spref=arcpy.Describe(ref).SpatialReference for raster_set in raster_sets: print(raster_set) out= "G:\\migration\\landuse\\2001\\"+raster_set+'.tif' #arcpy.ProjectRaster_management(raster, out, ref) arcpy.ExtractSubDataset_management (raster_set, out , '1') print(raster_set+" has done") print("All done")
Output_LocationGrid = outPathscrap + os.sep + str(jdate) + 'NDVIgrids' if not os.path.exists(Output_LocationGrid): os.makedirs(Output_LocationGrid) env.workspace = outHdf InputHDFs = arcpy.ListRasters() list = [] i = 10 HDFnum = '0' for InputHDF in InputHDFs: OutputTIF = Output_LocationGrid + os.sep + str(jdate) + str( i) # gridfile outHdf + "\\" + print(OutputTIF) # Process: Extract Subdataset... arcpy.ExtractSubDataset_management(InputHDF, OutputTIF, HDFnum) list.append(OutputTIF) i = i + 1 NDVI = str(year) + str(jdate) + "." + str(product) print(NDVI) Coordsystem = r'C:\WaterSmart\Users\Olena\Projects\ArcGIS\WGS 1984.prj' arcpy.CreateFileGDB_management(outPathscrap, "lst.gdb", "CURRENT") arcpy.CreateMosaicDataset_management(outPathscrap + os.sep + "lst.gdb", "lst2", Coordsystem, "1", "16_BIT_SIGNED", "NONE", "") mosimg = arcpy.AddRastersToMosaicDataset_management( outPathscrap + os.sep + "lst.gdb" + os.sep + "lst2", "Raster Dataset", Output_LocationGrid, "UPDATE_CELL_SIZES", "UPDATE_BOUNDARY", "NO_OVERVIEWS", "#", "0", "1500", "#", "#", "SUBFOLDERS", "OVERWRITE_DUPLICATES", "BUILD_PYRAMIDS",
import arcpy import warnings warnings.filterwarnings("ignore") from arcpy.sa import * arcpy.CheckOutExtension("Spatial") arcpy.env.workspace=ras_path #arcpy.env.snapRaster = samp for yd in date: extrac_tif_name=[] extrac_tif='' for hdf_f in hdfs: if hdf_f.startswith(hdf_f[0:tps]+yd): orgras=tmp_path+'\\'+hdf_f[0:len(hdf_f)-3]+"tif" arcpy.ExtractSubDataset_management(hdf_f,orgras,str(band)) stnras = tmp_path+'\\'+hdf_f[0:len(hdf_f)-4]+"_stn.tif" arcpy.gp.RasterCalculator_sa("SetNull(('"+orgras+"' < "+str(efrg[0])+") | ('"+orgras+"' > "+str(efrg[1])+"),'"+orgras+"'*"+str(sf)+")", stnras) # setnull arcpy.Delete_management(orgras) extrac_tif=extrac_tif+';'+stnras extrac_tif_name.append(stnras) extrac_tif=extrac_tif[1:len(extrac_tif)] rasmosiac=hdfs[0][0:tps]+yd+".tif" arcpy.MosaicToNewRaster_management(extrac_tif,out_path,rasmosiac,"#",valid,"#","1","LAST","FIRST") for exfn in extrac_tif_name: arcpy.Delete_management(exfn) # rasprj = out_path+'\\'+rasmosiac # arcpy.ProjectRaster_management(rasmosiac,rasprj,samp,'NEAREST',samp,'#','#','#') # arcpy.Delete_management(rasmosiac)
#----Created by Matthew Farr-------------------# #----------------------------------------------# import arcpy, shutil, os, csv from arcpy.sa import * #Working environment within HDF folder arcpy.env.workspace = "C:\\Users\\farrm\\Documents\\GitHub\\Ungulate\\GIS\\HDF\\" #List of HDF files rasterlist = arcpy.ListRasters("*" "HDF") #UTM projection projection = arcpy.SpatialReference(4326) #Extract NDVI from HDF files for raster in rasterlist: name = raster.split('.') filename = "..\\NDVI\\NDVI" + str(name[1][1:]) + ".tif" arcpy.ExtractSubDataset_management(raster, filename, "0") #Working environment within site folder arcpy.env.workspace = "C:\\Users\\farrm\\Documents\\GitHub\\Ungulate\\GIS\\Site\\" #List of shapefiles(i.e., sites) shlist = arcpy.ListFeatureClasses() #Buffer sites 650 meters for fc in shlist: name = fc.split('.') filename = "C:\\Users\\farrm\\Documents\\GitHub\\Ungulate\\GIS\\SiteBuffer\\" + name[ 0] + ".shp" arcpy.Buffer_analysis(fc, filename, "1000 meters", "FULL", "FLAT") #Working environment within sitebuffer folder arcpy.env.workspace = "C:\\Users\\farrm\\Documents\\GitHub\\Ungulate\\GIS\\SiteBuffer\\" #List of shapefiles(i.e., site buffers)
def HDF(filelist, layerlist, layernames = False, outdir = False): """ Function extracts tifs from HDFs. Use "Extract_MODIS_HDF" in the modis module for better handling of MODIS data with sinusoidal projections. inputs: filelist list of '.hdf' files from which data should be extracted layerlist list of layer numbers to pull out as individual tifs should be integers such as [0,4] for the 0th and 4th layer respectively. layernames list of layer names to put more descriptive names to each layer outdir directory to which tif files should be saved if outdir is left as 'False', files are saved in the same directory as the input file was found. """ # Set up initial arcpy modules, workspace, and parameters, and sanitize inputs. core.Check_Spatial_Extension() arcpy.env.overwriteOutput = True # enforce lists for iteration purposes filelist = core.enforce_list(filelist) layerlist = core.enforce_list(layerlist) layernames = core.enforce_list(layernames) # ignore user input layernames if they are invalid, but print warnings if layernames and not len(layernames) == len(layerlist): print('layernames must be the same length as layerlist!') print('ommiting user defined layernames!') layernames=False # create empty list to add failed file names into failed=[] # iterate through every file in the input filelist for infile in filelist: # pull the filename and path apart path,name = os.path.split(infile) arcpy.env.workspace = path for i in range(len(layerlist)): layer=layerlist[i] # specify the layer names. if layernames: layername = layernames[i] else: layername = str(layer).zfill(3) # use the input output directory if the user input one, otherwise build one if outdir: if not os.path.exists(os.path.join(outdir,layername)): os.makedirs(os.path.join(outdir,layername)) outname=os.path.join(outdir,layername,name[:-4] +'_'+ layername +'.tif') else: if not os.path.exists(os.path.join(path,layername)): os.makedirs(os.path.join(path,layername)) outname = os.path.join(path,layername,name[:-4] +'_'+ layername +'.tif') # perform the extracting and projection definition try: # extract the subdataset arcpy.ExtractSubDataset_management(infile, outname, str(layer)) print('Extracted ' + outname) except: print('Failed extract '+ outname + ' from ' + infile) failed.append(infile) return(failed)
inPath = os.path.join('I:/MODIS_CA/LAI/', str(yearn)) outPath = os.path.join('I:/MODIS_CA/LAI/TIFF/', str(yearn)) if not os.path.exists(outPath): os.mkdir(outPath) oway = 'I:/MODIS_CA/LAI/TIFF/' + str(yearn) + ".txt" f = open(oway, 'a') env.workspace = inPath arcpy.env.scratchWorkspace = inPath hdfList = arcpy.ListRasters('MOD15A2H.*', 'hdf') name1 = ["name"] num1 = 0 for hdf in hdfList: eviName1 = hdf[9:17] data1 = arcpy.ExtractSubDataset_management( hdf, outPath + "/" + hdf[9:17] + "_" + str(num1) + ".tif", "1") aa = len(name1) # if aa > 0: if (eviName1 != name1[aa - 1]): name1.append(eviName1) f.write(str(eviName1) + '\n') print str(eviName1) else: print "++" + str(eviName1) + "++" # else: num1 = num1 + 1 print "finish " del inPath del outPath del env.workspace
rasterlist = arcpy.ListRasters("*","HDF") projection = arcpy.SpatialReference(4326) #spliting of name MOD13A1.A2001001.h24v05.005.2008270033303.hdf for raster in rasterlist: name=raster.split('.') name1=name[1] name2=name[2] newName="n"+str(name1[3:])+str(name2[1:3])+str(name2[4:]) newSubFolder=str(name1[5:]) filename=Result+"\\"+newSubFolder+"\\"+newName path=Result+"\\"+newSubFolder if os.path.exists(path): print("Folder exists"+newSubFolder) arcpy.ExtractSubDataset_management(raster, filename+".tif", "0") arcpy.ProjectRaster_management(filename+".tif", filename+"p"+".tif",projection,"","","","","") arcpy.Delete_management(filename+".tif") else: arcpy.CreateFolder_management(Result,newSubFolder) print("folder made"+newSubFolder) arcpy.ExtractSubDataset_management(raster, filename+".tif", "0") arcpy.ProjectRaster_management(filename+".tif", filename+"p"+".tif",projection,"","","","","") arcpy.Delete_management(filename+".tif") arcpy.env.workspace="D:\\Shivaprakash\\TEST\\modis_uttrakanda\\TEST\\2015" rasterlist = arcpy.ListRasters("*","HDF") for raster in rasterlist: delete_hdf=Result+"\\"+raster os.remove(delete_hdf)
def Extract_MODIS_HDF(filelist,layerlist,layernames=False,outdir=False,Quiet=False): #-------------------------------------------------------------------------------------- # Function extracts tifs from HDFs such as MODIS data. # # inputs: # filelist list of '.hdf' files from which data should be extracted # layerlist list of layer numbers to pull out as individual tifs should be integers # such as [0,4] for the 0th and 4th layer respectively. # layernames list of layer names to put more descriptive names to each layer # outdir directory to which tif files should be saved # if outdir is left as 'False', files are saved in the same directory as # the input file was found. #-------------------------------------------------------------------------------------- # import modules import sys, os, arcpy, time # Set up initial arcpy modules, workspace, and parameters, and sanitize inputs. Check_Spatial_Extension() arcpy.env.overwriteOutput = True # enforce lists for iteration purposes filelist=Enforce_List(filelist) layerlist=Enforce_List(layerlist) layernames=Enforce_List(layernames) # ignore user input layernames if they are invalid, but print warnings if layernames and not len(layernames)==len(layerlist): print '{Extract_MODIS_HDF} layernames must be the same length as layerlist!' print '{Extract_MODIS_HDF} ommiting user defined layernames!' layernames=False # create empty list to add failed file names into failed=[] print '{Extract_MODIS_HDF} Beginning to extract!' # iterate through every file in the input filelist for infile in filelist: # pull the filename and path apart path,name = os.path.split(infile) arcpy.env.workspace = path for i in range(len(layerlist)): layer=layerlist[i] # specify the layer names. if layernames: layername=layernames[i] else: layername=str(layer).zfill(3) # use the input output directory if the user input one, otherwise build one if not os.path.exists(outdir): os.makedirs(outdir) outname=os.path.join(outdir,name[:-4] +'_'+ layername +'.tif') # perform the extracting and projection definition try: # extract the subdataset arcpy.ExtractSubDataset_management(infile, outname, str(layer)) # define the projection as the MODIS Sinusoidal Define_MODIS_Projection(outname) if not Quiet: print '{Extract_MODIS_HDF} Extracted ' + outname except: if not Quiet: print '{Extract_MODIS_HDF} Failed extract '+ outname + ' from ' + infile failed.append(infile) if not Quiet:print '{Extract_MODIS_HDF} Finished!' return(failed)
#Because had 100 missing days, redo it with missing data in subfolder LST_missing arcpy.env.workspace = r"F:\Hexsim\Data\MODIS_LST_download\LST_missing\LST_raw" arcpy.env.overwriteOutput = True dir = "F:\Hexsim\Data\MODIS_LST_download\LST_missing\LST_raw" ref_dataset = 'F:\\Hexsim\\Data\MODIS_LST_download\\LST_records\\ref_prof_layer\\MOD2000065.tif' sr_out = arcpy.SpatialReference("NAD 1983") for name in glob.glob(dir + '\\MOD11A1*'): print(name) base = os.path.basename(name) file_name, file_ext = os.path.splitext(base) if file_ext == '.hdf': outfile = 'F:\\Hexsim\\Data\MODIS_LST_download\\LST_missing\\LST_records\\' + file_name[0:3] + file_name[9:16] arcpy.ExtractSubDataset_management(name, outfile + ".tif", "0") #Define projection sr_def = arcpy.Describe(ref_dataset).spatialReference arcpy.DefineProjection_management(name, sr_def) #Project raster arcpy.ProjectRaster_management(in_raster=outfile + ".tif", out_raster=outfile + "pr.tif", out_coor_system= sr_out, resampling_type="CUBIC", geographic_transform = "MODIS V5 Sinusoidal to GCS NAD 1983") #################################################################################################### ################# CREATE LAYER OF POINTS WITH LOGGER LOCATIONS ##############################
days, np.where(days == 177)[0]) # a scene was missing from the MODIS data #extract subdatasets arcpy.CreateFileGDB_management(datafolder, "extracts.gdb") arcpy.CreateFileGDB_management(datafolder2, "extracts.gdb") #Temperature arcpy.env.workspace = datafolder alltiles = arcpy.ListRasters('*.hdf') for j in range(len(alltiles)): for i in range(len(dataindices)): theday = alltiles[j][13:16] out_name = datafolder + 'extracts.gdb\\' + datanames[i] + "_" + theday arcpy.ExtractSubDataset_management(datafolder + '\\' + alltiles[j], out_name, dataindices[i]) #ET and PET arcpy.env.workspace = datafolder2 alltiles = arcpy.ListRasters('*.hdf') for j in range(len(alltiles)): for i in range(len(dataindices2)): theday = alltiles[j][13:16] out_name = datafolder2 + 'extracts.gdb\\' + datanames2[i] + "_" + theday arcpy.ExtractSubDataset_management(datafolder2 + '\\' + alltiles[j], out_name, dataindices2[i]) tempdata = np.ndarray(shape=(len(dataindices), len(days))) for i in range(len(dataindices)): for j in range(len(days)):
lista = arcpy.ListDatasets() hdf_files = [x for x in lista if '.hdf' in x] fn_vars = ['a14', 'da', 'ni'] for img in hdf_files: day = str(img.split('.')[1][5:]) for index in ['0', '4']: outname = hdf2raster + '\\' + fn_vars[0] + day + 'lst' + '_' if index == '0': outname += 'da' else: outname += 'ni' print img print outname, '-----', index arcpy.ExtractSubDataset_management(img, outname, index) arcpy.env.workspace = hdf2raster os.chdir(hdf2raster) # poject lista = arcpy.ListDatasets() for img in lista: raster_file = arcpy.Raster(img) print outname = prodata + '\\' + raster_file.name print outname arcpy.ProjectRaster_management( raster_file, outname, "PROJCS['WGS_1984_UTM_zone_24S',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['false_easting',500000.0],PARAMETER['false_northing',10000000.0],PARAMETER['central_meridian',-39.0],PARAMETER['scale_factor',0.9996],PARAMETER['latitude_of_origin',0.0],UNIT['Meter',1.0]]", "NEAREST", "926.625433138333", "", "",
def extract_from_hdf(file_list, layer_list, layer_names = False, outdir = None): """ Extracts tifs from MODIS HDF files, ensures proper projection. :param file_list: either a list of '.hdf' files from which data should be extracted, or a directory containing '.hdf' files. :param layer_list: list of layer numbers to pull out as individual tifs should be integers such as [0,4] for the 0th and 4th layer respectively. :param layer_names: list of layer names to put more descriptive file suffixes to each layer :param outdir: directory to which tif files should be saved if outdir is left as 'None', files are saved in the same directory as the input file was found. :return output_filelist: returns a list of all files created by this function """ if outdir is not None: if not os.path.exists(outdir): os.makedirs(outdir) # enforce lists for iteration purposes and sanitize inputs file_list = core.enf_filelist(file_list) for filename in file_list: if '.xml' in filename or '.ovr' in filename or not '.hdf' in filename: file_list.remove(filename) layer_list = core.enf_list(layer_list) layer_names = core.enf_list(layer_names) # ignore user input layer_names if they are invalid, but print warnings if layer_names and not len(layer_names) == len(layer_list): Warning('layer_names must be the same length as layer_list!') Warning('Omitting user defined layer_names!') layer_names = False output_filelist = [] # iterate through every file in the input file_list for infile in file_list: # pull the filename and path apart path,name = os.path.split(infile) arcpy.env.workspace = path for i, layer in enumerate(layer_list): # specify the layer names. if layer_names: layername = layer_names[i] else: layername = str(layer).zfill(3) # use the input output directory if the user input one, otherwise build one if outdir: outname = os.path.join(outdir, "{0}_{1}.tif".format(name[:-4], layername)) else: outname = os.path.join(path, "{0}_{1}.tif".format(name[:-4], layername)) # perform the extracting and projection definition try: arcpy.ExtractSubDataset_management(infile, outname, str(layer)) define_projection(outname) output_filelist.append(outname) print("Extracted {0}".format(os.path.basename(outname))) except: print("Failed to extract {0} from {1}".format(os.path.basename(outname), os.path.basename(infile))) return output_filelist
# --------------------------------------------------------------------------- # cn002.py # Created on: 2011-04-25 13:31:26.00000 # (generated by ArcGIS/ModelBuilder) # Description: # --------------------------------------------------------------------------- # Import arcpy module import arcpy # Local variables: a2003_002 = "C:\\Users\\ekloog\\Documents\\$Doc\\3.PostDoc\\3.1.Projetcs\\3.1.3.TEMP_MODELS\\3.1.1.1.Raw_data\\MODIS_TEMP\\Night only\\a2003_002.hdf" a2003_002_tif = "C:\\Users\\ekloog\\Documents\\$Doc\\3.PostDoc\\3.1.Projetcs\\3.1.3.TEMP_MODELS\\3.1.1.4.Work\\2.Gather_data\\hdf_2_tiff\\a2003_002.tif" arcpy.ExtractSubDataset_management(a2003_002, a2003_002_tif, "0")
if len(mod44w_seltiles) > 1 or len(mod44w_seltiles) == 0: mod44w_seltiles = get_inters_tiles(ref_extent=gladtile_extent, tileiterator=mod44w_wgsextdict, containsonly=False) #Extract QA dataset mod44w_seltilesQA = [] for tile in mod44w_seltiles: modtileid = re.search( '(?<=MOD44W_A2015001_)h[0-9]{2}v[0-9]{2}(?=[0-9_]{18}[.]hdf)', os.path.split(tile)[1]).group() outQA = os.path.join(mod44w_resgdb, "QA_{}".format(modtileid)) if not arcpy.Exists(outQA): print(outQA) arcpy.ExtractSubDataset_management(in_raster=tile, out_raster=outQA, subdataset_index=1) mod44w_seltilesQA.append(outQA) #If multiple MODIS tiles intersect GLAD tile, mosaick them if len(mod44w_seltilesQA) > 1: mod44w_gladmatch = os.path.join( mod44w_resgdb, 'mod44wQA_gladmosaic{}'.format(gladtileid)) if not arcpy.Exists(mod44w_gladmatch): arcpy.MosaicToNewRaster_management( input_rasters=mod44w_seltilesQA, output_location=os.path.split(mod44w_gladmatch)[0], raster_dataset_name_with_extension=os.path.split( mod44w_gladmatch)[1], number_of_bands=1, mosaic_method='FIRST')