<gh_stars>1-10
# ---------------------------------------------------------------------------
# tool_fixIoapi.py
# Created on: 2011-11-30 23:11:16.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: tool_fixIoapi <Input_netCDF_File> <o3> <Variable> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Used for arange
import numpy

# HTTP stuff
import urllib2
import MultipartPostHandler
#from os.path import basename
import os
from urlparse import urlsplit

# Just some other utilities
from re import sub
from time import time

# Little constants class
import const

############################################################
# Some configurations..
#const.upload_url = 'http://khea.asilika.com/~matt/geog/ArcGIS/fixNetcdfProjection.py/upload'
# PythonWin doesn't start new sessions to run this, so constants cause errors the second time their run
UPLOAD_URL = 'http://pontus.cee.carleton.ca/~matt/geog/ArcGIS/fixIoapiProjection.py/upload'
REPARED_IOAPI_FILE_FMT = "t:\\tmp.out.arc.%d.nc"
FAKE_INPUTS = False
ACTUALLY_UPLOAD = True
ACTUALLY_LOAD_NETCDF_FILE = True
ADD_RASTER_TO_DATAFRAME = True
############################################################

def download(url, params, localFileName = None):
	localName = "ioapi.out.nc"

	# No idea what these two lines does right now
	opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
	urllib2.install_opener(opener)

	arcpy.AddMessage("DEBUG: Requesting: %s"%url)
	req = urllib2.Request(url, params)
	r = urllib2.urlopen(req)
	arcpy.AddMessage("DEBUG: Downloaded complete..")
	if r.info().has_key('Content-Disposition'):
		# If the response has Content-Disposition, we take file name from it
		localName = r.info()['Content-Disposition'].split('filename=')[1]
		if localName[0] == '"' or localName[0] == "'":
			localName = localName[1:-1]
	elif r.url != url: 
		# if we were redirected, the real file name we take from the final URL
		localName = url2name(r.url)
	if localFileName: 
		# we can force to save the file as specified name
		localName = localFileName
	try:
		f = open(localName, 'wb')
		f.write(r.read())
		f.close()
	except IOError	as (errno, strerror):
		arcpy.AddError("IOError: %s"%strerror)

	arcpy.AddMessage("DEBUG: End of function")

	return localName


############################################################
# Script arguments
#
# 0: Input ioapi file
# 1: What variable to extract
# 2: The ArcGIS raster file to output
if FAKE_INPUTS:
	Input_netCDF_File = 'T:\\aconc.nc'
	Variable = "O3"
	#out_raster_file = "t:\\raster_out1"
	#overwrite_raster = True
	shift_cells = True
else:
	# Oh my god, not having the .encade at the end just wasted HOURS
	Input_netCDF_File = arcpy.GetParameterAsText(0).encode('ascii')
	Input_netCDF_File = Input_netCDF_File.replace("\\","/")
	Variable = arcpy.GetParameterAsText(1).encode('ascii')
	#out_raster_file = arcpy.GetParameterAsText(2).encode('ascii')
	#overwrite_raster = arcpy.GetParameterAsText(3)
	shift_cells = arcpy.GetParameterAsText(2).encode('ascii')
	if shift_cells == "true":
		shift_cells=True

arcpy.AddMessage("shift_cells: %s"%str(shift_cells))

# Prevent errors that look like caching errors
raster_name = "t:\\%s_%d"%(Variable,time())

# Local variables:
arcpy.AddMessage("Assuming X dim is COL and Y dim is ROW")
X_Dimension = "COL"
Y_Dimension = "ROW"

if not os.access(Input_netCDF_File, os.R_OK):
	arcpy.AddError("Cannot access IOAPI file: %s"%Input_netCDF_File)
	raise Exception("Cannot access: %s"%Input_netCDF_File)

############################################################
# Upload the broken IOAPI file to download a corrected Netcdf file
if ACTUALLY_UPLOAD == True:
	arcpy.AddMessage("Sending IOAPI file to webservice to repair spatial information")
	arcpy.AddMessage("Uploading %s..."%Input_netCDF_File)
	params = {'shift_cells':('1' if shift_cells else '0'),'file':open(Input_netCDF_File, 'rb')}
	#arcpy.AddMessage("DEBUG: Params: %s"%params)

        downloaded_netcdf_file = REPARED_IOAPI_FILE_FMT%(1)
        counter=1
        while os.access(downloaded_netcdf_file, os.R_OK):
                counter=counter+1
                downloaded_netcdf_file = REPARED_IOAPI_FILE_FMT%(counter)

	fileName = download(UPLOAD_URL, params, downloaded_netcdf_file)
	if os.access(fileName, os.R_OK):
		arcpy.AddMessage("Downloaded file: %s"%fileName)
	else:
		arcpy.AddError("Repaired IOAPI file was not downloaded!")
		raise Exception("Temporary file %s wasn't downloaded!"%fileName)
	if not fileName == downloaded_netcdf_file:
		arcpy.AddWarning("Weird, the downloaded file has a different name than expected.  Expected: %s, received: %s"%(downloaded_netcdf_file, fileName))
else:
	arcpy.AddMessage("Skipped actually file fixing.")

############################################################
# Run tool to read in Netcdf file
if ACTUALLY_LOAD_NETCDF_FILE == True:
	#if overwrite_raster:
	#	if os.access(out_raster_file, os.R_OK):
	#		arcpy.AddMessage("File %s exists, removing..."%out_raster_file)
	#		try:
	#			os.remove(raster_name)
	#			arcpy.AddMessage("File %s deleted")
	#		except IOError	as (errno, strerror):
	#			arcpy.AddMessage("Cannot remove %s\nIOError: %s"%(out_raster_file,strerror))

	# Process: Make NetCDF Raster Layer
	arcpy.AddMessage("Calling NetCDF to Raster tool, outputting to %s"%raster_name)
	# MakeNetCDFRasterLayer_md (in_netcdf_file, variable, x_dimension, y_dimension, out_raster_layer, {band_dimension}, {dimension_values}, {value_selection_method})
	arcpy.MakeNetCDFRasterLayer_md(downloaded_netcdf_file, Variable, X_Dimension, Y_Dimension, raster_name, "", "", "BY_VALUE")
	#if not os.access(out_raster_file, os.R_OK):
	#	arcpy.AddMessage("Something's wrong, %s was not created"%out_raster_file)
	#	raise Exception("%s was not created"%out_raster_file)
else:
	arcpy.AddMessage("Skipped loading netcdf file into a raster")


############################################################
# Load the layer into ArcGIS
if ADD_RASTER_TO_DATAFRAME:
	arcpy.AddMessage("Loading raster file to data frame")

	# get the map document
	mxd = arcpy.mapping.MapDocument("CURRENT")

	# get the data frame (Change 0 to end!)
	df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

	# create a new layer
	newlayer = arcpy.mapping.Layer(raster_name)


	# add the layer to the map at the bottom of the TOC in data frame 0
	arcpy.mapping.AddLayer(df, newlayer, "BOTTOM")
else:
	arcpy.AddMessage("Skipped adding layer to dataframe")

# ---------------------------------------------------------------------------
# tool_fixIoapi.py
# Created on: 2011-11-30 23:11:16.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: tool_fixIoapi <Input_netCDF_File> <o3> <Variable> 
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Used for arange
import numpy

# HTTP stuff
import urllib2
import MultipartPostHandler
#from os.path import basename
import os
from urlparse import urlsplit

# Just some other utilities
from re import sub
from time import time

# Little constants class
import const

############################################################
# Some configurations..
#const.upload_url = 'http://khea.asilika.com/~matt/geog/ArcGIS/fixNetcdfProjection.py/upload'
# PythonWin doesn't start new sessions to run this, so constants cause errors the second time their run
UPLOAD_URL = 'http://pontus.cee.carleton.ca/~matt/geog/ArcGIS/fixIoapiProjection.py/upload'
REPARED_IOAPI_FILE_FMT = "t:\\tmp.out.arc.%d.nc"
FAKE_INPUTS = False
ACTUALLY_UPLOAD = True
ACTUALLY_LOAD_NETCDF_FILE = True
ADD_RASTER_TO_DATAFRAME = True
############################################################

def download(url, params, localFileName = None):
	localName = "ioapi.out.nc"

	# No idea what these two lines does right now
	opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
	urllib2.install_opener(opener)

	arcpy.AddMessage("DEBUG: Requesting: %s"%url)
	req = urllib2.Request(url, params)
	r = urllib2.urlopen(req)
	arcpy.AddMessage("DEBUG: Downloaded complete..")
	if r.info().has_key('Content-Disposition'):
		# If the response has Content-Disposition, we take file name from it
		localName = r.info()['Content-Disposition'].split('filename=')[1]
		if localName[0] == '"' or localName[0] == "'":
			localName = localName[1:-1]
	elif r.url != url: 
		# if we were redirected, the real file name we take from the final URL
		localName = url2name(r.url)
	if localFileName: 
		# we can force to save the file as specified name
		localName = localFileName
	try:
		f = open(localName, 'wb')
		f.write(r.read())
		f.close()
	except IOError	as (errno, strerror):
		arcpy.AddError("IOError: %s"%strerror)

	arcpy.AddMessage("DEBUG: End of function")

	return localName


############################################################
# Script arguments
#
# 0: Input ioapi file
# 1: What variable to extract
# 2: The ArcGIS raster file to output
if FAKE_INPUTS:
	Input_netCDF_File = 'T:\\aconc.nc'
	Variable = "O3"
	#out_raster_file = "t:\\raster_out1"
	#overwrite_raster = True
	shift_cells = True
else:
	# Oh my god, not having the .encade at the end just wasted HOURS
	Input_netCDF_File = arcpy.GetParameterAsText(0).encode('ascii')
	Input_netCDF_File = Input_netCDF_File.replace("\\","/")
	Variable = arcpy.GetParameterAsText(1).encode('ascii')
	#out_raster_file = arcpy.GetParameterAsText(2).encode('ascii')
	#overwrite_raster = arcpy.GetParameterAsText(3)
	shift_cells = arcpy.GetParameterAsText(2).encode('ascii')
	if shift_cells == "true":
		shift_cells=True

arcpy.AddMessage("shift_cells: %s"%str(shift_cells))

# Prevent errors that look like caching errors
raster_name = "t:\\%s_%d"%(Variable,time())

# Local variables:
arcpy.AddMessage("Assuming X dim is COL and Y dim is ROW")
X_Dimension = "COL"
Y_Dimension = "ROW"

if not os.access(Input_netCDF_File, os.R_OK):
	arcpy.AddError("Cannot access IOAPI file: %s"%Input_netCDF_File)
	raise Exception("Cannot access: %s"%Input_netCDF_File)

############################################################
# Upload the broken IOAPI file to download a corrected Netcdf file
if ACTUALLY_UPLOAD == True:
	arcpy.AddMessage("Sending IOAPI file to webservice to repair spatial information")
	arcpy.AddMessage("Uploading %s..."%Input_netCDF_File)
	params = {'shift_cells':('1' if shift_cells else '0'),'file':open(Input_netCDF_File, 'rb')}
	#arcpy.AddMessage("DEBUG: Params: %s"%params)

        downloaded_netcdf_file = REPARED_IOAPI_FILE_FMT%(1)
        counter=1
        while os.access(downloaded_netcdf_file, os.R_OK):
                counter=counter+1
                downloaded_netcdf_file = REPARED_IOAPI_FILE_FMT%(counter)

	fileName = download(UPLOAD_URL, params, downloaded_netcdf_file)
	if os.access(fileName, os.R_OK):
		arcpy.AddMessage("Downloaded file: %s"%fileName)
	else:
		arcpy.AddError("Repaired IOAPI file was not downloaded!")
		raise Exception("Temporary file %s wasn't downloaded!"%fileName)
	if not fileName == downloaded_netcdf_file:
		arcpy.AddWarning("Weird, the downloaded file has a different name than expected.  Expected: %s, received: %s"%(downloaded_netcdf_file, fileName))
else:
	arcpy.AddMessage("Skipped actually file fixing.")

############################################################
# Run tool to read in Netcdf file
if ACTUALLY_LOAD_NETCDF_FILE == True:
	#if overwrite_raster:
	#	if os.access(out_raster_file, os.R_OK):
	#		arcpy.AddMessage("File %s exists, removing..."%out_raster_file)
	#		try:
	#			os.remove(raster_name)
	#			arcpy.AddMessage("File %s deleted")
	#		except IOError	as (errno, strerror):
	#			arcpy.AddMessage("Cannot remove %s\nIOError: %s"%(out_raster_file,strerror))

	# Process: Make NetCDF Raster Layer
	arcpy.AddMessage("Calling NetCDF to Raster tool, outputting to %s"%raster_name)
	# MakeNetCDFRasterLayer_md (in_netcdf_file, variable, x_dimension, y_dimension, out_raster_layer, {band_dimension}, {dimension_values}, {value_selection_method})
	arcpy.MakeNetCDFRasterLayer_md(downloaded_netcdf_file, Variable, X_Dimension, Y_Dimension, raster_name, "", "", "BY_VALUE")
	#if not os.access(out_raster_file, os.R_OK):
	#	arcpy.AddMessage("Something's wrong, %s was not created"%out_raster_file)
	#	raise Exception("%s was not created"%out_raster_file)
else:
	arcpy.AddMessage("Skipped loading netcdf file into a raster")


############################################################
# Load the layer into ArcGIS
if ADD_RASTER_TO_DATAFRAME:
	arcpy.AddMessage("Loading raster file to data frame")

	# get the map document
	mxd = arcpy.mapping.MapDocument("CURRENT")

	# get the data frame (Change 0 to end!)
	df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

	# create a new layer
	newlayer = arcpy.mapping.Layer(raster_name)


	# add the layer to the map at the bottom of the TOC in data frame 0
	arcpy.mapping.AddLayer(df, newlayer, "BOTTOM")
else:
	arcpy.AddMessage("Skipped adding layer to dataframe")
# --------------------------------------------------------------------------- # tool_fixIoapi.py # Created on: 2011-11-30 23:11:16.00000 #   (generated by ArcGIS/ModelBuilder) # Usage: tool_fixIoapi <Input_netCDF_File> <o3> <Variable> # Description: # --------------------------------------------------------------------------- # Import arcpy module # Used for arange # HTTP stuff #from os.path import basename # Just some other utilities # Little constants class ############################################################ # Some configurations.. #const.upload_url = 'http://khea.asilika.com/~matt/geog/ArcGIS/fixNetcdfProjection.py/upload' # PythonWin doesn't start new sessions to run this, so constants cause errors the second time their run ############################################################ # No idea what these two lines does right now # If the response has Content-Disposition, we take file name from it # if we were redirected, the real file name we take from the final URL # we can force to save the file as specified name ############################################################ # Script arguments # # 0: Input ioapi file # 1: What variable to extract # 2: The ArcGIS raster file to output #out_raster_file = "t:\\raster_out1" #overwrite_raster = True # Oh my god, not having the .encade at the end just wasted HOURS #out_raster_file = arcpy.GetParameterAsText(2).encode('ascii') #overwrite_raster = arcpy.GetParameterAsText(3) # Prevent errors that look like caching errors # Local variables: ############################################################ # Upload the broken IOAPI file to download a corrected Netcdf file #arcpy.AddMessage("DEBUG: Params: %s"%params) ############################################################ # Run tool to read in Netcdf file #if overwrite_raster: #	if os.access(out_raster_file, os.R_OK): #		arcpy.AddMessage("File %s exists, removing..."%out_raster_file) #		try: #			os.remove(raster_name) #			arcpy.AddMessage("File %s deleted") #		except IOError	as (errno, strerror): #			arcpy.AddMessage("Cannot remove %s\nIOError: %s"%(out_raster_file,strerror)) # Process: Make NetCDF Raster Layer # MakeNetCDFRasterLayer_md (in_netcdf_file, variable, x_dimension, y_dimension, out_raster_layer, {band_dimension}, {dimension_values}, {value_selection_method}) #if not os.access(out_raster_file, os.R_OK): #	arcpy.AddMessage("Something's wrong, %s was not created"%out_raster_file) #	raise Exception("%s was not created"%out_raster_file) ############################################################ # Load the layer into ArcGIS # get the map document # get the data frame (Change 0 to end!) # create a new layer # add the layer to the map at the bottom of the TOC in data frame 0