Combine: three python scripts and make a one

3424
4
11-17-2015 09:32 PM
ZiaAhmed
New Contributor III

Hi,

In my previous post, the problem I mentioned I have solved it  using following three python scripts. Is there anyway to combine theses three scripts to make one?

Zia

# -----------------------------------------------
# Script_01: Create folders with file name
# Zia Ahmed
# ------------------------------------------------

try:
    import glob, os, shutil

    folder = r"J:\RapidEyr_Atcor\test\HDF" 

    for file_path in glob.glob(os.path.join(folder, '*.*')):
        new_dir = file_path.rsplit('.', 1)[0]
        os.mkdir(os.path.join(folder, new_dir))
        shutil.move(file_path, os.path.join(new_dir, os.path.basename(file_path)))
except:
    print "Folder creation failed."
    print arcpy.GetMessages()

# ---------------------------------------------------------------------------
# Script_02: Import HDF file as TIFF 
# Description: Extract HDF to geotiff
# Zia Ahmed
# ---------------------------------------------------------------------------

try:
    import arcpy
    arcpy.env.workspace = r"J:\RapidEyr_Atcor\test\HDF"

    # list all folders in a directory
    folders = arcpy.ListWorkspaces()
    for folder in folders:
        arcpy.env.workspace = folder
        rasterListA = arcpy.ListRasters()
        for raster in rasterListA:
           tifOutA=""+raster[15:46]
           arcpy.ExtractSubDataset_management(raster,tifOutA+"_B1"+".tif", "0")
           arcpy.ExtractSubDataset_management(raster,tifOutA+"_B2"+".tif", "1")
           arcpy.ExtractSubDataset_management(raster,tifOutA+"_B3"+".tif", "2")
           arcpy.ExtractSubDataset_management(raster,tifOutA+"_B4"+".tif", "3")
           arcpy.ExtractSubDataset_management(raster,tifOutA+"_B5"+".tif", "4")
except:
    print "Extract Subdataset failed."
    print arcpy.GetMessages()

#--------------------------------------------------
# Scrit_03: Batch processing - Stack RapidEyes bands
# Written by Zia Ahmed, CIMMYT, Bangladeash
#--------------------------------------------------

try:
    import arcpy, os
    # Define working folder
    arcpy.env.workspace = r'J:\RapidEyr_Atcor\test\HDF'

    # list all folders in a directory
    folders = arcpy.ListWorkspaces()
    for folder in folders:
        arcpy.env.workspace = folder
        # Create a raster list
        rasters = arcpy.ListRasters("*.tif")
        # output file name
        name = os.path.join(rasters[1].split("_")[1] +"_multi"+ ".tif")
        arcpy.CompositeBands_management(rasters, name)
except:
    print "Multiband failed."
    print arcpy.GetMessages()
Tags (3)
0 Kudos
4 Replies
DarrenWiens2
MVP Honored Contributor

Without fully reading your individual scripts, is there a problem with simply copy/pasting them one after the other into one script?

0 Kudos
ModyBuchbinder
Esri Regular Contributor

Is there any reason not to put all of them in one file and make one script out of them?

0 Kudos
DanPatterson_Retired
MVP Emeritus

turn them into functions then call them sequentially.  In this way you can reuse the functions at a later stage.  As a nonsensical example

def a(nums):
    sqrs = [i**2 for i in nums]
    return sqrs
   
def b(sqrs):
    roots = [i**0.5 for i in sqrs]
    return roots

def c(more):
    final = [i for i in more if i % 2]
    return final

#-------------------------------------
if __name__=="__main__":
    "do stuff with functions"
    nums = [1,2,3]
    sqrs = a(nums)
    roots = b(sqrs)
    final = c(roots)
    frmt = "nums {}  sqrs{}  roots{}  ... final {}"
    print(frmt.format(nums,sqrs,roots,final))
ZiaAhmed
New Contributor III

Thanks to all. I will try.

Zia

0 Kudos