1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*-------------------------------------------------------------------------
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from
the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
-------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "adf/ADF.h"
#include "ADFH.h"
#include "hdf5.h"
/* global variables */
static char TempFile[ADF_FILENAME_LENGTH+1];
static double InputRootID = -1.0;
static double OutputRootID = -1.0;
static int ErrStat;
static char ErrMsg[ADF_MAX_ERROR_STR_LENGTH+1];
static int IncludeLink = 0;
static int PrintFlag = 0;
static int NumDims;
static int PathLength;
static int DimVals[ADF_MAX_DIMENSIONS*2];
static char label[ADF_LABEL_LENGTH+1];
static char status[ADF_STATUS_LENGTH+1];
static char format[ADF_FORMAT_LENGTH+1];
static char name[ADF_NAME_LENGTH+1];
static char DataType[ADF_DATA_TYPE_LENGTH+1];
static char LinkFileName[ADF_FILENAME_LENGTH+1];
static char LinkPathName[ADF_MAX_LINK_DATA_SIZE+1];
/*-------------------------------------------------------------------*/
static void ErrorExit()
{
if (InputRootID >= 0.0)
ADFH_Database_Close (InputRootID, &ErrStat);
if (OutputRootID >= 0.0)
ADF_Database_Close (OutputRootID, &ErrStat);
if (TempFile[0])
unlink(TempFile);
exit(1) ;
}
/*-------------------------------------------------------------------*/
static void InputError(char *funcname)
{
fflush(stdout);
fprintf(stderr,"ADFH_%s: errno=%d",funcname,ErrStat);
if (ErrStat > 0) {
ADFH_Error_Message(ErrStat,ErrMsg);
fprintf(stderr,"%s\n",ErrMsg);
}
ErrorExit();
}
/*-------------------------------------------------------------------*/
static void OutputError(char *funcname)
{
fflush(stdout);
fprintf(stderr,"ADF_%s: errno=%d",funcname,ErrStat);
if (ErrStat > 0) {
ADF_Error_Message(ErrStat,ErrMsg);
fprintf(stderr,"%s\n",ErrMsg);
}
ErrorExit();
}
/*-------------------------------------------------------------------*/
static int CalculateDataSize(char *data_type, int ndims, int *dims)
{
char type[3];
int i, size = 1;
if (ndims == 0) return 0;
for (i = 0; i < ndims; i++)
size *= dims[i];
if (size <= 0) return 0;
strncpy(type, data_type, 2);
type[2] = 0;
for (i = 0; i < 2; i++)
if (islower(type[i]))
type[i] = toupper(type[i]);
if (0 == strcmp(type, "MT") ||
0 == strcmp(type, "LK")) return 0;
if (0 == strcmp(type, "B1") ||
0 == strcmp(type, "C1")) return (size * sizeof(char));
if (0 == strcmp(type, "I4") ||
0 == strcmp(type, "U4")) return (size * sizeof(int));
if (0 == strcmp(type, "I8") ||
0 == strcmp(type, "U8")) return (size * sizeof(long));
if (0 == strcmp(type, "R4")) return (size * sizeof(float));
if (0 == strcmp(type, "R8")) return (size * sizeof(double));
if (0 == strcmp(type, "X4")) return (size * 2 * sizeof(float));
if (0 == strcmp(type, "X8")) return (size * 2 * sizeof(double));
fflush(stdout);
fprintf(stderr,"unknown datatype: %s\n", data_type);
ErrorExit();
return 0;
}
/*-------------------------------------------------------------------*/
static void CopyTheNode (double InputID, double OutputID)
{
int DataSize;
void *DataBuffer;
/* Copy the node label type and size information */
ADFH_Get_Label(InputID, label, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Label");
ADF_Set_Label(OutputID, label, &ErrStat);
if (ErrStat != NO_ERROR) OutputError("Set_Label");
ADFH_Get_Data_Type(InputID, DataType, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Data_Type");
ADFH_Get_Number_of_Dimensions(InputID, &NumDims, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Number_of_Dimensions");
if (NumDims == 0) return;
ADFH_Get_Dimension_Values(InputID, DimVals, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Dimension_Values");
ADF_Put_Dimension_Information (OutputID, DataType, NumDims, DimVals,
&ErrStat);
if (ErrStat != NO_ERROR) OutputError("Put_Dimension_Information");
/* Copy any NodeData */
DataSize = CalculateDataSize(DataType, NumDims, DimVals);
if (DataSize == 0) return;
DataBuffer = (void *) malloc (DataSize);
if (DataBuffer == NULL) {
fflush(stdout);
fprintf(stderr, "malloc failed for the node data (%d bytes)\n",
DataSize);
ErrorExit();
}
ADFH_Read_All_Data(InputID, DataBuffer, &ErrStat);
if (ErrStat == NO_DATA) {
free (DataBuffer);
return;
}
if (ErrStat != NO_ERROR) InputError("Read_All_Data");
ADF_Write_All_Data(OutputID, DataBuffer, &ErrStat);
if (ErrStat != NO_ERROR) OutputError("Write_All_Data");
free (DataBuffer);
}
/*-------------------------------------------------------------------*/
static void WalkTheNodes (double InputID, double OutputID, int indent)
{
int ic, cnt, in;
int nchildren ;
double InputChildID ;
double OutputChildID ;
/* Copy the data from the current input node to the output node */
CopyTheNode (InputID, OutputID);
/* Loop through the children of the current node */
ADFH_Number_of_Children(InputID, &nchildren, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Number_of_Children");
if (nchildren == 0) return;
for (ic = 1; ic <= nchildren; ic++) {
/* Get child name and node ID */
ADFH_Children_IDs(InputID, ic, 1, &cnt, &InputChildID, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Children_IDs");
ADFH_Get_Name(InputChildID, name, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Name");
/* Write out a little status message for the user */
if (PrintFlag) {
for (in = 0; in < indent; in++)
putchar(' ');
printf ("%s\n", name);
}
/* Check for link, if it is a local link then we always link it!! */
ADFH_Is_Link(InputChildID, &PathLength, &ErrStat);
if (ErrStat != NO_ERROR) InputError("Is_Link");
if (PathLength > 0) {
ADFH_Get_Link_Path(InputChildID, LinkFileName, LinkPathName,
&ErrStat);
if (ErrStat != NO_ERROR) InputError("Get_Link_Path");
}
/* Create the node or the link */
if (PathLength > 0 && (!IncludeLink || !LinkFileName[0])) {
ADF_Link(OutputID, name, LinkFileName, LinkPathName,
&OutputChildID, &ErrStat);
if (ErrStat != NO_ERROR) OutputError("Link");
}
else {
ADF_Create(OutputID, name, &OutputChildID, &ErrStat);
if (ErrStat != NO_ERROR) OutputError("Create");
WalkTheNodes (InputChildID, OutputChildID, indent+1);
}
}
}
/*-------------------------------------------------------------------*/
int main (int argc, char **argv)
{
int n;
char *inpfile, *outfile = NULL;
struct stat inpst, outst;
time_t ts, te;
for (n = 1; n < argc; n++) {
if (argv[n][0] != '-') break;
if (argv[n][1] == '-') {
n++;
break;
}
if (argv[n][1] == 'p')
PrintFlag = 1;
else if (argv[n][1] == 'l')
IncludeLink = 1;
else {
fprintf(stderr, "unknown option %s\n", argv[n]);
exit (1);
}
}
if (n >= argc) {
fprintf (stderr, "usage: hdf2adf [-links] [-print] InputFile [OutputFile]\n");
exit (1);
}
inpfile = argv[n++];
if (stat (inpfile, &inpst)) {
fprintf (stderr, "can't stat %s\n", inpfile);
exit (1);
}
if (H5Fis_hdf5(inpfile) <= 0) {
fprintf (stderr, "%s is not a HDF5 file\n", inpfile);
exit (1);
}
/* output to temporary file */
outfile = n < argc ? argv[n] : inpfile;
sprintf(TempFile, "%s.temp", outfile);
printf("converting HDF5 file %s to ADF file %s\n", inpfile, outfile);
if (IncludeLink)
printf ("links will be included in output file\n");
fflush(stdout);
ts = time (NULL);
ADFH_Database_Open(inpfile, "READ_ONLY", "", &InputRootID, &ErrStat);
if (ErrStat != NO_ERROR) {
InputRootID = -1.0;
InputError("Database_Open");
}
ADF_Database_Open(TempFile, "NEW", "NATIVE", &OutputRootID, &ErrStat);
if (ErrStat != NO_ERROR) {
OutputRootID = -1.0;
OutputError("Database_Open");
}
WalkTheNodes (InputRootID, OutputRootID, 0);
ADFH_Database_Close(InputRootID, &ErrStat);
ADF_Database_Close(OutputRootID, &ErrStat);
te = time (NULL);
unlink (outfile);
if (rename (TempFile, outfile)) {
fprintf (stderr, "rename %s -> %s failed", TempFile, outfile);
exit (1);
}
if (stat (outfile, &outst)) {
fprintf (stderr, "can't stat %s\n", outfile);
exit (1);
}
printf ("HDF5 file size = %ld bytes\n", (long)inpst.st_size);
printf ("ADF file size = %ld bytes\n", (long)outst.st_size);
printf ("conversion time = %d secs\n", (int)(te - ts));
return 0;
}
|