Source: public/javascript/modules/serverApi.js

  1. import { DOMAccessor } from "./domelements.js";
  2. import { FITS_HEADER } from './fitsheader.js';
  3. import { URL_SPECTRO_SERVER } from './init.js';
  4. /**
  5. * Object querying the spectroscopy service
  6. */
  7. class SpectroApi {
  8. constructor() {
  9. // service endpoint
  10. this.server = URL_SPECTRO_SERVER;
  11. // will contain result of getMetadata for caching
  12. this.metadata = null;
  13. this.getTransitions = this.getTransitions.bind(this);
  14. this.getMetadata = this.getMetadata.bind(this);
  15. }
  16. /**
  17. * Get a list of transitions
  18. * @param {string} db selected database
  19. * @param {array} frequencies min/max frequencies
  20. * @param {array} atomcount min/max number of atoms
  21. * @param {number} energyup maximum value of upper energy (float)
  22. * @param {array} species list of species
  23. * @param {number} intensity maximum intensity value (float)
  24. * @param {function} callback callback function called on found transitions
  25. */
  26. getTransitions(db, frequencies, atomcount, energyup, species, intensity, callback) {
  27. let criteria = {
  28. "frequencies": frequencies,
  29. "atomcount": atomcount,
  30. "energyup": energyup,
  31. "species": species,
  32. "idealisedintensity": intensity,
  33. "sourcefiles": undefined
  34. };
  35. $.ajax({
  36. method: "POST",
  37. url: this.server + "/spectroscopy/" + db + "/lines",
  38. data: JSON.stringify(criteria),
  39. contentType: "application/json",
  40. crossDomain: true
  41. })
  42. .done(function (transitions) {
  43. callback(transitions);
  44. }).fail(function (jqXHR, textStatus) {
  45. console.log("error");
  46. console.log(jqXHR);
  47. });
  48. }
  49. /**
  50. * Get a complete dataset with all its transitions
  51. * @param {string} db name of selected database
  52. * @param {string} sourcefile data file name in source database
  53. * @param {function} callback callback function called on returned transitions
  54. */
  55. getDataset(db, sourcefile, callback) {
  56. let criteria = {
  57. "sourcefile": sourcefile
  58. };
  59. $.ajax({
  60. method: "POST",
  61. url: this.server + "/spectroscopy/" + db + "/dataset",
  62. data: JSON.stringify(criteria),
  63. contentType: "application/json",
  64. /*crossDomain : true*/
  65. })
  66. .done(function (transitions) {
  67. callback(transitions);
  68. }).fail(function (jqXHR, textStatus) {
  69. console.log("error");
  70. console.log(jqXHR);
  71. });
  72. }
  73. /**
  74. * Get metadata from spectroscopy database ( species by source database )
  75. * @param {function} callback function applied to returned data
  76. */
  77. getMetadata(callback) {
  78. let self = this;
  79. // no ajax request if metadata already available
  80. if (this.metadata === null) {
  81. $.ajax({
  82. method: "GET",
  83. url: this.server + "/metadata",
  84. contentType: "application/json",
  85. /*crossDomain : true*/
  86. })
  87. .done(function (metadata) {
  88. self.metadata = metadata;
  89. callback(metadata);
  90. }).fail(function (jqXHR, textStatus) {
  91. console.log("error");
  92. console.log(jqXHR);
  93. });
  94. }
  95. // performs callback on cached data
  96. else {
  97. callback(this.metadata);
  98. }
  99. }
  100. /**
  101. * Get status of dbs from spectroscopy database
  102. * @param {function} callback function applied to returned data
  103. */
  104. getStatuses(callback) {
  105. // no ajax request if metadata already available
  106. if (this.metadata === null) {
  107. $.ajax({
  108. method: "GET",
  109. url: this.server + "/spectroscopy/databases/status",
  110. contentType: "application/json",
  111. /*crossDomain : true*/
  112. })
  113. .done(function (results) {
  114. callback(results);
  115. }).fail(function (jqXHR, textStatus) {
  116. console.log("error");
  117. console.log(jqXHR);
  118. });
  119. }
  120. // performs callback on cached data
  121. else {
  122. callback(this.metadata);
  123. }
  124. }
  125. }
  126. /**
  127. * Access to Yafitss service
  128. */
  129. class ServerApi{
  130. getRADECRangeInDegrees(relFITSFilePath, processResult){
  131. //DOMAccessor.showLoaderAction(true);
  132. return $.post("", {
  133. "method": "RADECRangeInDegrees",
  134. "relFITSFilePath": relFITSFilePath,
  135. "sessionID": 0
  136. }).done(function(resp) {
  137. console.log('$.post("", {"method": "RADECRangeInDegrees", "relFITSFilePath": dataPaths.relFITSFilePath}).done(function (resp) { : entering');
  138. processResult(resp);
  139. //DOMAccessor.showLoaderAction(false);
  140. console.log('$.post("", {"method": "RADECRangeInDegrees", "relFITSFilePath": dataPaths.relFITSFilePath}).done(function(resp) {: exiting');
  141. });
  142. }
  143. getPixelValue(relFITSFilePath, iRA, iDEC, processResult){
  144. DOMAccessor.showLoaderAction(true);
  145. return $.get("getPixelValueAtiRAiDEC", {relFITSFilePath: relFITSFilePath, iRA: iRA, iDEC : iDEC}).done((resp)=>{
  146. if (resp["status"] == false) {
  147. // user probably clicked in an area outside the image, we log but don't show
  148. console.log(resp["message"]);
  149. } else {
  150. processResult(resp);
  151. }
  152. DOMAccessor.showLoaderAction(false);
  153. });
  154. }
  155. getPixelFreqValue(relFITSFilePath, iRA, iDEC, iFREQ, processResult){
  156. DOMAccessor.showLoaderAction(true);
  157. return $.get("getPixelValueAtiFreqiRAiDEC", {relFITSFilePath: relFITSFilePath, iRA: iRA, iDEC: iDEC, iFREQ: iFREQ}).done((resp)=>{
  158. if (resp["status"] == false) {
  159. // user probably clicked in an area outside the image, we log but don't show
  160. console.log(resp["message"]);
  161. } else {
  162. processResult(resp);
  163. }
  164. DOMAccessor.showLoaderAction(false);
  165. });
  166. }
  167. getSingleSpectrum(iRA, iDEC, relFITSFilePath, processResult){
  168. // get spectrum
  169. return $.post("", {
  170. "method": "getSpectrum",
  171. "relFITSFilePath": relFITSFilePath,
  172. "iRA": iRA,
  173. "iDEC": iDEC,
  174. "iFREQ0": 0,
  175. "iFREQ1": FITS_HEADER.naxis3 - 1
  176. }, null, "json").done(
  177. /*
  178. ** This is the function which actually performs the plot as a callback on
  179. ** return from a call to the FITS file server in order to get the spectrum to draw.
  180. */
  181. (resp) => {
  182. if (resp.data["status"] == false) {
  183. // an error occurred
  184. if (resp.data["message"] !== "IndexError") {
  185. console.log("getSpectrum callback error : " + resp.data["message"]);
  186. alert(resp.data["message"]);
  187. return;
  188. }
  189. // there was nothing at given corrdinates
  190. else {
  191. console.log("getSpectrum callback error : nothing at given coordinates");
  192. return;
  193. }
  194. }
  195. processResult(resp);
  196. DOMAccessor.showLoaderAction(false);
  197. }
  198. ).fail(function(jqXHR, textStatus, errorThrown){
  199. console.log(errorThrown);
  200. DOMAccessor.markLoadingDone();
  201. });
  202. }
  203. getSummedSpectrum(iRA0, iRA1, iDEC0, iDEC1, relFITSFilePath, processResult){
  204. DOMAccessor.showLoaderAction(true);
  205. return $.post("", {
  206. "method": "getAverageSpectrum",
  207. "relFITSFilePath": relFITSFilePath,
  208. "iRA0": iRA0,
  209. "iRA1": iRA1,
  210. "iDEC0": iDEC0,
  211. "iDEC1": iDEC1
  212. }).done((resp) => {
  213. if (resp["status"] == false) {
  214. alert(`Something went wrong with the calculation of the average spectrum. The message was '${resp["message"]}'`);
  215. } else {
  216. processResult(resp);
  217. }
  218. DOMAccessor.showLoaderAction(false);
  219. });
  220. }
  221. getSingleSlice(sliceIndex, relFITSFilePath, ittName, lutName, vmName, processResult){
  222. DOMAccessor.showLoaderAction(true);
  223. return $.post('png', {
  224. 'si': sliceIndex,
  225. 'relFITSFilePath': relFITSFilePath,
  226. 'ittName': ittName,
  227. 'lutName': lutName,
  228. 'vmName': vmName
  229. }).done(
  230. function (resp) {
  231. console.log("$.post('/png', {'si': sliceIndex, 'relFITSFilePath': _relFITSFilePath}).done(: entering");
  232. if (resp["status"] == false) {
  233. alert("Something went wrong during the generation of the image. The message was '" +
  234. resp["message"] + "'");
  235. } else if (resp["result"] === undefined) {
  236. alert("No data available in this channel");
  237. } else {
  238. processResult(resp);
  239. }
  240. DOMAccessor.showLoaderAction(false);
  241. console.log("$.post('/png', {'si': sliceIndex, 'path': _path}).done(: exiting");
  242. }
  243. )
  244. }
  245. getSummedSlice(sliceIndex0, sliceIndex1, relFITSFilePath, ittName, lutName, vmName, processResult){
  246. DOMAccessor.showLoaderAction(true);
  247. return $.post('sumpng', {
  248. 'si0': sliceIndex0,
  249. 'si1': sliceIndex1,
  250. 'relFITSFilePath': relFITSFilePath,
  251. 'ittName': ittName,
  252. 'lutName': lutName,
  253. 'vmName': vmName
  254. }).done(
  255. function (resp) {
  256. console.log(` $.post('/sumpng', {'si0': ${sliceIndex0}, 'si1': ${sliceIndex1}, 'relFITSFilePath': ${self._relFITSFilePath}}).done() : entering`);
  257. console.log("in _updateSummedSlicesWithPOST");
  258. if (resp["status"] == false) {
  259. alert("Something went wrong during the generation of the image. The message was " +
  260. resp["message"] + "'");
  261. } else if (resp["result"] === undefined) {
  262. alert("No data available in this channel");
  263. } else {
  264. processResult(resp);
  265. }
  266. DOMAccessor.showLoaderAction(false);
  267. console.log("$.post('/sumpng', {'si0': sliceIndex0, 'si1': sliceIndex1, 'relFITSFilePath': relFITSFilePath}).done() : exiting");
  268. });
  269. }
  270. getSummedSliceValueAtPixel(iRA, iDEC, iFREQ0, iFREQ1, relFITSFilePath, processResult){
  271. return $.post("", {
  272. "method": "getAverage",
  273. "relFITSFilePath": relFITSFilePath,
  274. "iRA0": iRA,
  275. "iRA1": iRA,
  276. "iDEC0": iDEC,
  277. "iDEC1": iDEC,
  278. "iFREQ0": iFREQ0,
  279. "iFREQ1": iFREQ1,
  280. "retFITS": false
  281. }).done(
  282. (resp) => {
  283. processResult(resp);
  284. }
  285. ).fail(
  286. function (err) {
  287. var msg = "POST failed" + JSON.stringify(err, 0, 4);
  288. alert(msg);
  289. });
  290. }
  291. getFITSSliceImage(sliceIndex, relFITSFilePath, processResult){
  292. return $.post("", {
  293. "method": "createFITSSliceImage",
  294. "relFITSFilePath": relFITSFilePath,
  295. "iFREQ": sliceIndex
  296. }).done(
  297. function (resp) {
  298. console.log("A FITS file has been created for the upper image.");
  299. let x = JSON.parse(resp);
  300. if (!x["status"]) {
  301. console.log(`Something went wrong during the generation of the image FITS file, the message was
  302. ${x["message"]}`);
  303. alert(x["message"]);
  304. processResult(x);
  305. }
  306. }
  307. ).fail(
  308. function (err) {
  309. let msg = "POST failed" + JSON.stringify(err, 0, 4);
  310. console.log(msg);
  311. alert(msg);
  312. }
  313. );
  314. }
  315. getSummedFITSSliceImage(sliceIndex0, sliceIndex1, relFITSFilePath, processResult){
  316. return $.post("", {
  317. "method": "createFITSSumSliceImage",
  318. "relFITSFilePath": relFITSFilePath,
  319. "iFREQ0": sliceIndex0,
  320. "iFREQ1": sliceIndex1
  321. }).done(
  322. function (resp) {
  323. console.log("A FITS file has been created for the bottom image." + resp);
  324. let x = JSON.parse(resp);
  325. if (!x["status"]) {
  326. console.log(`Something went wrong during the generation of the bottom image FITS file, the message was
  327. ${x["message"]}`);
  328. alert(x["message"]);
  329. processResult(x);
  330. }
  331. }
  332. ).fail(
  333. function (err) {
  334. let msg = "POST failed" + JSON.stringify(err, 0, 4);
  335. console.log(msg);
  336. alert(msg);
  337. }
  338. );
  339. }
  340. createFitsFile(iRA, iDEC, relFITSFilePath, processResult){
  341. return $.post("", {
  342. "method": "createFits",
  343. "relFITSFilePath": relFITSFilePath,
  344. "iRA": iRA,
  345. "iDEC": iDEC
  346. })
  347. .done(
  348. function (resp) {
  349. if (resp["status"] !== false) {
  350. processResult(resp);
  351. //dataPaths.spectrum = x["result"];
  352. } else {
  353. console.log(`Something went wrong during the generation of
  354. the spectrum FITS file, the message was ${resp["message"]}`);
  355. alert(resp["message"]);
  356. }
  357. }
  358. )
  359. .fail(
  360. function (err) {
  361. var msg = "POST failed" + JSON.stringify(err, 0, 4);
  362. console.log(msg);
  363. alert(msg);
  364. }
  365. );
  366. }
  367. measureBox(iRA0, iRA1, iDEC0, iDEC1, iFREQ, relFITSFilePath, processResult){
  368. return $.post("measureBox", {
  369. 'relFITSFilePath': relFITSFilePath,
  370. 'iFREQ': iFREQ,
  371. 'iRA0': iRA0,
  372. 'iRA1': iRA1,
  373. 'iDEC0': iDEC0,
  374. 'iDEC1': iDEC1
  375. },
  376. (resp) => {
  377. DOMAccessor.showLoaderAction(true);
  378. if (resp["status"] == false) {
  379. alert(`Something went wrong with the measurements of the box [[${iRA0}, ${iDEC0}], [${iRA1},${iDEC1}]]. The message was '${resp["message"]}'`);
  380. }
  381. else {
  382. processResult(resp);
  383. }
  384. DOMAccessor.showLoaderAction(false);
  385. });
  386. }
  387. getContours(postParams, processResult){
  388. return $.post('getContours', postParams,
  389. (resp) => {
  390. DOMAccessor.showLoaderAction(false);
  391. if (resp["status"] == false) {
  392. reject(`Error 2 : Something went wrong during the generation of the contours. The message was '${resp["message"]}'`);
  393. }
  394. else {
  395. processResult(resp);
  396. }
  397. });
  398. }
  399. measureContours(iFREQ, contour, level, relFITSFilePath, processResult){
  400. DOMAccessor.showLoaderAction(true);
  401. return $.post('measureContour', { 'relFITSFilePath': relFITSFilePath, 'iFREQ': iFREQ, 'contour': contour, 'level': level },
  402. (resp) => {
  403. DOMAccessor.showLoaderAction(false);
  404. processResult(resp);
  405. });
  406. }
  407. createSmoothCube(nbox, relFITSFilePath, processResult){
  408. return $.post("", {
  409. "method": "createSmoothCube",
  410. "relFITSFilePath": relFITSFilePath,
  411. "nbox": nbox
  412. }).done(
  413. function (resp) {
  414. console.log("A FITS file has been created for the smoothCube." + resp);
  415. processResult(resp);
  416. }
  417. ).fail(
  418. function (err) {
  419. var msg = "POST failed" + JSON.stringify(err, 0, 4);
  420. console.log(msg);
  421. alert(msg);
  422. }
  423. );
  424. }
  425. }
  426. export{
  427. ServerApi,
  428. SpectroApi
  429. }