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

Last change on this file since 16991 was 16074, checked in by tbretz, 12 years ago
Allow for observation types singlepe, drscalib and idle.
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 = obj.date.toString().toUpperCase()=="NOW" ? new Date() : new Date(obj.date);
20 if (isNaN(utc.valueOf()))
21 throw new Error('"'+obj.date+'" not a valid Date... try something like "2013-01-08 23:05 UTC".');
22 ret.start = utc;
23 ret.id = obj.id;
24
25 // If the given data is not an array, make it the first entry of an array
26 // so that we can simply loop over all entries
27 if (obj.measurements.length===undefined)
28 {
29 var cpy = obj.measurements;
30 obj.measurements = [];
31 obj.measurements[0] = cpy;
32 }
33
34 for (var i=0; i<obj.measurements.length; i++)
35 {
36 var obs = obj.measurements[i];
37
38 ret[i] = { };
39 ret[i].task = obs.task ? obs.task.toUpperCase() : "DATA";
40 ret[i].source = obs.source;
41 ret[i].ra = parseFloat(obs.ra);
42 ret[i].dec = parseFloat(obs.dec);
43 ret[i].sub = i;
44 ret[i].start = utc;
45
46 ret[i].toString = function()
47 {
48 var rc = this.task;
49 rc += "["+this.sub+"]";
50 if (this.source)
51 rc += ": " + this.source;
52 //rc += " ["+this.start.toUTCString()+"]";
53 return rc;
54 }
55
56 switch (ret[i].task)
57 {
58 case 'DATA':
59 if (i!=obj.measurements.length-1)
60 throw new Error("DATA must be the last in the list of measurements");
61 if (ret[i].source == undefined)
62 throw new Error("Observation must have either 'source' or 'task' " +
63 "if 'task' == 'data' it must have also have 'source' ");
64 break;
65
66 case 'STARTUP':
67 if (ret[i].source != undefined)
68 console.out("warning. Observation with task='startup' also has source defined");
69 break;
70
71 case 'SHUTDOWN':
72 if (ret[i].source != undefined)
73 console.out("warning. Observation with task='shutdown' also has source defined");
74 break;
75
76 case 'RATESCAN':
77 if (ret[i].source == undefined && (ret[i].ra == undefined || ret[i].dec == undefined))
78 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
79 "if 'task' == 'ratescan'");
80 break;
81
82 case 'SINGLEPE':
83 case 'DRSCALIB':
84 case 'IDLE':
85 break;
86
87 default:
88 throw new Error("The observation type "+ret[i].task+" is unknown.");
89 }
90 }
91
92 return ret;
93}
Note: See TracBrowser for help on using the repository browser.