<?xml version="1.0"?>
<table xmlns="http://query.yahooapis.com/v1/schema/table.xsd">
  <meta>
    <author>Jim O'Donnell</author>
    <documentationURL>http://eatyourgreens.org.uk/archives/2009/05/searching-the-sky-with-yql-execute.html</documentationURL>
  </meta>
  <bindings>
    <select produces="XML" itemPath="photos.photo">
      <urls>

        <url></url>
      </urls>
	  <paging model="offset">
		<start id="start" default="0"/>
		<pagesize id="count" max="50"/>
		<total default="20"/>
	  </paging>
      <inputs>
      </inputs>
	  <execute><![CDATA[
		var querystring = "select farm, server, id, secret, title, owner.username, urls.url.content, tags.tag.raw from flickr.photos.info where (tags.tag.raw like 'astro:%') and photo_id in (select id from flickr.photos.search("+start+","+count+") where machine_tags='astro:RA=')";
		var data = y.query(querystring);
		var photos = parseAstrotags(data);
		
		default xml namespace = '';
		var respdata = <photos/>;
		for each (var photo in photos) {
			
			var node = <photo>
						<id>{photo.id}</id>
						<title>{photo.title}</title>
						<url>{photo.url}</url>
						<imgroot>{photo.imgroot}</imgroot>
						<username>{photo.username}</username>
						<ra>{photo.RA}</ra>
						<dec>{photo.Dec}</dec>
						<orientation>{photo.orientation}</orientation>
					</photo>;
			if(photo.fov) {
				node.photo += <fov>
								<x>{photo.fov.x}</x>
								<y>{photo.fov.y}</y>
							</fov>;
			
			}
			for(i=0; i<photo.name.length; i++) {
				node.photo += <name>{photo.name[i]}</name>;
			}
			respdata.photos += node;
		}
		
		response.object = respdata;
		
		function parseAstrotags(data) {

			var tag; 
			var tmp;
			var photo;
			var id;
			var o = {};

			/* Walk through the returned data and build an associative array of
			photos, o, keyed on photo id. (yeah, I know JavaScript doesn't really use
			associative arrays).*/
			for each (var photo in data.results.photo) {
				id = photo.@['id'].toString();

				if (!o[id]) { 
					o[id] = {};
					o[id]['name'] = [];
					o[id]['id'] = id;
					o[id]['title']=photo.title.toString();
					o[id]['url'] = photo.urls.url.toString();
					o[id]['username'] = photo.owner.@['username'].toString();
					o[id]['imgroot'] = 'http://farm'+photo.@['farm'].toString()+'.static.flickr.com/'+photo.@['server'].toString()+'/'+id+'_'+photo.@['secret'].toString();
				}
				tag = photo.tags.tag.@['raw'].toString();
				//split machine tag name and value
				tmp = tag.split('=');
				//discard namespace from tag name by splitting tmp[0] on :
				// store data as o[predicate] = value eg. o[id].RA = 85.123456
				var tagname = tmp[0].split(':')[1];
				if (tagname == 'name') {
					o[id]['name'].push(tmp[1]);
				} else {
					o[id][tagname] = tmp[1];
				}
				//fieldsize is a string in degrees, arcminutes or arcseconds.
				//We will standardise on a value in degrees.
				if (tmp[0] == 'astro:fieldsize') {
					//convert fieldsize string into a numeric value in degrees, using the width of the photo.
					tmp = o[id].fieldsize.split(' ');
					o[id].fov = {};
					o[id].fov.x = parseFloat(tmp[0]);
					o[id].fov.y = parseFloat(tmp[2]);
					if (tmp[3] == 'arcminutes') {
						o[id].fov.x = o[id].fov.x/60;
						o[id].fov.y = o[id].fov.y/60;
					}
					if (tmp[3] == 'arcseconds') {
						o[id].fov.x = o[id].fov.x/3600;
						o[id].fov.y = o[id].fov.y/3600;
					}
				}
			}

			return o;
		}		
	 ]]></execute>
    </select>
  </bindings>
</table>
