00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <time.h>
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027
00028 #include "adf/ADF.h"
00029 #include "ADFH.h"
00030 #include "hdf5.h"
00031
00032
00033
00034 static char TempFile[ADF_FILENAME_LENGTH+1];
00035 static double InputRootID = -1.0;
00036 static double OutputRootID = -1.0;
00037 static int ErrStat;
00038 static char ErrMsg[ADF_MAX_ERROR_STR_LENGTH+1];
00039 static int IncludeLink = 0;
00040 static int PrintFlag = 0;
00041 static int NumDims;
00042 static int PathLength;
00043 static int DimVals[ADF_MAX_DIMENSIONS*2];
00044 static char label[ADF_LABEL_LENGTH+1];
00045 static char status[ADF_STATUS_LENGTH+1];
00046 static char format[ADF_FORMAT_LENGTH+1];
00047 static char name[ADF_NAME_LENGTH+1];
00048 static char DataType[ADF_DATA_TYPE_LENGTH+1];
00049 static char LinkFileName[ADF_FILENAME_LENGTH+1];
00050 static char LinkPathName[ADF_MAX_LINK_DATA_SIZE+1];
00051
00052
00053
00054 static int is_ADF_file(char *fname) {
00055 FILE *fp;
00056 char header[33];
00057
00058 if ((fp = fopen(fname, "r+b")) == NULL) {
00059 fprintf(stderr, "couldn't open %s for reading\n", fname);
00060 exit (1);
00061 }
00062 fread (header, sizeof(char), 32, fp);
00063 fclose (fp);
00064 header[32] = 0;
00065 return (strncmp (&header[4], "ADF Database Version", 20) == 0);
00066 }
00067
00068
00069
00070 static void ErrorExit()
00071 {
00072 if (InputRootID >= 0.0)
00073 ADF_Database_Close (InputRootID, &ErrStat);
00074 if (OutputRootID >= 0.0)
00075 ADFH_Database_Close (OutputRootID, &ErrStat);
00076 if (TempFile[0])
00077 unlink(TempFile);
00078 exit(1) ;
00079 }
00080
00081
00082
00083 static void InputError(char *funcname)
00084 {
00085 fflush(stdout);
00086 fprintf(stderr,"ADF_%s: errno=%d",funcname,ErrStat);
00087 if (ErrStat > 0) {
00088 ADF_Error_Message(ErrStat,ErrMsg);
00089 fprintf(stderr,"%s\n",ErrMsg);
00090 }
00091 ErrorExit();
00092 }
00093
00094
00095
00096 static void OutputError(char *funcname)
00097 {
00098 fflush(stdout);
00099 fprintf(stderr,"ADFH_%s: errno=%d",funcname,ErrStat);
00100 if (ErrStat > 0) {
00101 ADFH_Error_Message(ErrStat,ErrMsg);
00102 fprintf(stderr,"%s\n",ErrMsg);
00103 }
00104 ErrorExit();
00105 }
00106
00107
00108
00109 static int CalculateDataSize(char *data_type, int ndims, int *dims)
00110 {
00111 char type[3];
00112 int i, size = 1;
00113
00114 if (ndims == 0) return 0;
00115 for (i = 0; i < ndims; i++)
00116 size *= dims[i];
00117 if (size <= 0) return 0;
00118
00119 strncpy(type, data_type, 2);
00120 type[2] = 0;
00121 for (i = 0; i < 2; i++)
00122 if (islower(type[i]))
00123 type[i] = toupper(type[i]);
00124
00125 if (0 == strcmp(type, "MT") ||
00126 0 == strcmp(type, "LK")) return 0;
00127 if (0 == strcmp(type, "B1") ||
00128 0 == strcmp(type, "C1")) return (size * sizeof(char));
00129 if (0 == strcmp(type, "I4") ||
00130 0 == strcmp(type, "U4")) return (size * sizeof(int));
00131 if (0 == strcmp(type, "I8") ||
00132 0 == strcmp(type, "U8")) return (size * sizeof(long));
00133 if (0 == strcmp(type, "R4")) return (size * sizeof(float));
00134 if (0 == strcmp(type, "R8")) return (size * sizeof(double));
00135 if (0 == strcmp(type, "X4")) return (size * 2 * sizeof(float));
00136 if (0 == strcmp(type, "X8")) return (size * 2 * sizeof(double));
00137
00138 fflush(stdout);
00139 fprintf(stderr,"unknown datatype: %s\n", data_type);
00140 ErrorExit();
00141 return 0;
00142 }
00143
00144
00145
00146 static void CopyTheNode (double InputID, double OutputID)
00147 {
00148 int DataSize;
00149 void *DataBuffer;
00150
00151
00152
00153 ADF_Get_Label(InputID, label, &ErrStat);
00154 if (ErrStat != NO_ERROR) InputError("Get_Label");
00155 ADFH_Set_Label(OutputID, label, &ErrStat);
00156 if (ErrStat != NO_ERROR) OutputError("Set_Label");
00157
00158 ADF_Get_Data_Type(InputID, DataType, &ErrStat);
00159 if (ErrStat != NO_ERROR) InputError("Get_Data_Type");
00160 ADF_Get_Number_of_Dimensions(InputID, &NumDims, &ErrStat);
00161 if (ErrStat != NO_ERROR) InputError("Get_Number_of_Dimensions");
00162 if (NumDims == 0) return;
00163 ADF_Get_Dimension_Values(InputID, DimVals, &ErrStat);
00164 if (ErrStat != NO_ERROR) InputError("Get_Dimension_Values");
00165 ADFH_Put_Dimension_Information (OutputID, DataType, NumDims, DimVals,
00166 &ErrStat);
00167 if (ErrStat != NO_ERROR) OutputError("Put_Dimension_Information");
00168
00169
00170
00171 DataSize = CalculateDataSize(DataType, NumDims, DimVals);
00172 if (DataSize == 0) return;
00173 DataBuffer = (void *) malloc (DataSize);
00174 if (DataBuffer == NULL) {
00175 fflush(stdout);
00176 fprintf(stderr, "malloc failed for the node data (%d bytes)\n",
00177 DataSize);
00178 ErrorExit();
00179 }
00180 ADF_Read_All_Data(InputID, DataBuffer, &ErrStat);
00181 if (ErrStat == NO_DATA) {
00182 free (DataBuffer);
00183 return;
00184 }
00185 if (ErrStat != NO_ERROR) InputError("Read_All_Data");
00186 ADFH_Write_All_Data(OutputID, DataBuffer, &ErrStat);
00187 if (ErrStat != NO_ERROR) OutputError("Write_All_Data");
00188 free (DataBuffer);
00189 }
00190
00191
00192
00193 static void FixName (char *type, char *name)
00194 {
00195 char *p;
00196
00197 if (strchr(name, '/') == NULL && strchr(" .", *name) == NULL) return;
00198
00199 printf("WARNING:changed %s name from \"%s\" to ", type, name);
00200 if (strchr(" .", *name) != NULL) *name = '_';
00201 for (p = name; *p; p++) {
00202 if (*p == '/') *p = ':';
00203 }
00204 printf("\"%s\"\n", name);
00205 }
00206
00207
00208
00209 static void WalkTheNodes (double InputID, double OutputID, int indent)
00210 {
00211 int ic, cnt, in;
00212 int nchildren ;
00213 double InputChildID ;
00214 double OutputChildID ;
00215
00216
00217
00218 CopyTheNode (InputID, OutputID);
00219
00220
00221
00222 ADF_Number_of_Children(InputID, &nchildren, &ErrStat);
00223 if (ErrStat != NO_ERROR) InputError("Number_of_Children");
00224 if (nchildren == 0) return;
00225
00226 for (ic = 1; ic <= nchildren; ic++) {
00227
00228
00229
00230 ADF_Children_IDs(InputID, ic, 1, &cnt, &InputChildID, &ErrStat);
00231 if (ErrStat != NO_ERROR) InputError("Children_IDs");
00232 ADF_Get_Name(InputChildID, name, &ErrStat);
00233 if (ErrStat != NO_ERROR) InputError("Get_Name");
00234
00235
00236
00237 if (PrintFlag) {
00238 for (in = 0; in < indent; in++)
00239 putchar(' ');
00240 printf ("%s\n", name);
00241 }
00242 FixName("node", name);
00243
00244
00245
00246 ADF_Is_Link(InputChildID, &PathLength, &ErrStat);
00247 if (ErrStat != NO_ERROR) InputError("Is_Link");
00248 if (PathLength > 0) {
00249 ADF_Get_Link_Path(InputChildID, LinkFileName, LinkPathName,
00250 &ErrStat);
00251 if (ErrStat != NO_ERROR) InputError("Get_Link_Path");
00252 FixName("link", LinkPathName);
00253 }
00254
00255
00256
00257 if (PathLength > 0 && (!IncludeLink || !LinkFileName[0])) {
00258 ADFH_Link(OutputID, name, LinkFileName, LinkPathName,
00259 &OutputChildID, &ErrStat);
00260 if (ErrStat != NO_ERROR) OutputError("Link");
00261 }
00262 else {
00263 ADFH_Create(OutputID, name, &OutputChildID, &ErrStat);
00264 if (ErrStat != NO_ERROR) OutputError("Create");
00265 WalkTheNodes (InputChildID, OutputChildID, indent+1);
00266 }
00267 }
00268 }
00269
00270
00271
00272 int main (int argc, char **argv)
00273 {
00274 int n;
00275 char *inpfile, *outfile = NULL;
00276 struct stat inpst, outst;
00277 time_t ts, te;
00278
00279 for (n = 1; n < argc; n++) {
00280 if (argv[n][0] != '-') break;
00281 if (argv[n][1] == '-') {
00282 n++;
00283 break;
00284 }
00285 if (argv[n][1] == 'p')
00286 PrintFlag = 1;
00287 else if (argv[n][1] == 'l')
00288 IncludeLink = 1;
00289 else {
00290 fprintf(stderr, "unknown option %s\n", argv[n]);
00291 exit (1);
00292 }
00293 }
00294
00295 if (n >= argc) {
00296 fprintf (stderr, "usage: adf2hdf [-links] [-print] InputFile [OutputFile]\n");
00297 exit (1);
00298 }
00299 inpfile = argv[n++];
00300 if (stat (inpfile, &inpst)) {
00301 fprintf (stderr, "can't stat %s\n", inpfile);
00302 exit (1);
00303 }
00304 if (!is_ADF_file(inpfile)) {
00305 fprintf (stderr, "%s is not an ADF file\n", inpfile);
00306 exit (1);
00307 }
00308
00309
00310
00311 outfile = n < argc ? argv[n] : inpfile;
00312 sprintf(TempFile, "%s.temp", outfile);
00313
00314 printf("converting ADF file %s to HDF5 file %s\n", inpfile, outfile);
00315 if (IncludeLink)
00316 printf ("links will be included in output file\n");
00317 fflush(stdout);
00318
00319 ts = time (NULL);
00320 ADF_Database_Open(inpfile, "READ_ONLY", "", &InputRootID, &ErrStat);
00321 if (ErrStat != NO_ERROR) {
00322 InputRootID = -1.0;
00323 InputError("Database_Open");
00324 }
00325 ADFH_Database_Open(TempFile, "NEW", "NATIVE", &OutputRootID, &ErrStat);
00326 if (ErrStat != NO_ERROR) {
00327 OutputRootID = -1.0;
00328 OutputError("Database_Open");
00329 }
00330
00331 WalkTheNodes (InputRootID, OutputRootID, 0);
00332
00333 ADF_Database_Close(InputRootID, &ErrStat);
00334 ADFH_Database_Close(OutputRootID, &ErrStat);
00335 te = time (NULL);
00336
00337 unlink (outfile);
00338 if (rename (TempFile, outfile)) {
00339 fprintf (stderr, "rename %s -> %s failed", TempFile, outfile);
00340 exit (1);
00341 }
00342
00343 if (stat (outfile, &outst)) {
00344 fprintf (stderr, "can't stat %s\n", outfile);
00345 exit (1);
00346 }
00347
00348 printf ("ADF file size = %ld bytes\n", (long)inpst.st_size);
00349 printf ("HDF5 file size = %ld bytes\n", (long)outst.st_size);
00350 printf ("conversion time = %d secs\n", (int)(te - ts));
00351 return 0;
00352 }
00353