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 <ctype.h>
00023 #include <string.h>
00024 #include <stdlib.h>
00025 #include "ADF.h"
00026 #ifdef MEM_DEBUG
00027 #include "cg_malloc.h"
00028 #endif
00029
00030 #if defined(_WIN32) && defined(BUILD_DLL)
00031 # define CGNSDLL _declspec(dllexport)
00032 #else
00033 # define CGNSDLL
00034 #endif
00035
00036
00037
00038 #ifndef FALSE
00039 #define FALSE 0
00040 #endif
00041 #ifndef TRUE
00042 #define TRUE 1
00043 #endif
00044 #define StatusOK -1
00045 #define NO_DATA 33
00046
00047
00048
00049 #define TO_UPPER( c ) ((islower(c))?(toupper(c)):(c))
00050 #define EVAL_2_BYTES( C0, C1 ) (((C0)<<8)+((C1)))
00051
00052
00053
00054 #if defined (__cplusplus)
00055 extern "C" {
00056 #endif
00057
00058 static void ErrorExit(char *name, int error_return) ;
00059 static void WalkTheNodes( double InputID, double OutputID ) ;
00060 static void CopyTheNode( double InputID, double OutputID ) ;
00061 static int CalculateDataSize( char *type, int ndims, int *dimlist ) ;
00062
00063
00064
00065 static int ErrStat ;
00066 static int IncludeLink = FALSE ;
00067 static int PrintFlag = FALSE ;
00068 static int NumDims ;
00069 static int PathLength ;
00070 static int DimVals[ADF_MAX_DIMENSIONS*2] ;
00071 static char input[ADF_FILENAME_LENGTH+1] ;
00072 static char label[ADF_LABEL_LENGTH+1] ;
00073 static char format[ADF_FORMAT_LENGTH+1] ;
00074 static char name[ADF_NAME_LENGTH+1] ;
00075 static char DataType[ADF_DATA_TYPE_LENGTH+1] ;
00076 static char LinkFileName[ADF_FILENAME_LENGTH+1] ;
00077 static char LinkPathName[ADF_MAX_LINK_DATA_SIZE+1] ;
00078
00079
00080
00081
00082
00083 CGNSDLL void adf_cond(double InputRootID, double OutputRootID) {
00084
00085 sprintf(format,"NATIVE") ;
00086 IncludeLink = FALSE ;
00087
00088
00089
00090 if ( PrintFlag == TRUE ) {
00091 printf ( "\n**** Begin ADF file conditioner ****" ) ;
00092 printf ( "\n---Output file type is %s.", format ) ;
00093 if ( IncludeLink == TRUE )
00094 printf ( "\n---Links will be included in output file." ) ;
00095 else
00096 printf ( "\n---Links will not be included in output file." ) ;
00097 }
00098
00099 WalkTheNodes( InputRootID, OutputRootID ) ;
00100
00101 ADF_Database_Close ( OutputRootID, &ErrStat ) ;
00102
00103 if ( PrintFlag == TRUE )
00104 printf ( "\n**** ADF file conditioner completed sucessfully ****\n" ) ;
00105
00106 }
00107
00108
00109
00110
00111 static void WalkTheNodes( double InputID, double OutputID )
00112 {
00113 int ic, cnt ;
00114 int nchildren ;
00115 double InputChildID ;
00116 double OutputChildID ;
00117
00118
00119
00120 CopyTheNode ( InputID, OutputID ) ;
00121
00122
00123
00124 ADF_Number_of_Children(InputID,&nchildren,&ErrStat) ;
00125 if ( ErrStat != StatusOK )
00126 ErrorExit( "WalkTheNodes:ADF_Number_of_Children", ErrStat ) ;
00127 if (nchildren == 0)
00128
00129 return ;
00130 else
00131 {
00132 for (ic=1; ic<=nchildren; ic++)
00133 {
00134
00135
00136 #ifdef NULL_NODEID_POINTER
00137 ADF_Children_IDs( InputID, ic, 1, &cnt, &InputChildID, &ErrStat );
00138 if ( ErrStat != StatusOK )
00139 ErrorExit( "WalkTheNodes:ADF_Children_IDs", ErrStat );
00140 ADF_Get_Name (InputChildID, name, &ErrStat );
00141 if ( ErrStat != StatusOK )
00142 ErrorExit( "WalkTheNodes:ADF_Get_Name", ErrStat ) ;
00143 #else
00144 ADF_Children_Names( InputID, ic, 1, ADF_NAME_LENGTH+1, &cnt, name,
00145 &ErrStat );
00146 if ( ErrStat != StatusOK )
00147 ErrorExit( "WalkTheNodes:ADF_Children_Names", ErrStat );
00148 ADF_Get_Node_ID( InputID, name, &InputChildID, &ErrStat ) ;
00149 if ( ErrStat != StatusOK )
00150 ErrorExit( "WalkTheNodes:ADF_Node_ID", ErrStat ) ;
00151 #endif
00152
00153
00154
00155 if ( PrintFlag == TRUE )
00156 printf ( "\nWorking on ADF node %s", name ) ;
00157
00158
00159
00160 ADF_Is_Link ( InputChildID, &PathLength, &ErrStat ) ;
00161 if ( ErrStat != StatusOK )
00162 ErrorExit( "WalkTheNodes:ADF_Is_Link", ErrStat ) ;
00163 if ( PathLength > 0 ) {
00164 ADF_Get_Link_Path( InputChildID, LinkFileName, LinkPathName,
00165 &ErrStat ) ;
00166 if ( ErrStat != StatusOK )
00167 ErrorExit( "WalkTheNode:ADF_Link_Path", ErrStat ) ;
00168 }
00169
00170
00171
00172 if ( PathLength > 0 &&
00173 ( LinkFileName[0] == '\0' || IncludeLink == FALSE ) ) {
00174
00175 ADF_Link( OutputID, name, LinkFileName, LinkPathName,
00176 &OutputChildID, &ErrStat ) ;
00177 if ( ErrStat != StatusOK )
00178 ErrorExit( "WalkTheNodes:ADF_Link", ErrStat ) ;
00179 continue ;
00180 }
00181 else {
00182
00183 ADF_Create ( OutputID, name, &OutputChildID, &ErrStat ) ;
00184 if ( ErrStat != StatusOK )
00185 ErrorExit( "WalkTheNodes:ADF_Create", ErrStat ) ;
00186 WalkTheNodes ( InputChildID, OutputChildID ) ;
00187 }
00188 }
00189 }
00190 }
00191
00192
00193
00194 static void CopyTheNode( double InputID, double OutputID )
00195 {
00196 int DataSize ;
00197 char *DataBuffer ;
00198
00199
00200
00201 ADF_Get_Label( InputID, label, &ErrStat ) ;
00202 if ( ErrStat != StatusOK )
00203 ErrorExit( "CopyTheNode:ADF_Get_Label", ErrStat ) ;
00204 ADF_Set_Label( OutputID, label, &ErrStat ) ;
00205 if ( ErrStat != StatusOK )
00206 ErrorExit( "CopyTheNode:ADF_Set_Label", ErrStat ) ;
00207
00208 ADF_Get_Data_Type( InputID, DataType, &ErrStat ) ;
00209 if ( ErrStat != StatusOK )
00210 ErrorExit( "CopyTheNode:ADF_Get_Data_Type", ErrStat ) ;
00211 ADF_Get_Number_of_Dimensions( InputID, &NumDims, &ErrStat ) ;
00212 if ( ErrStat != StatusOK )
00213 ErrorExit( "CopyTheNode:ADF_Get_Number_of_Dimensions", ErrStat ) ;
00214 if ( NumDims == 0 ) return ;
00215 ADF_Get_Dimension_Values( InputID, DimVals, &ErrStat ) ;
00216 if ( ErrStat != StatusOK )
00217 ErrorExit( "CopyTheNode:ADF_Get_Dimension_Values", ErrStat ) ;
00218 ADF_Put_Dimension_Information( OutputID, DataType, NumDims, DimVals,
00219 &ErrStat ) ;
00220 if ( ErrStat != StatusOK )
00221 ErrorExit( "CopyTheNode:ADF_Put_Dimension_Information", ErrStat ) ;
00222
00223
00224
00225 DataSize = CalculateDataSize ( DataType, NumDims, DimVals ) ;
00226 if ( DataSize == 0 ) return ;
00227 DataBuffer = (char *) malloc ( DataSize*sizeof(char) ) ;
00228 if ( DataBuffer == NULL )
00229 ErrorExit( "CopyTheNode:Failed Memory Allocation", -2 ) ;
00230 ADF_Read_All_Data( InputID, DataBuffer, &ErrStat ) ;
00231 if ( ErrStat == NO_DATA ) {
00232 free ( DataBuffer ) ;
00233 return ;
00234 }
00235 else if ( ErrStat != StatusOK )
00236 ErrorExit( "CopyTheNode:ADF_Read_All_Data", ErrStat ) ;
00237 ADF_Write_All_Data( OutputID, DataBuffer, &ErrStat ) ;
00238 if ( ErrStat != StatusOK )
00239 ErrorExit( "CopyTheNode:ADF_Write_All_Data", ErrStat ) ;
00240 free ( DataBuffer ) ;
00241 }
00242
00243
00244
00245
00246
00247 static int CalculateDataSize( char *data_type_string, int ndims, int *dimlist )
00248 {
00249 int i ;
00250 int str_position ;
00251 int str_len ;
00252 int size_machine ;
00253 int machine_bytes ;
00254
00255 if ( ndims == 0 ) return 0 ;
00256
00258 str_len = strlen( data_type_string ) ;
00259 if ( str_len == 0 ) return 0 ;
00260 for( i=0; i<str_len; i++ )
00261 data_type_string[i] = (char)TO_UPPER( data_type_string[i] ) ;
00262
00263
00264
00265 str_position = 0 ;
00266 machine_bytes = 0 ;
00267 while( data_type_string[ str_position ] != '\0' ) {
00268
00269 size_machine = 0 ;
00270
00272 switch( EVAL_2_BYTES( data_type_string[str_position],
00273 data_type_string[str_position+1])) {
00274 case EVAL_2_BYTES( 'M', 'T' ) :
00275 if( (str_position == 0) && (data_type_string[ 2 ] == '\0') )
00276 return 0 ;
00277 else {
00278 ErrorExit( "CalculateDataSize:Invalid Data Type.", -2 );
00279 }
00280 break ;
00281
00282 case EVAL_2_BYTES( 'I', '4' ) :
00283 size_machine = sizeof( int ) ;
00284 break ;
00285
00286 case EVAL_2_BYTES( 'I', '8' ) :
00287 size_machine = sizeof( long ) ;
00288 break ;
00289
00290 case EVAL_2_BYTES( 'U', '4' ) :
00291 size_machine = sizeof( int ) ;
00292 break ;
00293
00294 case EVAL_2_BYTES( 'U', '8' ) :
00295 size_machine = sizeof( long ) ;
00296 break ;
00297
00298 case EVAL_2_BYTES( 'R', '4' ) :
00299 size_machine = sizeof( float ) ;
00300 break ;
00301
00302 case EVAL_2_BYTES( 'R', '8' ) :
00303 size_machine = sizeof( double ) ;
00304 break ;
00305
00306 case EVAL_2_BYTES( 'X', '4' ) :
00307 size_machine = 2 * sizeof( float ) ;
00308 break ;
00309
00310 case EVAL_2_BYTES( 'X', '8' ) :
00311 size_machine = 2 * sizeof( double ) ;
00312 break ;
00313
00314 case EVAL_2_BYTES( 'B', '1' ) :
00315 size_machine = 1 ;
00316 break ;
00317
00318 case EVAL_2_BYTES( 'C', '1' ) :
00319 size_machine = sizeof( char ) ;
00320 break ;
00321
00322 default :
00323 ErrorExit( "CalculateDataSize:Invalid Data Type.", -2 );
00324 break ;
00325 }
00326
00327 str_position += 2 ;
00328
00331 switch( data_type_string[ str_position ] ) {
00332 case '\0' :
00333 machine_bytes += size_machine ;
00334 break ;
00335
00336 case '[' :
00337 {
00338 int array_size = 0 ;
00339 str_position += 1 ;
00340 while( (data_type_string[ str_position ] >= '0') &&
00341 (data_type_string[ str_position ] <= '9') ) {
00342 array_size = array_size * 10 +
00343 (data_type_string[ str_position ] - '0') ;
00344 str_position += 1 ;
00345 }
00346 if( data_type_string[ str_position ] != ']' ) {
00347 ErrorExit( "CalculateDataSize:Invalid Data Type.", -2 );
00348 }
00349 str_position += 1 ;
00351 if( data_type_string[ str_position ] == ',' ) {
00352 str_position += 1 ;
00353 }
00354 machine_bytes = machine_bytes + size_machine * array_size ;
00355 }
00356 break ;
00357
00358 case ',' :
00359 str_position += 1 ;
00360 machine_bytes += size_machine ;
00361 break ;
00362
00363 default :
00364 ErrorExit( "CalculateDataSize:Invalid Data Type.", -2 );
00365 break ;
00366 }
00367 }
00368
00369
00370
00371 for ( i=0; i<ndims; i++ ) machine_bytes *= dimlist[i] ;
00372
00373 return machine_bytes ;
00374 }
00375
00376
00377
00378 static void ErrorExit( char *ermsg, int ErrStat )
00379 {
00380 sprintf(input,"\nError return from %s; errno = %d",ermsg,ErrStat) ;
00381 fprintf(stderr,"%s\n",input) ;
00382 if ( ErrStat > 0 ) {
00383 ADF_Error_Message(ErrStat,input) ;
00384 fprintf(stderr,"%s\n",input) ;
00385 }
00386 exit(1) ;
00387 }
00388
00389 #if defined (__cplusplus)
00390 }
00391 #endif
00392