46 #include <sys/types.h> 47 #include <sys/socket.h> 48 #include <netinet/in.h> 55 #include "gravi_eop.h" 56 #include "gravi_cpl.h" 57 #include "gravi_pfits.h" 59 #include "gravi_utils.h" 66 #define BUFFER_LENGTH 32768 68 char * gravity_eop_get_ftp_file (
int socketfd,
int * data_length);
69 int gravity_get_socket_connection (
const char * host,
const char * port);
70 int gravity_eop_send_ftpcmd (
int sockfd,
const char *cmd);
71 int gravity_eop_send_pasv (
int sockfd,
const char *cmd);
72 int gravity_eop_ftp_reply (
int sockfd,
char ** message);
73 int gravity_eop_verify_ftp_code (
char * msg,
int length);
75 cpl_error_code gravi_eop_interpolate (cpl_size n,
double *mjd,
76 double *pmx,
double *pmy,
78 cpl_table * eop_table,
79 cpl_propertylist * header);
81 void eraAtboq (
double rc,
double dc, eraASTROM *astrom,
double enuob[3]);
82 void eraAtcoq (
double rc,
double dc,
double pmr,
double pmd,
double px,
83 double rv, eraASTROM *astrom,
double enuob[3]);
84 void dtp2s (
double xi,
double eta,
double raz,
85 double decz,
double *ra,
double *dec);
86 void rotate_vector (
double in[3],
double angle,
double axis[3],
double out[3]);
87 void difference (
double x[3],
double y[3],
double z[3]);
88 void multiply (
double xyz[3],
double factor);
89 void normalize (
double xyz[3]);
90 void cross (
double x[3],
double y[3],
double z[3]);
101 cpl_error_code gravi_eop_interpolate (cpl_size n,
double *mjd,
102 double *pmx,
double *pmy,
104 cpl_table * eop_table,
105 cpl_propertylist * header)
107 gravi_msg_function_start(1);
108 cpl_ensure_code (eop_table, CPL_ERROR_NULL_INPUT);
109 cpl_ensure_code (header, CPL_ERROR_NULL_INPUT);
110 cpl_ensure_code (n>0, CPL_ERROR_ILLEGAL_INPUT);
111 cpl_ensure_code (mjd, CPL_ERROR_NULL_INPUT);
112 cpl_ensure_code (pmx, CPL_ERROR_NULL_INPUT);
113 cpl_ensure_code (pmy, CPL_ERROR_NULL_INPUT);
114 cpl_ensure_code (dut, CPL_ERROR_NULL_INPUT);
117 cpl_vector *vmjd = cpl_vector_wrap (n, mjd);
118 cpl_vector *vpmx = cpl_vector_wrap (n, pmx);
119 cpl_vector *vpmy = cpl_vector_wrap (n, pmy);
120 cpl_vector *vdut = cpl_vector_wrap (n, dut);
123 if (cpl_vector_get_min (vmjd) < cpl_table_get_column_min (eop_table,
"MJD") ||
124 cpl_vector_get_max (vmjd) > cpl_table_get_column_max (eop_table,
"MJD"))
126 cpl_msg_warning (cpl_func,
"Some MJD are outside the EOP_PARAM range (MJD=%.2f, %.2f..%.2f). Use EOP and DUT=0.0s",
127 cpl_vector_get_mean (vmjd),
128 cpl_table_get_column_min (eop_table,
"MJD"),
129 cpl_table_get_column_max (eop_table,
"MJD"));
130 cpl_vector_unwrap (vmjd);
131 cpl_vector_unwrap (vpmx);
132 cpl_vector_unwrap (vpmy);
133 cpl_vector_unwrap (vdut);
134 return CPL_ERROR_NONE;
138 if(cpl_vector_get_max (vmjd) > cpl_propertylist_get_double (header,
"ESO QC EOP MJD LAST FINAL"))
140 cpl_msg_warning (cpl_func,
"Some MJD are inside the EOP_PARAM prediction range...");
144 cpl_size nrow = cpl_table_get_nrow (eop_table);
145 cpl_vector * eop_mjd = cpl_vector_wrap (nrow, cpl_table_get_data_double (eop_table,
"MJD"));
146 cpl_vector * eop_pmx = cpl_vector_wrap (nrow, cpl_table_get_data_double (eop_table,
"PMX"));
147 cpl_vector * eop_pmy = cpl_vector_wrap (nrow, cpl_table_get_data_double (eop_table,
"PMY"));
148 cpl_vector * eop_dut = cpl_vector_wrap (nrow, cpl_table_get_data_double (eop_table,
"DUT"));
151 cpl_bivector *mjd_pmx = cpl_bivector_wrap_vectors (vmjd, vpmx);
152 cpl_bivector *eop_mjd_pmx = cpl_bivector_wrap_vectors (eop_mjd, eop_pmx);
153 cpl_bivector_interpolate_linear (mjd_pmx, eop_mjd_pmx);
154 cpl_bivector_unwrap_vectors (mjd_pmx);
155 cpl_bivector_unwrap_vectors (eop_mjd_pmx);
157 cpl_bivector *mjd_pmy = cpl_bivector_wrap_vectors (vmjd, vpmy);
158 cpl_bivector *eop_mjd_pmy = cpl_bivector_wrap_vectors (eop_mjd, eop_pmy);
159 cpl_bivector_interpolate_linear (mjd_pmy, eop_mjd_pmy);
160 cpl_bivector_unwrap_vectors (mjd_pmy);
161 cpl_bivector_unwrap_vectors (eop_mjd_pmy);
163 cpl_bivector *mjd_dut = cpl_bivector_wrap_vectors (vmjd, vdut);
164 cpl_bivector *eop_mjd_dut = cpl_bivector_wrap_vectors (eop_mjd, eop_dut);
165 cpl_bivector_interpolate_linear (mjd_dut, eop_mjd_dut);
166 cpl_bivector_unwrap_vectors (mjd_dut);
167 cpl_bivector_unwrap_vectors (eop_mjd_dut);
170 cpl_msg_info(cpl_func,
"EOP averages: MJD=%.2f DUT1=%.3f PMX=%.3farcsec PMY=%.3farcsec",
171 cpl_vector_get_mean (vmjd), cpl_vector_get_mean (vdut),
172 cpl_vector_get_mean (vpmx), cpl_vector_get_mean (vpmy));
175 cpl_vector_unwrap (vmjd);
176 cpl_vector_unwrap (vpmx);
177 cpl_vector_unwrap (vpmy);
178 cpl_vector_unwrap (vdut);
179 cpl_vector_unwrap (eop_mjd);
180 cpl_vector_unwrap (eop_pmx);
181 cpl_vector_unwrap (eop_pmy);
182 cpl_vector_unwrap (eop_dut);
184 CPLCHECK_MSG (
"Cannot interpolate EOP");
186 gravi_msg_function_exit(1);
187 return CPL_ERROR_NONE;
194 void eraAtboq (
double rc,
double dc, eraASTROM *astrom,
double enuob[3])
197 double aob, zob, hob, dob, rob;
200 eraAtciq (rc, dc, 0.0, 0.0, 0.0, 0.0, astrom, &ri, &di);
203 eraAtioq (ri, di, astrom, &aob, &zob, &hob, &dob, &rob);
206 eraS2c(CPL_MATH_PI/2.0-aob, CPL_MATH_PI/2.0-zob, enuob);
209 void rotate_vector (
double in[3],
double angle,
double axis[3],
double out[3])
213 eraSxp(angle, axis, rv);
243 cpl_propertylist * header,
244 cpl_table * eop_table,
245 cpl_propertylist * eop_header,
247 cpl_table * array_table)
249 gravi_msg_function_start(1);
250 cpl_ensure_code (input_table, CPL_ERROR_NULL_INPUT);
251 cpl_ensure_code (header, CPL_ERROR_NULL_INPUT);
254 if (save_pointing > 0) {
255 cpl_msg_info (cpl_func,
"Will save [E_U,E_V,E_W,E_AZ,E_ZD]");
258 if (array_table != NULL) {
259 cpl_msg_info (cpl_func,
"Will compute [UCOORD,VCOORD]");
265 double t_skip = 1./24/3600, mjd0 = -1.0, mjd1 = -1.0;
266 cpl_msg_info (cpl_func,
"Compute pointing with full ERFA every %.2f s",t_skip*24*3600.0);
272 double baseline[6][3];
276 for (
int base = 0; base < 6; base ++) {
277 int tel1=0;
while ( cpl_table_get (array_table,
"STA_INDEX", tel1, NULL) != gravi_table_get_value (input_table,
"STA_INDEX", base, 0) ) tel1++;
278 int tel2=0;
while ( cpl_table_get (array_table,
"STA_INDEX", tel2, NULL) != gravi_table_get_value (input_table,
"STA_INDEX", base, 1) ) tel2++;
279 baseline[base][0] = -(gravi_table_get_value (array_table,
"STAXYZ", tel2, 0) - gravi_table_get_value (array_table,
"STAXYZ", tel1, 0));
280 baseline[base][1] = -(gravi_table_get_value (array_table,
"STAXYZ", tel2, 1) - gravi_table_get_value (array_table,
"STAXYZ", tel1, 1));
281 baseline[base][2] = +(gravi_table_get_value (array_table,
"STAXYZ", tel2, 2) - gravi_table_get_value (array_table,
"STAXYZ", tel1, 2));
283 uCoord = cpl_table_get_data_double (input_table,
"UCOORD");
284 vCoord = cpl_table_get_data_double (input_table,
"VCOORD");
288 double *mjd = cpl_table_get_data_double (input_table,
"MJD");
289 double mean_mjd = cpl_table_get_column_mean (input_table,
"MJD");
298 cpl_msg_info (cpl_func,
"Saving E_U, E_V, E_W, E_AZ, E_ZD");
299 cpl_table_new_column_array (input_table,
"E_U", CPL_TYPE_DOUBLE, 3);
300 cpl_table_new_column_array (input_table,
"E_V", CPL_TYPE_DOUBLE, 3);
301 cpl_table_new_column_array (input_table,
"E_W", CPL_TYPE_DOUBLE, 3);
302 cpl_table_new_column_array (input_table,
"E_AZ", CPL_TYPE_DOUBLE, 3);
303 cpl_table_new_column_array (input_table,
"E_ZD", CPL_TYPE_DOUBLE, 3);
304 p_u = cpl_table_get_data_array (input_table,
"E_U");
305 p_v = cpl_table_get_data_array (input_table,
"E_V");
306 p_w = cpl_table_get_data_array (input_table,
"E_W");
307 p_az = cpl_table_get_data_array (input_table,
"E_AZ");
308 p_zd = cpl_table_get_data_array (input_table,
"E_ZD");
309 CPLCHECK_MSG (
"Cannot create [E_U, E_V, E_W, E_AZ, E_ZD]");
313 double elev = gravi_pfits_get_geoelev (header);
314 double lon = gravi_pfits_get_geolon (header) * CPL_MATH_RAD_DEG;
315 double lat = gravi_pfits_get_geolat (header) * CPL_MATH_RAD_DEG;
316 CPLCHECK_MSG (
"Cannot get the observer location");
319 double dut1 = 0, pmx = 0, pmy = 0;
320 if (eop_table != NULL) {
321 gravi_eop_interpolate (1, &mean_mjd, &pmx, &pmy, &dut1,
322 eop_table, eop_header);
323 CPLCHECK_MSG (
"Cannot interpolate");
325 cpl_msg_warning (cpl_func,
"No EOP_PARAM. Use EOP and DUT=0.0s");
329 double rc = gravi_pfits_get_mid_raep (header);
330 double dc = gravi_pfits_get_mid_decep (header);
331 double pmr = gravi_pfits_get_pmra (header) * CPL_MATH_RAD_DEG / 3600.0 / cos(dc);
332 double pmd = gravi_pfits_get_pmdec (header) * CPL_MATH_RAD_DEG / 3600.0;
333 double parallax = gravi_pfits_get_plx (header);
335 CPLCHECK_MSG (
"Cannot get the header data");
340 double eps = 10.0 / 3600.0 * CPL_MATH_RAD_DEG;
342 double eUb[3], eVb[3], eWb[3], eZb[3];
343 double eWo_up[3], eWo_um[3], eWo_vp[3], eWo_vm[3];
344 double eWb_up[3], eWb_um[3], eWb_vp[3], eWb_vm[3];
345 double rb_up, db_up, rb_um, db_um, rb_vp, db_vp, rb_vm, db_vm;
346 double eUo[3], eVo[3], eWo[3], eAZo[3], eZDo[3];
347 double ez[3] = {0.0, 0.0, 1.0};
349 double pressure = 0.0;
350 double temperature = 0.0;
351 double humidity = 0.0;
352 double wavelength = 0.0;
356 cpl_size n_row = cpl_table_get_nrow (input_table);
357 for (cpl_size row = 0 ; row < n_row ; row++) {
360 if (mjd[row] != mjd1 ) {
364 if ( fabs (mjd[row]-mjd0) > t_skip ) {
365 eraApco13 (2400000.5, mjd[row], dut1, lon, lat, elev,
366 pmx/3600.0*CPL_MATH_RAD_DEG, pmy/3600.0*CPL_MATH_RAD_DEG,
367 pressure, temperature, humidity, wavelength,
372 eraAper13 (2400000.5, mjd[row] + dut1/(24.0*3600.0), &astrom);
375 eraPmpx(rc, dc, pmr, pmd, parallax, sysvel, astrom.pmt, astrom.eb, eWb);
376 eraC2s(eWb, &rb, &db);
379 eraS2c(0.0, CPL_MATH_PI/2.0, eZb);
380 eraPxp(eZb, eWb, eUb);
381 eraPn(eUb, &norm, eUb);
382 eraPxp(eWb, eUb, eVb);
385 rotate_vector(eWb, -eps, eVb, eWb_up);
386 rotate_vector(eWb, +eps, eVb, eWb_um);
387 rotate_vector(eWb, +eps, eUb, eWb_vp);
388 rotate_vector(eWb, -eps, eUb, eWb_vm);
389 eraC2s(eWb_up, &rb_up, &db_up);
390 eraC2s(eWb_um, &rb_um, &db_um);
391 eraC2s(eWb_vp, &rb_vp, &db_vp);
392 eraC2s(eWb_vm, &rb_vm, &db_vm);
395 eraAtboq (rb , db , &astrom, eWo );
396 eraAtboq (rb_up, db_up, &astrom, eWo_up);
397 eraAtboq (rb_um, db_um, &astrom, eWo_um);
398 eraAtboq (rb_vp, db_vp, &astrom, eWo_vp);
399 eraAtboq (rb_vm, db_vm, &astrom, eWo_vm);
402 eraPxp(eWo_up, eWo_um, eUo);
403 eraPxp(eWo, eUo, eUo);
404 eraSxp(1./(2.0*eps), eUo, eUo);
405 eraPxp(eWo_vp, eWo_vm, eVo);
406 eraPxp(eWo, eVo, eVo);
407 eraSxp(1./(2.0*eps), eVo, eVo);
410 eraPxp(eWo, ez, eAZo);
411 eraPn(eAZo, &norm, eAZo);
414 eraPxp(eWo, eAZo, eZDo);
415 eraPn(eZDo, &norm, eZDo);
420 if (mjd[row] != mjd1 ) {
421 double * eU = cpl_malloc (
sizeof(
double) * 3);
422 double * eV = cpl_malloc (
sizeof(
double) * 3);
423 double * eW = cpl_malloc (
sizeof(
double) * 3);
424 double * eAZ = cpl_malloc (
sizeof(
double) * 3);
425 double * eZD = cpl_malloc (
sizeof(
double) * 3);
426 for ( cpl_size c = 0; c < 3; c++) {
435 p_u[row] = cpl_array_wrap_double (eU, 3);
436 p_v[row] = cpl_array_wrap_double (eV, 3);
437 p_w[row] = cpl_array_wrap_double (eW, 3);
438 p_az[row] = cpl_array_wrap_double (eAZ, 3);
439 p_zd[row] = cpl_array_wrap_double (eZD, 3);
441 p_u[row] = cpl_array_duplicate (p_u [row-1]);
442 p_v[row] = cpl_array_duplicate (p_v [row-1]);
443 p_w[row] = cpl_array_duplicate (p_w [row-1]);
444 p_az[row] = cpl_array_duplicate (p_az[row-1]);
445 p_zd[row] = cpl_array_duplicate (p_zd[row-1]);
453 double n_air = 1.0002028;
456 uCoord[row] = (eUo[0] * baseline[base][0] + eUo[1] * baseline[base][1] + eUo[2] * baseline[base][2])/n_air;
457 vCoord[row] = (eVo[0] * baseline[base][0] + eVo[1] * baseline[base][1] + eVo[2] * baseline[base][2])/n_air;
461 CPLCHECK_MSG (
"Cannot run the ERFA transform");
464 gravi_msg_function_exit(1);
465 return CPL_ERROR_NONE;
482 gravi_msg_function_start(1);
483 cpl_ensure_code (p2vmred_data, CPL_ERROR_NULL_INPUT);
486 cpl_propertylist * hdr_data = gravi_data_get_header (p2vmred_data);
492 int type_data, ntype_data = 2;
493 for (type_data = 0; type_data < ntype_data ; type_data ++) {
495 int npol = gravi_pfits_get_pola_num (hdr_data, type_data);
496 cpl_table * oi_vis = gravi_data_get_oi_vis (p2vmred_data, type_data, 0, npol);
499 cpl_msg_info(cpl_func,
"Compute pointing for %s ",type_data==GRAVI_FT?
"FT":
"SC");
504 (eop_data ? gravi_data_get_header (eop_data) : NULL),
505 type_data==GRAVI_FT?0:1,
507 CPLCHECK_MSG (
"Cannot compute pointing");
510 if ((npol > 1) && (type_data==GRAVI_SC)) {
511 cpl_msg_debug (cpl_func,
"Duplicate in the 2nd polarisation");
513 cpl_table * oi_vis_1 = gravi_data_get_oi_vis (p2vmred_data, type_data, 1, npol);
514 cpl_table_duplicate_column (oi_vis_1,
"E_U", oi_vis,
"E_U");
515 cpl_table_duplicate_column (oi_vis_1,
"E_V", oi_vis,
"E_V");
516 cpl_table_duplicate_column (oi_vis_1,
"E_W", oi_vis,
"E_W");
517 cpl_table_duplicate_column (oi_vis_1,
"E_AZ", oi_vis,
"E_AZ");
518 cpl_table_duplicate_column (oi_vis_1,
"E_ZD", oi_vis,
"E_ZD");
520 CPLCHECK_MSG (
"Cannot duplicate");
525 cpl_msg_info (cpl_func,
"Duplicate in the 2nd polarisation");
527 cpl_table * oi_vis_1 = gravi_data_get_oi_vis (p2vmred_data, type_data, 1, npol);
528 cpl_table_copy_data_double (oi_vis_1,
"UCOORD", cpl_table_get_data_double (oi_vis,
"UCOORD"));
529 cpl_table_copy_data_double (oi_vis_1,
"VCOORD", cpl_table_get_data_double (oi_vis,
"VCOORD"));
531 CPLCHECK_MSG (
"Cannot duplicate");
535 gravi_msg_function_exit(1);
536 return CPL_ERROR_NONE;
560 const char * eop_urlpath,
563 gravi_msg_function_start(1);
565 const char ftp_port[] =
"21";
566 int cmd_socket, data_socket;
569 cpl_ensure (eop_host, CPL_ERROR_NULL_INPUT, NULL);
570 cpl_ensure (eop_urlpath, CPL_ERROR_NULL_INPUT, NULL);
571 cpl_ensure (data_length, CPL_ERROR_NULL_INPUT, NULL);
572 cpl_msg_debug (cpl_func,
"Using URL ftp://%s%s", eop_host, eop_urlpath);
575 cmd_socket = gravity_get_socket_connection(eop_host, ftp_port);
578 char * msg = cpl_malloc(strlen(eop_host) + 31);
579 snprintf(msg, strlen(eop_host) + 30,
580 "Couldn't connect to the host %s", eop_host);
581 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
"%s", msg);
586 if(!gravity_eop_ftp_reply(cmd_socket, NULL))
588 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
589 "FTP server didn't reply");
593 cpl_msg_debug(cpl_func,
"SEND");
594 if(!gravity_eop_send_ftpcmd(cmd_socket,
"USER anonymous\n"))
596 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
597 "Failed to send anonymous user");
601 if(!gravity_eop_send_ftpcmd(cmd_socket,
"PASS ftp@eso.org\n"))
603 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
604 "Failed to send pasword");
608 int data_port = gravity_eop_send_pasv(cmd_socket,
"PASV\n");
611 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
612 "Failed to get data port");
618 char data_port_s[256];
619 snprintf(data_port_s, 255,
"%d", data_port);
620 data_socket = gravity_get_socket_connection(eop_host, data_port_s);
621 if (data_socket == 0)
623 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
624 "Couldn't open ftp data connection");
629 if(!gravity_eop_send_ftpcmd(cmd_socket,
"TYPE I\n"))
631 char * retr_command = cpl_malloc(strlen(eop_urlpath) + 7);
632 snprintf(retr_command, strlen(eop_urlpath) + 7,
"RETR %s\n", eop_urlpath);
633 if(!gravity_eop_send_ftpcmd(cmd_socket, retr_command))
637 cpl_free(retr_command);
640 cpl_free(retr_command);
642 char * data_eop = gravity_eop_get_ftp_file(data_socket, data_length);
648 gravi_msg_function_exit(1);
652 int gravity_get_socket_connection (
const char * host,
const char * port)
655 gravi_msg_function_start(0);
661 struct addrinfo hints = { 0 };
662 hints.ai_family = AF_UNSPEC;
663 hints.ai_flags = AI_ADDRCONFIG;
664 hints.ai_socktype = SOCK_STREAM;
666 cpl_msg_debug(cpl_func,
"Getting IP");
667 struct addrinfo * addr_list ;
668 if (getaddrinfo(host, port, &hints, &addr_list) != 0) {
669 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
670 "Couldn't get address for host");
677 cpl_msg_debug(cpl_func,
"Connecting to server");
678 struct addrinfo *this_addr;
679 for(this_addr = addr_list; this_addr != NULL; this_addr = this_addr->ai_next)
682 if ((sockfd = socket(this_addr->ai_family, this_addr->ai_socktype,
683 this_addr->ai_protocol)) == -1) {
687 if (connect(sockfd, this_addr->ai_addr, this_addr->ai_addrlen) == -1) {
689 if(errno == ECONNREFUSED)
693 cpl_msg_debug(cpl_func,
"Connection established");
697 if (this_addr == NULL)
699 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
700 "Couldn't connect to the host");
704 freeaddrinfo(addr_list);
706 gravi_msg_function_exit(0);
710 int gravity_eop_send_ftpcmd (
int sockfd,
const char *cmd)
712 gravi_msg_function_start(0);
713 cpl_msg_debug(cpl_func,
"Sending FTP command <<%s>>", cmd);
715 if(write(sockfd, cmd, strlen(cmd))==0)
717 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
718 "Problem during FTP transaction");
723 if(!gravity_eop_ftp_reply(sockfd, &msg))
725 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
726 "Problem during FTP transaction");
732 gravi_msg_function_exit(0);
737 int gravity_eop_send_pasv (
int sockfd,
const char *cmd)
739 gravi_msg_function_start(0);
741 if(write(sockfd, cmd, strlen(cmd))==0)
743 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
744 "Problem during FTP transaction");
752 if(!gravity_eop_ftp_reply(sockfd, &msg))
754 cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
755 "Problem during FTP transaction");
761 new_con = strchr(msg,
'(');
764 sscanf(new_con+1,
"%u,%u,%u,%u,%u,%u",&v[2],&v[3],&v[4],&v[5],&v[0],&v[1]);
769 int data_port = v[0] * 256 + v[1];
773 gravi_msg_function_exit(0);
777 int gravity_eop_ftp_reply (
int sockfd,
char ** message)
779 gravi_msg_function_start(0);
781 char buffer[BUFFER_LENGTH];
788 while( ( n = recv(sockfd, buffer, BUFFER_LENGTH - 1, 0) ) > 0)
790 void * tmp = realloc(msg, length + n + 1);
801 strncpy(msg + length, buffer, n);
806 if(msg[length-1]==
'\n')
809 char * eolchar= msg + length - 1;
810 while(--eolchar != msg)
811 if (*(eolchar - 1) ==
'\n')
815 if(*(eolchar+3) ==
' ')
827 cpl_msg_debug(cpl_func,
"FTP reply: <<%s>>", msg);
830 int verify = gravity_eop_verify_ftp_code(msg, length + 1);
832 if(message != NULL && verify)
837 gravi_msg_function_exit(0);
841 char * gravity_eop_get_ftp_file(
int sockfd,
int * data_length)
843 gravi_msg_function_start(1);
845 char buffer[BUFFER_LENGTH];
851 cpl_msg_info(cpl_func,
"Get the data");
852 while( ( n = recv(sockfd, buffer, BUFFER_LENGTH - 1, 0) ) > 0)
854 void * tmp = realloc(msg, length + n + 1);
865 strncpy(msg + length, buffer, n);
868 cpl_msg_debug(cpl_func,
"Received %d bytes so far",length);
873 *data_length = length+1;
875 gravi_msg_function_exit(1);
879 int gravity_eop_verify_ftp_code (
char * msg,
int length)
881 gravi_msg_function_start(0);
888 while(line[0] ==
'1' || line[0] ==
'2' || line[0] ==
'3')
890 line = strchr(line,
'\n');
891 if(line == NULL || line - msg + 2 == length)
896 gravi_msg_function_exit(0);
920 gravi_msg_function_start(1);
921 cpl_ensure (eop_data, CPL_ERROR_NULL_INPUT, NULL);
923 if(!((data_length - 1) % LINE_SIZE == 0))
925 cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
926 "Raw data doesn't have a fixed record width");
931 cpl_size n_entries = (data_length - 1 ) / LINE_SIZE;
932 cpl_table * eop_table = cpl_table_new (n_entries);
933 cpl_msg_info (cpl_func,
" EOP data has a total of %"CPL_SIZE_FORMAT
" entries", n_entries);
936 cpl_table_new_column (eop_table,
"MJD", CPL_TYPE_DOUBLE);
937 cpl_table_new_column (eop_table,
"PMX", CPL_TYPE_DOUBLE);
938 cpl_table_new_column (eop_table,
"PMY", CPL_TYPE_DOUBLE);
939 cpl_table_new_column (eop_table,
"DUT", CPL_TYPE_DOUBLE);
940 cpl_table_new_column (eop_table,
"FLAG", CPL_TYPE_STRING);
943 cpl_table_set_column_unit (eop_table,
"MJD",
"d");
944 cpl_table_set_column_unit (eop_table,
"PMX",
"arcsec");
945 cpl_table_set_column_unit (eop_table,
"PMY",
"arcsec");
946 cpl_table_set_column_unit (eop_table,
"DUT",
"s");
949 for(cpl_size i=0; i<n_entries; i++)
952 strncpy(flag, eop_data+i*LINE_SIZE+16, 1);
954 cpl_table_set_string(eop_table,
"FLAG", i, flag);
956 cpl_table_set_double(eop_table,
"MJD", i, atof(eop_data+i*LINE_SIZE+7));
957 if(!strncmp(flag,
"I", 1) || !strncmp(flag,
"P", 1))
959 cpl_table_set_double(eop_table,
"PMX", i, atof(eop_data+i*LINE_SIZE+18));
960 cpl_table_set_double(eop_table,
"PMY", i, atof(eop_data+i*LINE_SIZE+37));
961 cpl_table_set_double(eop_table,
"DUT", i, atof(eop_data+i*LINE_SIZE+58));
966 cpl_table_unselect_all (eop_table);
967 cpl_table_or_selected_invalid (eop_table,
"PMX");
968 cpl_table_or_selected_invalid (eop_table,
"PMY");
969 cpl_table_or_selected_invalid (eop_table,
"DUT");
970 cpl_msg_info (cpl_func,
"Found %lld invalid", cpl_table_count_selected (eop_table));
971 cpl_table_erase_selected (eop_table);
973 gravi_msg_function_exit(1);
cpl_table * gravi_data_get_table(gravi_data *self, const char *extname)
Return a pointer on a table extension by its EXTNAME.
char * gravity_eop_download_finals2000A(const char *eop_host, const char *eop_urlpath, int *data_length)
Retrieve the Earth Orientation Parameters computed by IERS.
cpl_error_code gravi_eop_pointing_uv(cpl_table *input_table, cpl_propertylist *header, cpl_table *eop_table, cpl_propertylist *eop_header, int save_pointing, cpl_table *array_table)
Compute the pointing directions and projected baselines.
cpl_error_code gravi_compute_pointing_uv(gravi_data *p2vmred_data, gravi_data *eop_data)
Compute the pointing directions and projected baselines in OI_VIS.
cpl_table * gravi_data_get_table_x(gravi_data *self, int i)
Get the table of an extension by position.
cpl_table * gravity_eop_data_totable(const char *eop_data, int data_length)
Export a raw string buffer containing EOP data to a CPL table.