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

Last change on this file since 18066 was 18046, checked in by Daniela Dorner, 10 years ago
added checks for new options, fixed bug in ra/dec check
File size: 4.1 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].lidclosed = obs.lidclosed;
46 ret[i].rstype = obs.rstype ? obs.rstype : "default";
47 ret[i].sub = i;
48 ret[i].start = utc;
49
50 ret[i].toString = function()
51 {
52 var rc = this.task;
53 rc += "["+this.sub+"]";
54 if (this.source)
55 rc += ": " + this.source;
56 //rc += " ["+this.start.toUTCString()+"]";
57 return rc;
58 }
59
60 switch (ret[i].task)
61 {
62 case 'DATA':
63 if (i!=obj.measurements.length-1)
64 throw new Error("DATA [n="+i+", "+utc.toUTCString()+"] must be the last in the list of measurements [cnt="+obj.measurements.length+"]");
65 if (ret[i].source == undefined)
66 throw new Error("Observation must have either 'source' or 'task' " +
67 "if 'task' == 'data' it must have also have 'source' ");
68 if (ret[i].lidclosed == true)
69 throw new Error("Observation must not have 'lidclosed'== true " +
70 "if 'task' == 'data' ");
71 break;
72
73 case 'STARTUP':
74 if (ret[i].source != undefined)
75 console.out("warning. Observation with task='startup' also has source defined");
76 break;
77
78 case 'SHUTDOWN':
79 if (ret[i].source != undefined)
80 console.out("warning. Observation with task='shutdown' also has source defined");
81 break;
82
83 case 'RATESCAN':
84 if (ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
85 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
86 "if 'task' == 'ratescan'");
87 if (ret[i].lidclosed == true)
88 throw new Error("Observation must not have 'lidclosed'== true " +
89 "if 'task' == 'ratescan' ");
90 break;
91
92 case 'RATESCAN2':
93 if ((ret[i].lidclosed != true) && ret[i].source == undefined && (isNaN(ret[i].ra) || isNaN(ret[i].dec)))
94 throw new Error("Observation must have either 'source' or 'ra' & 'dec' " +
95 "if 'task' == 'ratescan2' and lidclosed==false or not given");
96 if (ret[i].lidclosed == true && (isNaN(ret[i].az) || isNaN(ret[i].az)))
97 throw new Error("Observation must have 'zd' & 'az' " +
98 "if 'task' == 'ratescan2' and option 'lidclosed'=='true'");
99 break;
100
101 case 'SINGLEPE':
102 case 'OVTEST':
103 case 'DRSCALIB':
104 case 'IDLE':
105 break;
106
107 default:
108 throw new Error("The observation type "+ret[i].task+" is unknown.");
109 }
110 }
111
112 return ret;
113}
Note: See TracBrowser for help on using the repository browser.