source: branches/FACT++_lidctrl_new_eth/scripts/Observation_class.js@ 19904

Last change on this file since 19904 was 18714, checked in by tbretz, 8 years ago
Improved the partially wrong error messages, made them more consistent, added SUSPEND/RESUME
File size: 4.7 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
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].zd = parseFloat(obs.zd);
45 ret[i].az = parseFloat(obs.az);
46 ret[i].orbit = parseFloat(obs.orbit);
47 ret[i].angle = parseFloat(obs.angle);
48 ret[i].time = parseInt(obs.time);
49 ret[i].threshold = parseInt(obs.threshold);
50 ret[i].lidclosed = obs.lidclosed;
51 ret[i].biason = obs.biason;
52 ret[i].rstype = obs.rstype ? obs.rstype : "default";
53 ret[i].sub = i;
54 ret[i].start = utc;
55
56
57 ret[i].toString = function()
58 {
59 var rc = this.task;
60 rc += "["+this.sub+"]";
61 if (this.source)
62 rc += ": " + this.source;
63 //rc += " ["+this.start.toUTCString()+"]";
64 return rc;
65 }
66
67 switch (ret[i].task)
68 {
69 case 'DATA':
70 if (i!=obj.measurements.length-1)
71 throw new Error("Measurement DATA [n="+i+", "+utc.toUTCString()+"] must be the last in the list of measurements [cnt="+obj.measurements.length+"]");
72 if (ret[i].source == undefined)
73 throw new Error("Measurement DATA must have a source defined");
74 // This is obsolete. We cannot check everything which is not evaluated anyways
75 //if (ret[i].lidclosed == true)
76 // throw new Error("Observation must not have 'lidclosed'==true " +
77 // "if 'task'=='data'");
78 break;
79
80 case 'SUSPEND':
81 case 'RESUME':
82 if (obj.measurements.length!=1)
83 throw new Error("Measurement "+ret[i].task+" [n="+i+", "+utc.toUTCString()+"] must be the only measurements [cnt="+obj.measurements.length+"] in an observation");
84 break;
85
86 case 'STARTUP':
87 case 'SHUTDOWN':
88 if (ret[i].source != undefined)
89 console.out("WARNING - Measurement "+ret[i].task+" has a source defined");
90 break;
91
92 case 'RATESCAN':
93 if (ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
94 throw new Error("Measurement RATESCAN must have either a source or 'ra' & 'dec' defined");
95 // This is obsolete. We cannot check everything which is not evaluated anyways
96 //if (ret[i].lidclosed == true)
97 // throw new Error("Observation RATESCAN must not have 'lidclosed'==true");
98 break;
99
100 case 'RATESCAN2':
101 if ((ret[i].lidclosed != true) && ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
102 throw new Error("Measurement RATESCAN2 ('lidclosed'==false or undefined) must have either a source or 'ra' & 'dec' defined");
103 if (ret[i].lidclosed == true && (isNaN(ret[i].az) || isNaN(ret[i].az)))
104 throw new Error("Measurement RATESCAN2 ('lidclosed'==true) must have 'zd' & 'az' defined");
105 break;
106
107 case 'CUSTOM':
108 if (isNaN(ret[i].az) || isNaN(ret[i].az) || isNaN(ret[i].time) || isNaN(ret[i].threshold))
109 throw new Error("Measurement CUSTOM must have 'zd' & 'az', 'time' and 'threshold' defined.");
110 break;
111
112 case 'SINGLEPE':
113 case 'OVTEST':
114 case 'DRSCALIB':
115 case 'IDLE':
116 case 'SLEEP':
117 break;
118
119 default:
120 throw new Error("The measurement type "+ret[i].task+" is unknown.");
121 }
122 }
123
124 return ret;
125}
Note: See TracBrowser for help on using the repository browser.