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 void ErrorExit()
00055 {
00056 if (InputRootID >= 0.0)
00057 ADFH_Database_Close (InputRootID, &ErrStat);
00058 if (OutputRootID >= 0.0)
00059 ADF_Database_Close (OutputRootID, &ErrStat);
00060 if (TempFile[0])
00061 unlink(TempFile);
00062 exit(1) ;
00063 }
00064
00065
00066
00067 static void InputError(char *funcname)
00068 {
00069 fflush(stdout);
00070 fprintf(stderr,"ADFH_%s: errno=%d",funcname,ErrStat);
00071 if (ErrStat > 0) {
00072 ADFH_Error_Message(ErrStat,ErrMsg);
00073 fprintf(stderr,"%s\n",ErrMsg);
00074 }
00075 ErrorExit();
00076 }
00077
00078
00079
00080 static void OutputError(char *funcname)
00081 {
00082 fflush(stdout);
00083 fprintf(stderr,"ADF_%s: errno=%d",funcname,ErrStat);
00084 if (ErrStat > 0) {
00085 ADF_Error_Message(ErrStat,ErrMsg);
00086 fprintf(stderr,"%s\n",ErrMsg);
00087 }
00088 ErrorExit();
00089 }
00090
00091
00092
00093 static int CalculateDataSize(char *data_type, int ndims, int *dims)
00094 {
00095 char type[3];
00096 int i, size = 1;
00097
00098 if (ndims == 0) return 0;
00099 for (i = 0; i < ndims; i++)
00100 size *= dims[i];
00101 if (size <= 0) return 0;
00102
00103 strncpy(type, data_type, 2);
00104 type[2] = 0;
00105 for (i = 0; i < 2; i++)
00106 if (islower(type[i]))
00107 type[i] = toupper(type[i]);
00108
00109 if (0 == strcmp(type, "MT") ||
00110 0 == strcmp(type, "LK")) return 0;
00111 if (0 == strcmp(type, "B1") ||
00112 0 == strcmp(type, "C1")) return (size * sizeof(char));
00113 if (0 == strcmp(type, "I4") ||
00114 0 == strcmp(type, "U4")) return (size * sizeof(int));
00115 if (0 == strcmp(type, "I8") ||
00116 0 == strcmp(type, "U8")) return (size * sizeof(long));
00117 if (0 == strcmp(type, "R4")) return (size * sizeof(float));
00118 if (0 == strcmp(type, "R8")) return (size * sizeof(double));
00119 if (0 == strcmp(type, "X4")) return (size * 2 * sizeof(float));
00120 if (0 == strcmp(type, "X8")) return (size * 2 * sizeof(double));
00121
00122 fflush(stdout);
00123 fprintf(stderr,"unknown datatype: %s\n", data_type);
00124 ErrorExit();
00125 return 0;
00126 }
00127
00128
00129
00130 static void CopyTheNode (double InputID, double OutputID)
00131 {
00132 int DataSize;
00133 void *DataBuffer;
00134
00135
00136
00137 ADFH_Get_Label(InputID, label, &ErrStat);
00138 if (ErrStat != NO_ERROR) InputError("Get_Label");
00139 ADF_Set_Label(OutputID, label, &ErrStat);
00140 if (ErrStat != NO_ERROR) OutputError("Set_Label");
00141
00142 ADFH_Get_Data_Type(InputID, DataType, &ErrStat);
00143 if (ErrStat != NO_ERROR) InputError("Get_Data_Type");
00144 ADFH_Get_Number_of_Dimensions(InputID, &NumDims, &ErrStat);
00145 if (ErrStat != NO_ERROR) InputError("Get_Number_of_Dimensions");
00146 if (NumDims == 0) return;
00147 ADFH_Get_Dimension_Values(InputID, DimVals, &ErrStat);
00148 if (ErrStat != NO_ERROR) InputError("Get_Dimension_Values");
00149 ADF_Put_Dimension_Information (OutputID, DataType, NumDims, DimVals,
00150 &ErrStat);
00151 if (ErrStat != NO_ERROR) OutputError("Put_Dimension_Information");
00152
00153
00154
00155 DataSize = CalculateDataSize(DataType, NumDims, DimVals);
00156 if (DataSize == 0) return;
00157 DataBuffer = (void *) malloc (DataSize);
00158 if (DataBuffer == NULL) {
00159 fflush(stdout);
00160 fprintf(stderr, "malloc failed for the node data (%d bytes)\n",
00161 DataSize);
00162 ErrorExit();
00163 }
00164 ADFH_Read_All_Data(InputID, DataBuffer, &ErrStat);
00165 if (ErrStat == NO_DATA) {
00166 free (DataBuffer);
00167 return;
00168 }
00169 if (ErrStat != NO_ERROR) InputError("Read_All_Data");
00170 ADF_Write_All_Data(OutputID, DataBuffer, &ErrStat);
00171 if (ErrStat != NO_ERROR) OutputError("Write_All_Data");
00172 free (DataBuffer);
00173 }
00174
00175
00176
00177 static void WalkTheNodes (double InputID, double OutputID, int indent)
00178 {
00179 int ic, cnt, in;
00180 int nchildren ;
00181 double InputChildID ;
00182 double OutputChildID ;
00183
00184
00185
00186 CopyTheNode (InputID, OutputID);
00187
00188
00189
00190 ADFH_Number_of_Children(InputID, &nchildren, &ErrStat);
00191 if (ErrStat != NO_ERROR) InputError("Number_of_Children");
00192 if (nchildren == 0) return;
00193
00194 for (ic = 1; ic <= nchildren; ic++) {
00195
00196
00197
00198 ADFH_Children_IDs(InputID, ic, 1, &cnt, &InputChildID, &ErrStat);
00199 if (ErrStat != NO_ERROR) InputError("Children_IDs");
00200 ADFH_Get_Name(InputChildID, name, &ErrStat);
00201 if (ErrStat != NO_ERROR) InputError("Get_Name");
00202
00203
00204
00205 if (PrintFlag) {
00206 for (in = 0; in < indent; in++)
00207 putchar(' ');
00208 printf ("%s\n", name);
00209 }
00210
00211
00212
00213 ADFH_Is_Link(InputChildID, &PathLength, &ErrStat);
00214 if (ErrStat != NO_ERROR) InputError("Is_Link");
00215 if (PathLength > 0) {
00216 ADFH_Get_Link_Path(InputChildID, LinkFileName, LinkPathName,
00217 &ErrStat);
00218 if (ErrStat != NO_ERROR) InputError("Get_Link_Path");
00219 }
00220
00221
00222
00223 if (PathLength > 0 && (!IncludeLink || !LinkFileName[0])) {
00224 ADF_Link(OutputID, name, LinkFileName, LinkPathName,
00225 &OutputChildID, &ErrStat);
00226 if (ErrStat != NO_ERROR) OutputError("Link");
00227 }
00228 else {
00229 ADF_Create(OutputID, name, &OutputChildID, &ErrStat);
00230 if (ErrStat != NO_ERROR) OutputError("Create");
00231 WalkTheNodes (InputChildID, OutputChildID, indent+1);
00232 }
00233 }
00234 }
00235
00236
00237
00238 int main (int argc, char **argv)
00239 {
00240 int n;
00241 char *inpfile, *outfile = NULL;
00242 struct stat inpst, outst;
00243 time_t ts, te;
00244
00245 for (n = 1; n < argc; n++) {
00246 if (argv[n][0] != '-') break;
00247 if (argv[n][1] == '-') {
00248 n++;
00249 break;
00250 }
00251 if (argv[n][1] == 'p')
00252 PrintFlag = 1;
00253 else if (argv[n][1] == 'l')
00254 IncludeLink = 1;
00255 else {
00256 fprintf(stderr, "unknown option %s\n", argv[n]);
00257 exit (1);
00258 }
00259 }
00260
00261 if (n >= argc) {
00262 fprintf (stderr, "usage: hdf2adf [-links] [-print] InputFile [OutputFile]\n");
00263 exit (1);
00264 }
00265 inpfile = argv[n++];
00266 if (stat (inpfile, &inpst)) {
00267 fprintf (stderr, "can't stat %s\n", inpfile);
00268 exit (1);
00269 }
00270 if (H5Fis_hdf5(inpfile) <= 0) {
00271 fprintf (stderr, "%s is not a HDF5 file\n", inpfile);
00272 exit (1);
00273 }
00274
00275
00276
00277 outfile = n < argc ? argv[n] : inpfile;
00278 sprintf(TempFile, "%s.temp", outfile);
00279
00280 printf("converting HDF5 file %s to ADF file %s\n", inpfile, outfile);
00281 if (IncludeLink)
00282 printf ("links will be included in output file\n");
00283 fflush(stdout);
00284
00285 ts = time (NULL);
00286 ADFH_Database_Open(inpfile, "READ_ONLY", "", &InputRootID, &ErrStat);
00287 if (ErrStat != NO_ERROR) {
00288 InputRootID = -1.0;
00289 InputError("Database_Open");
00290 }
00291 ADF_Database_Open(TempFile, "NEW", "NATIVE", &OutputRootID, &ErrStat);
00292 if (ErrStat != NO_ERROR) {
00293 OutputRootID = -1.0;
00294 OutputError("Database_Open");
00295 }
00296
00297 WalkTheNodes (InputRootID, OutputRootID, 0);
00298
00299 ADFH_Database_Close(InputRootID, &ErrStat);
00300 ADF_Database_Close(OutputRootID, &ErrStat);
00301 te = time (NULL);
00302
00303 unlink (outfile);
00304 if (rename (TempFile, outfile)) {
00305 fprintf (stderr, "rename %s -> %s failed", TempFile, outfile);
00306 exit (1);
00307 }
00308
00309 if (stat (outfile, &outst)) {
00310 fprintf (stderr, "can't stat %s\n", outfile);
00311 exit (1);
00312 }
00313
00314 printf ("HDF5 file size = %ld bytes\n", (long)inpst.st_size);
00315 printf ("ADF file size = %ld bytes\n", (long)outst.st_size);
00316 printf ("conversion time = %d secs\n", (int)(te - ts));
00317 return 0;
00318 }
00319