Skip to content

Instantly share code, notes, and snippets.

@lukecampbell
Created February 9, 2012 18:08
Show Gist options
  • Save lukecampbell/1781675 to your computer and use it in GitHub Desktop.
Save lukecampbell/1781675 to your computer and use it in GitHub Desktop.
Utility
This is a utility gist, containing utility scripts
/* Author: Luke Campbell
* Description: Aggregation map (in map/reduce) for granules
*/
/* Structure
{
"name" : "field name",
"type" : "field type",
}
*/
function traverse(doc) {
for(var i in doc.identifiables)
{
var identifiable = doc.identifiables[i];
emit([doc.stream_resource_id, doc.identifiables.time_bounds.value_pair[0], i], [i,identifiable]);
}
}
function (doc) {
if(doc.type_=="StreamGranuleContainer")
{
traverse(doc);
}
}
/*
* Author: Luke Campbell
* Description: Reduction and merging of granules in JS
*/
function print_dic(obj) {
var s = "\n{\n";
for(var key in obj)
{
s+='\t';
s+=key;
s+= " : "
s+= obj[key];
s+= ",\n"
}
s += "}\n";
debug(s);
}
function DumpObject(obj)
{
var od = new Object;
var result = "";
var len = 0;
for (var property in obj)
{
var value = obj[property];
if (typeof value == 'string')
value = "'" + value + "'";
else if (typeof value == 'object')
{
if (value instanceof Array)
{
value = "[ " + value + " ]";
}
else
{
var ood = DumpObject(value);
value = "{ " + ood.dump + " }";
}
}
result += "'" + property + "' : " + value + ", \n";
len++;
}
od.dump = result;
od.len = len;
return od;
}
function get_keys(obj){
var keys = [];
for(var key in obj)
keys.push(key);
return keys;
}
function value_in(value,ary) {
for(var i in ary)
{
if(value == ary[i])
return i;
}
}
function merge_record_count(c1,c2) {
var retval = {
"definition": "",
"reference_frame": "",
"description": "",
"quality_id": "",
"axisID": "",
"constraint": merge_constraint(c1.constraint, c2.constraint),
"value": c1.value + c2.value,
"updatable": (c1.updatable && c2.updatable),
"type_": "CountElement",
"nil_value_ids": c1.nil_value_ids.concat(c2.nil_value_ids),
"label": "",
"optional": (c1.optional && c2.optional),
"id": ""
}
return retval;
}
function merge_constraint(c1, c2) {
var sig_figures = Math.max(c1.significant_figures, c2.significant_figures);
var type_ = "AllowedValues";
var intervals = [];
var values = [];
values = values.concat(c1.values, c2.values);
intervals = intervals.concat(c1.intervals, c2.intervals);
var retval = {
"significant_figures": sig_figures,
"type_": "AllowedValues",
"intervals": intervals,
"values": values
};
return retval;
}
function merge_mesh_location(c1,c2) {
var retval = {
"definition": "",
"reference_frame": "",
"description": "",
"quality_id": "",
"axisID": "",
"constraint": merge_constraint(c1.constraint, c2.constraint),
"code_space": "",
"value": "",
"updatable": (c1.updatable && c2.updatable),
"type_": "CategoryElement",
"nil_value_ids": c1.nil_value_ids.concat(c2.nil_value_ids),
"label": "",
"optional": (c1.optional && c2.optional),
"id": ""
};
return retval;
}
function merge_range_set(c1,c2) {
var retval = {
"definition": "",
"reference_frame": "",
"nil_values_ids": [ ],
"unit_of_measure": {
"type_": "UnitReferenceProperty",
"code": "",
"reference": ""
},
"description": "",
"quality_id": "",
"mesh_location": merge_mesh_location(c1.mesh_location, c2.mesh_location),
"constraint": merge_constraint(c1.constraint, c2.constraint),
"reference_value": "",
"updatable": (c1.updatable && c2.updatable),
"type_": "RangeSet",
"bounds_id": "conductivity_bounds",
"label": "",
"optional": (c1.optional && c2.optional),
"id": "",
"values_path": ""
};
return retval;
}
function merge_data_stream(c1,c2) {
var retval = {
"description": "",
"element_type_id": "",
"element_count_id": "",
"type_": "DataStream",
"label": "",
"encoding_id": "",
"values": "",
"id": ""
};
return retval;
}
function merge_unit_reference_property(c1,c2) {
var retval = {
"type_": "UnitReferenceProperty",
"code": "",
"reference": ""
};
return retval;
}
function merge_axis(c1,c2) {
var retval = {
"definition": "",
"reference_frame": "",
"description": "",
"quality_id": "",
"axisID": "",
"constraint": merge_constraint(c1.constraint, c2.constraint),
"code_space": "",
"value": "",
"updatable": (c1.updatable && c2.updatable),
"type_": "CategoryElement",
"nil_value_ids": c1.nil_value_ids.concat(c2.nil_value_ids),
"label": "",
"optional": (c1.optional && c2.optional),
"id": ""
};
return retval;
}
function merge_coordinate_axis(c1,c2) {
var retval = {
"definition": "",
"reference_frame": "",
"nil_values_ids": [ ],
"unit_of_measure": merge_unit_reference_property(c1.unit_of_measure, c2.unit_of_measure),
"description": "",
"quality_id": "",
"axis": merge_axis(c1.axis, c2.axis),
"mesh_location": merge_mesh_location(c1.mesh_location, c2.mesh_location),
"constraint": merge_constraint(c1.constraint, c2.constraint),
"reference_value": "",
"updatable": (c1.updatable && c2.updatable),
"type_": "CoordinateAxis",
"bounds_id": c1.bounds_id,
"label": "",
"optional": (c1.optional && c2.optional),
"id": "",
"values_path": ""
};
}
function merge_quantity_range_element(c1,c2) {
var value_pair = [
Math.min(c1.value_pair[0], c2.value_pair[0]),
Math.max(c1.value_pair[1], c2.value_pair[1])
];
debug("here");
var constraint = merge_constraint(c1.constraint, c2.constraint);
var nil_value_ids = c1.nil_value_ids.concat(c2.nil_value_ids);
var updatable = c1.updatable && c2.updatable;
var optional = c1.optional && c2.optional;
var retval = {
"definition": "",
"reference_frame": "",
"unit_of_measure": {
"type_": "UnitReferenceProperty",
"code": "",
"reference": ""
},
"description": "",
"quality_id": "",
"axisID": "",
"constraint": constraint,
"updatable": updatable,
"type_": "QuantityRangeElement",
"value_pair": value_pair,
"nil_value_ids": nil_value_ids,
"label": "",
"optional": optional,
"id": ""
};
return retval;
}
function merge(c1,c2) {
if(c1.type_ != c2.type_)
return null;
if(c1.type_ == "QuantityRangeElement")
{
return merge_quantity_range_element(c1,c2);
}
if(c1.type_ == "CoordinateAxis")
{
return merge_coordinate_axis(c1,c2);
}
if(c1.type_ == "CategoryElement")
{
return merge_axis(c1,c2);
}
if(c1.type_ == "DataStream")
{
return merge_data_stream(c1,c2);
}
if(c1.type_ == "RangeSet")
{
return merge_range_set(c1,c2);
}
if(c1.type_ == "CountElement")
{
return merge_record_count(c1,c2);
}
}
function reduce(keys,values,rereduce)
{
var master = {};
if(!rereduce)
{
var key_ring = [];
var master = {};
for(var i in values)
{
var key = values[i][0];
var value = values[i][1];
if(!value_in(key,key_ring))
{
key_ring.push(key);
master[key] = value;
}
else
{
master[key] = merge(master[key], value);
}
}
return master;
}
else
{
var key_ring = get_keys(values[0]);
var master = values[0];
for(var i in values)
{
var value = values[i];
if(i==0)
continue;
var keys = get_keys(value);
for(j in keys)
{
var key = keys[j];
if(!value_in(key,keys))
{
key_ring.push(key);
master[key] = value;
}
else
{
master[key] = merge(master[key],value)
}
}
}
return master;
}
return master;
}
var bull =
{"total_rows":34,"offset":0,"rows":[
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"conductivity_bounds"],"value":["conductivity_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[2,2],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"conductivity_data"],"value":["conductivity_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"conductivity_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"data_stream"],"value":["data_stream",{"description":"","element_type_id":"","element_count_id":"","type_":"DataStream","label":"","encoding_id":"","values":"","id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"height_bounds"],"value":["height_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"height_data"],"value":["height_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"height_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"latitude_bounds"],"value":["latitude_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"latitude_data"],"value":["latitude_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"latitude_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"longitude_bounds"],"value":["longitude_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"longitude_data"],"value":["longitude_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"longitude_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"pressure_bounds"],"value":["pressure_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[1,1],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"pressure_data"],"value":["pressure_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"pressure_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"record_count"],"value":["record_count",{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"value":7,"updatable":true,"type_":"CountElement","nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"stream_encoding"],"value":["stream_encoding",{"sha1":"67163547351897233CF5C4E76DDD8AF051250BDC","compression":null,"encoding_type":"hdf5","type_":"Encoding","record_dimension":"","label":"","id":"","description":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"temperature_bounds"],"value":["temperature_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[48.658104161804005,51.372301018019606],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"temperature_data"],"value":["temperature_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"temperature_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"time_bounds"],"value":["time_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[1,7],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"0f1b2896bd2448b88ccc6f2af077c712","key":["new",1,"time_data"],"value":["time_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"time_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"conductivity_bounds"],"value":["conductivity_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[2,2],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"conductivity_data"],"value":["conductivity_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"conductivity_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"data_stream"],"value":["data_stream",{"description":"","element_type_id":"","element_count_id":"","type_":"DataStream","label":"","encoding_id":"","values":"","id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"height_bounds"],"value":["height_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"height_data"],"value":["height_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"height_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"latitude_bounds"],"value":["latitude_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"latitude_data"],"value":["latitude_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"latitude_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"longitude_bounds"],"value":["longitude_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[0,0],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"longitude_data"],"value":["longitude_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"longitude_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"pressure_bounds"],"value":["pressure_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[1,1],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"pressure_data"],"value":["pressure_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"pressure_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"record_count"],"value":["record_count",{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"value":5,"updatable":true,"type_":"CountElement","nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"stream_encoding"],"value":["stream_encoding",{"sha1":"93A96C63984B684D6F69A1C5CCE9219C4ABD0402","compression":null,"encoding_type":"hdf5","type_":"Encoding","record_dimension":"","label":"","id":"","description":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"temperature_bounds"],"value":["temperature_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[43.42495730636228,57.36993605256888],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"temperature_data"],"value":["temperature_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"RangeSet","bounds_id":"temperature_bounds","label":"","optional":false,"id":"","values_path":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"time_bounds"],"value":["time_bounds",{"definition":"","reference_frame":"","unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axisID":"","constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"updatable":true,"type_":"QuantityRangeElement","value_pair":[8,12],"nil_value_ids":[],"label":"","optional":false,"id":""}]},
{"id":"43ffc22f29294d9e969c70abea2d2787","key":["new",8,"time_data"],"value":["time_data",{"definition":"","reference_frame":"","nil_values_ids":[],"unit_of_measure":{"type_":"UnitReferenceProperty","code":"","reference":""},"description":"","quality_id":"","axis":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"mesh_location":{"definition":"","reference_frame":"","description":"","quality_id":"","axisID":"","constraint":{"pattern":"","type_":"AllowedTokens","values":[]},"code_space":"","value":"","updatable":true,"type_":"CategoryElement","nil_value_ids":[],"label":"","optional":false,"id":""},"constraint":{"significant_figures":-1,"type_":"AllowedValues","intervals":[],"values":[]},"reference_value":"","updatable":true,"type_":"CoordinateAxis","bounds_id":"time_bounds","label":"","optional":false,"id":"","values_path":""}]}
]};
var key_ring = [];
var master = {};
for(var row in bull.rows)
{
var key = bull.rows[row]["value"][0];
var value = bull.rows[row]["value"][1];
if(!value_in(key,key_ring))
{
key_ring.push(key);
debug("Pushing " + key);
master[key] = value;
}
else
{
master[key] = merge(master[key], value);
}
}
debug(DumpObject(master).dump);
debug("No syntax problems");
quit();
/* Bulk Min */
function(keys, values, rereduce) {
var min_lat = 0.0;
var min_lon = 0.0;
var min_time = "";
var min_depth = 0.0;
var max_lat=0.0;
var max_lon=0.0;
var max_time="";
var max_depth=0.0;
if(! rereduce)
{
for(var i in values) {
if(values[i].lat > max_lat)
max_lat = values[i].lat;
if(values[i].lon > max_lon)
max_lon = values[i].lon;
if(values[i].time.localeCompare(max_time) > 0)
max_time = values[i].time;
if(values[i].depth > max_depth)
max_depth = values[i].depth;
if(min_lat == 0.0)
min_lat = values[i].lat;
if(min_lon == 0.0)
min_lon = values[i].lon;
if(min_time.localeCompare("")==0)
min_time = values[i].time;
if(min_depth==0.0)
min_depth = values[i].depth;
if(values[i].lat < min_lat)
min_lat = values[i].lat;
if(values[i].lon < min_lon)
min_lon = values[i].lon;
if(values[i].time.localeCompare(min_time) < 0)
min_time = values[i].time;
if(values[i].depth < min_depth)
min_depth = values[i].depth;
}
return {"min_lat":min_lat, "min_lon":min_lon, "min_time":min_time, "min_depth":min_depth,
"max_lat":max_lat, "max_lon":max_lon, "max_time":max_time, "max_depth":max_depth};
}
for(var i in values) {
if(values[i].max_lat > max_lat)
max_lat = values[i].max_lat;
if(values[i].max_lon > max_lon)
max_lon = values[i].max_lon;
if(values[i].max_time.localeCompare(max_time) > 0)
max_time = values[i].max_time;
if(values[i].max_depth > max_depth)
max_depth = values[i].max_depth;
if(min_lat == 0.0)
min_lat = values[i].min_lat;
if(min_lon == 0.0)
min_lon = values[i].min_lon;
if(min_time.localeCompare("")==0)
min_time = values[i].min_time;
if(min_depth==0.0)
min_depth = values[i].min_depth;
if(values[i].min_lat < min_lat)
min_lat = values[i].min_lat;
if(values[i].min_lon < min_lon)
min_lon = values[i].min_lon;
if(values[i].min_time.localeCompare(min_time) < 0)
min_time = values[i].min_time;
if(values[i].min_depth < min_depth)
min_depth = values[i].min_depth;
}
return {"min_lat":min_lat, "min_lon":min_lon, "min_time":min_time, "min_depth":min_depth,
"max_lat":max_lat, "max_lon":max_lon, "max_time":max_time, "max_depth":max_depth};
}
/********************************
* Author: Luke Campbell
* Description: simple map to order by time
********************************/
// If the doc has a time_bounds display it
function get_time(doc) {
if(doc.identifiables.time_bounds) {
emit([doc.stream_resource_id,doc.identifiables.time_bounds.value_pair[0]],doc._id);
}
}
function(doc) {
get_time(doc);
}
/*
* Author: Luke Campbell <lcampbell@asascience.com>
* Description: Utility and map functions to map the DataContainer's bounding elements.
*/
/*
* Traverses an "identifiable" in a document to see if it contains a CoordinateAxis
*/
function traverse(identifiable) {
if('type_' in identifiable)
{
if(identifiable.type_=="CoordinateAxis")
{
return identifiable.bounds_id;
}
else return null;
}
}
/*
* Gets the CoordinateAxis objects and their bounds_ids
*/
function get_bounds(doc)
{
identifiables = doc.identifiables;
var bounds = [];
for(var i in identifiables)
{
var bounds_id = traverse(identifiables[i]);
if(bounds_id)
bounds.push(bounds_id);
}
return bounds;
}
/* Data map */
function (doc) {
if(doc.type_ == "DataContainer"){
var bounds = get_bounds(doc);
for(var b in bounds)
{
var key = bounds[b];
var s = String(key)
var pack = {};
pack[key] = doc.identifiables[key].value_pair;
emit(doc._id, pack);
}
}
}
{
"_id": "_design/test",
"_rev": "18-620dbc17196be2d20a5b7dae7f2cbec6",
"language": "javascript",
"views": {
"dataset_by_latlong": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = {\"lat\":doc.lattitude, \"long\":doc.longitude, \"lat_h\":doc.lattitude_hemisphere, \"long_h\":doc.longitude_hemisphere};\n emit(pack,doc._id);\n }\n}"
},
"dataset_by_lat": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.lattitude_hemisphere,doc.lattitude];\n emit(pack,doc._id);\n }\n}"
},
"dataset_by_long": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.longitude_hemisphere,doc.longitude];\n emit(pack,doc._id);\n }\n}"
},
"lat_max": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.lattitude_hemisphere,doc.lattitude];\n emit(pack,doc.lattitude);\n }\n}",
"reduce": "\nfunction (keys,values,rereduce) {\n var max = 0.0;\n for(var i in values) {\n if(values[i] > max)\n max = values[i];\n }\n return max;\n \n}\n"
},
"longitude_max": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.longitude_hemisphere,doc.longitude];\n emit(pack,doc.longitude);\n }\n}",
"reduce": "function (keys,values,rereduce) {\n var max = 0.0;\n for(var i in values) {\n if(values[i] > max)\n max = values[i];\n }\n return max;\n \n}\n"
},
"longitude_min": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.longitude_hemisphere,doc.longitude];\n emit(pack,doc.longitude);\n }\n}",
"reduce": "function (keys,values,rereduce) {\n var min = 0.0;\n for(var i in values) {\n if(min==0.0)\n min = values[i];\n else if(values[i] < min)\n min = values[i];\n }\n return min;\n \n}\n"
},
"lattitude_max": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.lattitude_hemisphere,doc.lattitude];\n emit(pack,doc.lattitude);\n }\n}",
"reduce": "\nfunction (keys,values,rereduce) {\n var max = 0.0;\n for(var i in values) {\n if(values[i] > max)\n max = values[i];\n }\n return max;\n \n}\n"
},
"lattitude_min": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n var pack = [doc.lattitude_hemisphere,doc.lattitude];\n emit(pack,doc.lattitude);\n }\n}",
"reduce": "function (keys,values,rereduce) {\n var min = 0.0;\n for(var i in values) {\n if(min==0.0)\n min = values[i];\n else if(values[i] < min)\n min = values[i];\n }\n return min;\n \n}\n"
},
"dataset_by_depth": {
"map": "function(doc) {\n if(doc.type_ == \"SciData\") {\n\temit(doc.depth, doc._id); \n }\n}"
}
}
}
#!/usr/bin/python
import urllib
"""
gausian
"""
def gaussian(num,mean,stdev):
params = urllib.urlencode({
'num':num,
'mean':mean,
'stdev':stdev,
'dec':10,
'col':1,
'notation':'scientific',
'format':'plain',
'rnd':'new'
})
url = "http://www.random.org/gaussian-distributions/?"
f = urllib.urlopen(url + params)
s = f.read()
return map(float,s.split())
from prototype.sci_data.ctd_stream import *
from pyon.datastore.datastore import DataStore
import random
import time
db = cc.datastore_manager.get_datastore('test',DataStore.DS_PROFILE.SCIDATA)
def rands(entropy):
random_pressures = [(random.random()*100) for i in xrange(entropy)]
random_salinity = [(random.random()*28) for i in xrange(entropy)]
random_temperature = [(random.random()*10)+32 for i in xrange(entropy)]
random_times = [random.randrange(1328205227, 1328896395) for i in xrange(entropy)]
random_lat = [(random.random()*10)+30 for i in xrange(entropy)]
random_lon = [(random.random()*10)+70 for i in xrange(entropy)]
return [random_pressures, random_salinity, random_temperature, random_times, random_lat, random_lon]
def generate_point(entropy=5):
points = []
random_values = rands(entropy)
point = ctd_stream_packet(stream_id='ctd_data', p=random_values[0], c=random_values[1], t=random_values[2],time=random_values[3], lat=random_values[4], lon=random_values[5], create_hdf=False)
return point
for i in xrange(1000):
point = generate_point()
print point.identifiables['temp_bounds'].value_pair
print db.create(point)
# coding: utf-8
import os
def notify_growl(notification, app_title, msg_title, description):
s = """
osascript -e '
tell application "System Events"
set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell
if isRunning then
tell application id "com.Growl.GrowlHelperApp"
set the allNotificationsList to {"%(notification)s"}
set the enabledNotificationsList to {"%(notification)s"}
register as application "%(app_title)s" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "OOICI"
notify with name "%(notification)s" title "%(msg_title)s" description "%(description)s" application name "%(app_title)s"
end tell
end if
'
"""
os.system(s % {'notification':notification, 'app_title':app_title, 'msg_title':msg_title, 'description':description})
notify_growl('Testing Complete', 'Lukes App', 'Test', 'Your Tests Are COmplete')
function (doc) {
if(doc.type_ == "StreamDefinitionContainer")
emit([doc.stream_resource_id,0],doc._id);
else if(doc.type_ == "StreamGranuleContainer")
emit([doc.stream_resource_id,1], doc._id);
}
/*
* Author: Luke Campbell <lcampbell@asascience.com>
* Description: Utility and map functions to map the DataContainer's bounding elements.
*/
/*
* Traverses an "identifiable" in a document to see if it contains a CoordinateAxis
*/
function traverse(identifiable) {
if(identifiable.type_=="CoordinateAxis")
{
return identifiable.bounds_id;
}
else return null;
}
/*
* Gets the CoordinateAxis objects and their bounds_ids
*/
function get_bounds(doc)
{
identifiables = doc.identifiables;
var bounds = [];
for(var i in identifiables)
{
var bounds_id = traverse(identifiables[i]);
if(bounds_id)
bounds.push(bounds_id);
}
return bounds;
}
/* Data map */
function (doc) {
if(doc.type_ == "StreamGranuleContainer"){
var bounds = get_bounds(doc);
for(var b in bounds)
{
var key = bounds[b];
var s = String(key)
var pack = {};
pack[key] = doc.identifiables[key].value_pair;
emit([doc.stream_resource_id,1], pack);
}
}
}
var values = [{"latitude_bounds": [32.82749434232304, 35.88136580695911]},
{"longitude_bounds": [72.13537707453355, 79.47096912220914]},
{"pressure_bounds": [23.639431356878404, 99.0065973822419]},
{"time_bounds": [1328335674, 1328815194]},
{"latitude_bounds": [30.56511956376543, 39.8668534952689]},
{"longitude_bounds": [70.08879029021078, 79.83677131241231]},
{"pressure_bounds": [5.803856260555584, 92.48816689195833]},
{"time_bounds": [1328279085, 1328813844]},
{"latitude_bounds": [30.73539817388742, 39.964984394042006]},
{"longitude_bounds": [72.08876296429352, 79.5133985816489]},
{"pressure_bounds": [9.221997093799816, 79.76757195734481]},
{"time_bounds": [1328248954, 1328759449]},
{"latitude_bounds": [30.166858369808082, 38.25430998669332]},
{"longitude_bounds": [71.85294514716199, 77.16380594509812]},
{"pressure_bounds": [29.99730989462185, 82.81372002248642]},
{"time_bounds": [1328512630, 1328813665]},
{"latitude_bounds": [31.343091015925914, 38.59620338300263]},
{"longitude_bounds": [74.0292244624546, 78.68686066191046]},
{"pressure_bounds": [13.605053941661804, 93.92189694912912]},
{"time_bounds": [1328256830, 1328683615]},
{"latitude_bounds": [34.35376474823513, 39.957527786854286]},
{"longitude_bounds": [71.16988257909982, 78.00292358437143]},
{"pressure_bounds": [30.01808225812428, 94.13311797282411]},
{"time_bounds": [1328507186, 1328802093]},
{"latitude_bounds": [30.69232497126897, 39.576248953469886]},
{"longitude_bounds": [74.10886557497031, 79.8752728552944]},
{"pressure_bounds": [22.921413253511467, 95.40074981825215]},
{"time_bounds": [1328248168, 1328848298]},
{"latitude_bounds": [30.582360856184543, 37.836764159500596]},
{"longitude_bounds": [70.3273647975362, 79.93160201052886]},
{"pressure_bounds": [0.09340316296984064, 81.12757748904214]},
{"time_bounds": [1328226950, 1328741612]},
{"latitude_bounds": [31.453810773991492, 39.908149501559734]},
{"longitude_bounds": [70.6640187985041, 78.99301324789279]},
{"pressure_bounds": [6.774143595815407, 96.10012982587001]},
{"time_bounds": [0, 1328750003]},
{"latitude_bounds": [32.92028042679569, 38.671872613892354]},
{"longitude_bounds": [71.35892349393315, 79.1408830457507]},
{"pressure_bounds": [9.090840136197375, 78.85173290467694]},
{"time_bounds": [1328519568, 1328885354]}]
function value_in(value,ary) {
for(var i in ary)
{
if(value == ary[i])
return i;
}
}
function get_keys(obj){
var keys = [];
for(var key in obj)
keys.push(key);
return keys;
}
function print_dic(obj) {
for(var key in obj)
{
debug(key);
debug(obj[key]);
}
}
function reduce(keys,values,rereduce) {
var res_keys = [];
var results = values[0];
/* Not a rereduce so populate the results dictionary */
for(var i in values)
{
var keys = get_keys(values[i]);
for(var j in keys)
{
var key = keys[j];
if(! value_in(key, res_keys))
{
res_keys.push(key);
results[key] = values[i][key];
continue;
}
var value = values[i][key];
results[key][0] = Math.min(value[0], results[key][0]);
results[key][1] = Math.max(value[1], results[key][1]);
}
}
return results;
}
var result = reduce(null,values,true);
print_dic(result);
quit();
function (keys,values,rereduce) {
var max_lat=0.0;
var max_lon=0.0;
var max_time="";
var max_depth=0.0;
for(var i in values){
if(values[i].lat > max_lat)
max_lat = values[i].lat;
if(values[i].lon > max_lon)
max_lon = values[i].lon;
if(values[i].time.localeCompare(max_time) > 0)
max_time = values[i].time;
if(values[i].depth > max_depth)
max_depth = values[i].depth;
}
return {"lat":max_lat,"lon":max_lon,"time":max_time,"depth":max_depth};
}
function (keys,values,rereduce) {
var min = 0.0;
for(var i in values) {
if(min==0.0)
min = values[i];
else if(values[i].localeCompare(min) < 0)
min = values[i];
}
return min;
}
class SciData(dict):
def __init__(self, *args, **kwargs):
for k,v in kwargs.iteritems():
self[k] = v
self['type_'] = "SciData"
def __setattr__(self, key, value):
self[key] = value
def __getattr__(self, key):
"""docstring for __getattr__"""
return self[key]
def generate_points(number):
import random
import time
random_temps = list(random.normalvariate(48.0,8) for x in xrange(80))
points = []
for d in xrange(number):
sci_point = {
"type_": "SciData",
"temp": random.normalvariate(48.0,8),
"depth": ((random.random() * 20) + 50),
"origin_id": 1,
"area": 1,
"latitude": ((random.random() * 10)+30),
"longitude": ((random.random() * 10)+70),
"latitude_hemisphere": "N",
"longitude_hemisphere": "W",
"latitude_precision": 8,
"longitude_precision": 8,
"time": time.strftime("%Y-%m-%dT%H:%M:%S-05"),
"submitting_entity": "OOICI-DM",
"instutition": "OOICI",
"organization": "ASA",
"platform": None,
"depth_units": "ft"
}
points.append(sci_point)
return points
if __name__ == "__main__":
import pprint
import couchdb
from sets import Set
lat_set = Set()
long_set = Set()
pp = pprint.PrettyPrinter()
printf=pp.pprint
couch = couchdb.Server()
db = couch['ion_lukes-asa-macbook_local_scidata']
for point in generate_points(10):
db.create(point)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment