summaryrefslogtreecommitdiffstats
path: root/tools/h4toh5main.c
blob: c936b1b1ad21125768199d840249d44d3ff8b159 (plain)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
/*-------------------------------------------------------------------------
 *
 * Copyright (C) 2000   National Center for Supercomputing Applications.
 *                      All rights reserved.
 *
 *-------------------------------------------------------------------------
 */

/******************************************************************************

  Description: 

1. converter

See HDF4 to HDF5 mapping specification at
(http://hdf.ncsa.uiuc.edu/HDF5/papers/h4toh5) for the default mapping 
from HDF4 object to HDF5 object.
 
The whole converter includes 10 files, h4toh5util.h, h4toh5main.h, h4toh5util.c, h4toh5main.c, h4toh5sds.c, h4toh5image.c,h4toh5vdata.c,h4toh5vgroup.c,h4toh5pal.c and h4toh5anno.c.

2. this file 

This file describes the main driver of hdf to hdf5 converter. It checks
the inputting parameters, initializes the global tables, sets up the root level
hdf5 structure and also check the special case fof vgroup loops at HDF file.


Author:  Kent Yang(ymuqun@ncsa.uiuc.edu)
 

*****************************************************************************/


#include "h4toh5main.h"

int32 estnum_vg;
int32 estnum_vd;
int32 num_sds;
int32 num_images;
int   num_objects;
int32 num_glsdsattrs;
int32 num_glgrattrs;
struct table* sds_hashtab;
struct table* gr_hashtab;
struct table* vg_hashtab;
struct table* vd_hashtab;
struct table* pal_hashtab;
struct name_table* name_hashtab;
struct name_table* dim_hashtab;

/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:     driver routine to handle all objects of hdf4 file.
 *             
 * Return:	FAIL if failed, SUCCEED if successful.
 
   Modfication: 
 *-------------------------------------------------------------------------
 */	



int main(int argc, char ** argv) {

        char *h5_filename=NULL;
	char *h4_filename=NULL;
	char *h5_extension;
	int status = 0;

	argc--;
	argv++;

	if (argc == 0) {
		fprintf(stderr,"\nError: Invalid Arguments\n");
		PrintOptions_h4toh5();
		return FAIL;
	}

	/* take care -h (help) option first */
	{   int i;
	    for (i=0; i < argc; i++)
		if ( HDstrcmp(argv[i],"-h") == 0 ) {
			PrintOptions_h4toh5();
			return SUCCEED;
		}
	}


	switch(argc) {

	case 0:

		PrintOptions_h4toh5();
		break;

	case 1:	/* h4toh5 file1 */
		h4_filename = argv[0];
#ifndef WIN32
		if (test_file(h4_filename,O_EXCL,292) != 0 ) { 
		  /* 292 Decimal - 0444 Octal, a+r */
		  printf("the current hdf4 file name is not set properly.\n");
		  status = -1;
		  break;
		}
		if (test_dir(h4_filename) != 0 ) {
		   fprintf(stderr,"%s: Is a directory\n",h4_filename);
		   status = -1;
		   break;
		}
#endif
		/*0. check whether this file is an hdf file. */

		if(!Hishdf(h4_filename)){
		  printf("error: not an hdf file. \n");
		  printf("the file will not be converted. \n");
		  status = -1;
		  break;
		}
		h5_extension = HDstrdup("h5");
		h5_filename = BuildFilename(h4_filename,h5_extension);
		if (h5_filename == NULL) {
		  printf("error in creating hdf5 file name.\n");
		  status = -1;
		  break;
		}
#ifndef WIN32
		if (test_file(h5_filename,O_CREAT|O_EXCL,436) != 0) {
		  /* 436 Decimal - 0664 Octal, ug+rw,o+r */
		  printf("permission of hdf5 file is not set properly.\n");
		  status = -1;
		  break;
		}
#endif
		status = h4toh5(h4_filename, h5_filename);

		if ( status == FAIL ) {
		  printf("error in converting %s into %s\n",h4_filename,h5_filename);
		  break;
		}
		if (h5_filename != NULL) {
		   HDfree(h5_filename);
		}

		break;
	
	case 2:	/* h4toh5 file_in file_out */

		h4_filename = argv[0];
		h5_filename = argv[1];

#ifndef WIN32
		if (test_file(h4_filename,O_EXCL,292) != 0 ) { 
		  /* 292 Decimal - 0444 Octal, a+r */
		  printf("permission of hdf4 file is not set properly.\n");    
		  status = -1;
		  break;
		}

		if (test_dir(h4_filename) != 0 ) {
		   fprintf(stderr,"%s: Is a directory\n",h4_filename);
		   status = -1;
		   break;
		}

#endif
		/*0. check whether this file is a hdf file. */

		if(!Hishdf(h4_filename)){
		  printf("error: not an hdf file. \n");
		  printf("the file will not be converted. \n");
		  status = -1;
		  break;
		}

#ifndef WIN32
		if (test_file(h5_filename,O_CREAT|O_RDWR,436) != 0) { /* 436 Decimal - 0664 Octal, ug+rw,o+r */
		  printf("permission of hdf5 file is not set properly.\n");
		  status = -1;
		  break;
		}

		if (test_dir(h4_filename) != 0 ) {
		  fprintf(stderr,"%s: Is a directory\n",h4_filename);
		  status = -1;
		  break;
		}

#endif
		status = h4toh5(h4_filename, h5_filename);
		if ( status == FAIL ) {
		   printf("error in converting %sinto %s\n",h4_filename,h5_filename);
		  break;
		}
		break;

	default:	
		break;
	}

	return status;

}

/*-------------------------------------------------------------------------
 * Function:	h4toh5
 *
 * Purpose:    This routine checks out arguments sent, makes sure that hdf4 
               file is valid, makes sure filename for hdf5 file is correct,
	       and then call h4toh5().
	     
 *-------------------------------------------------------------------------
 */	   
int h4toh5(char*filename4, char*filename5) {
  
  /* define variables for hdf4. */
  int32  istat ; /* hdf4 library routine return value. */
  int32  file_id;/* file identfier of hdf file.*/
  int32  sd_id;/* sd interface identifer*/
  int32  gr_id;/* gr interface identifer*/
  int    check_glo;

  /* define variables for hdf5. */
  hid_t  file5_id;/* hdf5 file identifier. */
  hid_t  h5_root;/* new hdf5 root group identifier.*/
  
  hid_t  h5_dimg;/* hdf5 dimensional scale group identifier. */
  hid_t  h5_palg;/* hdf5 palette group identifier. */

  /*1. open the current hdf4 file. */

  file_id = Hopen(filename4, DFACC_READ, 0);
  if(file_id == FAIL) {
    printf("error: no such hdf4 files. \n");
    return FAIL;
  }

  /* open sd interface.*/
  sd_id = SDstart(filename4,DFACC_READ);
  if(sd_id == FAIL) {
    printf("error: cannot start SD interface. \n");
    Hclose(file_id);
    return FAIL;
  }

  /* open gr interface.*/
  gr_id = GRstart(file_id);
  if(gr_id == FAIL) {
    printf("error in obtaining gr id. \n");
    SDend(sd_id);
    Hclose(file_id);
    return FAIL;
  }

  /* open V interface. */
  istat = Vstart(file_id);
  if(istat == FAIL) {
    printf("error in starting V interface. \n");
    SDend(sd_id);
    GRend(gr_id);
    Hclose(file_id);
    return FAIL;
  }

  /* 2. obtain number of hdf4 objects(sds,image,vdata,vgroup,palette) 
     in this hdf4 file.  */

  if(get_numof_hdf4obj(filename4,file_id) == FAIL) {
    printf("error in obtaining number of hdf4 objects.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    return FAIL;
  }
  
  /* set up global hash tables for hdf4 objects. */
  if(set_hashtables() == FAIL){
    printf("error in setting hashtables. \n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    return FAIL;
  }

  /* create hdf5 file. */
  file5_id = H5Fcreate(filename5,H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT);

  if (file5_id < 0) {
     fprintf(stderr, "unable to create hdf5 file \n");
     SDend(sd_id);
     GRend(gr_id);
     Vend(file_id);
     Hclose(file_id);
     free_allhashmemory();
     return FAIL;
  }

  /* Initialize hdf5 group interface. */
  h5_root = H5Gopen(file5_id,"/");

  if(h5_root < 0) {
    printf("error in opening hdf5 root group. \n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

   /**** build up helper groups(dimensional scale and palette) ****/
  if(set_helpgroups(h5_root,&h5_dimg,&h5_palg)==FAIL) {
    printf("error setting up dimensional scale and palette groups.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }
  
  /* convert global sds attributes into global attributes under root group.*/
  check_glo = 1;

  if(sds_transattrs(sd_id, h5_root,num_glsdsattrs,check_glo)==FAIL) {
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
   }
 

  /* convert global image attributes into global attributes under root group.*/
  check_glo = 1;

  if(gr_tranattrs(gr_id, h5_root,num_glgrattrs,check_glo)==FAIL) {
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
   }

 /* convert all objects in lone vgroups into corresponding hdf5 objects. */  
  if(h4toh5lonevgs(file_id,sd_id,h5_root,h5_dimg,h5_palg)== FAIL) {
    printf("error in translating lone vgroup into hdf5 objects.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

/*convert all objects in group rings into corresponding hdf5 objects. */
  if(h4toh5vgrings(file_id,sd_id,h5_root,h5_dimg,h5_palg) == FAIL){
    printf("error in translating vgroup rings into hdf5 objects.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

  /*convert all independent lone vdata into corresponding hdf5 datasets with 
    compound data type. */
  if(h4toh5lonevds(file_id,h5_root) == FAIL){
    printf("error in translating lone independent vdata into hdf5 objects.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

  /*** convert hdf file annotations into hdf5 attributes under the root.***/
  if(Annofil_h4_to_h5(file_id,h5_root) == FAIL) {
    printf("error in translating file annotations into root attributes.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

  /*** deal with untouched sds objects.convert them into hdf5 datasets under root group.***/

  if(h4toh5unvisitedsds(file_id,sd_id,h5_root,h5_dimg) == FAIL) {
    printf("error in converting unvisited sds objects into hdf5 file.\n");  
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

  /*** deal with untouched image objects. convert them into hdf5 datasets under root group. ***/

  if(h4toh5unvisitedimages(file_id,h5_root,h5_palg) == FAIL) {
    printf("error in converting unvisited image objects into hdf5 file.\n");
    SDend(sd_id);
    GRend(gr_id);
    Vend(file_id);
    Hclose(file_id);
    if(num_sds >0) H5Gclose(h5_dimg);
    if(num_images >0) H5Gclose(h5_palg);
    H5Gclose(h5_root);
    H5Fclose(file5_id);
    free_allhashmemory();
    return FAIL;
  }

  free_allhashmemory();
  SDend(sd_id);
  GRend(gr_id);
  Vend(file_id);
  Hclose(file_id); 
  if(num_sds >0) H5Gclose(h5_dimg);
  if(num_images >0) H5Gclose(h5_palg);
  H5Gclose(h5_root);
  H5Fclose(file5_id);
  return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:	get_numof_hdf4obj
 *
 * Purpose:     get number or estimated number of hdf4 objects
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
                file_id: hdf file identifier
		filename: hdf file name
   Out: 
   Modification: 
 *-------------------------------------------------------------------------
 */	 
int get_numof_hdf4obj(char*filename,int32 file_id) {

  int32  sd_id;/* sd interface identifer*/
  int32  gr_id;/* gr interface identifer*/
  int    num_lonevd;/* number of lone vdata*/
  int    num_lonevg;/* number of lone vgroup.*/
  int32  istat;

  estnum_vg     = 0;
  estnum_vd     = 0;
  num_sds       = 0;
  num_images    = 0;
  num_objects   = 0;

  /* obtain number of sds and number of global sds attribute. */ 

  sd_id = SDstart(filename,DFACC_READ);
  if(sd_id == FAIL) {
    printf("error: cannot start SD interface. \n");
    return FAIL;
  }

  if(SDfileinfo(sd_id,&num_sds,&num_glsdsattrs) == FAIL) {
    printf("error in obtaining SDS information from the file.\n");
    return FAIL;
  }
  
  /* obtain number of images and number of global image attributes.*/

  gr_id = GRstart(file_id);
  if(gr_id == FAIL) {
    printf("error in obtaining gr id. \n");
    return FAIL;
  }

  if(GRfileinfo(gr_id,&num_images,&num_glgrattrs) == FAIL) {
    printf("error in obtaining GR information from the file. \n");
    return FAIL;
  }

  /* obtain number of lone vgroup and lone vdata. */
  
  istat = Vstart(file_id);
  if (istat == FAIL) { 
     fprintf(stderr, "unable to start hdf4 V interface.\n");
     return FAIL;
  }

  num_lonevd = VSlone(file_id,NULL,0);
  if(num_lonevd == FAIL) {
    printf("error in obtaining lone vdata number. \n");
    return FAIL;
  }

  num_lonevg = Vlone(file_id,NULL,0);
  if(num_lonevg == FAIL) {
    printf("error in obtaining lone vgroup number. \n");
    return FAIL;
  }

  /* intelligent guess of the total number of vgroups,total number of 
     independent vdata. */

  estnum_vg = 6* num_lonevg;
  estnum_vd = 4* num_lonevd;
 
  /* set the size of name hashtable to num_objects. */
  num_objects = estnum_vg + estnum_vd + num_sds + num_images;
  
  return SUCCEED;
}



/*-------------------------------------------------------------------------
 * Function:	set_helpgroups
 *
 * Purpose:     get number or estimated number of hdf4 objects
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
                h5root: hdf5 group identifier
		h5dimgptr: h5 dimensional group pointer
		h5palgptr: h5 palette group pointer
   Modification:
 *-------------------------------------------------------------------------
 */	 

int set_helpgroups(hid_t h5root,hid_t* h5dimgptr,hid_t* h5palgptr){

  hid_t  h5_dimg;/* hdf5 dimensional scale group identifier. */
  hid_t  h5_palg;/* hdf5 palette group identifier. */
  
  /*1. dimensional scale group.*/

  if(num_sds > 0) {
    h5_dimg = H5Gcreate(h5root,HDF4_DIMG,0);
    if (h5_dimg <0) {
      printf("error in creating hdf5 dimensional scale group. \n");
      return FAIL;
    }

    *h5dimgptr = h5_dimg;
  }

  /*2. palette group.*/
  
  if(num_images >0) {
    h5_palg = H5Gcreate(h5root,HDF4_PALG,0);
    if(h5_palg <0) {
      printf("error in creating hdf5 palette group. \n");
      H5Gclose(h5_dimg);
      return FAIL;
    }

    *h5palgptr = h5_palg;
  }

  return SUCCEED;

}


/*-------------------------------------------------------------------------
 * Function:	set_hashtables
 *
 * Purpose:     set up hashtables
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
               
   Out:  
   Modification:       
 *-------------------------------------------------------------------------
 */	
int set_hashtables(void) {
 
  if(num_sds > 0) {
    sds_hashtab = malloc(sizeof(struct table)*2*num_sds);
    if(init_tab(2*num_sds,sds_hashtab)== FAIL){
      printf("cannot initialize sds hashing table. \n");
      return FAIL;
    }
  }

  if(num_images > 0) {
    gr_hashtab = malloc(sizeof(struct table)*2*num_images);
    if(init_tab(2*num_images,gr_hashtab) == FAIL){
      printf("cannot initialize image hashing table. \n");
      return FAIL;
    }
  }

  /*hashtable is made to be fixed for dimensional scale and palette.*/

  if(num_sds > 0) {
    dim_hashtab = malloc(sizeof(struct name_table)*DIM_HASHSIZE);
    if(init_nametab(DIM_HASHSIZE,dim_hashtab) == FAIL) {
      printf("can not initialize dimension hashing table.\n");
      return FAIL;
    }
  }

  /* initialize the palette table */
  if(num_images > 0){
    pal_hashtab = malloc(sizeof(struct table)*PAL_HASHSIZE);
    if(init_tab(PAL_HASHSIZE,pal_hashtab) == FAIL) {
      printf("can not initialize palette hashing table.\n");
      return FAIL;
    }
  }

  /* initialize the vgroup table */
  if(estnum_vg > 0) { 
    vg_hashtab = malloc(sizeof(struct table)*estnum_vg);
  }
  else {
    estnum_vg = VG_DEFHASHSIZE;
    vg_hashtab = malloc(sizeof(struct table)*estnum_vg);
  }
  if(init_tab(estnum_vg,vg_hashtab) == FAIL) {
      printf("error in allocating memory for vgroup hashing table.\n");
      return FAIL;
  }

  /* initialize the vdata table.*/
  if(estnum_vd > 0) {
    vd_hashtab = malloc(sizeof(struct table)*estnum_vd);
  }
  else {
    estnum_vd = VD_DEFHASHSIZE;
    vd_hashtab = malloc(sizeof(struct table)*estnum_vd);
  }

  if(init_tab(estnum_vd,vd_hashtab)== FAIL) {
     printf("cannot initialize vdata hashing table.\n");
     return FAIL;
  }
  
  /* The name hashtable is only for dealing with name clashing,
     num_objects is the size of the hash table. */

  if(num_objects != 0){
    name_hashtab = malloc(sizeof(struct name_table)*num_objects);
    if(init_nametab(num_objects,name_hashtab)== FAIL) {
      printf("cannot initialize name hashing table. \n");
      return FAIL;
    }
  }

  return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    h4toh5lonevgs
 *
 * Purpose:     Recursively convert hdf4 objects in lone vgroups into
                corresponding hdf5 datasets
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        file_id: hdf file id
                sd_id:   hdf sd interface id
		h5group: hdf5 group id
		h5_dimg: hdf5 dimensional scale group id
		h5_palg: hdf5 palette group id
               
   Out:  
   Modification:
 *-------------------------------------------------------------------------
 */	
int h4toh5lonevgs(int32 file_id,int32 sd_id,hid_t h5group,hid_t h5_dimg,hid_t h5_palg) {
		   
  int32  vgroup_id;
  int    num_lonevg; /* number of lone vgroup.*/
  int32  *ref_array;
  int32  istat;
  char   vgroup_name[VGNAMELENMAX];
  char*  cor_vgroupname;
  char   vgroup_class[VGNAMELENMAX];
  char   refstr[MAXREF_LENGTH];
  int    check_vgroup;
  int    check_tabst;
  int    lone_vg_number;
  char   *h5cgroup_name;

  istat = Vstart(file_id);
  if (istat == FAIL) { 
     fprintf(stderr, "unable to start hdf4 V interface.\n");
     return FAIL;
  }

  num_lonevg = Vlone(file_id,NULL,0);
  
  if (num_lonevg == FAIL) {
    printf("error in obtaining lone vgroup number. \n");
    return FAIL;
  }

  /* obtain object reference array. */

  /* if no lone vgroup, quit from this function. */
  if(num_lonevg == 0) 
    return SUCCEED;

  ref_array = (int32 *)malloc(sizeof(int32) *num_lonevg);

  if(ref_array == NULL) {
     printf("error in allocating memory for ref_array.\n");
     return FAIL;
  }

  num_lonevg = Vlone(file_id,ref_array,num_lonevg);

  /* walk through every lone group in the file */

  for(lone_vg_number = 0; lone_vg_number < num_lonevg;
      lone_vg_number++) {

     vgroup_id = Vattach(file_id,ref_array[lone_vg_number],"r");

     if(vgroup_id ==FAIL) {
       printf("error in attaching lone vgroup.\n");
       free(ref_array);
       return FAIL;
     }
	
     /*obtain group name and class name.*/
     h4toh5_ZeroMemory(vgroup_class,VGNAMELENMAX);
     istat = Vgetclass(vgroup_id,vgroup_class);
     if(istat == FAIL) {
       printf("error in getting vgroup class.\n");
       free(ref_array);
       Vdetach(vgroup_id);
       return FAIL;
     }

     h4toh5_ZeroMemory(vgroup_name,VGNAMELENMAX);
     istat = Vgetname(vgroup_id,vgroup_name);
     if(istat == FAIL ) {
       printf("error in getting vgroup name. \n");
       Vdetach(vgroup_id);
       free(ref_array);
       return FAIL;
     }

     /* check for CDF0.0 and RIG0.0, if yes
        don't go into this group.*/
         
      if(strcmp(vgroup_class,_HDF_CDF)==0) {
	Vdetach(vgroup_id);
	continue;
      }
      if(strcmp(vgroup_class,GR_NAME)==0) {
	Vdetach(vgroup_id);
	continue;
      }

     /* converting integer number into string format. */
     if(conv_int_str(ref_array[lone_vg_number],refstr) == FAIL) {
       printf("ref. is negative, error in converting\n");
       Vdetach(vgroup_id);
       free(ref_array);
       return FAIL;
     }

     /* checking whether vgroup name contains ORI_SLASH, changing into CHA_SLASH.*/
     cor_vgroupname = correct_name(vgroup_name);
     if(cor_vgroupname == NULL) {
       printf("error in generating corrected vgroup name. \n");
       Vdetach(vgroup_id);
       free(ref_array);
       return FAIL;
     }
     
     /* obtaining group name of the converted lone vgroup. In this call,
      we will deal with cases such as name clashing and no available vgroup
     name. */

     h5cgroup_name = get_obj_aboname(cor_vgroupname,refstr,NULL,HDF4_VGROUP);
	
     if(h5cgroup_name == NULL) {
       printf("error in getting group name.\n");
       Vdetach(vgroup_id);
       free(ref_array);
       free(cor_vgroupname);
       return FAIL;
     }
    
     /* free memory of corrected name. */
     free(cor_vgroupname);

     /* updating lookup table for vgroups.*/

     check_vgroup = lookup(ref_array[lone_vg_number],estnum_vg,vg_hashtab);

     if(check_vgroup == 0) { /* adding this vgroup into the list. */

       check_tabst = set_name(ref_array[lone_vg_number],estnum_vg,
			      vg_hashtab,h5cgroup_name);
       if(check_tabst == FAIL) {
	 printf("not enough memory to be allocated for vgroup name. \n");
	 Vdetach(vgroup_id);
	 free(h5cgroup_name);
	 free(ref_array);
	 return FAIL;
       }
     }

     /* this line should never fail, if failed, something is wrong with converter or hdf library. */

     if(check_vgroup == 1){
       fprintf(stderr,"this vgroup should not be touched. \n");
       Vdetach(vgroup_id);
       free(h5cgroup_name);
       free(ref_array);
       return FAIL;
     }
     
     if(Vgroup_h4_to_h5(file_id,vgroup_id,sd_id,h5group,h5_dimg,h5_palg)==FAIL){
       printf("error in translating vgroup into hdf5 objects.\n");
       Vdetach(vgroup_id);
       free(h5cgroup_name);
       free(ref_array);
       return FAIL;
     }
      
     Vdetach(vgroup_id);
     free(h5cgroup_name);
  }
  free(ref_array);
  return SUCCEED;
}



/*-------------------------------------------------------------------------
 * Function:    h4toh5vgrings
 *
 * Purpose:     Recursively convert objects at special hdf4 vgroups
                (vgroup rings) 
                into objects of corresponding hdf5 groups. The strategy here 
		is to arbitrily grab any vgroup in the group ring and put it 
		under hdf5 root.
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        file_id: hdf file id
                sd_id:   hdf sds  id
		h5group: hdf5 group id
		h5_dimg: hdf5 dimensional scale group id
		h5_palg: hdf5 palette group id
               
   Out:   
   Modification:
 *-------------------------------------------------------------------------
 */	

int h4toh5vgrings(int32 file_id,int32 sd_id,hid_t h5group,hid_t h5_dimg,hid_t h5_palg){

  int32  vgroup_id;
  int32  ref_num;
  char   vgroup_name[VGNAMELENMAX];
  char*  cor_vgroupname;
  char   vgroup_class[VGNAMELENMAX];
  char   refstr[MAXREF_LENGTH];
  int    check_vgroup;
  int32  istat;
  char   *h5cgroup_name;

  ref_num = Vgetid(file_id,-1);

  while (ref_num != -1) {
  
  /* if we find a group that is not touched, grab it under root group.*/

    check_vgroup = lookup(ref_num,estnum_vg,vg_hashtab);

    if (check_vgroup == 0){

      vgroup_id = Vattach(file_id,ref_num,"r");
      if(vgroup_id ==FAIL) {
	printf("error in attaching group in a group ring. \n");
	return FAIL;
      }

      h4toh5_ZeroMemory(vgroup_name,VGNAMELENMAX);
      istat = Vgetname(vgroup_id,vgroup_name);
      if(istat ==FAIL) {
	printf("error in obtaining vgroup names. \n");
	Vdetach(vgroup_id);
	return FAIL;
      }

      h4toh5_ZeroMemory(vgroup_class,VGNAMELENMAX);
      if(Vgetclass(vgroup_id,vgroup_class) == FAIL) {
        printf("error in obtaining vgroup class name. \n");
	Vdetach(vgroup_id);
        return FAIL;
      }

       /* do nothing for those predefined attribute.*/

      if(vgroup_class[0] != '\0') {

	 if(strcmp(vgroup_class,_HDF_ATTRIBUTE)==0) {
	   ref_num = Vgetid(file_id,ref_num); 
	   Vdetach(vgroup_id);
	   continue;
	 }

	 if(strcmp(vgroup_class,_HDF_VARIABLE)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }

	 if(strcmp(vgroup_class,_HDF_DIMENSION)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }

	 if(strcmp(vgroup_class,_HDF_UDIMENSION)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }

	 if(strcmp(vgroup_class,_HDF_CDF)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }

         if(strcmp(vgroup_class,GR_NAME)==0)  {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }

	 if(strcmp(vgroup_class,RI_NAME)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }
       }

       if(vgroup_name[0] != '\0') {
	 if(strcmp(vgroup_name,GR_NAME)==0) {
	   ref_num = Vgetid(file_id,ref_num);
	   Vdetach(vgroup_id);
	   continue;
	 }
       }

       /* convert reference number into string format. */
       if(conv_int_str(ref_num,refstr) == FAIL) {
	 printf("ref. is negative, error in converting\n");
	 Vdetach(vgroup_id);
	 return FAIL;
       }

        /* checking whether vgroup name contains ORI_SLASH, changing into CHA_SLASH.*/
       cor_vgroupname = correct_name(vgroup_name);
       if(cor_vgroupname == NULL) {
	 printf("error in generating corrected vgroup name. \n");
	 Vdetach(vgroup_id);
	 return FAIL;
       }
       /* obtain the hdf5 group name. */
       h5cgroup_name = get_obj_aboname(cor_vgroupname,refstr,NULL,HDF4_VGROUP);

       if(h5cgroup_name == NULL) {
	 printf("error in getting vgroup name.\n");
	 Vdetach(vgroup_id);
	 free(cor_vgroupname);
	 return FAIL;
       }

       free(cor_vgroupname);
       if(set_name(ref_num,estnum_vg,vg_hashtab,h5cgroup_name)==FAIL) {
	 printf("error in setting h5 group name.\n");
	 Vdetach(vgroup_id);
	 free(h5cgroup_name);
	 return FAIL;
       }

       if(Vgroup_h4_to_h5(file_id,vgroup_id,sd_id,h5group,h5_dimg,h5_palg)
	  ==FAIL){
			  
	printf("error in translating vgroup into hdf5 group\n");
	Vdetach(vgroup_id);
	free(h5cgroup_name);
	return FAIL;
       }
      
      Vdetach(vgroup_id);
      free(h5cgroup_name);
    }
    ref_num = Vgetid(file_id,ref_num); 
  }
  return SUCCEED;

}

 
/*-------------------------------------------------------------------------
 * Function:    h4toh5lonevds
 *
 * Purpose:     convert hdf4 lone vdata into
                the corresponding hdf5 datasets
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        file_id: hdf file id
		h5group: hdf5 group id
               
   Out:   
   Modification:
 *-------------------------------------------------------------------------
 */	
int h4toh5lonevds(int32 file_id, hid_t h5group){

  int32  vdata_id;
  int32  *ref_vdata_array;
  int32   vdata_tag;
  int32   vdata_ref;
  int32  istat;
  char   vdata_name[VGNAMELENMAX];
  char*  cor_vdataname;
  char   vdata_class[VGNAMELENMAX];
  char   refstr[MAXREF_LENGTH];
  int    check_vdata;
  int    lone_vd_number;
  int    num_lonevd;
  char   *h5cvdata_name;

  num_lonevd = VSlone(file_id,NULL,0);
  
  if (num_lonevd == FAIL) {
    printf("error in obtaining lone vgroup number. \n");
    return FAIL;
  }

  if (num_lonevd > 0) {

     ref_vdata_array = (int32 *)malloc(sizeof(int32) *(num_lonevd));

     num_lonevd = VSlone(file_id,ref_vdata_array,num_lonevd);

     if(num_lonevd == FAIL) {
       printf("error in obtaining lone vdata number the second time.\n");
       free(ref_vdata_array);
     }
     /* walk through all lone vdatas. */

     for(lone_vd_number = 0; lone_vd_number < num_lonevd;lone_vd_number++) 
       {
	 vdata_id = VSattach(file_id,ref_vdata_array[lone_vd_number],"r");
	
	 if(vdata_id == FAIL) {
	   printf("error in obtaining vdata id for lone vdata.\n");
	   free(ref_vdata_array);
	 }

	 /* Make sure this vdata is not an attribute of other hdf4 objects.*/

	 if(!VSisattr(vdata_id)) {
	   vdata_ref = VSQueryref(vdata_id);
	   if(vdata_ref == FAIL) {
	     printf("error in getting vdata reference number.\n");
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     return FAIL;
	   }

	   vdata_tag = VSQuerytag(vdata_id);
	   if(vdata_tag == FAIL){
	     printf("error in getting vdata tag.\n");
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     return FAIL;
	   }
	   
           h4toh5_ZeroMemory(vdata_class,VGNAMELENMAX);
	   istat = VSgetclass(vdata_id,vdata_class);
	   if(istat == FAIL) {
	     printf("error in getting vdata class name.\n");
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     return FAIL;
	   }
	
	    h4toh5_ZeroMemory(vdata_name,VGNAMELENMAX);
	   istat = VSQueryname(vdata_id,vdata_name);
	   if(istat == FAIL) {
	     printf("error in getting vdata name. \n");
	     free(ref_vdata_array);
             VSdetach(vdata_id);
	     return FAIL;
	   }

	   /* converting reference number into string format.*/
	   if(conv_int_str(ref_vdata_array[lone_vd_number],refstr)==FAIL) {
	     printf("error in converting int to string.\n");
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     return FAIL;
	   }  
	   /* checking whether vdata name contains ORI_SLASH, changing into CHA_SLASH.*/
	   cor_vdataname = correct_name(vdata_name);
	   if(cor_vdataname == NULL) {
	     printf("error in generating corrected vgroup name. \n");
	     VSdetach(vdata_id);
	     free(ref_vdata_array);
	     return FAIL;
	   }
	   /* obtaining hdf5 dataset name that is converted from hdf4 vdata.*/
           h5cvdata_name = get_obj_aboname(cor_vdataname,refstr,NULL,HDF4_VDATA);
	   if(h5cvdata_name == NULL) {
	     printf("error in getting vdata name.\n");
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     free(cor_vdataname);
	     return FAIL;
	   }

	   free(cor_vdataname);
	   check_vdata = lookup(ref_vdata_array[lone_vd_number],estnum_vd,
				vd_hashtab);

           /* check_vdata should be 1, if it is 1, either converter or hdf lib has bugs. */
	   if(check_vdata == 1){
	     printf("lone vdata should not be checked before.\n");
	     free(h5cvdata_name);
	     free(ref_vdata_array);
             VSdetach(vdata_id);
	     return FAIL;
	   }

           if(set_name(ref_vdata_array[lone_vd_number],estnum_vd,vd_hashtab,
		       h5cvdata_name)==FAIL) {
	     printf("error in setting lone vdata name. \n");
	     free(ref_vdata_array);
	     free(h5cvdata_name);
	     VSdetach(vdata_id);
	     return FAIL;
	   }

	   if(Vdata_h4_to_h5(file_id,vdata_id,h5group)== FAIL) {
	     printf("error in translating independent vdata into");
	     printf(" hdf5 datasets.\n");
	     free(h5cvdata_name);
	     free(ref_vdata_array);
	     VSdetach(vdata_id);
	     return FAIL;
	   }
           free(h5cvdata_name);
	 }
       
       VSdetach(vdata_id);
       }
     free(ref_vdata_array);
  }
  return SUCCEED;   
}


/*-------------------------------------------------------------------------
 * Function:    h4toh5unvisitedsds
 *
 * Purpose:     convert unvisited sds objects into hdf5 datasets and put these
                datasets under hdf5 root group
                This routine will cover old hdf file that doesn't have vgroups.
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
                sd_id:   hdf sds  id
		h5root: hdf5 root id
		h5_dimg: hdf5 dimensional scale group id
               
   Out:   
   Modification:
 *-------------------------------------------------------------------------
 */	


int h4toh5unvisitedsds(int32 file_id,int32 sd_id,hid_t h5root,hid_t h5_dimg) {

  int     i;
  int32   sds_id;/* sd dataset identifer*/
  int32   sds_rank;/* sds dataset dimension rank.*/
  int32   sds_dimsizes[DIM_HASHSIZE];/* array that contains the size of the each dimension in sds dataset.*/ 
  int32   sds_dtype;/*sds dataset datatype.*/
  int32   num_sdsattrs;/* number of sds attributes. */
  char    sds_name[MAX_NC_NAME];/* sds name.*/
  char*   cor_sdsname;
  int32   obj_ref; /* obj reference number assigned to sds and images.*/
  char   refstr[MAXREF_LENGTH];/*object reference number in character string format.*/
  int    check_sds;/* flag to check whether this sds is visited. 1 for visited  and 0 for non-visited.*/
  char   *h5csds_name;/* absolute path name of hdf5 dataset transferred from old sds.*/

  if(sd_id == FAIL) {
    printf("error: cannot start SD interface. \n");
    return FAIL;
  }

  /* check all sds objects. */
  for(i=0;i<num_sds;i++){
       
     sds_id    = SDselect(sd_id,i);

     if (sds_id == FAIL) {
	 printf("error in obtaining sd id.\n");
 	 return FAIL;
     }
       
     /* if this sds is dimensional scale, the converting should be ignored. dimensional scale will be converted separately. */
     if(SDiscoordvar(sds_id)) continue;

     /* obtain sds information. */
     if(SDgetinfo(sds_id,sds_name,&sds_rank,sds_dimsizes,
		  &sds_dtype,&num_sdsattrs)== FAIL) {
       printf("error in obtaining SD info at ");
       printf("the unvisited sds routine.\n");
       SDendaccess(sds_id);
       return FAIL;
     }

     /* obtain object reference number of the current sds dataset.*/
     obj_ref = SDidtoref(sds_id);
     if(obj_ref == FAIL) {
       printf("error in obtaining sds object reference at ");
       printf("the unvisited sds routine.\n");
       SDendaccess(sds_id);
       return FAIL;
     }

     /* convert object reference number into string format. */
     if(conv_int_str(obj_ref,refstr) == FAIL) {
       printf("error in converting integer into string.\n");
       SDendaccess(sds_id);
       return FAIL;
     }

     /* check whether the current sds is visited or not. */
     check_sds = lookup(obj_ref,2*num_sds,sds_hashtab);

     /* if not visited, we will do the convertion. */

     if(check_sds == 0) {
     /* since different hdf sds may hold the same name and it is also 
	legal that sds may not have a name; but for hdf5 dataset, 
	it must hold a name, so we will use get_obj_aboname to assure 
	that each new hdf5 dataset converted from
	sds objects will have a disabiguous name. */

       /* checking whether vgroup name contains ORI_SLASH, changing into CHA_SLASH.*/
       cor_sdsname = correct_name(sds_name);
       if(cor_sdsname == NULL) {
	 printf("error in generating corrected sds name. \n");
	 SDendaccess(sds_id);
	 return FAIL;
       }

       h5csds_name = get_obj_aboname(cor_sdsname,refstr,NULL,HDF4_SDS);
       if(h5csds_name == NULL) {
	  printf("error in obtaining sds name.\n");
          SDendaccess(sds_id);
	  free(cor_sdsname);
	  return FAIL;
       }
       free(cor_sdsname);
       /* put this name into hashtable. */
       if(set_name(obj_ref,2*num_sds,sds_hashtab,h5csds_name)==FAIL) {
	  printf("error in setting object name.\n");
          SDendaccess(sds_id);
	  free(h5csds_name);
	  return FAIL;
       }

       /* do the convertion from sds into hdf5 dataset.*/
       if(Sds_h4_to_h5(file_id,sds_id,h5root,h5_dimg)== FAIL){
	  printf("error in translating sds into hdf5 dataset.\n");
          SDendaccess(sds_id);
	  free(h5csds_name);
	  return FAIL;
       }
       free(h5csds_name);

     }
     SDendaccess(sds_id);

  }
  return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    h4toh5unvisitedimages
 *
 * Purpose:     convert unvisited images into hdf5 dataset and put it
                under hdf5 root group
                This routine will cover old hdf file.
 *              
 * Return:	FAIL if failed, SUCCEED if successful.
 *
 * In :	        
                file_id:   hdf file id
		h5_root: hdf5 root id
		h5_palg: hdf5 palette group id
               
   Out:
   Modification:         
 *-------------------------------------------------------------------------
 */	

int h4toh5unvisitedimages(int32 file_id,hid_t h5_root,hid_t h5_palg) {

  int     i;
  int32   istat;
  int32   gr_id;
  int32   ri_id;/*raster image identifer.*/
  char    image_name[MAX_GR_NAME];/* image name.*/
  char*   cor_imagename;
  int     check_image;/* flag to check whether this image is visited. 1 for visited  and 0 for non-visited.*/
  int32   obj_ref; /* obj reference number assigned to sds and images.*/
  char    refstr[MAXREF_LENGTH];/*object reference number in character string format.*/
  char   *h5cimage_name;/* absolute path name of hdf5 dataset transferred from old image.*/

  gr_id = GRstart(file_id);
  if(gr_id == FAIL) {
    printf("error in obtaining gr id. \n");
    return FAIL;
  }
   
  /* check all images. */
  for (i=0;i<num_images;i++) {
      
      ri_id = GRselect(gr_id,i);
      if(ri_id ==FAIL) {
	printf("error in selecting gr interface.\n");
	return FAIL;
      }
      
      /* obtain information of GR */
      istat = GRgetiminfo(ri_id, image_name, NULL, NULL, NULL, NULL, NULL);
      
      if(istat == FAIL) {
	 printf("error in getting GR images.\n");
	 GRendaccess(ri_id);
	 return FAIL;
      }
      
      /* obtain object reference number and convert it into string format. */
      obj_ref = GRidtoref(ri_id);
      if(obj_ref  == 0) {
	printf("error in obtaining image reference number");
	printf(" at h4toh5unvisitedimages routine.\n");
	GRendaccess(ri_id);
	return FAIL;
      }

      if(conv_int_str(obj_ref,refstr)== FAIL) {
	printf("error in converting object reference number");
	printf(" into string at h4toh5unvisitedimages routine.\n");
	GRendaccess(ri_id);
	return FAIL;
      }

      /* check whether the current image is visited or not. */
      check_image = lookup(obj_ref,2*num_images,gr_hashtab);

      if(check_image == 0) {

	 /* since different hdf image may hold the same name and it is 
	    also legal that an image may not have a name; but for hdf5 
	    dataset, it must hold a name, so we will use get_obj_aboname 
	    to guarrtte that each new hdf5 dataset converted from
	    image objects will have a disabiguous name. */

	 /* checking whether vgroup name contains ORI_SLASH, 
	    changing into CHA_SLASH.*/

	cor_imagename = correct_name(image_name);
	if(cor_imagename == NULL) {
	  printf("error in generating corrected image name. \n");
	  GRendaccess(ri_id);
	  return FAIL;
	}
	h5cimage_name = get_obj_aboname(cor_imagename,refstr,NULL,
					HDF4_IMAGE);
        if(h5cimage_name == NULL) {
	   printf("error in getting image name.\n");
	   GRendaccess(ri_id);
	   free(cor_imagename);
	   return FAIL;
	}
	free(cor_imagename);

        if(set_name(obj_ref,2*num_images,gr_hashtab,h5cimage_name)==FAIL) {
	   printf("error setting image name.\n");
	   GRendaccess(ri_id);
	   free(h5cimage_name);
	   return FAIL;
	}

	/* do the convertion from the image into hdf5 dataset.*/
	if(Image_h4_to_h5(file_id,ri_id,h5_root,h5_palg)== FAIL) {
	   printf("error in transferring image name into hdf5 dataset.\n");
	   GRendaccess(ri_id);
	   free(h5cimage_name);
	   return FAIL;
	}
	free(h5cimage_name);
      }
      GRendaccess(ri_id);
    }
  return SUCCEED;
}

/*-------------------------------------------------------------------------
 * Function:    free_allhashmemory()
 *
 * Purpose:     free memory allocated for all hashtables
 *              
 * Return:      
 *
 * In :	        
               
               
   Out:
   Modification:         
 *-------------------------------------------------------------------------
 */	

void free_allhashmemory(){

  if(estnum_vg != 0) freetable(estnum_vg,vg_hashtab);
  if(estnum_vd != 0) freetable(estnum_vd,vd_hashtab);

  if(num_sds !=0) {
    freetable(2*num_sds,sds_hashtab);
    freenametable(DIM_HASHSIZE,dim_hashtab);
  }

  if(num_images !=0) {
    freetable(2*num_images,gr_hashtab);
    freetable(PAL_HASHSIZE,pal_hashtab);
  }

  if(num_objects !=0) freenametable(num_objects,name_hashtab);

}



/********The following routines are adapted from h5toh4 converter. *******/
/*****************************************************************************

  Routine: test_file
 
  Description: Test a file for read/write - ability.
 
  Input: filename	- Unix filename
 
  Output: function return,  global variable - errno

*****************************************************************************/

int test_file(char *filename,int oflag,mode_t mode)
{
	int	fid;

	errno = 0;

	fid = open(filename, oflag, mode);
	if (fid < 0) {
		perror(filename);
	}
	close(fid);

	return errno;

}


/*****************************************************************************

  Routine: test_dir
 
  Description: Test pathway to determine if it is a directory
 
  Input: path	- pathname given
 
  Output:  function return TRUE/FALSE

*****************************************************************************/

int test_dir(char *path)
{

	struct stat buf;
	struct stat *buf_ptr;
	int idir;

	buf_ptr = &buf;

	idir = stat(path, buf_ptr);
	if (idir < 0) {
		if (errno == 2) {
			return 0;
		} else {
			perror(path);
		}
	}

	return S_ISDIR(buf_ptr->st_mode);
}

/*****************************************************************************

  Routine: BuildFilename()
 
  Description: Build a filename with new extension
 
  Input: filename	- present filename
		 ext		- extension to root of filename
 
  Output: (filename:r).ext

*****************************************************************************/

char *BuildFilename(char *filename, char *ext)
{
 	/* build outgoing filename */

	char *filename_out;
	char *lastper_ptr, *lastdir_ptr;
	int root_len;

	lastper_ptr = strrchr(filename,'.');
	lastdir_ptr = strrchr(filename,'/');

	if ( lastper_ptr <= lastdir_ptr ) { /* no extension */
		root_len = strlen(filename);
	} else {	/* existing extension */
		root_len = (int)(lastper_ptr - filename); 
	}

	filename_out = (char *)HDmalloc(root_len + strlen(ext) + 2);
	filename_out = strncpy(filename_out, filename, (size_t)root_len);
	filename_out[root_len] = '\0';
	filename_out = strcat(filename_out,".");
	filename_out = strcat(filename_out,ext);

	return filename_out;
}


/*****************************************************************************

  Routine: PrintOptions_h4toh5()
 
  Description: This routine prints the acceptable argument formats out to stderr.
           
  Input: None
 
  Output: output to stderr

*****************************************************************************/

void PrintOptions_h4toh5(void)
{
		fprintf(stderr,"\nUsage: ");
		fprintf(stderr,"\n  h4toh5 -h (gives this print-out)\n");
		fprintf(stderr,"  h4toh5 input.hdf output.h5\n");
		fprintf(stderr,"  h4toh5 input.hdf\n");
}