FeatureLayers and RasterLayers are layers created in memory, not in the scratch workspace (in background processing they create a file referencing the memory position but that is all).
In order to remove those layers residing in memory, you need to explicitly delete them one by one using arcpy (they do not fall out of scope until Python exits). Note that deleting the variable holding them in a Pythonic way (del varname) will NOT work.
Your code needs to be revised as follows (changes in the end of the code block):
for month in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]:
ENA_Layer = SCRATCH +"\\ENA"+variable+str(month)
NRM_Layer = SCRATCH +"\\NRM"+variable+str(month)
PNW_Layer = SCRATCH +"\\PNW"+variable+str(month)
PSW_Layer = SCRATCH +"\\PSW"+variable+str(month)
SRM_Layer = SCRATCH +"\\SRM"+variable+str(month)
# Process: Make NetCDF Raster Layer (169)
arcpy.MakeNetCDFRasterLayer_md(ENA, variable, "x", "y", ENA_Layer, "", "time "+ str(month), "BY_INDEX")
# Process: Make NetCDF Raster Layer (157)
arcpy.MakeNetCDFRasterLayer_md(NRM, variable, "x", "y", NRM_Layer, "", "time "+ str(month), "BY_INDEX")
# Process: Make NetCDF Raster Layer (145)
arcpy.MakeNetCDFRasterLayer_md(PNW, variable, "x", "y", PNW_Layer, "", "time "+ str(month), "BY_INDEX")
# Process: Make NetCDF Raster Layer (133)
arcpy.MakeNetCDFRasterLayer_md(PSW, variable, "x", "y", PSW_Layer, "", "time "+ str(month), "BY_INDEX")
# Process: Make NetCDF Raster Layer (121)
arcpy.MakeNetCDFRasterLayer_md(SRM, variable, "x", "y", SRM_Layer, "", "time "+ str(month), "BY_INDEX")
# Process: Mosaic To New Raster (25)
arcpy.MosaicToNewRaster_management("\"" + ENA_Layer +";"+ NRM_Layer +";"+ PNW_Layer +";"+ PSW_Layer +";"+ SRM_Layer +"\"", Output_Location, variable+"_"+str(month)+"2032", "PROJCS['WGS_1984_Lambert_Conformal_Conic',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Lambert_Conformal_Conic'],PARAMETER['false_easting',0.0],PARAMETER['false_northing',0.0],PARAMETER['central_meridian',-102.3000030517578],PARAMETER['standard_parallel_1',30.0],PARAMETER['standard_parallel_2',60.0],PARAMETER['latitude_of_origin',52.0],UNIT['Kilometer',1000.0]]", "", "1", "1", "MEAN", "FIRST")
##########################################
# NEW LINES TO ADD TO DELETE MEMORY LAYERS
# Delete layers from memory
arcpy.Delete_management(ENA_Layer)
arcpy.Delete_management(NRM_Layer)
arcpy.Delete_management(PNW_Layer)
arcpy.Delete_management(PSW_Layer)
arcpy.Delete_management(SRM_Layer)
This is not explicitly mentioned in the help files from ESRI, but it is necessary if you want to deal with data in memory. If you monitor the memory use from the task manager you will notice the drop as soon as the Delete statements are hit (in my case, from 1.6GB use it drops to 100MB in every loop).
One thing that makes writing WHERE clauses a lot easier is to use the AddFieldDelimiters
function, which automatically adds the correct, DBMS-specific delimiters for field identifiers, such as double-quotes for FGDB and brackets for PGDB.
The other thing you have to consider is whether the value is a number, string, or other data type. Specifically, strings are wrapped in single quotes while numbers are not. You could check the field type and add single quotes if it is a string field.
E.g.:
import arcpy
def buildWhereClause(table, field, value):
"""Constructs a SQL WHERE clause to select rows having the specified value
within a given field and table."""
# Add DBMS-specific field delimiters
fieldDelimited = arcpy.AddFieldDelimiters(table, field)
# Determine field type
fieldType = arcpy.ListFields(table, field)[0].type
# Add single-quotes for string field values
if str(fieldType) == 'String':
value = "'%s'" % value
# Format WHERE clause
whereClause = "%s = %s" % (fieldDelimited, value)
return whereClause
if __name__ == "__main__":
inputfc = r"C:\input.shp"
outputfc = r"C:\output.shp"
fieldname = "StudyID"
fieldvalue = 101
whereclause = buildWhereClause(inputfc, fieldname, fieldvalue)
arcpy.Select_analysis(inputfc, outputfc, whereclause)
See also the function in this answer for a multi-value version of the above function.
Best Answer
The built-in Feature Class to Feature Class tool does everything you want.
If you want more control than that, you can create a script tool and customize its validation behavior if necessary (for example, to list all the unique values of a chosen field).
To simply provide a drop-down of available fields you don't need to customize the behavior, just set up an input field parameter that is "Obtained from" the input feature class or feature layer parameter.
(source: arcgis.com)
If you want to limit what the user can enter to a single SQL expression where the operator is chosen from a list, you could create a String parameter and set up its Filter property with the desired values. Then, in the script you would create the SQL where clause with Python, using string formatting (new or old styles) or concatenation to build the statement from the variable elements (field name, operator and value).