int main() { char ctypeS[9]; int i, stat[NWCSFIX], status; struct wcsprm wcs; printf("Testing WCSLIB translator for non-standard usage (twcsfix.c)\n" "------------------------------------------------------------\n\n"); wcs.flag = -1; parser(&wcs); /* Print the unmodified struct. */ wcsprt(&wcs); printf("\n------------------------------------" "------------------------------------\n"); /* Fix non-standard WCS keyvalues. */ if ((status = wcsfix(7, 0, &wcs, stat))) { printf("wcsfix error, status returns: ("); for (i = 0; i < NWCSFIX; i++) { printf(i ? ", %d" : "%d", stat[i]); } printf(")\n"); return 1; } wcsprt(&wcs); printf("\n------------------------------------" "------------------------------------\n"); /* Should now have a 'VOPT-F2W' axis, translate it to frequency. */ strcpy(ctypeS, "FREQ-???"); i = -1; if ((status = wcssptr(&wcs, &i, ctypeS))) { printf("wcssptr ERROR %d: %s.n", status, wcs_errmsg[status]); return 1; } if ((status = wcsset(&wcs))) { printf("wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); return 1; } wcsprt(&wcs); wcsfree(&wcs); return 0; }
int spcfix(struct wcsprm *wcs) { char *scode; int i, status; /* Initialize if required. */ if (wcs == 0x0) return 1; if (wcs->flag != WCSSET) { if (status = wcsset(wcs)) return status; } if ((i = wcs->spec) < 0) { /* Look for a linear spectral axis. */ for (i = 0; i < wcs->naxis; i++) { if (wcs->types[i]/100 == 30) { break; } } if (i >= wcs->naxis) { /* No spectral axis. */ return -1; } } /* Was an AIPS-convention spectral type translated? */ scode = wcs->ctype[i] + 4; if (strcmp(scode, "-LSR") == 0) { strcpy(wcs->specsys, "LSRK"); } else if (strcmp(scode, "-HEL") == 0) { strcpy(wcs->specsys, "BARYCENT"); } else if (strcmp(scode, "-OBS") == 0) { strcpy(wcs->specsys, "TOPOCENT"); } else { return -1; } strcpy(scode, "\0\0\0\0"); if (strcmp(wcs->ctype[i], "FELO") == 0) { strcpy(wcs->ctype[i], "VOPT-F2W"); } return 0; }
int spcfix(struct wcsprm *wcs) { char ctype[9], specsys[9]; int i, status; /* Initialize if required. */ if (wcs == 0x0) return FIXERR_NULL_POINTER; if (wcs->flag != WCSSET) { if ((status = wcsset(wcs))) return status; } if ((i = wcs->spec) < 0) { /* Look for a linear spectral axis. */ for (i = 0; i < wcs->naxis; i++) { if (wcs->types[i]/100 == 30) { break; } } if (i >= wcs->naxis) { /* No spectral axis. */ return FIXERR_NO_CHANGE; } } /* Translate an AIPS-convention spectral type if present. */ if ((status = spcaips(wcs->ctype[i], wcs->velref, ctype, specsys))) { return status; } strcpy(wcs->ctype[i], ctype); if (wcs->specsys[1] == '\0') strcpy(wcs->specsys, specsys); wcsutil_null_fill(72, wcs->ctype[i]); wcsutil_null_fill(72, wcs->specsys); return 0; }
/** * @brief * unregister_scm - unregister_scm: return 0 for success; non-zero for fail * * @param[in] svc_name - service name. * * @return int * @retval 0 : success * @retval non-zero : fail */ int unregister_scm(char *svc_name) { SC_LOCK sclLock = NULL; SC_HANDLE schService = NULL; SC_HANDLE schSCManager = NULL; int ret = 1; SERVICE_STATUS ss; int try; schSCManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); if (!schSCManager) { fprintf(stderr, "OpenSCManager failed - %d\n", GetLastError()); goto unregister_scm_cleanup; } /* Open a handle to the service instance. */ schService = OpenService(schSCManager, svc_name, SERVICE_ALL_ACCESS); if (!schService) { fprintf(stderr, "OpenService %s failed - %d\n", svc_name, GetLastError()); goto unregister_scm_cleanup; } /* Get the SCM database lock before changing the password. */ sclLock = LockServiceDatabase(schSCManager); if (sclLock == NULL) { fprintf(stderr, "LockServiceDatabase failed - %d\n", GetLastError()); goto unregister_scm_cleanup; } /* Stop the service first */ ControlService(schService, SERVICE_CONTROL_STOP, &ss); try = 0; ss.dwCurrentState = SERVICE_RUNNING; while ((try < WAIT_RETRY_MAX) && ss.dwCurrentState != SERVICE_STOPPED) { printf("[try %d] waiting for service %s to die\n", try, svc_name); sleep(3); if (!QueryServiceStatus(schService, &ss)) break; try++; } if (!DeleteService(schService)) { fprintf(stderr, "DeleteService(%s) failed - %d\n", svc_name, GetLastError()); goto unregister_scm_cleanup; } printf("\nDeleted service %s\n", svc_name); ret = 0; unregister_scm_cleanup: if (sclLock) UnlockServiceDatabase(sclLock); if (schService) CloseServiceHandle(schService); if (schSCManager) CloseServiceHandle(schSCManager); return (ret); } /** * @brief * prompt_to_get_password - prompt for password. * * @param[out] pass - password. */ void prompt_to_get_password(char pass[LM20_PWLEN+1]) { int ch, j; printf("Please enter password: "******""); if ((p=strstr((const char *)account, "\\"))) { *p = '\0'; strcpy(dname, (const char *)account); *p = '\\'; strcpy(uname, p+1); } mbstowcs(unamew, uname, UNLEN+1); mbstowcs(dnamew, dname, PBS_MAXHOSTNAME+1); mbstowcs(passwordw, password, LM20_PWLEN+1); si.cb = sizeof(si); si.lpDesktop = L""; if( !for_info_only && \ ((rc=CreateProcessWithLogonW(unamew, dnamew, passwordw, 0, NULL, L"cmd /c echo okay", flags, NULL, NULL, &si, &pi)) == 0)) { fprintf(stderr, "Password did not validate against %s err=%d\n\nClick BACK button to retry a different password.\nClick NEXT button to abort installation.", account, GetLastError()); wcsset(passwordw, 0); return (0); } WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); printf("%s password for %s\n", (for_info_only?"Validating":"Validated"), account); wcsset(passwordw, 0); return (1); }
int do_wcs_stuff(fitsfile *fptr, struct wcsprm *wcs) { int i1, i2, i3, k, naxis1, naxis2, naxis3, stat[8], status; double phi[8], pixcrd[8][4], imgcrd[8][4], theta[8], world[8][4], x1, x2, x3; /* Initialize the wcsprm struct, also taking control of memory allocated by * fits_read_wcstab(). */ if ((status = wcsset(wcs))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); return 1; } /* Print the struct. */ if ((status = wcsprt(wcs))) return status; /* Compute coordinates in the corners. */ fits_read_key(fptr, TINT, "NAXIS1", &naxis1, NULL, &status); fits_read_key(fptr, TINT, "NAXIS2", &naxis2, NULL, &status); fits_read_key(fptr, TINT, "NAXIS3", &naxis3, NULL, &status); k = 0; x3 = 1.0f; for (i3 = 0; i3 < 2; i3++) { x2 = 0.5f; for (i2 = 0; i2 < 2; i2++) { x1 = 0.5f; for (i1 = 0; i1 < 2; i1++) { pixcrd[k][0] = x1; pixcrd[k][1] = x2; pixcrd[k][2] = x3; pixcrd[k][3] = 1.0f; k++; x1 = naxis1 + 0.5f; } x2 = naxis2 + 0.5f; } x3 = naxis3; } if ((status = wcsp2s(wcs, 8, 4, pixcrd[0], imgcrd[0], phi, theta, world[0], stat))) { fprintf(stderr, "\n\nwcsp2s ERROR %d: %s.\n", status, wcs_errmsg[status]); /* Invalid pixel coordinates. */ if (status == 8) status = 0; } if (status == 0) { printf("\n\nCorner world coordinates:\n" " Pixel World\n"); for (k = 0; k < 8; k++) { printf(" (%5.1f,%6.1f,%4.1f,%4.1f) -> (%7.3f,%8.3f,%9g,%11.5f)", pixcrd[k][0], pixcrd[k][1], pixcrd[k][2], pixcrd[k][3], world[k][0], world[k][1], world[k][2], world[k][3]); if (stat[k]) printf(" (BAD)"); printf("\n"); } } return 0; }
int wcshdo(int relax, struct wcsprm *wcs, int *nkeyrec, char **header) /* ::: CUBEFACE and STOKES handling? */ { static const char *function = "wcshdo"; char alt, comment[72], keyvalue[72], keyword[16], obsg[8] = "OBSG?", obsgeo[8] = "OBSGEO-?", ptype, xtype, xyz[] = "XYZ"; int bintab, col0, *colax, colnum, i, j, k, naxis, pixlist, primage, status = 0; struct wcserr **err; *nkeyrec = 0; *header = 0x0; if (wcs == 0x0) return WCSHDRERR_NULL_POINTER; err = &(wcs->err); if (wcs->flag != WCSSET) { if ((status = wcsset(wcs))) return status; } if ((naxis = wcs->naxis) == 0) { return 0; } /* These are mainly for convenience. */ alt = wcs->alt[0]; if (alt == ' ') alt = '\0'; colnum = wcs->colnum; colax = wcs->colax; primage = 0; bintab = 0; pixlist = 0; if (colnum) { bintab = 1; col0 = colnum; } else if (colax[0]) { pixlist = 1; col0 = colax[0]; } else { primage = 1; } /* WCS dimension. */ if (!pixlist) { sprintf(keyvalue, "%20d", naxis); wcshdo_util(relax, "WCSAXES", "WCAX", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Number of coordinate axes", nkeyrec, header, &status); } /* Reference pixel coordinates. */ for (j = 0; j < naxis; j++) { wcsutil_double2str(keyvalue, "%20.12G", wcs->crpix[j]); wcshdo_util(relax, "CRPIX", "CRP", WCSHDO_CRPXna, "CRPX", 0, j+1, 0, alt, colnum, colax, keyvalue, "Pixel coordinate of reference point", nkeyrec, header, &status); } /* Linear transformation matrix. */ k = 0; for (i = 0; i < naxis; i++) { for (j = 0; j < naxis; j++, k++) { if (i == j) { if (wcs->pc[k] == 1.0) continue; } else { if (wcs->pc[k] == 0.0) continue; } wcsutil_double2str(keyvalue, "%20.12G", wcs->pc[k]); wcshdo_util(relax, "PC", bintab ? "PC" : "P", WCSHDO_TPCn_ka, bintab ? 0x0 : "PC", i+1, j+1, 0, alt, colnum, colax, keyvalue, "Coordinate transformation matrix element", nkeyrec, header, &status); } } /* Coordinate increment at reference point. */ for (i = 0; i < naxis; i++) { wcsutil_double2str(keyvalue, "%20.12G", wcs->cdelt[i]); comment[0] = '\0'; if (wcs->cunit[i][0]) sprintf(comment, "[%s] ", wcs->cunit[i]); strcat(comment, "Coordinate increment at reference point"); wcshdo_util(relax, "CDELT", "CDE", WCSHDO_CRPXna, "CDLT", i+1, 0, 0, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* Units of coordinate increment and reference value. */ for (i = 0; i < naxis; i++) { if (wcs->cunit[i][0] == '\0') continue; sprintf(keyvalue, "'%s'", wcs->cunit[i]); wcshdo_util(relax, "CUNIT", "CUN", WCSHDO_CRPXna, "CUNI", i+1, 0, 0, alt, colnum, colax, keyvalue, "Units of coordinate increment and value", nkeyrec, header, &status); } /* Coordinate type. */ for (i = 0; i < naxis; i++) { if (wcs->ctype[i][0] == '\0') continue; sprintf(keyvalue, "'%s'", wcs->ctype[i]); strcpy(comment, "Coordinate type code"); if (i == wcs->lng || i == wcs->lat) { if (strncmp(wcs->ctype[i], "RA--", 4) == 0) { strcpy(comment, "Right ascension, "); } else if (strncmp(wcs->ctype[i], "DEC-", 4) == 0) { strcpy(comment, "Declination, "); } else if (strncmp(wcs->ctype[i]+1, "LON", 3) == 0 || strncmp(wcs->ctype[i]+1, "LAT", 3) == 0) { switch (wcs->ctype[i][0]) { case 'G': strcpy(comment, "galactic "); break; case 'E': strcpy(comment, "ecliptic "); case 'H': strcpy(comment, "helioecliptic "); case 'S': strcpy(comment, "supergalactic "); } if (i == wcs->lng) { strcat(comment, "longitude, "); } else { strcat(comment, "latitude, "); } wcs->ctype[i][0] = toupper(wcs->ctype[i][0]); } strcat(comment, wcs->cel.prj.name); strcat(comment, " projection"); } else if (i == wcs->spec) { spctyp(wcs->ctype[i], 0x0, 0x0, comment, 0x0, &ptype, &xtype, 0x0); if (ptype == xtype) { strcat(comment, " (linear)"); } else { switch (xtype) { case 'F': strcat(comment, " (linear in frequency)"); break; case 'V': strcat(comment, " (linear in velocity)"); break; case 'W': strcat(comment, " (linear in wavelength)"); break; } } } wcshdo_util(relax, "CTYPE", "CTY", WCSHDO_CRPXna, "CTYP", i+1, 0, 0, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* Coordinate value at reference point. */ for (i = 0; i < naxis; i++) { wcsutil_double2str(keyvalue, "%20.12G", wcs->crval[i]); comment[0] = '\0'; if (wcs->cunit[i][0]) sprintf(comment, "[%s] ", wcs->cunit[i]); strcat(comment, "Coordinate value at reference point"); wcshdo_util(relax, "CRVAL", "CRV", WCSHDO_CRPXna, "CRVL", i+1, 0, 0, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* Parameter values. */ for (k = 0; k < wcs->npv; k++) { wcsutil_double2str(keyvalue, "%20.12G", (wcs->pv[k]).value); if ((wcs->pv[k]).i == (wcs->lng + 1)) { switch ((wcs->pv[k]).m) { case 1: strcpy(comment, "[deg] Native longitude of the reference point"); break; case 2: strcpy(comment, "[deg] Native latitude of the reference point"); break; case 3: if (primage) { sprintf(keyword, "LONPOLE%c", alt); } else if (bintab) { sprintf(keyword, "LONP%d%c", colnum, alt); } else { sprintf(keyword, "LONP%d%c", colax[(wcs->pv[k]).i - 1], alt); } sprintf(comment, "[deg] alias for %s (has precedence)", keyword); break; case 4: if (primage) { sprintf(keyword, "LATPOLE%c", alt); } else if (bintab) { sprintf(keyword, "LATP%d%c", colnum, alt); } else { sprintf(keyword, "LATP%d%c", colax[(wcs->pv[k]).i - 1], alt); } sprintf(comment, "[deg] alias for %s (has precedence)", keyword); break; } } else if ((wcs->pv[k]).i == (wcs->lat + 1)) { sprintf(comment, "%s projection parameter", wcs->cel.prj.code); } else { strcpy(comment, "Coordinate transformation parameter"); } wcshdo_util(relax, "PV", "V", WCSHDO_PVn_ma, "PV", wcs->pv[k].i, -1, wcs->pv[k].m, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } for (k = 0; k < wcs->nps; k++) { sprintf(keyvalue, "'%s'", (wcs->ps[k]).value); wcshdo_util(relax, "PS", "S", WCSHDO_PVn_ma, "PS", wcs->ps[k].i, -1, wcs->ps[k].m, alt, colnum, colax, keyvalue, "Coordinate transformation parameter", nkeyrec, header, &status); } /* Celestial and spectral transformation parameters. */ if (!undefined(wcs->lonpole)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->lonpole); wcshdo_util(relax, "LONPOLE", "LONP", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[deg] Native longitude of celestial pole", nkeyrec, header, &status); } if (!undefined(wcs->latpole)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->latpole); wcshdo_util(relax, "LATPOLE", "LATP", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[deg] Native latitude of celestial pole", nkeyrec, header, &status); } if (!undefined(wcs->restfrq)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->restfrq); wcshdo_util(relax, "RESTFRQ", "RFRQ", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[Hz] Line rest frequency", nkeyrec, header, &status); } if (!undefined(wcs->restwav)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->restwav); wcshdo_util(relax, "RESTWAV", "RWAV", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[Hz] Line rest wavelength", nkeyrec, header, &status); } /* Coordinate system title. */ if (wcs->wcsname[0]) { sprintf(keyvalue, "'%s'", wcs->wcsname); if (bintab) { wcshdo_util(relax, "WCSNAME", "WCSN", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Coordinate system title", nkeyrec, header, &status); } else { /* TWCS was a mistake. */ wcshdo_util(relax, "WCSNAME", "TWCS", WCSHDO_WCSNna, "WCSN", 0, 0, 0, alt, colnum, colax, keyvalue, "Coordinate system title", nkeyrec, header, &status); } } /* Coordinate axis title. */ if (wcs->cname) { for (i = 0; i < naxis; i++) { if (wcs->cname[i][0] == '\0') continue; sprintf(keyvalue, "'%s'", wcs->cname[i]); wcshdo_util(relax, "CNAME", "CNA", WCSHDO_CNAMna, "CNAM", i+1, 0, 0, alt, colnum, colax, keyvalue, "Axis name for labelling purposes", nkeyrec, header, &status); } } /* Random error in coordinate. */ if (wcs->crder) { for (i = 0; i < naxis; i++) { if (undefined(wcs->crder[i])) continue; wcsutil_double2str(keyvalue, "%20.12G", wcs->crder[i]); comment[0] = '\0'; if (wcs->cunit[i][0]) sprintf(comment, "[%s] ", wcs->cunit[i]); strcat(comment, "Random error in coordinate"); wcshdo_util(relax, "CRDER", "CRD", WCSHDO_CNAMna, "CRDE", i+1, 0, 0, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } } /* Systematic error in coordinate. */ if (wcs->csyer) { for (i = 0; i < naxis; i++) { if (undefined(wcs->csyer[i])) continue; wcsutil_double2str(keyvalue, "%20.12G", wcs->csyer[i]); comment[0] = '\0'; if (wcs->cunit[i][0]) sprintf(comment, "[%s] ", wcs->cunit[i]); strcat(comment, "Systematic error in coordinate"); wcshdo_util(relax, "CSYER", "CSY", WCSHDO_CNAMna, "CSYE", i+1, 0, 0, alt, colnum, colax, keyvalue, comment, nkeyrec, header, &status); } } /* Equatorial coordinate system type. */ if (wcs->radesys[0]) { sprintf(keyvalue, "'%s'", wcs->radesys); wcshdo_util(relax, "RADESYS", "RADE", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Equatorial coordinate system", nkeyrec, header, &status); } /* Equinox of equatorial coordinate system. */ if (!undefined(wcs->equinox)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->equinox); wcshdo_util(relax, "EQUINOX", "EQUI", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[yr] Equinox of equatorial coordinates", nkeyrec, header, &status); } /* Reference frame of spectral coordinates. */ if (wcs->specsys[0]) { sprintf(keyvalue, "'%s'", wcs->specsys); wcshdo_util(relax, "SPECSYS", "SPEC", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Reference frame of spectral coordinates", nkeyrec, header, &status); } /* Reference frame of spectral observation. */ if (wcs->ssysobs[0]) { sprintf(keyvalue, "'%s'", wcs->ssysobs); wcshdo_util(relax, "SSYSOBS", "SOBS", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Reference frame of spectral observation", nkeyrec, header, &status); } /* Observer's velocity towards source. */ if (!undefined(wcs->velosys)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->velosys); wcshdo_util(relax, "VELOSYS", "VSYS", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "[m/s] Velocity towards source", nkeyrec, header, &status); } /* Reference frame of source redshift. */ if (wcs->ssyssrc[0]) { sprintf(keyvalue, "'%s'", wcs->ssyssrc); wcshdo_util(relax, "SSYSSRC", "SSRC", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Reference frame of source redshift", nkeyrec, header, &status); } /* Redshift of the source. */ if (!undefined(wcs->zsource)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->zsource); wcshdo_util(relax, "ZSOURCE", "ZSOU", 0, 0x0, 0, 0, 0, alt, colnum, colax, keyvalue, "Redshift of the source", nkeyrec, header, &status); } /* Observatory coordinates. */ for (k = 0; k < 3; k++) { if (undefined(wcs->obsgeo[k])) continue; wcsutil_double2str(keyvalue, "%20.12G", wcs->obsgeo[k]); sprintf(comment, "[m] ITRF observatory %c-coordinate", xyz[k]); obsgeo[7] = xyz[k]; obsg[4] = xyz[k]; wcshdo_util(relax, obsgeo, obsg, 0, 0x0, 0, 0, 0, ' ', colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* MJD of observation. */ if (!undefined(wcs->mjdobs)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->mjdobs); strcpy(comment, "[d] MJD of observation"); if (wcs->dateobs[0]) { if (primage || (relax & 1) == 0) { sprintf(comment+22, " matching DATE-OBS"); } else { sprintf(comment+22, " matching DOBS%d", col0); } } wcshdo_util(relax, "MJD-OBS", "MJDOB", 0, 0x0, 0, 0, 0, ' ', colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* MJD mid-observation time. */ if (!undefined(wcs->mjdavg)) { wcsutil_double2str(keyvalue, "%20.12G", wcs->mjdavg); strcpy(comment, "[d] MJD mid-observation"); if (wcs->dateavg[0]) { if (primage) { sprintf(comment+23, " matching DATE-AVG"); } else { sprintf(comment+23, " matching DAVG%d", col0); } } wcshdo_util(relax, "MJD-AVG", "MJDA", 0, 0x0, 0, 0, 0, ' ', colnum, colax, keyvalue, comment, nkeyrec, header, &status); } /* ISO-8601 date corresponding to MJD-OBS. */ if (wcs->dateobs[0]) { sprintf(keyvalue, "'%s'", wcs->dateobs); strcpy(comment, "ISO-8601 observation date"); if (!undefined(wcs->mjdobs)) { if (primage) { sprintf(comment+25, " matching MJD-OBS"); } else { sprintf(comment+25, " matching MJDOB%d", col0); } } if (relax & 1) { /* Allow DOBSn. */ wcshdo_util(relax, "DATE-OBS", "DOBS", WCSHDO_DOBSn, 0x0, 0, 0, 0, ' ', colnum, colax, keyvalue, comment, nkeyrec, header, &status); } else { /* Force DATE-OBS. */ wcshdo_util(relax, "DATE-OBS", 0x0, 0, 0x0, 0, 0, 0, ' ', 0, 0x0, keyvalue, comment, nkeyrec, header, &status); } } /* ISO-8601 date corresponding to MJD-OBS. */ if (wcs->dateavg[0]) { sprintf(keyvalue, "'%s'", wcs->dateavg); strcpy(comment, "ISO-8601 mid-observation date"); if (!undefined(wcs->mjdavg)) { if (primage) { sprintf(comment+29, " matching MJD-AVG"); } else { sprintf(comment+29, " matching MJDA%d", col0); } } wcshdo_util(relax, "DATE-AVG", "DAVG", 0, 0x0, 0, 0, 0, ' ', colnum, colax, keyvalue, comment, nkeyrec, header, &status); } if (status == WCSHDRERR_MEMORY) { wcserr_set(WCSHDR_ERRMSG(status)); } return status; }
int main() { char ctypeS[9]; int i, stat[NWCSFIX], status; struct wcsprm wcs; struct wcserr info[NWCSFIX]; wcsprintf("Testing WCSLIB translator for non-standard usage (twcsfix.c)\n" "------------------------------------------------------------\n\n"); wcs.flag = -1; parser(&wcs); /* Note: to print the unfixed wcsprm struct using wcsprt() the struct would first have to be initialized by wcsset(). However, if the struct contains non-standard keyvalues then wcsset() will either fix them itself or else fail (e.g. for non-standard units). Thus, in general, wcsprt() cannot be used to print the unmodified struct. */ /* Fix non-standard WCS keyvalues. */ wcserr_enable(1); status = wcsfixi(7, 0, &wcs, stat, info); wcsprintf("wcsfix status returns: ("); for (i = 0; i < NWCSFIX; i++) { wcsprintf(i ? ", %d" : "%d", stat[i]); } wcsprintf(")\n"); for (i = 0; i < NWCSFIX; i++) { if (info[i].status < -1 || 0 < info[i].status) { wcsprintf("\n"); wcserr_prt(info+i, 0x0); } } if (status) { wcsprintf("\nwcsfix error %d", status); return 1; } /* Extract information from the FITS header. */ if (wcsset(&wcs)) { wcsprintf("\n"); wcserr_prt(wcs.err, 0x0); } wcsprintf("\n"); wcsprt(&wcs); wcsprintf("\n------------------------------------" "------------------------------------\n"); /* Should now have a 'VOPT-F2W' axis, translate it to frequency. */ strcpy(ctypeS, "FREQ-???"); i = -1; if (wcssptr(&wcs, &i, ctypeS)) { wcserr_prt(wcs.err, 0x0); return 1; } if (wcsset(&wcs)) { wcserr_prt(wcs.err, 0x0); return 1; } wcsprt(&wcs); wcsfree(&wcs); return 0; }
int wcsset_(int *wcs) { return wcsset((struct wcsprm *)wcs); }
int cylfix(const int naxis[], struct wcsprm *wcs) { static const char *function = "cylfix"; unsigned short icnr, indx[NMAX], ncnr; int j, k, stat[4], status; double img[4][NMAX], lat, lng, phi[4], phi0, phimax, phimin, pix[4][NMAX], *pixj, theta[4], theta0, world[4][NMAX], x, y; struct wcserr **err; if (naxis == 0x0) return FIXERR_NO_CHANGE; if (wcs == 0x0) return FIXERR_NULL_POINTER; err = &(wcs->err); /* Initialize if required. */ if (wcs->flag != WCSSET) { if ((status = wcsset(wcs))) return status; } /* Check that we have a cylindrical projection. */ if (wcs->cel.prj.category != CYLINDRICAL) return FIXERR_NO_CHANGE; if (wcs->naxis < 2) return FIXERR_NO_CHANGE; /* Compute the native longitude in each corner of the image. */ ncnr = 1 << wcs->naxis; for (k = 0; k < NMAX; k++) { indx[k] = 1 << k; } phimin = 1.0e99; phimax = -1.0e99; for (icnr = 0; icnr < ncnr;) { /* Do four corners at a time. */ for (j = 0; j < 4; j++, icnr++) { pixj = pix[j]; for (k = 0; k < wcs->naxis; k++) { if (icnr & indx[k]) { *(pixj++) = naxis[k] + 0.5; } else { *(pixj++) = 0.5; } } } if (!(status = wcsp2s(wcs, 4, NMAX, pix[0], img[0], phi, theta, world[0], stat))) { for (j = 0; j < 4; j++) { if (phi[j] < phimin) phimin = phi[j]; if (phi[j] > phimax) phimax = phi[j]; } } } if (phimin > phimax) return status; /* Any changes needed? */ if (phimin >= -180.0 && phimax <= 180.0) return FIXERR_NO_CHANGE; /* Compute the new reference pixel coordinates. */ phi0 = (phimin + phimax) / 2.0; theta0 = 0.0; if ((status = prjs2x(&(wcs->cel.prj), 1, 1, 1, 1, &phi0, &theta0, &x, &y, stat))) { if (status == PRJERR_BAD_PARAM) { return wcserr_set(WCSFIX_ERRMSG(FIXERR_BAD_PARAM)); } return wcserr_set(WCSFIX_ERRMSG(FIXERR_NO_REF_PIX_COORD)); } for (k = 0; k < wcs->naxis; k++) { img[0][k] = 0.0; } img[0][wcs->lng] = x; img[0][wcs->lat] = y; if ((status = linx2p(&(wcs->lin), 1, 0, img[0], pix[0]))) { return wcserr_set(WCSFIX_ERRMSG(status)); } /* Compute celestial coordinates at the new reference pixel. */ if ((status = wcsp2s(wcs, 1, 0, pix[0], img[0], phi, theta, world[0], stat))) { if (wcs->err->status == WCSERR_BAD_PIX) { wcs->err->status = FIXERR_NO_REF_PIX_COORD; } return wcs->err->status; } /* Compute native coordinates of the celestial pole. */ lng = 0.0; lat = 90.0; (void)sphs2x(wcs->cel.euler, 1, 1, 1, 1, &lng, &lat, phi, theta); wcs->crpix[wcs->lng] = pix[0][wcs->lng]; wcs->crpix[wcs->lat] = pix[0][wcs->lat]; wcs->crval[wcs->lng] = world[0][wcs->lng]; wcs->crval[wcs->lat] = world[0][wcs->lat]; wcs->lonpole = phi[0] - phi0; return wcsset(wcs); }
int main(int argc, char **argv) { char alt = ' ', *header, idents[3][80], *infile; int alts[27], c, dofix = 0, doprt = 0, dopix = 0, doworld = 0, hdunum = 1, hdutype, i, j, nelem, nkeyrec, nreject, nwcs, *stat = 0x0, status; double *imgcrd = 0x0, phi, *pixcrd = 0x0, theta, *world = 0x0; struct wcsprm *wcs; fitsfile *fptr; /* Parse options. */ for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (!argv[i][1]) break; switch (argv[i][1]) { case 'a': alt = toupper(argv[i][2]); break; case 'f': dofix = 1; break; case 'h': hdunum = atoi(argv[i]+2); break; case 'p': doprt = 1; break; case 'x': dopix = 1; break; case 'w': doworld = 1; break; default: fprintf(stderr, "%s", usage); return 1; } } if (i < argc) { infile = argv[i++]; if (i < argc) { fprintf(stderr, "%s", usage); return 1; } } else { infile = "-"; } /* Check accessibility of the input file. */ if (strcmp(infile, "-") && access(infile, R_OK) == -1) { printf("wcsware: Cannot access %s.\n", infile); return 1; } if (!dopix && !doworld) doprt = 1; /* Open the FITS file and move to the required HDU. */ status = 0; if (fits_open_file(&fptr, infile, READONLY, &status)) goto fitserr; if (fits_movabs_hdu(fptr, hdunum, &hdutype, &status)) goto fitserr; if (hdutype != IMAGE_HDU) { fprintf(stderr, "ERROR, HDU number %d does not contain an image array.\n", hdunum); return 1; } /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) { goto fitserr; } /* Interpret the WCS keywords. */ if ((status = wcspih(header, nkeyrec, WCSHDR_all, -3, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcshdr_errmsg[status]); return 1; } free(header); if (wcs == 0x0) { fprintf(stderr, "No world coordinate systems found.\n"); return 1; } /* Read -TAB arrays from the binary table extension (if necessary). */ if (fits_read_wcstab(fptr, wcs->nwtb, (wtbarr *)wcs->wtb, &status)) { goto fitserr; } fits_close_file(fptr, &status); /* Translate non-standard WCS keyvalues? */ if (dofix) { stat = malloc(NWCSFIX * sizeof(int)); if ((status = wcsfix(7, 0, wcs, stat))) { for (i = 0; i < NWCSFIX; i++) { if (stat[i] > 0) { fprintf(stderr, "wcsfix ERROR %d: %s.\n", status, wcsfix_errmsg[stat[i]]); } } return 1; } } /* Sort out alternates. */ if (alt) { wcsidx(nwcs, &wcs, alts); if (alt == ' ') { if (alts[0] == -1) { fprintf(stderr, "WARNING, no primary coordinate representation.\n"); alt = '\0'; } } else if (alt < 'A' || alt > 'Z') { fprintf(stderr, "WARNING, alternate specifier \"%c\" is invalid.\n", alt); alt = '\0'; } else { if (alts[alt - 'A' + 1] == -1) { fprintf(stderr, "WARNING, no alternate coordinate representation " "\"%c\".\n", alt); alt = '\0'; } } } /* Initialize and possibly print the structs. */ for (i = 0; i < nwcs; i++) { if (alt && (wcs+i)->alt[0] != alt) { continue; } else if (i) { printf("\nType <CR> for next: "); fgetc(stdin); } if ((status = wcsset(wcs+i))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); continue; } /* Get WCSNAME out of the wcsprm struct. */ strcpy(idents[2], (wcs+i)->wcsname); if (strlen(idents[2])) { printf("\n%s\n", idents[2]); } /* Print the struct. */ if (doprt) { wcsprt(wcs+i); } /* Transform coordinates? */ if (dopix || doworld) { nelem = (wcs+i)->naxis; world = realloc(world, nelem * sizeof(double)); imgcrd = realloc(imgcrd, nelem * sizeof(double)); pixcrd = realloc(pixcrd, nelem * sizeof(double)); stat = realloc(stat, nelem * sizeof(int)); if (dopix) { /* Transform pixel coordinates. */ while (1) { printf("\nEnter %d pixel coordinate element%s: ", nelem, (nelem==1)?"":"s"); c = fgetc(stdin); if (c == EOF || c == '\n') { if (c == EOF) printf("\n"); break; } ungetc(c, stdin); scanf("%lf", pixcrd); for (j = 1; j < nelem; j++) { scanf("%*[ ,]%lf", pixcrd+j); } while (fgetc(stdin) != '\n'); printf("Pixel: "); for (j = 0; j < nelem; j++) { printf("%s%14.9g", j?", ":"", pixcrd[j]); } if ((status = wcsp2s(wcs+i, 1, nelem, pixcrd, imgcrd, &phi, &theta, world, stat))) { fprintf(stderr, "wcsp2s ERROR %d: %s.\n", status, wcs_errmsg[status]); } else { printf("\nImage: "); for (j = 0; j < nelem; j++) { if (j == (wcs+i)->lng || j == (wcs+i)->lat) { /* Print angles in fixed format. */ printf("%s%14.6f", j?", ":"", imgcrd[j]); } else { printf("%s%14.9g", j?", ":"", imgcrd[j]); } } printf("\nWorld: "); for (j = 0; j < nelem; j++) { if (j == (wcs+i)->lng || j == (wcs+i)->lat) { /* Print angles in fixed format. */ printf("%s%14.6f", j?", ":"", world[j]); } else { printf("%s%14.9g", j?", ":"", world[j]); } } printf("\n"); } } } if (doworld) { /* Transform world coordinates. */ while (1) { printf("\nEnter %d world coordinate element%s: ", nelem, (nelem==1)?"":"s"); c = fgetc(stdin); if (c == EOF || c == '\n') { if (c == EOF) printf("\n"); break; } ungetc(c, stdin); scanf("%lf", world); for (j = 1; j < nelem; j++) { scanf("%*[ ,]%lf", world+j); } while (fgetc(stdin) != '\n'); printf("World: "); for (j = 0; j < nelem; j++) { if (j == (wcs+i)->lng || j == (wcs+i)->lat) { /* Print angles in fixed format. */ printf("%s%14.6f", j?", ":"", world[j]); } else { printf("%s%14.9g", j?", ":"", world[j]); } } if ((status = wcss2p(wcs+i, 1, nelem, world, &phi, &theta, imgcrd, pixcrd, stat))) { fprintf(stderr, "wcss2p ERROR %d: %s.\n", status, wcs_errmsg[status]); } else { printf("\nImage: "); for (j = 0; j < nelem; j++) { if (j == (wcs+i)->lng || j == (wcs+i)->lat) { /* Print angles in fixed format. */ printf("%s%14.6f", j?", ":"", imgcrd[j]); } else { printf("%s%14.9g", j?", ":"", imgcrd[j]); } } printf("\nPixel: "); for (j = 0; j < nelem; j++) { printf("%s%14.9g", j?", ":"", pixcrd[j]); } printf("\n"); } } } } } status = wcsvfree(&nwcs, &wcs); return 0; fitserr: fits_report_error(stderr, status); fits_close_file(fptr, &status); return 1; }
int celfix(struct wcsprm *wcs) { int k, status; struct celprm *wcscel = &(wcs->cel); struct prjprm *wcsprj = &(wcscel->prj); /* Initialize if required. */ if (wcs == 0x0) return 1; if (wcs->flag != WCSSET) { if (status = wcsset(wcs)) return status; } /* Was an NCP or GLS projection code translated? */ if (wcs->lat >= 0) { /* Check ctype. */ if (strcmp(wcs->ctype[wcs->lat]+5, "NCP") == 0) { strcpy(wcs->ctype[wcs->lng]+5, "SIN"); strcpy(wcs->ctype[wcs->lat]+5, "SIN"); if (wcs->npvmax < wcs->npv + 2) { /* Allocate space for two more PVi_ja cards. */ if (wcs->m_flag == WCSSET && wcs->pv == wcs->m_pv) { if (!(wcs->pv = calloc(wcs->npv+2, sizeof(struct pvcard)))) { wcs->pv = wcs->m_pv; return 2; } wcs->npvmax = wcs->npv + 2; wcs->m_flag = WCSSET; for (k = 0; k < wcs->npv; k++) { wcs->pv[k] = wcs->m_pv[k]; } if (wcs->m_pv) free(wcs->m_pv); wcs->m_pv = wcs->pv; } else { return 2; } } wcs->pv[wcs->npv].i = wcs->lat + 1; wcs->pv[wcs->npv].m = 1; wcs->pv[wcs->npv].value = wcsprj->pv[1]; (wcs->npv)++; wcs->pv[wcs->npv].i = wcs->lat + 1; wcs->pv[wcs->npv].m = 2; wcs->pv[wcs->npv].value = wcsprj->pv[2]; (wcs->npv)++; return 0; } else if (strcmp(wcs->ctype[wcs->lat]+5, "GLS") == 0) { strcpy(wcs->ctype[wcs->lng]+5, "SFL"); strcpy(wcs->ctype[wcs->lat]+5, "SFL"); return 0; } } return -1; }
int main() { char infile[] = "bth.fits"; char a, *hptr; short alts[1000][28]; int colsel[8], ctrl, ialt, iblock, icol, ifix, ikeyrec, iwcs, keysel, nkeyrec, nreject, nwcs, relax, stat[NWCSFIX], status; struct wcsprm *wcs; #if defined HAVE_CFITSIO && defined DO_CFITSIO char *header; fitsfile *fptr; #else char keyrec[81], header[288001]; int gotend, k; FILE *fptr; #endif /* Set line buffering in case stdout is redirected to a file, otherwise * stdout and stderr messages will be jumbled (stderr is unbuffered). */ setvbuf(stdout, NULL, _IOLBF, 0); printf("Testing WCSLIB parser for FITS binary table headers (tbth1.c)\n" "-------------------------------------------------------------\n\n"); /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ #if defined HAVE_CFITSIO && defined DO_CFITSIO status = 0; if (fits_open_file(&fptr, infile, READONLY, &status)) { fits_report_error(stderr, status); return 1; } if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) { fits_report_error(stderr, status); return 1; } fits_close_file(fptr, &status); #else if ((fptr = fopen(infile, "r")) == 0) { fprintf(stderr, "ERROR opening %s\n", infile); return 1; } k = 0; nkeyrec = 0; gotend = 0; for (iblock = 0; iblock < 100; iblock++) { for (ikeyrec = 0; ikeyrec < 36; ikeyrec++) { if (fgets(keyrec, 81, fptr) == 0) { break; } if (strncmp(keyrec, " ", 8) == 0) continue; if (strncmp(keyrec, "COMMENT ", 8) == 0) continue; if (strncmp(keyrec, "HISTORY ", 8) == 0) continue; strncpy(header+k, keyrec, 80); k += 80; nkeyrec++; if (strncmp(keyrec, "END ", 8) == 0) { /* An END keyrecord was read, but read the rest of the block. */ gotend = 1; } } if (gotend) break; } fclose(fptr); #endif fprintf(stderr, "Found %d non-comment header keyrecords.\n\n", nkeyrec); /* Parse the header, allowing all recognized non-standard WCS keywords and * usage. WCS keyrecords that are used are culled from the header, illegal * ones are reported. */ relax = WCSHDR_all; ctrl = -2; keysel = 0; colsel[0] = 0; fprintf(stderr, "\nIllegal or extraneous WCS header keyrecords rejected " "by wcsbth():\n"); if ((status = wcsbth(header, nkeyrec, relax, ctrl, keysel, colsel, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcsbth ERROR %d: %s.\n", status, wcs_errmsg[status]); } if (!nreject) fprintf(stderr, "(none)\n"); /* List the remaining keyrecords. */ printf("\n\nNon-WCS header keyrecords ignored by wcsbth():\n"); hptr = header; while (*hptr) { printf("%.80s\n", hptr); hptr += 80; } #if defined HAVE_CFITSIO && defined DO_CFITSIO free(header); #endif /* Summarize what was found. */ printf("\n\nExtracted %d coordinate description%s:\n", nwcs, (nwcs == 1) ? "" : "s"); printf("\n Unattached image header(s)"); status = wcsbdx(nwcs, &wcs, 0, alts); if (alts[0][27]) { printf(" with indices:\n -"); for (a = 'A'; a <= 'Z'; a++) { printf("%2c", a); } printf("\n "); for (ialt = 0; ialt < 27; ialt++) { if (alts[0][ialt] < 0) { printf(" -"); } else { printf("%2d", alts[0][ialt]); } } printf("\n"); } else { printf(": (none)\n"); } printf("\n Binary table image array(s)"); for (icol = 1; icol <= 999; icol++) { if (alts[icol][27]) { printf(" with indices:\n Col. -"); for (a = 'A'; a <= 'Z'; a++) { printf("%2c", a); } printf("\n"); for (icol = 1; icol <= 999; icol++) { for (ialt = 0; ialt < 27; ialt++) { if (alts[icol][ialt] >= 0) { printf("%5d: ", icol); for (ialt = 0; ialt < 27; ialt++) { if (alts[icol][ialt] < 0) { printf(" -"); } else { printf("%2d", alts[icol][ialt]); } } printf("\n"); break; } } } icol = 9999; } } if (icol < 9999) { printf(": (none)\n"); } printf("\n Pixel list(s)"); status = wcsbdx(nwcs, &wcs, 1, alts); for (icol = 1; icol <= 999; icol++) { if (alts[icol][27]) { printf(" with indices:\n Col. -"); for (a = 'A'; a <= 'Z'; a++) { printf("%2c", a); } printf("\n"); for (icol = 1; icol <= 999; icol++) { for (ialt = 0; ialt < 27; ialt++) { if (alts[icol][ialt] >= 0) { printf("%5d: ", icol); for (ialt = 0; ialt < 27; ialt++) { if (alts[icol][ialt] < 0) { printf(" -"); } else { printf("%2d", alts[icol][ialt]); } } printf("\n"); break; } } } icol = 9999; } } if (icol < 9999) { printf(": (none)\n"); } /* Fix non-standard usage and print each of the wcsprm structs. */ for (iwcs = 0; iwcs < nwcs; iwcs++) { printf("\n------------------------------------" "------------------------------------\n"); /* Fix non-standard WCS keyvalues. */ if ((status = wcsfix(7, 0, wcs+iwcs, stat))) { printf("wcsfix ERROR, status returns: ("); for (ifix = 0; ifix < NWCSFIX; ifix++) { printf(ifix ? ", %d" : "%d", stat[ifix]); } printf(")\n\n"); } if ((status = wcsset(wcs+iwcs))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); continue; } if ((status = wcsprt(wcs+iwcs))) { fprintf(stderr, "wcsprt ERROR %d: %s.\n", status, wcs_errmsg[status]); } } status = wcsvfree(&nwcs, &wcs); /* Do it again to check that wcsbth() can handle multiple invokations. */ printf("\nInvoking wcsbth() a second time on the same header...\n"); ctrl = 0; if ((status = wcsbth(header, nkeyrec, relax, 0, keysel, colsel, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcsbth ERROR %d: %s.\n", status, wcs_errmsg[status]); } else { printf("OK, extracted %d coordinate description%s.\n", nwcs, (nwcs == 1) ? "" : "s"); } return 0; }
int main(int argc, char **argv) { char alt = '\0', *header, idents[3][80], *infile, keyword[16], nlcprm[1], opt[2], pgdev[16]; int c0[] = {-1, -1, -1, -1, -1, -1, -1}; int alts[27], gcode[2], hdunum = 1, hdutype, i, ic, naxes, naxis[2], nkeyrec, nreject, nwcs, stat[NWCSFIX], status; float blc[2], trc[2]; double cache[257][4], grid1[3], grid2[3], nldprm[1]; struct wcsprm *wcs; nlfunc_t pgwcsl_; fitsfile *fptr; /* Parse options. */ strcpy(pgdev, "/XWINDOW"); for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (!argv[i][1]) break; switch (argv[i][1]) { case 'a': alt = toupper(argv[i][2]); break; case 'd': if (argv[i][2] == '?') { cpgldev(); return 0; } if (argv[i][2] == '/') { strncpy(pgdev+1, argv[i]+3, 15); } else { strncpy(pgdev+1, argv[i]+2, 15); } break; case 'h': hdunum = atoi(argv[i]+2); break; default: fprintf(stderr, "%s", usage); return 1; } } if (i < argc) { infile = argv[i++]; if (i < argc) { fprintf(stderr, "%s", usage); return 1; } } else { infile = "-"; } /* Check accessibility of the input file. */ if (strcmp(infile, "-") && access(infile, R_OK) == -1) { printf("wcsgrid: Cannot access %s.\n", infile); return 1; } /* Open the FITS file and move to the required HDU. */ status = 0; if (fits_open_file(&fptr, infile, READONLY, &status)) goto fitserr; if (fits_movabs_hdu(fptr, hdunum, &hdutype, &status)) goto fitserr; if (hdutype != IMAGE_HDU) { fprintf(stderr, "ERROR, HDU number %d does not contain an image array.\n", hdunum); return 1; } /* Check that we have at least two image axes. */ if (fits_read_key(fptr, TINT, "NAXIS", &naxes, NULL, &status)) { goto fitserr; } if (naxes < 2) { fprintf(stderr, "ERROR, HDU number %d does not contain a 2-D image.\n", hdunum); return 1; } else if (naxes > 2) { printf("HDU number %d contains a %d-D image array.\n", hdunum, naxes); } /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) { goto fitserr; } /* Interpret the WCS keywords. */ if ((status = wcspih(header, nkeyrec, WCSHDR_all, -3, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcshdr_errmsg[status]); return 1; } free(header); /* Read -TAB arrays from the binary table extension (if necessary). */ if (fits_read_wcstab(fptr, wcs->nwtb, (wtbarr *)wcs->wtb, &status)) { goto fitserr; } /* Translate non-standard WCS keyvalues. */ if ((status = wcsfix(7, 0, wcs, stat))) { status = 0; for (i = 0; i < NWCSFIX; i++) { if (stat[i] > 0) { fprintf(stderr, "wcsfix ERROR %d: %s.\n", stat[i], wcsfix_errmsg[stat[i]]); /* Ignore problems with CDi_ja and DATE-OBS. */ if (!(i == CDFIX || i == DATFIX)) status = 1; } } if (status) return 1; } /* Sort out alternates. */ if (alt) { wcsidx(nwcs, &wcs, alts); if (alt == ' ') { if (alts[0] == -1) { fprintf(stderr, "WARNING, no primary coordinate representation, " "doing all.\n"); alt = '\0'; } } else if (alt < 'A' || alt > 'Z') { fprintf(stderr, "WARNING, alternate specifier \"%c\" is invalid, " "doing all.\n", alt); alt = '\0'; } else { if (alts[alt - 'A' + 1] == -1) { fprintf(stderr, "WARNING, no alternate coordinate representation " "\"%c\", doing all.\n", alt); alt = '\0'; } } } /* Get image dimensions from the header. */ sprintf(keyword, "NAXIS%d", wcs->lng + 1); fits_read_key(fptr, TINT, "NAXIS1", naxis, NULL, &status); sprintf(keyword, "NAXIS%d", wcs->lat + 1); fits_read_key(fptr, TINT, "NAXIS2", naxis+1, NULL, &status); if ((naxis[0] < 2) || (naxis[1] < 2)) { fprintf(stderr, "ERROR, HDU number %d contains degenerate image axes.\n", hdunum); return 1; } fits_close_file(fptr, &status); /* Plot setup. */ blc[0] = 0.5f; blc[1] = 0.5f; trc[0] = naxis[0] + 0.5f; trc[1] = naxis[1] + 0.5f; if (cpgbeg(0, pgdev, 1, 1) != 1) { fprintf(stderr, "ERROR, failed to open PGPLOT device %s.\n", pgdev); return 1; } cpgvstd(); cpgwnad(blc[0], trc[0], blc[0], trc[1]); cpgask(1); cpgpage(); /* Compact lettering. */ cpgsch(0.8f); /* Draw full grid lines. */ gcode[0] = 2; gcode[1] = 2; grid1[0] = 0.0; grid2[0] = 0.0; /* These are for the projection boundary. */ grid1[1] = -180.0; grid1[2] = 180.0; grid2[1] = -90.0; grid2[2] = 90.0; cpgsci(1); for (i = 0; i < nwcs; i++) { if (alt && (wcs+i)->alt[0] != alt) { continue; } if ((status = wcsset(wcs+i))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); continue; } /* Draw the frame. */ cpgbox("BC", 0.0f, 0, "BC", 0.0f, 0); /* Axis labels; use CNAMEia in preference to CTYPEia. */ if ((wcs+i)->cname[0][0]) { strcpy(idents[0], (wcs+i)->cname[0]); } else { strcpy(idents[0], (wcs+i)->ctype[0]); } if ((wcs+i)->cname[1][0]) { strcpy(idents[1], (wcs+i)->cname[1]); } else { strcpy(idents[1], (wcs+i)->ctype[1]); } /* Title; use WCSNAME. */ strcpy(idents[2], (wcs+i)->wcsname); if (strlen(idents[2])) { printf("\n%s\n", idents[2]); } /* Formatting control for celestial coordinates. */ if (strncmp((wcs+i)->ctype[0], "RA", 2) == 0) { /* Right ascension in HMS, declination in DMS. */ opt[0] = 'G'; opt[1] = 'E'; } else { /* Other angles in decimal degrees. */ opt[0] = 'A'; opt[1] = 'B'; } /* Draw the celestial grid. The grid density is set for each world */ /* coordinate by specifying LABDEN = 1224. */ ic = -1; cpgsbox(blc, trc, idents, opt, 0, 1224, c0, gcode, 0.0, 0, grid1, 0, grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256, &ic, cache, &status); /* Delimit the projection boundary. */ if ((wcs+i)->cel.prj.category != ZENITHAL) { /* Reset to the native coordinate graticule. */ (wcs+i)->crval[0] = (wcs+i)->cel.prj.phi0; (wcs+i)->crval[1] = (wcs+i)->cel.prj.theta0; (wcs+i)->lonpole = 999.0; (wcs+i)->latpole = 999.0; status = wcsset(wcs+i); ic = -1; cpgsbox(blc, trc, idents, opt, -1, 0, c0, gcode, 0.0, 2, grid1, 2, grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256, &ic, cache, &status); } cpgpage(); } status = wcsvfree(&nwcs, &wcs); return 0; fitserr: fits_report_error(stderr, status); fits_close_file(fptr, &status); return 1; }
int main() { char infile[] = "pih.fits"; char a, *hptr; int alts[27], ctrl, ialt, iblock, ifix, ikeyrec, iwcs, nkeyrec, nreject, nwcs, relax, stat[NWCSFIX], status; struct wcsprm *wcs; #if defined HAVE_CFITSIO && defined DO_CFITSIO char *header; fitsfile *fptr; #else char keyrec[81], header[288001]; int gotend, k; FILE *fptr; #endif /* Set line buffering in case stdout is redirected to a file, otherwise * stdout and stderr messages will be jumbled (stderr is unbuffered). */ setvbuf(stdout, NULL, _IOLBF, 0); printf("Testing WCSLIB parser for FITS image headers (tpih1.c)\n" "------------------------------------------------------\n\n"); /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ #if defined HAVE_CFITSIO && defined DO_CFITSIO status = 0; if (fits_open_file(&fptr, infile, READONLY, &status)) { fits_report_error(stderr, status); return 1; } if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) { fits_report_error(stderr, status); return 1; } fits_close_file(fptr, &status); #else if ((fptr = fopen(infile, "r")) == 0) { fprintf(stderr, "ERROR opening %s\n", infile); return 1; } k = 0; nkeyrec = 0; gotend = 0; for (iblock = 0; iblock < 100; iblock++) { for (ikeyrec = 0; ikeyrec < 36; ikeyrec++) { if (fgets(keyrec, 81, fptr) == 0) { break; } if (strncmp(keyrec, " ", 8) == 0) continue; if (strncmp(keyrec, "COMMENT ", 8) == 0) continue; if (strncmp(keyrec, "HISTORY ", 8) == 0) continue; strncpy(header+k, keyrec, 80); k += 80; nkeyrec++; if (strncmp(keyrec, "END ", 8) == 0) { /* An END keyrecord was read, but read the rest of the block. */ gotend = 1; } } if (gotend) break; } fclose(fptr); #endif fprintf(stderr, "Found %d non-comment header keyrecords.\n\n", nkeyrec); /* Parse the header, allowing all recognized non-standard WCS keywords and * usage. All WCS keyrecords are culled from the header, illegal ones are * reported. */ relax = WCSHDR_all; ctrl = -2; fprintf(stderr, "\nIllegal or extraneous WCS header keyrecords rejected " "by wcspih():\n"); if ((status = wcspih(header, nkeyrec, relax, ctrl, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcs_errmsg[status]); } if (!nreject) fprintf(stderr, "(none)\n"); /* List the remaining keyrecords. */ printf("\n\nNon-WCS header keyrecords ignored by wcspih():\n"); hptr = header; while (*hptr) { printf("%.80s\n", hptr); hptr += 80; } #if defined HAVE_CFITSIO && defined DO_CFITSIO free(header); #endif /* Summarize what was found. */ status = wcsidx(nwcs, &wcs, alts); printf("\n\nFound %d alternate coordinate descriptions with indices:\n ", nwcs); for (a = 'A'; a <= 'Z'; a++) { printf("%2c", a); } printf("\n"); for (ialt = 0; ialt < 27; ialt++) { if (alts[ialt] < 0) { printf(" -"); } else { printf("%2d", alts[ialt]); } } printf("\n"); /* Fix non-standard usage and print each of the wcsprm structs. The output * from wcsprt() will be written to an internal buffer and then printed just * to show that it can be done. */ wcsprintf_set(0x0); for (iwcs = 0; iwcs < nwcs; iwcs++) { wcsprintf("\n------------------------------------" "------------------------------------\n"); /* Fix non-standard WCS keyvalues. */ if ((status = wcsfix(7, 0, wcs+iwcs, stat))) { printf("wcsfix ERROR, status returns: ("); for (ifix = 0; ifix < NWCSFIX; ifix++) { printf(ifix ? ", %d" : "%d", stat[ifix]); } printf(")\n\n"); } if ((status = wcsset(wcs+iwcs))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); continue; } if ((status = wcsprt(wcs+iwcs))) { fprintf(stderr, "wcsprt ERROR %d: %s.\n", status, wcs_errmsg[status]); } } printf("%s", wcsprintf_buf()); status = wcsvfree(&nwcs, &wcs); return 0; }
int main(int argc, char *argv[]) { char *header, *hptr; int dohdr = 0, dopixel = 0, doworld = 0; int i, nkeyrec, nreject, nwcs, stat[NWCSFIX], status = 0; double imgcrd[2], phi, pixcrd[2], theta, world[2]; fitsfile *fptr; struct wcsprm *wcs; /* Parse options. */ for (i = 1; i < argc && argv[i][0] == '-'; i++) { if (!argv[i][1]) break; switch (argv[i][1]) { case 'h': dohdr = 1; break; case 'p': dopixel = 1; break; case 'w': doworld = 1; break; default: fprintf(stderr, "Usage: twcshdr [-h | -p | -w] <file>\n"); return 1; } } if (i != (argc-1)) { fprintf(stderr, "Usage: twcshdr [-h | -p | -w] <file>\n"); return 1; } /* Open the FITS test file and read the primary header. */ fits_open_file(&fptr, argv[i], READONLY, &status); if ((status = fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status))) { fits_report_error(stderr, status); return 1; } /*-----------------------------------------------------------------------*/ /* Basic steps required to interpret a FITS WCS header, including -TAB. */ /*-----------------------------------------------------------------------*/ /* Parse the primary header of the FITS file. */ if ((status = wcspih(header, nkeyrec, WCSHDR_all, 2, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcspih ERROR %d: %s.\n", status,wcshdr_errmsg[status]); } /* Read coordinate arrays from the binary table extension. */ if ((status = fits_read_wcstab(fptr, wcs->nwtb, (wtbarr *)wcs->wtb, &status))) { fits_report_error(stderr, status); return 1; } /* Translate non-standard WCS keyvalues. */ if ((status = wcsfix(7, 0, wcs, stat))) { for (i = 0; i < NWCSFIX; i++) { if (stat[i] > 0) { fprintf(stderr, "wcsfix ERROR %d: %s.\n", status, wcsfix_errmsg[stat[i]]); } } return 1; } /*-----------------------------------------------------------------------*/ /* The wcsprm struct is now ready for use. */ /*-----------------------------------------------------------------------*/ /* Finished with the FITS file. */ fits_close_file(fptr, &status); free(header); /* Initialize the wcsprm struct, also taking control of memory allocated by * fits_read_wcstab(). */ if ((status = wcsset(wcs))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); return 1; } if (dohdr) { if ((status = wcshdo(WCSHDO_all, wcs, &nkeyrec, &header))) { return 1; } hptr = header; printf("\n\n"); for (i = 0; i < nkeyrec; i++, hptr += 80) { printf("%.80s\n", hptr); } free(header); } else if (dopixel) { while (1) { printf("Enter pixel coordinates: "); if (scanf("%lf%*[ ,]%lf", pixcrd, pixcrd+1) != wcs->naxis) break; status = wcsp2s(wcs, 1, 2, pixcrd, imgcrd, &phi, &theta, world, stat); printf(" (%20.15f, %20.15f) ->\n (%20.15f, %20.15f)\n\n", pixcrd[0], pixcrd[1], world[0], world[1]); } } else if (doworld) { while (1) { printf("Enter world coordinates: "); if (scanf("%lf%*[ ,]%lf", world, world+1) != wcs->naxis) break; status = wcss2p(wcs, 1, 2, world, &phi, &theta, imgcrd, pixcrd, stat); printf(" (%20.15f, %20.15f) ->\n (%20.15f, %20.15f)\n\n", world[0], world[1], pixcrd[0], pixcrd[1]); } } else { /* Print the struct. */ if ((status = wcsprt(wcs))) { return 1; } } /* Clean up. */ status = wcsvfree(&nwcs, &wcs); return 0; }
/** * @brief * add_service_account: creates the PBS service account if it doesn't exist, * otherwise, validate the password against the existing the service * account. * * @param[in] password - The password to be validated. * * @return int */ int add_service_account(char *password) { char dname[PBS_MAXHOSTNAME+1]; char dctrl[PBS_MAXHOSTNAME+1]; wchar_t unamew[UNLEN+1]; wchar_t dnamew[UNLEN+1]; wchar_t dctrlw[PBS_MAXHOSTNAME+1]; NET_API_STATUS nstatus; USER_INFO_1 *ui1_ptr = NULL; /* better indicator of lookup */ /* permission */ struct passwd *pw = NULL; char sa_name[PBS_MAXHOSTNAME+UNLEN+2]; /* service account fullname */ /* domain\user\0 */ int ret_val = 0; int in_domain_environment; USER_INFO_1 ui; wchar_t passwordw[LM20_PWLEN+1]; /* find domain name, group name to add service account to */ in_domain_environment = GetComputerDomainName(dname); strcpy(dctrl, dname); if (in_domain_environment) { char dname_a[PBS_MAXHOSTNAME+1]; get_dcinfo(dname, dname_a, dctrl); } mbstowcs(unamew, service_accountname, UNLEN+1); mbstowcs(dnamew, dname, PBS_MAXHOSTNAME+1); mbstowcs(dctrlw, dctrl, PBS_MAXHOSTNAME+1); /* create account if it doesn't exist */ /* FIX: Perform the following "if action" if either */ /* 1) in a domain environment, and the */ /* executing account (i.e. intaller) is an account in */ /* the domain, */ /* 2) in a standalone environment, and the */ /* executing account (i.e. installer) is a local account */ /* in the local computer. */ /* This fix is needed as during testing, I was finding that */ /* the local "Administrator" account itself has permission */ /* to query the domain, and to create accounts on the domain. */ /* However, the created domain "pbsadmin" account would have */ /* weirdness to it in that attempts to impersonate it would */ /* initially fail, and even after adding the account to the */ /* local "Administrators" group, that user entry on the group */ /* would suddenly disappear. */ if ((stricmp(exec_dname, dname) == 0) && ((nstatus=wrap_NetUserGetInfo(dctrlw, unamew, 1, (LPBYTE *)&ui1_ptr)) == NERR_UserNotFound)) { mbstowcs(passwordw, password, LM20_PWLEN+1); ui.usri1_name = (wchar_t *)unamew; ui.usri1_password = (wchar_t *)passwordw; ui.usri1_password_age = 0; ui.usri1_priv = USER_PRIV_USER; ui.usri1_home_dir = NULL; ui.usri1_comment = NULL; ui.usri1_flags = UF_PASSWD_CANT_CHANGE|UF_DONT_EXPIRE_PASSWD; ui.usri1_script_path = NULL; if (for_info_only) nstatus = NERR_Success; else nstatus=NetUserAdd(dctrlw, 1, (LPBYTE)&ui, NULL); if ((nstatus != NERR_Success) && (nstatus != NERR_UserExists)) { fprintf(stderr, "Failed to create %s\\%S: error status=%d\n", dname, unamew, nstatus); goto end_add_service_account; } printf("%s account %s\\%S\n", (for_info_only?"Creating":"Created"), dname, unamew); set_account_expiration(dnamew, dctrlw, unamew, TIMEQ_FOREVER); /* cache new token since the account was just created */ cache_usertoken_and_homedir(service_accountname, NULL, 0, read_sa_password, (char *)service_accountname, decrypt_sa_password, 1); if (add_to_administrators_group(dnamew, unamew) != 0) goto end_add_service_account; } /* Verify password */ if (pw == NULL) { pw = getpwnam(service_accountname); if (pw == NULL) { fprintf(stderr, "Password could not be validated against %s\\%s.\n", dname, service_accountname); goto end_add_service_account; } } /* validate password */ sprintf(sa_name, "%s\\%s", dname, service_accountname); if (!for_info_only) { if (pw->pw_userlogin != INVALID_HANDLE_VALUE) { if (ImpersonateLoggedOnUser(pw->pw_userlogin) == 0) { /* fail */ if (validate_account_password(sa_name, password) == 0) { /* we still call validate_account_password() as backup since */ /* under Windows 2000, LogonUser(), called from */ /* cache_usertoken_and_homedir(), might fail due to not */ /* having the SE_TCB_NAME privilege. This must be */ /* already set before calling the "cmd" process that */ /* executes the install program. */ fprintf(stderr, "Password did not validate against %s\\%s err=%d\n\nClick BACK button to retry a different password.\nClick NEXT button to abort installation.", dname, service_accountname, GetLastError()); goto end_add_service_account; } } else { printf("Validated password for %s\n", sa_name); RevertToSelf(); } } } else { printf("Validating password for %s\n", sa_name); } /* add service account to appropriate Admin group */ if (!for_info_only && !isLocalAdminMember(service_accountname)) { if (add_to_administrators_group(dnamew, unamew) != 0) goto end_add_service_account; } wcsset(passwordw, 0); ret_val = 1; if (for_info_only) { printf("%s will need the following privileges:\n", sa_name); printf("\n\tCreate Token Object\n"); printf("\n\tReplace Process Level Token\n"); printf("\n\tLogon On As a Service\n"); printf("\n\tAct As Part of the Operating System\n"); } end_add_service_account: if (ui1_ptr != NULL) NetApiBufferFree(ui1_ptr); return (ret_val); }
int main(int argc, char *argv[]) { char *infile = "TPV7.fits"; char keyrec[81], header[288001], *disfn; int dopoly, gotend, iblock, ikeyrec, inc, itest, j, k, n, naxis[2], naxis1, naxis2, nClosure, nFail, nkeyrec, nsamp, nreject, nTest, nwcs, p1, p2, status; clock_t t0, tp2x, tx2p; double absmax, dp1, dp2, *img, *img1, *img2, pix[8], pixblc[2], pixsamp[2], pixtrc[2], px, *px0, *px1, pxi[8], rel, resid, relmax; double *avgdis, *avgtot, *maxdis, *maxtot, *rmsdis, *rmstot, stats[9]; FILE *fptr; struct linprm affine, *lin, *linpol, *lintpv; struct wcsprm *wcs, wcspol; wcserr_enable(1); wcsprintf_set(stdout); /* Set line buffering in case stdout is redirected to a file, otherwise * stdout and stderr messages will be jumbled (stderr is unbuffered). */ setvbuf(stdout, NULL, _IOLBF, 0); wcsprintf("Testing closure of WCSLIB distortion routines (tdis1.c)\n" "-------------------------------------------------------\n"); /* List status return messages. */ wcsprintf("\nList of dis status return values:\n"); for (status = 1; status <= 5; status++) { wcsprintf("%4d: %s.\n", status, dis_errmsg[status]); } wcsprintf("\n"); /* Optional file name specified? */ if (1 < argc) { infile = argv[1]; } /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ if ((fptr = fopen(infile, "r")) == 0) { wcsprintf("ERROR opening %s\n", infile); return 1; } memset(naxis, 0, 2*sizeof(int)); k = 0; nkeyrec = 0; gotend = 0; for (iblock = 0; iblock < 100; iblock++) { for (ikeyrec = 0; ikeyrec < 36; ikeyrec++) { if (fgets(keyrec, 81, fptr) == 0) { break; } if (strncmp(keyrec, " ", 8) == 0) continue; if (strncmp(keyrec, "COMMENT ", 8) == 0) continue; if (strncmp(keyrec, "HISTORY ", 8) == 0) continue; if (strncmp(keyrec, "NAXIS", 5) == 0) { if (keyrec[5] == ' ') { sscanf(keyrec+10, "%d", &n); if (n != 2) { wcsprintf("ERROR, expecting a 2D image.\n"); return 1; } continue; } sscanf(keyrec+5, "%d = %d", &j, &n); naxis[j-1] = n; continue; } strncpy(header+k, keyrec, 80); k += 80; nkeyrec++; if (strncmp(keyrec, "END ", 10) == 0) { /* An END keyrecord was read, but read the rest of the block. */ gotend = 1; } } if (gotend) break; } fclose(fptr); /* Parse the header. */ if ((wcspih(header, nkeyrec, WCSHDR_none, 2, &nreject, &nwcs, &wcs))) { wcsperr(wcs, 0x0); return 1; } /* Is it TPV? */ dopoly = 0; if (strcmp(wcs->ctype[0], "RA---TPV") == 0) { /* Copy it and translate to Polynomial for later use. */ wcspol.flag = -1; if (wcscopy(1, wcs, &wcspol)) { wcsperr(wcs, 0x0); return 1; } /* Translate TPV to Polynomial. */ tpv2poly(&wcspol); wcspol.flag = -1; if (wcsset(&wcspol)) { wcsperr(&wcspol, 0x0); return 1; } dopoly = 1; } /* wcsset() translates the TPV "projection" into a sequent distortion. */ if (wcsset(wcs)) { wcsperr(wcs, 0x0); return 1; } /* Henceforth, we will work with linprm. */ lin = &(wcs->lin); /* Get statistics on the distortion in the inner quarter of the image. */ maxdis = stats; maxtot = maxdis + 2; avgdis = maxtot + 1; avgtot = avgdis + 2; rmsdis = avgtot + 1; rmstot = rmsdis + 2; pixblc[0] = 0.25 * naxis[0]; pixblc[1] = 0.25 * naxis[1]; pixtrc[0] = 0.75 * naxis[0]; pixtrc[1] = 0.75 * naxis[1]; pixsamp[0] = (pixtrc[0] - pixblc[0])/512.0; pixsamp[1] = (pixtrc[1] - pixblc[1])/512.0; if (pixsamp[0] < 1.0) pixsamp[0] = 1.0; if (pixsamp[1] < 1.0) pixsamp[1] = 1.0; if (linwarp(lin, pixblc, pixtrc, pixsamp, &nsamp, maxdis, maxtot, avgdis, avgtot, rmsdis, rmstot)) { linperr(lin, 0x0); return 1; } for (k = 0; k < 9; k++) { if (fabs(stats[k]) < 0.0005) stats[k] = 0.0; } wcsprintf("linwarp() statistics computed over %d sample points:\n" " Max distortion, axis 1: %8.3f pixels\n" " axis 2: %8.3f pixels\n" " total: %8.3f pixels\n" " Mean distortion, axis 1: %8.3f pixels\n" " axis 2: %8.3f pixels\n" " total: %8.3f pixels\n" " RMS distortion, axis 1: %8.3f pixels\n" " axis 2: %8.3f pixels\n" " total: %8.3f pixels\n", nsamp, maxdis[0], maxdis[1], *maxtot, avgdis[0], avgdis[1], *avgtot, rmsdis[0], rmsdis[1], *rmstot); if (lin->disseq) { /* Exercise diswarp() as well. */ wcsprintf("\n"); /* Define a rectangle in intermediate pixel coordinates that just */ /* encompasses the inner quarter of the image. For this we need */ /* to switch off CDELTia scaling and all distortions. */ affine.flag = -1; if ((status = lincpy(1, lin, &affine))) { linperr(lin, 0x0); return 1; } affine.cdelt[0] = 1.0; affine.cdelt[1] = 1.0; if ((status = (lindis(1, &affine, 0x0) || lindis(2, &affine, 0x0) || linset(&affine)))) { linperr(&affine, 0x0); return 1; } pix[0] = pixblc[0]; pix[1] = pixblc[1]; pix[2] = pixtrc[0]; pix[3] = pixblc[1]; pix[4] = pixtrc[0]; pix[5] = pixtrc[1]; pix[6] = pixblc[0]; pix[7] = pixtrc[1]; if (linp2x(&affine, 4, 2, pix, pxi)) { linperr(&affine, 0x0); return 1; } linfree(&affine); pixblc[0] = pxi[0]; pixblc[1] = pxi[1]; pixtrc[0] = pxi[0]; pixtrc[1] = pxi[1]; k = 2; for (j = 1; j < 4; j++) { if (pixblc[0] > pxi[k]) pixblc[0] = pxi[k]; if (pixtrc[0] < pxi[k]) pixtrc[0] = pxi[k]; k++; if (pixblc[1] > pxi[k]) pixblc[1] = pxi[k]; if (pixtrc[1] < pxi[k]) pixtrc[1] = pxi[k]; k++; } pixsamp[0] = (pixtrc[0] - pixblc[0])/512.0; pixsamp[1] = (pixtrc[1] - pixblc[1])/512.0; if (diswarp(lin->disseq, pixblc, pixtrc, pixsamp, &nsamp, maxdis, maxtot, avgdis, avgtot, rmsdis, rmstot)) { wcserr_prt(lin->disseq->err, 0x0); return 1; } for (k = 0; k < 9; k++) { if (fabs(stats[k]) < 0.0005) stats[k] = 0.0; } wcsprintf("diswarp() statistics computed over %d sample points:\n" " Max distortion, axis 1: %8.3f units\n" " axis 2: %8.3f units\n" " total: %8.3f units\n" " Mean distortion, axis 1: %8.3f units\n" " axis 2: %8.3f units\n" " total: %8.3f units\n" " RMS distortion, axis 1: %8.3f units\n" " axis 2: %8.3f units\n" " total: %8.3f units\n", nsamp, maxdis[0], maxdis[1], *maxtot, avgdis[0], avgdis[1], *avgtot, rmsdis[0], rmsdis[1], *rmstot); } /* The image size determines the test domain. */ if ((naxis1 = naxis[0]) == 0) { naxis1 = 2*wcs->crpix[0] + 1; } if ((naxis2 = naxis[1]) == 0) { naxis2 = 2*wcs->crpix[1] + 1; } /* Limit the number of tests. */ inc = 1; while ((naxis1/inc)*(naxis2/inc) > 800000) { inc *= 2; } n = naxis1 / inc; px0 = calloc(4*(2*n), sizeof(double)); px1 = px0 + 2*n ; img = px1 + 2*n ; img1 = img; img2 = img + 2*n; for (itest = 0; itest < 2; itest++) { if (itest) { if (!dopoly) break; lin = &(wcspol.lin); } if (lin->dispre) { disfn = lin->dispre->dtype[0]; } else if (lin->disseq) { disfn = lin->disseq->dtype[0]; } wcsprintf("\n"); /* Now the closure test. */ tp2x = 0; tx2p = 0; nTest = 0; nFail = 0; nClosure = 0; absmax = 0.0; relmax = 0.0; for (p2 = 1; p2 <= naxis2; p2 += inc) { k = 0; for (p1 = 1; p1 <= naxis1; p1 += inc) { px0[k++] = (double)p1; px0[k++] = (double)p2; } t0 = clock(); if (linp2x(lin, n, 2, px0, img)) { linperr(lin, 0x0); nFail = 1; break; } tp2x += clock() - t0; t0 = clock(); if (linx2p(lin, n, 2, img, px1)) { linperr(lin, 0x0); nFail = 1; break; } tx2p += clock() - t0; /* Check closure. */ k = 0; for (k = 0; k < 2*n ; k += 2) { dp1 = fabs(px1[k] - px0[k]); dp2 = fabs(px1[k+1] - px0[k+1]); resid = (dp1 > dp2) ? dp1 : dp2; if (resid > absmax) absmax = resid; if (resid > ATOL) { nClosure++; wcsprintf("Absolute closure error:\n"); wcsprintf(" pix: %18.12f %18.12f\n", px0[k], px0[k+1]); wcsprintf(" -> img: %18.12f %18.12f\n", img[k], img[k+1]); wcsprintf(" -> pix: %18.12f %18.12f\n", px1[k], px1[k+1]); wcsprintf("\n"); continue; } resid = 0.0; if ((px = fabs(px0[k])) > 1.0) resid = dp1/px; if ((px = fabs(px0[k+1])) > 1.0) { if ((rel = dp2/px) > resid) resid = rel; } if (resid > relmax) relmax = resid; if (resid > FTOL) { nClosure++; wcsprintf("Relative closure error:\n"); wcsprintf(" pix: %18.12f %18.12f\n", px0[k], px0[k+1]); wcsprintf(" -> img: %18.12f %18.12f\n", img[k], img[k+1]); wcsprintf(" -> pix: %18.12f %18.12f\n", px1[k], px1[k+1]); wcsprintf("\n"); } } nTest += n; } if (nFail) { wcsprintf("\nFAIL: The %s test failed to complete.\n", disfn); } else { wcsprintf("linp2x/linx2p with %s distortions:\n" " Completed %d closure tests.\n" " Maximum absolute closure residual = %.2e pixel.\n" " Maximum relative closure residual = %.2e.\n", disfn, nTest, absmax, relmax); wcsprintf("\n"); wcsprintf(" linp2x time (ns): %6.0f\n linx2p time (ns): %6.0f\n\n", 1.0e9*((double)tp2x/CLOCKS_PER_SEC)/nTest, 1.0e9*((double)tx2p/CLOCKS_PER_SEC)/nTest); if (nClosure) { wcsprintf("FAIL: %d closure residuals exceed reporting tolerance.\n", nClosure); } else { wcsprintf("PASS: All %s closure residuals are within reporting " "tolerance.\n", disfn); } } } /* Compare TPV with Polynomial over the test domain. */ if (dopoly) { wcsprintf("\n"); nTest = 0; nFail = 0; absmax = 0.0; lintpv = &(wcs->lin); linpol = &(wcspol.lin); for (p2 = 1; p2 <= naxis2; p2 += inc) { k = 0; for (p1 = 1; p1 <= naxis1; p1 += inc) { px0[k++] = (double)p1; px0[k++] = (double)p2; } if (linp2x(lintpv, n, 2, px0, img1)) { linperr(lintpv, 0x0); break; } if (linp2x(linpol, n, 2, px0, img2)) { linperr(linpol, 0x0); break; } /* Check agreement. */ k = 0; for (k = 0; k < 2*n ; k += 2) { dp1 = fabs(img2[k] - img1[k]); dp2 = fabs(img2[k+1] - img1[k+1]); resid = (dp1 > dp2) ? dp1 : dp2; if (resid > absmax) absmax = resid; if (resid > ATOL) { nFail++; wcsprintf("TPV - Polynomial disagreement:\n"); wcsprintf(" pix: %18.12f %18.12f\n", px0[k], px0[k+1]); wcsprintf(" -> TPV: %18.12f %18.12f\n", img1[k], img1[k+1]); wcsprintf(" -> Pol: %18.12f %18.12f\n", img2[k], img2[k+1]); wcsprintf("\n"); continue; } } nTest += n; } wcsprintf("linp2x, TPV vs Polynomial distortions:\n" " Completed %d comparisons.\n" " Maximum absolute disagreement = %.2e units.\n", nTest, absmax); wcsprintf("\n"); if (nFail) { wcsprintf("FAIL: %d comparisons exceed reporting tolerance.\n", nFail); } else { wcsprintf("PASS: All TPV vs Polynomial comparisons are within " "reporting tolerance.\n"); } } free(px0); wcsvfree(&nwcs, &wcs); wcsfree(&wcspol); return nFail || nClosure; }
int main() { #define NELEM 9 char ok[] = "", mismatch[] = " (WARNING, mismatch)", *s; int i, k, lat, lng, nFail1 = 0, nFail2 = 0, stat[361], status; double freq, img[361][NELEM], lat1, lng1, phi[361], pixel1[361][NELEM], pixel2[361][NELEM], r, resid, residmax, theta[361], time, world1[361][NELEM], world2[361][NELEM]; struct wcsprm *wcs; printf("Testing closure of WCSLIB world coordinate transformation " "routines (twcs.c)\n" "----------------------------------------------------------" "-----------------\n"); /* List status return messages. */ printf("\nList of wcs status return values:\n"); for (status = 1; status <= 13; status++) { printf("%4d: %s.\n", status, wcs_errmsg[status]); } printf("\nSize of data types (bytes):\n"); printf(" char:%5"MODZ"u\n", sizeof(char)); printf(" short int:%5"MODZ"u\n", sizeof(short int)); printf(" int:%5"MODZ"u\n", sizeof(int)); printf(" long int:%5"MODZ"u\n", sizeof(long int)); printf(" float:%5"MODZ"u\n", sizeof(float)); printf(" double:%5"MODZ"u\n", sizeof(double)); printf(" char *:%5"MODZ"u\n", sizeof(char *)); printf(" char (*)[72]:%5"MODZ"u\n", sizeof(char (*)[72])); printf(" int *:%5"MODZ"u\n", sizeof(int *)); printf(" float *:%5"MODZ"u\n", sizeof(float *)); printf(" double *:%5"MODZ"u\n", sizeof(double *)); printf("struct pvcard *:%5"MODZ"u\n", sizeof(struct pvcard *)); printf("struct pscard *:%5"MODZ"u\n", sizeof(struct pscard *)); printf("\nSize of structs (bytes/ints):\n"); s = (sizeof(struct celprm) == sizeof(int)*CELLEN) ? ok : mismatch; printf(" celprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct celprm), CELLEN, s); s = (sizeof(struct fitskey) == sizeof(int)*KEYLEN) ? ok : mismatch; printf(" fitskey:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct fitskey), KEYLEN, s); s = (sizeof(struct fitskeyid) == sizeof(int)*KEYIDLEN) ? ok : mismatch; printf(" fitskeyid:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct fitskeyid), KEYIDLEN, s); s = (sizeof(struct linprm) == sizeof(int)*LINLEN) ? ok : mismatch; printf(" linprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct linprm), LINLEN, s); s = (sizeof(struct prjprm) == sizeof(int)*PRJLEN) ? ok : mismatch; printf(" prjprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct prjprm), PRJLEN, s); s = (sizeof(struct spcprm) == sizeof(int)*SPCLEN) ? ok : mismatch; printf(" spcprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct spcprm), SPCLEN, s); s = (sizeof(struct spxprm) == sizeof(int)*SPXLEN) ? ok : mismatch; printf(" spxprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct spxprm), SPXLEN, s); s = (sizeof(struct tabprm) == sizeof(int)*TABLEN) ? ok : mismatch; printf(" tabprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct tabprm), TABLEN, s); s = (sizeof(struct wcserr) == sizeof(int)*ERRLEN) ? ok : mismatch; printf(" wcserr:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct wcserr), ERRLEN, s); s = (sizeof(struct wcsprm) == sizeof(int)*WCSLEN) ? ok : mismatch; printf(" wcsprm:%5"MODZ"u /%4"MODZ"u%s\n", sizeof(struct wcsprm), WCSLEN, s); /* Set the PVi_ma keyvalues for the longitude axis. */ /*----------------------------------------------------------*/ /* For test purposes, these are set so that the fiducial */ /* native coordinates are at the native pole, i.e. so that */ /* (phi0,theta0) = (0,90), but without any fiducial offset, */ /* i.e. iwith PVi_0a == 0 (by default). */ /*----------------------------------------------------------*/ PV[0].i = 4; /* Longitude is on axis 4. */ PV[0].m = 1; /* Parameter number 1. */ PV[0].value = 0.0; /* Fiducial native longitude. */ PV[1].i = 4; /* Longitude is on axis 4. */ PV[1].m = 2; /* Parameter number 2. */ PV[1].value = 90.0; /* Fiducial native latitude. */ /* Set the PVi_m keyvaluess for the latitude axis. */ PV[2].i = 2; /* Latitude is on axis 2. */ PV[2].m = 1; /* Parameter number 1. */ PV[2].value = -30.0; /* PVi_1. */ /* The following routine simulates the actions of a FITS header parser. */ wcs = malloc(sizeof(struct wcsprm)); wcs->flag = -1; parser(wcs); printf("\nReporting tolerance %5.1g pixel.\n", tol); /* Initialize non-celestial world coordinates. */ time = 1.0; freq = 1.42040595e9 - 180.0 * 62500.0; for (k = 0; k < 361; k++) { world1[k][0] = 0.0; world1[k][1] = 0.0; world1[k][2] = 0.0; world1[k][3] = 0.0; world1[k][2] = time; time *= 1.01; world1[k][wcs->spec] = 2.99792458e8 / freq; freq += 62500.0; } residmax = 0.0; for (lat = 90; lat >= -90; lat--) { lat1 = (double)lat; for (lng = -180, k = 0; lng <= 180; lng++, k++) { lng1 = (double)lng; world1[k][wcs->lng] = lng1; world1[k][wcs->lat] = lat1; } if (wcss2p(wcs, 361, NELEM, world1[0], phi, theta, img[0], pixel1[0], stat)) { printf(" At wcss2p#1 with lat1 == %f\n", lat1); wcsperr(wcs, " "); continue; } if (wcsp2s(wcs, 361, NELEM, pixel1[0], img[0], phi, theta, world2[0], stat)) { printf(" At wcsp2s with lat1 == %f\n", lat1); wcsperr(wcs, " "); continue; } if (wcss2p(wcs, 361, NELEM, world2[0], phi, theta, img[0], pixel2[0], stat)) { printf(" At wcss2p#2 with lat1 == %f\n", lat1); wcsperr(wcs, " "); continue; } for (k = 0; k < 361; k++) { resid = 0.0; for (i = 0; i < NAXIS; i++) { r = pixel2[k][i] - pixel1[k][i]; resid += r*r; } resid = sqrt(resid); if (resid > residmax) residmax = resid; if (resid > tol) { nFail1++; printf("\nClosure error:\n" "world1:%18.12f%18.12f%18.12f%18.12f\n" "pixel1:%18.12f%18.12f%18.12f%18.12f\n" "world2:%18.12f%18.12f%18.12f%18.12f\n" "pixel2:%18.12f%18.12f%18.12f%18.12f\n", world1[k][0], world1[k][1], world1[k][2], world1[k][3], pixel1[k][0], pixel1[k][1], pixel1[k][2], pixel1[k][3], world2[k][0], world2[k][1], world2[k][2], world2[k][3], pixel2[k][0], pixel2[k][1], pixel2[k][2], pixel2[k][3]); } } } printf("wcsp2s/wcss2p: Maximum closure residual = %.1e pixel.\n", residmax); /* Test wcserr and wcsprintf() as well. */ nFail2 = 0; wcsprintf_set(stdout); wcsprintf("\n\nIGNORE messages marked with 'OK', they test wcserr " "(and wcsprintf):\n"); wcserr_enable(1); /* Test 1. */ wcs->pv[2].value = UNDEFINED; status = wcsset(wcs); nFail2 += check_error(wcs, status, WCSERR_BAD_PARAM, "Invalid parameter value"); nFail2 += test_errors(); if (nFail1 || nFail2) { if (nFail1) { printf("\nFAIL: %d closure residuals exceed reporting tolerance.\n", nFail1); } if (nFail2) { printf("FAIL: %d error messages differ from that expected.\n", nFail2); } } else { printf("\nPASS: All closure residuals are within reporting tolerance.\n"); printf("PASS: All error messages reported as expected.\n"); } /* Clean up. */ wcsfree(wcs); free(wcs); return nFail1 + nFail2; }
int celfix(struct wcsprm *wcs) { static const char *function = "celfix"; int k, status; struct celprm *wcscel = &(wcs->cel); struct prjprm *wcsprj = &(wcscel->prj); struct wcserr **err; if (wcs == 0x0) return FIXERR_NULL_POINTER; err = &(wcs->err); /* Initialize if required. */ if (wcs->flag != WCSSET) { if ((status = wcsset(wcs))) return status; } /* Was an NCP or GLS projection code translated? */ if (wcs->lat >= 0) { /* Check ctype. */ if (strcmp(wcs->ctype[wcs->lat]+5, "NCP") == 0) { strcpy(wcs->ctype[wcs->lng]+5, "SIN"); strcpy(wcs->ctype[wcs->lat]+5, "SIN"); if (wcs->npvmax < wcs->npv + 2) { /* Allocate space for two more PVi_ja keyvalues. */ if (wcs->m_flag == WCSSET && wcs->pv == wcs->m_pv) { if (!(wcs->pv = calloc(wcs->npv+2, sizeof(struct pvcard)))) { wcs->pv = wcs->m_pv; return wcserr_set(WCSFIX_ERRMSG(FIXERR_MEMORY)); } wcs->npvmax = wcs->npv + 2; wcs->m_flag = WCSSET; for (k = 0; k < wcs->npv; k++) { wcs->pv[k] = wcs->m_pv[k]; } if (wcs->m_pv) free(wcs->m_pv); wcs->m_pv = wcs->pv; } else { return wcserr_set(WCSFIX_ERRMSG(FIXERR_MEMORY)); } } wcs->pv[wcs->npv].i = wcs->lat + 1; wcs->pv[wcs->npv].m = 1; wcs->pv[wcs->npv].value = wcsprj->pv[1]; (wcs->npv)++; wcs->pv[wcs->npv].i = wcs->lat + 1; wcs->pv[wcs->npv].m = 2; wcs->pv[wcs->npv].value = wcsprj->pv[2]; (wcs->npv)++; return FIXERR_SUCCESS; } else if (strcmp(wcs->ctype[wcs->lat]+5, "GLS") == 0) { strcpy(wcs->ctype[wcs->lng]+5, "SFL"); strcpy(wcs->ctype[wcs->lat]+5, "SFL"); if (wcs->crval[wcs->lng] != 0.0 || wcs->crval[wcs->lat] != 0.0) { /* In the AIPS convention, setting the reference longitude and * latitude for GLS does not create an oblique graticule. A non-zero * reference longitude introduces an offset in longitude in the normal * way, whereas a non-zero reference latitude simply translates the * reference point (i.e. the map as a whole) to that latitude. This * might be effected by adjusting CRPIXja but that is complicated by * the linear transformation and instead is accomplished here by * setting theta_0. */ if (wcs->npvmax < wcs->npv + 2) { /* Allocate space for three more PVi_ja keyvalues. */ if (wcs->m_flag == WCSSET && wcs->pv == wcs->m_pv) { if (!(wcs->pv = calloc(wcs->npv+3, sizeof(struct pvcard)))) { wcs->pv = wcs->m_pv; return wcserr_set(WCSFIX_ERRMSG(FIXERR_MEMORY)); } wcs->npvmax = wcs->npv + 3; wcs->m_flag = WCSSET; for (k = 0; k < wcs->npv; k++) { wcs->pv[k] = wcs->m_pv[k]; } if (wcs->m_pv) free(wcs->m_pv); wcs->m_pv = wcs->pv; } else { return wcserr_set(WCSFIX_ERRMSG(FIXERR_MEMORY)); } } wcs->pv[wcs->npv].i = wcs->lng + 1; wcs->pv[wcs->npv].m = 0; wcs->pv[wcs->npv].value = 1.0; (wcs->npv)++; /* Note that the reference longitude is still zero. */ wcs->pv[wcs->npv].i = wcs->lng + 1; wcs->pv[wcs->npv].m = 1; wcs->pv[wcs->npv].value = 0.0; (wcs->npv)++; wcs->pv[wcs->npv].i = wcs->lng + 1; wcs->pv[wcs->npv].m = 2; wcs->pv[wcs->npv].value = wcs->crval[wcs->lat]; (wcs->npv)++; } return FIXERR_SUCCESS; } } return FIXERR_NO_CHANGE; }
int main(int argc, char *argv[]) { char *infile = "SIP.fits"; char keyrec[81], header[288001]; int axes[4], gotend, iblock, ikeyrec, j, k, n, naxis[4], nkeyrec, nreject, nsamp, nsub, nwcs, status; double pixblc[4], pixsamp[4], pixtrc[4]; double *avgdis, *avgtot, *maxdis, *maxtot, *rmsdis, *rmstot, stats[15]; FILE *fptr; struct linprm *lin; struct wcsprm *wcs, wcsext; wcserr_enable(1); wcsprintf_set(stdout); /* Set line buffering in case stdout is redirected to a file, otherwise * stdout and stderr messages will be jumbled (stderr is unbuffered). */ setvbuf(stdout, NULL, _IOLBF, 0); wcsprintf("Testing wcssub() with distortions (tdis2.c)\n" "-------------------------------------------\n"); /* Optional file name specified? */ if (1 < argc) { infile = argv[1]; } /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ if ((fptr = fopen(infile, "r")) == 0) { wcsprintf("ERROR opening %s\n", infile); return 1; } memset(naxis, 0, 2*sizeof(int)); k = 0; nkeyrec = 0; gotend = 0; for (iblock = 0; iblock < 100; iblock++) { for (ikeyrec = 0; ikeyrec < 36; ikeyrec++) { if (fgets(keyrec, 81, fptr) == 0) { break; } if (strncmp(keyrec, " ", 8) == 0) continue; if (strncmp(keyrec, "COMMENT ", 8) == 0) continue; if (strncmp(keyrec, "HISTORY ", 8) == 0) continue; if (strncmp(keyrec, "NAXIS", 5) == 0) { if (keyrec[5] == ' ') { sscanf(keyrec+10, "%d", &n); if (4 < n) { wcsprintf("ERROR, can't handle more than 4 axes.\n"); return 1; } continue; } sscanf(keyrec+5, "%d = %d", &j, &n); naxis[j-1] = n; continue; } strncpy(header+k, keyrec, 80); k += 80; nkeyrec++; if (strncmp(keyrec, "END ", 10) == 0) { /* An END keyrecord was read, but read the rest of the block. */ gotend = 1; } } if (gotend) break; } fclose(fptr); /* Parse the header. */ if ((wcspih(header, nkeyrec, WCSHDR_none, 2, &nreject, &nwcs, &wcs))) { wcsperr(wcs, 0x0); return 1; } /* Extract the coordinate description for a transposed subimage and prepend a new axis. Also tests wcssub() on a struct that hasn't been set up. */ nsub = 3; axes[0] = 0; axes[1] = WCSSUB_LATITUDE; axes[2] = WCSSUB_LONGITUDE; wcsext.flag = -1; if ((status = wcssub(1, wcs, &nsub, axes, &wcsext))) { wcsperr(&wcsext, ""); goto cleanup; } else if (nsub == 0) { printf("None of the requested subimage axes were found.\n"); goto cleanup; } /* Print the original and extracted structs. */ printf("\nInitial contents of wcsprm struct:\n"); if ((status = wcsset(wcs))) { wcsperr(wcs, ""); goto cleanup; }; wcsprt(wcs); printf("\n\nExtracted contents of wcsprm struct:\n"); if ((status = wcsset(&wcsext))) { wcsperr(&wcsext, ""); goto cleanup; } wcsprt(&wcsext); /* Compute distortion statistics in the initial struct. */ maxdis = stats; maxtot = maxdis + 4; avgdis = maxtot + 1; avgtot = avgdis + 4; rmsdis = avgtot + 1; rmstot = rmsdis + 4; pixblc[0] = 1.0; pixblc[1] = 1.0; pixblc[2] = 1.0; pixblc[3] = 1.0; pixtrc[0] = 256.0; pixtrc[1] = 256.0; pixtrc[2] = 1.0; pixtrc[3] = 1.0; pixsamp[0] = 1.0; pixsamp[1] = 1.0; pixsamp[2] = 1.0; pixsamp[3] = 1.0; lin = &(wcs->lin); if (linwarp(lin, pixblc, pixtrc, pixsamp, &nsamp, maxdis, maxtot, avgdis, avgtot, rmsdis, rmstot)) { linperr(lin, 0x0); return 1; } for (k = 0; k < 12; k++) { if (fabs(stats[k]) < 0.0005) stats[k] = 0.0; } wcsprintf("\n\n" "Initial linwarp() statistics computed over %d sample points:\n" " Max distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " axis 4: %10.5f pixels\n" " total: %10.5f pixels\n" " Mean distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " axis 4: %10.5f pixels\n" " total: %10.5f pixels\n" " RMS distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " axis 4: %10.5f pixels\n" " total: %10.5f pixels\n", nsamp, maxdis[0], maxdis[1], maxdis[2], maxdis[3], *maxtot, avgdis[0], avgdis[1], avgdis[2], avgdis[3], *avgtot, rmsdis[0], rmsdis[1], rmsdis[2], rmsdis[3], *rmstot); /* Compute distortion statistics in the extracted struct. */ pixtrc[0] = 1.0; pixtrc[1] = 256.0; pixtrc[2] = 256.0; lin = &(wcsext.lin); if (linwarp(lin, pixblc, pixtrc, pixsamp, &nsamp, maxdis, maxtot, avgdis, avgtot, rmsdis, rmstot)) { linperr(lin, 0x0); return 1; } for (k = 0; k < 12; k++) { if (fabs(stats[k]) < 0.0005) stats[k] = 0.0; } wcsprintf("\n" "linwarp() statistics for extract computed over the same %d points:\n" " Max distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " total: %10.5f pixels\n" " Mean distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " total: %10.5f pixels\n" " RMS distortion, axis 1: %10.5f pixels\n" " axis 2: %10.5f pixels\n" " axis 3: %10.5f pixels\n" " total: %10.5f pixels\n", nsamp, maxdis[0], maxdis[1], maxdis[2], *maxtot, avgdis[0], avgdis[1], avgdis[2], *avgtot, rmsdis[0], rmsdis[1], rmsdis[2], *rmstot); cleanup: wcsvfree(&nwcs, &wcs); wcsfree(&wcsext); return status; }
int main() { char infile[] = "pih.fits"; char devtyp[16], idents[3][80], nlcprm[1], opt[2]; int c0[] = {-1, -1, -1, -1, -1, -1, -1}; int i, ic, gcode[2], naxis[2], nkeyrec, nreject, nwcs, relax, status; float blc[2], trc[2]; double cache[257][4], grid1[1], grid2[1], nldprm[1]; struct wcsprm *wcs; nlfunc_t pgwcsl_; #if defined HAVE_CFITSIO && defined DO_CFITSIO char *header; fitsfile *fptr; #else char keyrec[81], header[28801]; int gotend, j, k; FILE *fptr; #endif /* Set line buffering in case stdout is redirected to a file, otherwise * stdout and stderr messages will be jumbled (stderr is unbuffered). */ setvbuf(stdout, NULL, _IOLBF, 0); printf("Testing WCSLIB parser for FITS image headers (tpih2.c)\n" "------------------------------------------------------\n\n"); /* Read in the FITS header, excluding COMMENT and HISTORY keyrecords. */ #if defined HAVE_CFITSIO && defined DO_CFITSIO status = 0; if (fits_open_file(&fptr, infile, READONLY, &status)) { fits_report_error(stderr, status); return 1; } if (fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status)) { fits_report_error(stderr, status); return 1; } fits_close_file(fptr, &status); #else if ((fptr = fopen(infile, "r")) == 0x0) { printf("ERROR opening %s\n", infile); return 1; } k = 0; nkeyrec = 0; gotend = 0; for (j = 0; j < 10; j++) { for (i = 0; i < 36; i++) { if (fgets(keyrec, 81, fptr) == 0) { break; } if (strncmp(keyrec, " ", 8) == 0) continue; if (strncmp(keyrec, "COMMENT ", 8) == 0) continue; if (strncmp(keyrec, "HISTORY ", 8) == 0) continue; strncpy(header+k, keyrec, 80); k += 80; nkeyrec++; if (strncmp(keyrec, "END ", 8) == 0) { /* An END keyrecord was read, but read the rest of the block. */ gotend = 1; } } if (gotend) break; } fclose(fptr); #endif fprintf(stderr, "Found %d non-comment header keyrecords.\n", nkeyrec); relax = WCSHDR_all; if ((status = wcspih(header, nkeyrec, relax, 2, &nreject, &nwcs, &wcs))) { fprintf(stderr, "wcspih ERROR %d: %s.\n", status, wcs_errmsg[status]); } #if defined HAVE_CFITSIO && defined DO_CFITSIO free(header); #endif /* Plot setup. */ naxis[0] = 1024; naxis[1] = 1024; blc[0] = 0.5f; blc[1] = 0.5f; trc[0] = naxis[0] + 0.5f; trc[1] = naxis[1] + 0.5f; strcpy(devtyp, "/XWINDOW"); cpgbeg(0, devtyp, 1, 1); cpgvstd(); cpgwnad(0.0f, 1.0f, 0.0f, 1.0f); cpgask(1); cpgpage(); /* Annotation. */ strcpy(idents[0], "Right ascension"); strcpy(idents[1], "Declination"); opt[0] = 'G'; opt[1] = 'E'; /* Compact lettering. */ cpgsch(0.8f); /* Draw full grid lines. */ cpgsci(1); gcode[0] = 2; gcode[1] = 2; grid1[0] = 0.0; grid2[0] = 0.0; for (i = 0; i < nwcs; i++) { if ((status = wcsset(wcs+i))) { fprintf(stderr, "wcsset ERROR %d: %s.\n", status, wcs_errmsg[status]); continue; } /* Get WCSNAME out of the wcsprm struct. */ strcpy(idents[2], (wcs+i)->wcsname); printf("\n%s\n", idents[2]); /* Draw the celestial grid. The grid density is set for each world */ /* coordinate by specifying LABDEN = 1224. */ ic = -1; cpgsbox(blc, trc, idents, opt, 0, 1224, c0, gcode, 0.0, 0, grid1, 0, grid2, 0, pgwcsl_, 1, WCSLEN, 1, nlcprm, (int *)(wcs+i), nldprm, 256, &ic, cache, &status); /* Draw the frame. */ cpgbox("BC", 0.0f, 0, "BC", 0.0f, 0); cpgpage(); } status = wcsvfree(&nwcs, &wcs); return 0; }
/* Read the WCS information from the header. Unfortunately, WCS lib is not thread safe, so it needs a mutex. In case you are not using multiple threads, just pass a NULL pointer as the mutex. After you finish with this WCS, you should free the space with: status = wcsvfree(&nwcs,&wcs); If the WCS structure is not recognized, then this function will return a NULL pointer for the wcsprm structure and a zero for nwcs. It will also report the fact to the user in stderr. =================================== WARNING: wcspih IS NOT THREAD SAFE! =================================== Don't call this function within a thread or use a mutex. */ struct wcsprm * gal_wcs_read_fitsptr(fitsfile *fptr, size_t hstartwcs, size_t hendwcs, int *nwcs) { /* Declaratins: */ int nkeys=0, status=0; struct wcsprm *wcs=NULL; char *fullheader, *to, *from; int relax = WCSHDR_all; /* Macro: use all informal WCS extensions. */ int ctrl = 0; /* Don't report why a keyword wasn't used. */ int nreject = 0; /* Number of keywords rejected for syntax. */ /* CFITSIO function: */ if( fits_hdr2str(fptr, 1, NULL, 0, &fullheader, &nkeys, &status) ) gal_fits_io_error(status, NULL); /* Only consider the header keywords in the current range: */ if(hendwcs>hstartwcs) { /* Mark the last character in the desired region. */ fullheader[hendwcs*(FLEN_CARD-1)]='\0'; /*******************************************************/ /****************************************************** printf("%s\n", fullheader); ******************************************************/ /*******************************************************/ /* Shift all the characters to the start of the string. */ if(hstartwcs) /* hstartwcs!=0 */ { to=fullheader; from=&fullheader[hstartwcs*(FLEN_CARD-1)-1]; while(*from++!='\0') *to++=*from; } nkeys=hendwcs-hstartwcs; /*******************************************************/ /****************************************************** printf("\n\n\n###############\n\n\n\n\n\n"); printf("%s\n", &fullheader[1*(FLEN_CARD-1)]); exit(0); ******************************************************/ /*******************************************************/ } /* WCSlib function to parse the FITS headers. */ status=wcspih(fullheader, nkeys, relax, ctrl, &nreject, nwcs, &wcs); if(status) { fprintf(stderr, "\n##################\n" "WCSLIB Warning: wcspih ERROR %d: %s.\n" "##################\n", status, wcs_errmsg[status]); wcs=NULL; *nwcs=0; } if (fits_free_memory(fullheader, &status) ) gal_fits_io_error(status, "problem in fitsarrayvv.c for freeing " "the memory used to keep all the headers"); /* Set the internal structure: */ if(wcs) { /* CTYPE is a mandatory WCS keyword, so if it hasn't been given (its '\0'), then the headers didn't have a WCS structure. However, WCSLIB still fills in the basic information (for example the dimensionality of the dataset). */ if(wcs->ctype[0][0]=='\0') { wcsfree(wcs); wcs=NULL; *nwcs=0; } else { /* Set the WCS structure. */ status=wcsset(wcs); if(status) { fprintf(stderr, "\n##################\n" "WCSLIB Warning: wcsset ERROR %d: %s.\n" "##################\n", status, wcs_errmsg[status]); wcsfree(wcs); wcs=NULL; *nwcs=0; } else /* A correctly useful WCS is present. When no PC matrix elements were present in the header, the default PC matrix (a unity matrix) is used. In this case WCSLIB doesn't set `altlin' (and gives it a value of 0). In Gnuastro, later on, we might need to know the type of the matrix used, so in such a case, we will set `altlin' to 1. */ if(wcs->altlin==0) wcs->altlin=1; } } /* Return the WCS structure. */ return wcs; }