import arcpy, sys, os
from arcpy import env

# black or white map? get from command line
# if len(sys.argv) < 3: 
	# print "Usage: map_creator_batch.py <black/white> <year>"
	# exit(0)
# backcolor = sys.argv[1]
# year = sys.argv[2]

for backcolor in ['white','black']:
	for y in range(1940,2060,10):
		
		year = str(y)
	
		# input and output directories
		basedir = "z:/Housing_data/PBG_change/"
		bounddir = "x:/boundaries/"
		outdir = basedir + "maps2/gifs_mask/" + backcolor + "/"
		if not os.path.isdir(outdir): os.mkdir(outdir)


		# map documents
		landscapemap = basedir + "maps/Housing_landscape_template_" + backcolor + "_CONUS.mxd"
		landscape_mxd = arcpy.mapping.MapDocument(landscapemap)

		# symbology layers
		data_symbol = basedir + "maps/layer_files/housing_denscode_raster.lyr"
		data_symbol_Layer = arcpy.mapping.Layer(data_symbol)


		# # # # # start main
		data_layer_file =  "z:/Housing_data/PBG_change/data/WebMap_Rasters/pbg00_100m_proj_" + year + "_WGS.tif"
		outGIF = outdir + "CONUS_Housing_PBG_project_" + year + "_" + backcolor + ".gif"
		if not os.path.exists(outGIF):

			print "... Creating map for CONUS", year
			map_mxd = landscape_mxd
				
			# data frames (main map)
			dfs = arcpy.mapping.ListDataFrames(map_mxd)
			main_df = dfs[0]
				
			# # # Main Layers
			# add data layer to main map
			dataLayer = arcpy.mapping.Layer(data_layer_file)
			arcpy.mapping.AddLayer(main_df,dataLayer, "BOTTOM")
			updateLayer = arcpy.mapping.ListLayers(map_mxd, "*", main_df)[2]
			# update data symbology based on year
			arcpy.mapping.UpdateLayer(main_df, updateLayer, data_symbol_Layer, True)
			

			# update state and year text elements
			arcpy.mapping.ListLayoutElements(map_mxd, "TEXT_ELEMENT","Year")[0].text = year + " Housing Density in the Conterminous United States"
			
			# if year == '1990': 
				# arcpy.mapping.ListLayoutElements(map_mxd, "TEXT_ELEMENT","HText")[0].text = year + ' housing allocated to 2010 TIGER blocks'

			# if year == '2000': 
				# arcpy.mapping.ListLayoutElements(map_mxd, "TEXT_ELEMENT","HText")[0].text = year + ' housing allocated to 2010 TIGER blocks'

			 # if year == '2010': 
			arcpy.mapping.ListLayoutElements(map_mxd, "TEXT_ELEMENT","HText")[0].text = 'Partial Block Groups (PBG)'



			# export map
			print "arcpy Export To GIF ->", outGIF
			arcpy.mapping.ExportToGIF(map_mxd, outGIF,"PAGE_LAYOUT",resolution=300)	
			# arcpy.mapping.ExportToGIF(map_mxd, outGIF)	
			
			print 
			del map_mxd
				
		else:
			print outGIF, "already exists...skipping"
			
		del landscape_mxd
exit(0)