source: trunk/FACT++/scripts/Observation_class.js@ 15325

Last change on this file since 15325 was 15288, checked in by tbretz, 12 years ago
Copy property id if available -- needed to read the schedule from the db.
File size: 2.9 KB
Line 
1'use strict';
2
3//
4// this file contains just the implementation of the
5// Observation class (I know there are no classes in javascript...)
6//
7
8function Observation(obj)
9{
10 if (typeof(obj)!='object')
11 throw new Error("Observation object can only be constructed using an object.");
12
13 if (!obj.date)
14 throw new Error("Observation object must have a 'date' parameter");
15
16 var ret = [];
17
18 // FIXME: Check transisiton from summer- and winter-time!!
19 var utc = new Date(obj.date);
20 if (isNaN(utc.valueOf()))
21 throw new Error(obj.date+' is not a valid Date String.'+
22 ' Try something like "2013-01-08 23:05 UTC" .');
23 ret.start = utc;
24 ret.id = obj.id;
25
26 // If the given data is not an array, make it the first entry of an array
27 // so that we can simply loop over all entries
28 if (obj.measurements.length===undefined)
29 {
30 var cpy = obj.measurements;
31 obj.measurements = [];
32 obj.measurements[0] = cpy;
33 }
34
35 for (var i=0; i<obj.measurements.length; i++)
36 {
37 var obs = obj.measurements[i];
38
39 ret[i] = { };
40 ret[i].task = obs.task ? obs.task.toUpperCase() : "DATA";
41 ret[i].source = obs.source;
42 ret[i].ra = parseFloat(obs.ra);
43 ret[i].dec = parseFloat(obs.dec);
44 ret[i].sub = i;
45 ret[i].start = utc;
46
47 ret[i].toString = function()
48 {
49 var rc = this.task;
50 rc += "["+this.sub+"]";
51 if (this.source)
52 rc += ": " + this.source;
53 //rc += " ["+this.start.toUTCString()+"]";
54 return rc;
55 }
56
57 switch (ret[i].task)
58 {
59 case 'DATA':
60 if (i!=obj.measurements.length-1)
61 throw new Error("DATA must be the last in the list of measurements");
62 if (ret[i].source == undefined)
63 throw new Error("Observation must have either 'source' or 'task' " +
64 "if 'task' == 'data' it must have also have 'source' ");
65 break;
66
67 case 'STARTUP':
68 if (ret[i].source != undefined)
69 console.out("warning. Observation with task='startup' also has source defined");
70 break;
71
72 case 'SHUTDOWN':
73 if (ret[i].source != undefined)
74 console.out("warning. Observation with task='shutdown' also has source defined");
75 break;
76
77 case 'RATESCAN':
78 if (ret[i].source == undefined && (ret[i].ra == undefined || ret[i].dec == undefined))
79 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
80 "if 'task' == 'ratescan'");
81 break;
82
83 default:
84 throw new Error(" the task of this observation:"+this.task+
85 "is not implemented. use one of: DATA, STARTUP, SHUTDOWN, RATESCAN.");
86 }
87 }
88
89 return ret;
90}
Note: See TracBrowser for help on using the repository browser.