source: branches/testFACT++branch/scripts/Observation_class.js@ 18788

Last change on this file since 18788 was 18172, checked in by dneise, 10 years ago
Also this file has been changed by Daniela Dorner in February, while introducing the new CUSTOM run feature. It has been tested now since quite a while, so I take the liberty to check it in.
File size: 4.6 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].zd = parseFloat(obs.zd);
44 ret[i].az = parseFloat(obs.az);
45 ret[i].time = parseInt(obs.time);
46 ret[i].threshold = parseInt(obs.threshold);
47 ret[i].lidclosed = obs.lidclosed;
48 ret[i].biason = obs.biason;
49 ret[i].rstype = obs.rstype ? obs.rstype : "default";
50 ret[i].sub = i;
51 ret[i].start = utc;
52
53 ret[i].toString = function()
54 {
55 var rc = this.task;
56 rc += "["+this.sub+"]";
57 if (this.source)
58 rc += ": " + this.source;
59 //rc += " ["+this.start.toUTCString()+"]";
60 return rc;
61 }
62
63 switch (ret[i].task)
64 {
65 case 'DATA':
66 if (i!=obj.measurements.length-1)
67 throw new Error("DATA [n="+i+", "+utc.toUTCString()+"] must be the last in the list of measurements [cnt="+obj.measurements.length+"]");
68 if (ret[i].source == undefined)
69 throw new Error("Observation must have either 'source' or 'task' " +
70 "if 'task' == 'data' it must have also have 'source' ");
71 if (ret[i].lidclosed == true)
72 throw new Error("Observation must not have 'lidclosed'== true " +
73 "if 'task' == 'data' ");
74 break;
75
76 case 'STARTUP':
77 if (ret[i].source != undefined)
78 console.out("warning. Observation with task='startup' also has source defined");
79 break;
80
81 case 'SHUTDOWN':
82 if (ret[i].source != undefined)
83 console.out("warning. Observation with task='shutdown' also has source defined");
84 break;
85
86 case 'RATESCAN':
87 if (ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
88 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
89 "if 'task' == 'ratescan'");
90 if (ret[i].lidclosed == true)
91 throw new Error("Observation must not have 'lidclosed'== true " +
92 "if 'task' == 'ratescan' ");
93 break;
94
95 case 'RATESCAN2':
96 if ((ret[i].lidclosed != true) && ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
97 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
98 "if 'task' == 'ratescan2' and lidclosed==false or not given");
99 if (ret[i].lidclosed == true && (isNaN(ret[i].az) || isNaN(ret[i].az)))
100 throw new Error("Observation must have 'zd' & 'az' " +
101 "if 'task' == 'ratescan2' and option 'lidclosed'=='true'");
102 break;
103
104 case 'CUSTOM':
105
106 if (isNaN(ret[i].az) || isNaN(ret[i].az) || isNaN(ret[i].time) || isNaN(ret[i].threshold))
107 throw new Error("Observation must have 'zd' & 'az', 'time' and 'threshold' " +
108 "if 'task' == 'custom' ");
109 break;
110
111 case 'SINGLEPE':
112 case 'OVTEST':
113 case 'DRSCALIB':
114 case 'IDLE':
115 case 'SLEEP':
116 break;
117
118 default:
119 throw new Error("The observation type "+ret[i].task+" is unknown.");
120 }
121 }
122
123 return ret;
124}
Note: See TracBrowser for help on using the repository browser.