def test(self):
		s = self.session
		dao = SA_Result_DAO(session = s)

		# Drop/Create tables.
		sa_metadata.metadata.drop_all(s.bind)
		sa_metadata.metadata.create_all(s.bind)
		s.commit()
		
		# Generate test result set.
		results = results_util.generate_results()

		## Merge generated objects for testing.
		for o_key, o in util_registry.object_registry.items():
			try:
				if not o_key[0] in ['Result']:
					s.add(o)
			except: pass
		s.commit()

		# Test DAO operations.
		dao.save_results(results)
		fetched_results = dao.get_results(filters=[
			{
				'field': 'tag',
				'op': '==',
				'value': 'a'
				}
			])

		result_field = {
				'field': 'a',
				'aggregate_funcs': ['sum']
				}

		mapserver_data_string = dao.get_mapserver_data_string(result_field=result_field)
		print mapserver_data_string

		self.failUnless(True)
def main():

	output_dir = '/home/adorsk/projects/sasi/sasi_model/outputs/shapefiles'

	result_tag = 'gc30_all'

	t = 1999

	# Get result dao.
	db_session = sa_session.get_session()
	result_dao = SA_Result_DAO(session=db_session)

	# Get values by time, cell, and field for results.
	values_by_t_c_f = result_dao.get_values_by_t_c_f(filters=[
		{'attr': 'tag', 'op': '==', 'value': result_tag}, 
		{'attr': 'time', 'op': '==', 'value': t}
		])

	#
	# Make fiona collection from result set.
	#

	# Define schema.
	geometry_type = 'MultiPolygon'
	schema = {
			'geometry': geometry_type,
			'properties': {
				'type_id': 'str',
				'hab_type': 'str'
				}
			}
	generic_attrs = ['A', 'Y', 'X', 'Z', 'ZZ']
	for generic_attr in generic_attrs: schema['properties'][generic_attr] = 'float'

	# Write shpfile.
	filename = "%s/%s.%s.shp" % (output_dir, result_tag, t) 
	driver = 'ESRI Shapefile'
	crs = {'init': "epsg:4326"}

	with collection(
			filename, "w",
			driver=driver,
			schema=schema,
			crs=crs
			) as c:
		record_counter = 1
		for cell, cell_fields in values_by_t_c_f[t].items():

			if (record_counter % 1000) == 0:
				print >> sys.stderr, "%s" % record_counter

			# Populate record properties.
			habitat_types = set(["(%s)" % h.habitat_type.id for h in cell.habitats])
			properties = {
					'type_id': cell.type_id,
					'hab_type': ' & '.join(habitat_types)
					}
			for generic_attr in generic_attrs:
				properties[generic_attr] = cell_fields.get(generic_attr,0.0)

			# Populate record geometry.
			wkb_geom = "%s" % cell.geom.geom_wkb
			geometry = GeomBuilder().build_wkb(wkb_geom)

			# Assemble the record.
			record = {
					'id': record_counter,
					'geometry': geometry,
					'properties': properties
					}

			# Write the record.
			c.write(record)
			record_counter += 1
from datetime import datetime
import sys

import sasi.util.sasi_model as sasi_model_util


if __name__ == '__main__':

	conf.conf['verbose'] = True

	db_session = sa_session.get_session()

	#db_session.bind.echo = True

	result_dao = SA_Result_DAO(session=db_session)

	effort_dao = SA_Effort_DAO(session=db_session)

	#t0 = 1989	
	t0 = 2008
	tf = 2010
	dt = 1
	times = range(t0,tf+1,dt)

	#grid_model = StaticGridModel(cell_dao=SA_Cell_DAO(session=db_session), default_filters=[{'field': 'type','op': '==', 'value': 'km100' }, {'field': 'type_id', 'op': '==', 'value': '817'}]) 
	grid_model = StaticGridModel(cell_dao=SA_Cell_DAO(session=db_session), default_filters=[{'field': 'type','op': '==', 'value': 'km100'} ])

	# Filter domain for cells w/ depth less than 138 (for G3).
	#grid_model = StaticGridModel(cell_dao=SA_Cell_DAO(session=db_session), default_filters=[{'field': 'type','value': ['km100'] }, {'field': 'depth', 'op': '>=', 'value': -138}])