Changeset 3028 for trunk/psLib/test/fileUtils/tst_psFits.c
- Timestamp:
- Jan 17, 2005, 5:15:04 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/fileUtils/tst_psFits.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/fileUtils/tst_psFits.c
r3025 r3028 6 6 * @author Robert DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $9 * @date $Date: 2005-01-1 7 20:58:22$8 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2005-01-18 03:15:03 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 17 #include <unistd.h> 18 18 #include <string.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 22 23 #define GENIMAGE(img,c,r,TYP, valueFcn) \ 24 img = psImageAlloc(c,r,PS_TYPE_##TYP); \ 25 for (psU32 row=0;row<r;row++) { \ 26 ps##TYP* imgRow = img->data.TYP[row]; \ 27 for (psU32 col=0;col<c;col++) { \ 28 imgRow[col] = (ps##TYP)(valueFcn); \ 29 } \ 30 } 19 31 20 32 static bool makeMulti(void); // implicitly tests psFitsSetExtName … … 23 35 const char* tableFilename = "table.fits"; 24 36 const int tableNumRows = 10; 37 38 39 // N.B., the tests to Image read/write was liberally taken from the now 40 // deprecated psImageReadSection/psImageWriteSection function tests. 41 static psS32 testImageRead(void); 42 static psS32 testImageWrite(void); 25 43 26 44 static psS32 tst_psFitsAlloc( void ); … … 41 59 {tst_psFitsReadHeaderSet,805, "psFitsReadHeaderSet", 0, false}, 42 60 {tst_psFitsReadTable,809, "psFitsReadTable", 0, false}, 61 {testImageRead,567, "psFitsReadImage", 0, false}, 62 {testImageWrite,569, "psFitsWriteImage", 0, false}, 43 63 {NULL} 44 64 }; … … 95 115 // set the pixels in the image 96 116 psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32)); 97 if (! psFitsWriteImage(fitsFile,header,image,1 ) ) {117 if (! psFitsWriteImage(fitsFile,header,image,1, extname) ) { 98 118 psError(PS_ERR_UNKNOWN, false, 99 119 "Could not write image."); 100 120 return false; 101 121 } 102 if (! psFitsSetExtName(fitsFile,extname) ) {103 psError(PS_ERR_UNKNOWN, false,104 "Could not write extension name.");105 return false;106 }107 122 108 123 psFree(header); … … 130 145 psImage* image = psImageAlloc(16,16,PS_TYPE_F32); 131 146 132 if (! psFitsWriteImage(fitsFile,NULL,image,1 ) ) {147 if (! psFitsWriteImage(fitsFile,NULL,image,1,"primary") ) { 133 148 psError(PS_ERR_UNKNOWN, false, 134 149 "Could not write PHU image."); … … 873 888 return 0; 874 889 } 890 891 psS32 testImageRead(void) 892 { 893 psS32 N = 256; 894 psS32 M = 128; 895 896 /* 897 This function shall open the specified FITS file, read the specified data 898 and place the data into a psImage structure. This function shall generate 899 an error message and return NULL if any of the input parameters are out of 900 range, image file doesn't exist or image is zero or one dimensional. 901 902 Verify the returned psImage structure contains expected values, if the input 903 parameter filename specifies an available FITS file with known 2dimensional 904 data, input parameters col, row, ncol, nrow, z specify data range with the 905 FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and 906 total FITS file image. (done in macro) 907 908 Verify the returned psImage structure is equal to the input parameter 909 'output', if specified. (done in macro) 910 911 */ 912 913 /* generate FITS file to read */ 914 915 #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \ 916 { \ 917 psImage* img = NULL; \ 918 psImage* img2 = NULL; \ 919 psImage* img3 = NULL; \ 920 psImage* img4 = NULL; \ 921 /* psImagimge* img_ref = NULL; */ \ 922 \ 923 GENIMAGE(img,m,n,TYP,row+2*col); \ 924 img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \ 925 GENIMAGE(img3,m,n,TYP,row+2*col); \ 926 psImageClip(img3,32.0,32.0,120.0,120.0); \ 927 img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \ 928 remove(filename); \ 929 psFits* fits = psFitsAlloc(filename); \ 930 psRegion region = {0,0,0,0}; \ 931 if (! psFitsWriteImage(fits, NULL, img, 2, "primary")) { \ 932 psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \ 933 return 1; \ 934 } \ 935 if (! psFitsUpdateImage(fits,img3, region, 1)) { \ 936 psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \ 937 return 2; \ 938 } \ 939 if (! psFitsWriteImage(fits,NULL, img3, 2, "extension")) { \ 940 psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \ 941 return 3; \ 942 } \ 943 if (! psFitsUpdateImage(fits,img,region, 1)) { \ 944 psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \ 945 return 4; \ 946 } \ 947 psFree(img); \ 948 psFree(fits); \ 949 img = NULL; \ 950 psFree(img3); \ 951 img3 = NULL; \ 952 fits = psFitsAlloc(filename); \ 953 psRegion reg = {readM0, readM, readN0, readN}; \ 954 img = psFitsReadImage(img, fits, reg, 0); \ 955 img3 = psFitsReadImage(img3, fits, reg, 1); \ 956 if (img3 == NULL) { \ 957 psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \ 958 return 6; \ 959 } \ 960 for (psU32 row = readN0; row < readN; row++) { \ 961 ps##TYP* imgRow = img->data.TYP[row-readN0]; \ 962 ps##TYP* img2Row = img2->data.TYP[row]; \ 963 ps##TYP* img3Row = img3->data.TYP[row-readN0]; \ 964 ps##TYP* img4Row = img4->data.TYP[row]; \ 965 for (psU32 col = readM0; col < readM; col++) { \ 966 if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \ 967 psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \ 968 col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \ 969 return 7; \ 970 } \ 971 if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \ 972 psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \ 973 col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \ 974 return 8; \ 975 } \ 976 } \ 977 } \ 978 psFree(img); \ 979 img = NULL; \ 980 psFree(img3); \ 981 img3 = NULL; \ 982 psFitsMoveExtNum(fits,1, false); \ 983 img3 = psFitsReadImage(img3, fits, reg, 0); \ 984 img = psFitsReadImage(img, fits, reg, 1); \ 985 if (img == NULL) { \ 986 psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \ 987 return 9; \ 988 } \ 989 for (psU32 row = readN0; row < readN; row++) { \ 990 ps##TYP* imgRow = img->data.TYP[row-readN0]; \ 991 ps##TYP* img2Row = img2->data.TYP[row]; \ 992 ps##TYP* img3Row = img3->data.TYP[row-readN0]; \ 993 ps##TYP* img4Row = img4->data.TYP[row]; \ 994 for (psU32 col = readM0; col < readM; col++) { \ 995 if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \ 996 psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \ 997 col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \ 998 return 10; \ 999 } \ 1000 if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \ 1001 psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \ 1002 col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \ 1003 return 11; \ 1004 } \ 1005 } \ 1006 } \ 1007 psFree(img); \ 1008 psFree(img2); \ 1009 psFree(img3); \ 1010 psFree(img4); \ 1011 psFree(fits); \ 1012 } 1013 1014 #define testReadType(TYP,filename) \ 1015 testReadTypeSize(1,1,0,0,0,0,TYP,"tmpImages/1x1_" filename); \ 1016 testReadTypeSize(M,1,M/4,0,M*3/4,0,TYP,"tmpImages/Mx1_" filename); \ 1017 testReadTypeSize(1,N,0,N/4,0,N*3/4,TYP,"tmpImages/1xN_" filename); \ 1018 testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,TYP,"tmpImages/MxN_" filename); 1019 1020 mkdir("tmpImages",0777); 1021 1022 testReadTypeSize(1,1,0,0,0,0,U8,"tmpImages/1x1_" "U8.fits"); 1023 testReadTypeSize(M,1,M/4,0,M*3/4,0,U8,"tmpImages/Mx1_" "U8.fits"); 1024 testReadTypeSize(1,N,0,N/4,0,N*3/4,U8,"tmpImages/1xN_" "U8.fits"); 1025 testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,U8,"tmpImages/MxN_" "U8.fits"); 1026 1027 1028 testReadType(U8,"U8.fits"); 1029 testReadType(S8,"S8.fits"); // Not a requirement 1030 testReadType(S16,"S16.fits"); 1031 testReadType(U16,"U16.fits"); // Not a requirement 1032 testReadType(S32,"S32.fits"); 1033 testReadType(U32,"U32.fits"); // Not a requirement 1034 testReadType(F32,"F32.fits"); 1035 testReadType(F64,"F64.fits"); 1036 1037 return 0; 1038 } 1039 1040 psS32 testImageWrite(void) 1041 { 1042 psImage* img = NULL; 1043 psImage* img2 = NULL; 1044 psS32 m = 64; 1045 psS32 n = 96; 1046 1047 /* 1048 This function shall write the specified section within a psImage structure 1049 to a FITS file. If the specifiedfile exists, then data should overwrite the 1050 section to write. If the specified file doesn't exist, it shall be created. 1051 If an extenstion is specified, then a basic primary header data unit shall 1052 be created. 1053 */ 1054 1055 /* 1056 Verify a FITS file named filename is generated and contains expected 1057 values, if the input parameter input contains known data values, input 1058 parameters col, row, ncol, nrow specify a valid data region within psImage 1059 structure. 1060 1061 Verify a FITS file named filename is generated and contains a primary 1062 header data unit with extension with expected values, if the input 1063 parameter input contains known data values, input parameters col, row, 1064 ncol, nrow specify a valid data region within psImage structure and 1065 extname and/or extnum specify an extenstion to write. 1066 1067 N.B. : these are done in testImageRead tests, see above. 1068 */ 1069 1070 /* 1071 Verify a FITS file named filename is overwritten and contains 1072 expected values, if the input parameter input contains known data values, 1073 input parameters col, row, ncol, nrow specify a valid data region within 1074 psImage structure. 1075 */ 1076 1077 GENIMAGE(img,m,n,F32,0); 1078 GENIMAGE(img2,m,n,F32,row+2*col); 1079 mkdir("tmpImages",0777); 1080 remove 1081 ("tmpImages/writeTest.fits"); 1082 psRegion region = { 1083 0,0,0,0 1084 }; 1085 psFits* fits = psFitsAlloc("tmpImages/writeTest.fits"); 1086 1087 if (! psFitsWriteImage(fits, NULL, img,1,NULL)) { 1088 psError(PS_ERR_UNKNOWN, true,"Couldn't write writeTest.fits."); 1089 return 14; 1090 } 1091 if (! psFitsUpdateImage(fits, img2, region, 0)) { 1092 psError(PS_ERR_UNKNOWN, true,"Couldn't update writeTest.fits."); 1093 return 15; 1094 } 1095 psFree(img); 1096 psFree(img2); 1097 1098 // Did it really overwrite the pixel values? Let's read it in and see. 1099 psFree(fits); 1100 fits = psFitsAlloc("tmpImages/writeTest.fits"); 1101 img = psFitsReadImage(NULL, fits, region, 0); 1102 if (img == NULL) { 1103 psError(PS_ERR_UNKNOWN, true,"Could not read in writeTest.fits."); 1104 return 16; 1105 } 1106 for (psU32 row=0;row<n;row++) { 1107 psF32* imgRow = img->data.F32[row]; 1108 for (psU32 col=0;col<m;col++) { 1109 if (fabsf(imgRow[col] - (row+2*col)) > FLT_EPSILON) { 1110 psError(PS_ERR_UNKNOWN, true,"The image values were not overwritten at %d,%d (%.2f vs %.2f)", 1111 col,row,imgRow[col],(row+2*col)); 1112 return 17; 1113 } 1114 } 1115 } 1116 1117 psFree(img); 1118 1119 /* 1120 Verify false is returned and program execution is not stopped, if the input image 1121 is null. 1122 */ 1123 psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message because input image is null."); 1124 if ( psFitsWriteImage(fits,NULL,NULL, 1, NULL) ) { 1125 psError(PS_ERR_UNKNOWN, true,"psImageWriteSection did not return false when input image is NULL."); 1126 return 20; 1127 } 1128 1129 psFree(fits); 1130 1131 return 0; 1132 }
Note:
See TracChangeset
for help on using the changeset viewer.
