long CreatePoissonDistributionTable(double **x, double **pos_CDF, double mean) { long i, npoints=20, count=0; double *pos=NULL; SDDS_DATASET pos_out; *x = *pos_CDF = NULL; if (!(*x=malloc(sizeof(**x)*npoints)) || !(pos=malloc(sizeof(*pos)*npoints)) || !(*pos_CDF=malloc(sizeof(**pos_CDF)*npoints))) SDDS_Bomb("memeroy allocation failure."); i = count = 0; while (1) { if (count+2>=npoints) { npoints += 20; *x = SDDS_Realloc(*x, sizeof(**x)*npoints); *pos_CDF = SDDS_Realloc(*pos_CDF, sizeof(**pos_CDF)*npoints); pos = SDDS_Realloc(pos, sizeof(*pos)*npoints); } (*x)[count] = i; if (!i) { pos[i] = exp(-mean); (*pos_CDF)[count] = pos[i]; count ++; } else { pos[i] = pos[i-1]*mean/i; (*pos_CDF)[count] = (*pos_CDF)[count-1]; (*pos_CDF)[count+1] = (*pos_CDF)[count-1] + pos[i]; (*x)[count+1] = i; if (1.0-(*pos_CDF)[count+1]<=1.0e-15) break; count +=2; } i++; } /* fprintf(stderr,"lamda=%f\n", mean); if (!SDDS_InitializeOutput(&pos_out, SDDS_BINARY, 0, NULL, NULL, "pos_dist.sdds")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_DefineSimpleColumn(&pos_out, "Count", NULL, SDDS_DOUBLE) || !SDDS_DefineSimpleColumn(&pos_out, "P", NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_SaveLayout(&pos_out) || !SDDS_WriteLayout(&pos_out)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_StartPage(&pos_out, count) || !SDDS_SetColumnFromDoubles(&pos_out, SDDS_SET_BY_NAME, *x, count, "Count") || !SDDS_SetColumnFromDoubles(&pos_out, SDDS_SET_BY_NAME, *pos_CDF, count, "P")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_WritePage(&pos_out) || !SDDS_Terminate(&pos_out)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); */ free(pos); return count; }
void *AllocateColumnData(long type, void *values, int32_t rows) { switch (type) { case SDDS_SHORT: return SDDS_Realloc(values,rows*(sizeof(short))); case SDDS_LONG: return SDDS_Realloc(values,rows*(sizeof(int32_t))); case SDDS_FLOAT: return SDDS_Realloc(values,rows*(sizeof(float))); case SDDS_DOUBLE: return SDDS_Realloc(values,rows*(sizeof(double))); case SDDS_CHARACTER: return SDDS_Realloc(values,rows*2*(sizeof(char))); } return values; }
int32_t SDDS_CopyArrays(SDDS_DATASET *SDDS_target, SDDS_DATASET *SDDS_source) { int32_t i, j, target_index; char messageBuffer[1024]; for (i=0; i<SDDS_source->layout.n_arrays; i++) { if ((target_index=SDDS_GetArrayIndex(SDDS_target, SDDS_source->layout.array_definition[i].name))<0) continue; SDDS_target->array[target_index].definition = SDDS_target->layout.array_definition+target_index; SDDS_target->array[target_index].elements = SDDS_source->array[i].elements; if (!(SDDS_target->array[target_index].dimension = (int32_t*)SDDS_Malloc(sizeof(*SDDS_target->array[i].dimension)*SDDS_target->array[target_index].definition->dimensions)) || !(SDDS_target->array[target_index].data = SDDS_Realloc(SDDS_target->array[target_index].data, SDDS_type_size[SDDS_target->array[target_index].definition->type-1]* SDDS_target->array[target_index].elements))) { SDDS_SetError("Unable to copy arrays--allocation failure (SDDS_CopyArrays)"); return(0); } for (j=0; j<SDDS_target->array[target_index].definition->dimensions; j++) SDDS_target->array[target_index].dimension[j] = SDDS_source->array[i].dimension[j]; if (!SDDS_source->array[i].data) { SDDS_target->array[target_index].data=NULL; continue; } if (SDDS_source->layout.array_definition[i].type!=SDDS_target->layout.array_definition[target_index].type) { if (!SDDS_NUMERIC_TYPE(SDDS_source->layout.array_definition[i].type) || !SDDS_NUMERIC_TYPE(SDDS_target->layout.array_definition[target_index].type)) { sprintf(messageBuffer, "Can't cast between nonnumeric types for parameters %s and %s (SDDS_CopyArrays)", SDDS_source->layout.array_definition[i].name, SDDS_target->layout.array_definition[target_index].name); SDDS_SetError(messageBuffer); return 0; } for (j=0;j<SDDS_source->array[i].elements;j++) { if (!SDDS_CastValue(SDDS_source->array[i].data, j, SDDS_source->layout.array_definition[i].type, SDDS_target->layout.array_definition[target_index].type, (char*)(SDDS_target->array[target_index].data) +j*SDDS_type_size[SDDS_target->layout.array_definition[target_index].type-1])) { SDDS_SetError("Problem with cast (SDDS_CopyArrays)"); return 0; } } } else { if (SDDS_target->array[target_index].definition->type!=SDDS_STRING) memcpy(SDDS_target->array[target_index].data, SDDS_source->array[i].data, SDDS_type_size[SDDS_target->array[target_index].definition->type-1]*SDDS_target->array[target_index].elements); else if (!SDDS_CopyStringArray(SDDS_target->array[target_index].data, SDDS_source->array[i].data, SDDS_target->array[target_index].elements)) { SDDS_SetError("Unable to copy arrays (SDDS_CopyArrays)"); return(0); } } } return(1); }
long appendToStringArray(char ***item, long items, char *newItem) { if (!(*item = SDDS_Realloc(*item, sizeof(**item)*(items+1)))) SDDS_Bomb("allocation failure in appendToStringArray"); if (!SDDS_CopyString((*item)+items, newItem)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return items+1; }
void moveToStringArray(char ***target, long *targets, char **source, long sources) { long i, j; if (!sources) return; *target = SDDS_Realloc(*target, sizeof(**target)*(*targets+sources)); for (i=0; i<sources; i++) { for (j=0; j<*targets; j++) if (strcmp(source[i], (*target)[j])==0) break; if (j==*targets) { (*target)[j] = source[i]; *targets += 1; } } }
long addStatRequests(STAT_REQUEST **statRequest, long requests, char **item, long items, long code, unsigned long flags) { long i; if (!(*statRequest = SDDS_Realloc(*statRequest, sizeof(**statRequest)*(requests+1))) || !((*statRequest)[requests].sourceColumn = (char**)malloc(sizeof(*(*statRequest)[requests].sourceColumn)* (items-1)))) SDDS_Bomb("memory allocation failure"); for (i=0; i<items-1; i++) SDDS_CopyString(&((*statRequest)[requests].sourceColumn[i]), item[i+1]); (*statRequest)[requests].resultColumn = item[0]; (*statRequest)[requests].sourceColumns = items-1; (*statRequest)[requests].optionCode = code; (*statRequest)[requests].sumPower = 1; (*statRequest)[requests].flags = flags; (*statRequest)[requests].positionColumn = NULL; return requests+1; }
void addTransmutationSpec(char *name, char *type, char *exclude, long newType) { if (!(transmutationSpec = SDDS_Realloc(transmutationSpec, sizeof(*transmutationSpec)*(transmutationSpecs+1)))) bombElegant("memory allocation failure", NULL); transmutationSpec[transmutationSpecs].name = NULL; transmutationSpec[transmutationSpecs].type = NULL; transmutationSpec[transmutationSpecs].exclude = NULL; transmutationSpec[transmutationSpecs].newType = newType; if ((name && !SDDS_CopyString(&transmutationSpec[transmutationSpecs].name, name)) || (type && !SDDS_CopyString(&transmutationSpec[transmutationSpecs].type, type)) || (exclude && !SDDS_CopyString(&transmutationSpec[transmutationSpecs].exclude, exclude))) bombElegant("memory allocation failure", NULL); transmutationSpecs++; }
void replaceWithRank(double *data, long n) { static DATAnINDEX *indexedData = NULL; long i, j, iStart, iEnd; indexedData = SDDS_Realloc(indexedData, sizeof(*indexedData)*n); for (i=0; i<n; i++) { indexedData[i].data = data[i]; indexedData[i].originalIndex = i; } qsort((void*)indexedData, n, sizeof(*indexedData), compareData); for (i=0; i<n; i++) data[indexedData[i].originalIndex] = i; for (i=0; i<n-1; i++) { if (data[i]==data[i+1]) { iStart = i; for (j=i+2; j<n-1; j++) if (data[i]!=data[j]) break; iEnd = j-1; for (j=iStart; j<=iEnd; j++) data[i] = (iStart+iEnd)/2.0; } } }
long perform_sddsplot_time_filtering(SDDS_TABLE *table, TIME_FILTER_DEFINITION **time_filter, long time_filters) { long i, j, accept, skip_table; long n_left, row_deletion, n_rows; TIME_FILTER_DEFINITION *time_filter_ptr; PARAMETER_DEFINITION *pardefptr; double result; static int32_t *rowFlag1 = NULL, *rowFlag2 = NULL; static long rowFlags = 0; skip_table = 0; #if DEBUG fprintf(stderr, "%ld time filters being applied\n", time_filters); #endif if (time_filters==0) return !skip_table; n_rows = SDDS_RowCount(table); row_deletion = 1; for (i=0;i<time_filters;i++) { time_filter_ptr=time_filter[i]; if (time_filter_ptr->is_parameter) { if (!(pardefptr=SDDS_GetParameterDefinition(table, time_filter_ptr->name)) || (pardefptr->type==SDDS_STRING || pardefptr->type==SDDS_CHARACTER)) { fprintf(stderr, "error: unknown or non-numeric parameter %s given for time filter\n", time_filter_ptr->name); exit(1); } accept=SDDS_ItemInsideWindow(SDDS_GetParameter(table, time_filter_ptr->name, &result), 0, pardefptr->type, time_filter_ptr->after,time_filter_ptr->before); if (!accept) skip_table=1; if (time_filter_ptr->flags&TIMEFILTER_INVERT_GIVEN) skip_table=!skip_table; if (skip_table) break; } else if (n_rows) { if (!rowFlag1 || !rowFlag2 || n_rows>rowFlags) { if (!(rowFlag1 = SDDS_Realloc(rowFlag1, sizeof(*rowFlag1)*n_rows)) || !(rowFlag2 = SDDS_Realloc(rowFlag2, sizeof(*rowFlag2)*n_rows))) { fprintf(stderr, "Problem reallocating row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_GetRowFlags(table, rowFlag1, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #if DEBUG fprintf(stderr, " * applying time filter (column %s)", time_filter_ptr->name); #endif if ((n_left=SDDS_FilterRowsOfInterest(table, time_filter_ptr->name, time_filter_ptr->after, time_filter_ptr->before, time_filter_ptr->flags&TIMEFILTER_INVERT_GIVEN? SDDS_NEGATE_EXPRESSION:SDDS_AND ))<0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_GetRowFlags(table, rowFlag2, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } n_left=0; for (j=0; j<n_rows; j++) { rowFlag1[j] = rowFlag1[j]&rowFlag2[j]; if (rowFlag1[j]) n_left++; } if (!SDDS_AssertRowFlags(table, SDDS_FLAG_ARRAY, rowFlag1, n_rows)) { fprintf(stderr, "Unable to assert row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /* fprintf(stderr,"total_rows=%d,left rows=%d\n",n_rows,n_left); */ /* fprintf(stderr,"total_rows=%d, time filtered left=%d\n",n_rows,n_left); */ if (!n_left) skip_table=1; if (skip_table) break; row_deletion = 1; } } return !skip_table; }
int main(int argc, char **argv) { int iArg; char *input, *output,*meanPar, *sigmaPar, *maxPar, *minPar; long i, j, mainInputOpened, haltonID=0, requireInput=0; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout, *SDDSptr; long randomNumberSeed = 0; SEQ_REQUEST *seqRequest; long samples, values, seqRequests, randomizationGroups=0; double *sample, *IVValue, *CDFValue; char msgBuffer[1000]; RANDOMIZED_ORDER *randomizationData = NULL; long verbose, optimalHalton=0; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<2) { fprintf(stderr, "%s%s%s\n", USAGE1, USAGE2, USAGE3); return(1); } seqRequest = NULL; seqRequests = 0; output = input = NULL; pipeFlags = 0; samples = values = 0; sample = IVValue = CDFValue = NULL; verbose = 0; maxPar = minPar = meanPar = sigmaPar = NULL; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, CLO_OPTIONS, 0)) { case CLO_COLUMNS: if (scanned[iArg].n_items<3) SDDS_Bomb("invalid -columns syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); /*remove following pointer initialization because memset already initialize them */ seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].factor = 1; seqRequest[seqRequests].offset = 0; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "datafile", SDDS_STRING, &seqRequest[seqRequests].dataFileName, 1, SEQ_DATAFILE, "independentvariable", SDDS_STRING, &seqRequest[seqRequests].indepName, 1, SEQ_INDEPNAME, "cdf", SDDS_STRING, &seqRequest[seqRequests].CDFName, 1, SEQ_CDFNAME, "df", SDDS_STRING, &seqRequest[seqRequests].DFName, 1, SEQ_DFNAME, "output", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, "haltonradix", SDDS_LONG, &seqRequest[seqRequests].haltonRadix, 1, SEQ_HALTONRADIX, "haltonoffset", SDDS_LONG, &seqRequest[seqRequests].haltonOffset, 1, SEQ_HALTONOFFSET, "randomize", -1, NULL, 0, SEQ_RANDOMIZE, "group", SDDS_LONG, &seqRequest[seqRequests].randomizationGroup, 1, SEQ_RANDOMGROUP, "factor", SDDS_DOUBLE, &seqRequest[seqRequests].factor, 1, 0, "offset", SDDS_DOUBLE, &seqRequest[seqRequests].offset, 1, 0, NULL) || bitsSet(seqRequest[seqRequests].flags&(SEQ_INDEPNAME+SEQ_CDFNAME+SEQ_DFNAME))!=2) SDDS_Bomb("invalid -columns syntax"); if (seqRequest[seqRequests].flags&SEQ_RANDOMGROUP && seqRequest[seqRequests].randomizationGroup<=0) SDDS_Bomb("use a positive integer for the randomization group ID"); if (seqRequest[seqRequests].flags&SEQ_CDFNAME && seqRequest[seqRequests].flags&SEQ_DFNAME) SDDS_Bomb("give df or cdf for -columns, not both"); if (seqRequest[seqRequests].flags&SEQ_HALTONRADIX && !is_prime(seqRequest[seqRequests].haltonRadix)) SDDS_Bomb("halton radix must be a prime number"); seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_GAUSSIAN: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -gaussian syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 0; seqRequest[seqRequests].sigma = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "sigmaValue", SDDS_STRING, &sigmaPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -gaussian syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_GAUSSIAN; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for gaussian distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -gaussian distribution."); free(meanPar); meanPar = NULL; } if (sigmaPar) { if (wild_match(sigmaPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].sigmaPar, sigmaPar+1); else if (!get_double(&seqRequest[seqRequests].sigma, sigmaPar)) SDDS_Bomb("Invalid value given for sigma value of -gaussian distribution."); free(sigmaPar); sigmaPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_UNIFORM: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -uniform syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].min = 0; seqRequest[seqRequests].max = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "minimumValue", SDDS_STRING, &minPar, 1, 0, "maximumValue", SDDS_STRING, &maxPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -uniform syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_UNIFORM; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (minPar) { if (wild_match(minPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].minPar, minPar+1); else if (!get_double(&seqRequest[seqRequests].min, minPar)) SDDS_Bomb("Invalid value given for minimum value of -uniform distribution."); free(minPar); minPar = NULL; } if (maxPar) { if (wild_match(maxPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].maxPar, maxPar+1); else if (!get_double(&seqRequest[seqRequests].max, maxPar)) SDDS_Bomb("Invalid value given for maximum value of -uniform distribution."); free(maxPar); maxPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_POISSON: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -poisson syntax"); if (!(seqRequest = SDDS_Realloc(seqRequest, sizeof(*seqRequest)*(seqRequests+1)))) SDDS_Bomb("memory allocation failure"); memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); scanned[iArg].n_items -= 1; memset(seqRequest+seqRequests, 0, sizeof(*seqRequest)); seqRequest[seqRequests].randomizationGroup = -1; seqRequest[seqRequests].mean = 1; if (!scanItemList(&seqRequest[seqRequests].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "columnName", SDDS_STRING, &seqRequest[seqRequests].outputName, 1, SEQ_OUTPUTNAME, "meanValue", SDDS_STRING, &meanPar, 1, 0, "units", SDDS_STRING, &seqRequest[seqRequests].units, 1, SEQ_UNITSGIVEN, NULL)) SDDS_Bomb("invalid -poisson syntax"); seqRequest[seqRequests].flags |= SEQ_DIRECT_POISSON; if (!(seqRequest[seqRequests].flags&SEQ_OUTPUTNAME) || !(seqRequest[seqRequests].outputName)) SDDS_Bomb("columnName is not provided for uniform distribution/"); if (meanPar) { if (wild_match(meanPar, "@*")) SDDS_CopyString(&seqRequest[seqRequests].meanPar, meanPar+1); else if (!get_double(&seqRequest[seqRequests].mean, meanPar)) SDDS_Bomb("Invalid value given for mean value of -poisson distribution."); free(meanPar); meanPar = NULL; } seqRequests ++; scanned[iArg].n_items += 1; break; case CLO_SAMPLES: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &samples)!=1 || samples<=0) SDDS_Bomb("invalid -samples syntax"); break; case CLO_SEED: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%ld", &randomNumberSeed)!=1 || randomNumberSeed<=0) SDDS_Bomb("invalid -seed syntax"); break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_VERBOSE: verbose = 1; break; case CLO_OPTIMAL_HALTON: optimalHalton = 1; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } if (!seqRequests) SDDS_Bomb("give one or more -columns options"); if (samples<1) SDDS_Bomb("-samples option not given"); for (i=0; i<seqRequests; i++) { if (!(seqRequest[i].flags& (SEQ_DATAFILE|SEQ_DIRECT_GAUSSIAN|SEQ_DIRECT_UNIFORM|SEQ_DIRECT_POISSON))) break; } if (i==seqRequests) { /* all columns options have either their own input files or else use * one of the "direct" distributions. Hence, we don't expect an input * file. */ if (!input) pipeFlags |= USE_STDIN; /* not really, but fakes out processFilenames */ if (input && !output) { output = input; input = NULL; pipeFlags |= USE_STDIN; if (fexists(output)) { sprintf(msgBuffer, "%s exists already (sddssampledist)", output); SDDS_Bomb(msgBuffer); } } } processFilenames("sddssampledist", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Initialized output file %s\n", output); /* open and check input files */ for (i=mainInputOpened=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if (seqRequest[i].meanPar || seqRequest[i].sigmaPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].meanPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].sigmaPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].sigmaPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if (seqRequest[i].minPar || seqRequest[i].maxPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ((seqRequest[i].minPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].minPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || (seqRequest[i].maxPar && SDDS_CheckParameter(SDDSptr, seqRequest[i].maxPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_DOUBLE)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { if (seqRequest[i].meanPar) { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; if ( SDDS_CheckParameter(SDDSptr, seqRequest[i].meanPar, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_DefineSimpleColumn(&SDDSout, seqRequest[i].outputName, NULL, SDDS_LONG)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { if (seqRequest[i].flags&SEQ_RANDOMIZE) { long newGroupID=0; /* define randomization groups */ if (seqRequest[i].flags&SEQ_RANDOMGROUP) { newGroupID = seqRequest[i].randomizationGroup; for (j=0; j<randomizationGroups; j++) if (randomizationData[j].group==newGroupID) { newGroupID = 0; break; } } else { seqRequest[i].randomizationGroup = newGroupID = -(i+1); } if (newGroupID!=0) { if (!(randomizationData = SDDS_Realloc(randomizationData, sizeof(*randomizationData)*(randomizationGroups+1)))) SDDS_Bomb("memory allocation failure"); randomizationData[randomizationGroups].group = newGroupID; randomizationData[randomizationGroups].order = NULL; randomizationGroups ++; } } if (seqRequest[i].flags&SEQ_DATAFILE) { if (!SDDS_InitializeInput(&seqRequest[i].SDDSin, seqRequest[i].dataFileName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDSptr = &seqRequest[i].SDDSin; } else { if (!mainInputOpened) { if (!SDDS_InitializeInput(&SDDSin, input) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); mainInputOpened = 1; } requireInput = 1; SDDSptr = &SDDSin; } if (SDDS_CheckColumn(SDDSptr, seqRequest[i].indepName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK || ((seqRequest[i].flags&SEQ_CDFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].CDFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || ((seqRequest[i].flags&SEQ_DFNAME) && SDDS_CheckColumn(SDDSptr, seqRequest[i].DFName, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) || !SDDS_TransferColumnDefinition(&SDDSout, SDDSptr, seqRequest[i].indepName, seqRequest[i].outputName)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (seqRequest[i].flags&SEQ_UNITSGIVEN && !SDDS_ChangeColumnInformation(&SDDSout, "units", seqRequest[i].units, SDDS_SET_BY_NAME, seqRequest[i].outputName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Initialized input files\n"); if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (randomNumberSeed==0) { randomNumberSeed = (long)time((time_t *) NULL); randomNumberSeed = 2*(randomNumberSeed/2) + 1; #if defined(_WIN32) random_1(-labs(randomNumberSeed)); #else random_1(-FABS(randomNumberSeed)); #endif } else random_1(-randomNumberSeed); if (!((sample = calloc(sizeof(*sample), samples)))) SDDS_Bomb("memory allocation failure"); while (1) { if (verbose) fprintf(stderr, "Beginning page loop\n"); if (input && SDDS_ReadPage(&SDDSin)<=0) break; for (i=0; i<seqRequests; i++) { if (seqRequest[i].flags&SEQ_DATAFILE && SDDS_ReadPage(&seqRequest[i].SDDSin)<=0) break; } if (i!=seqRequests) break; if (!SDDS_StartPage(&SDDSout, samples) || (input && !SDDS_CopyParameters(&SDDSout, &SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbose) fprintf(stderr, "Defining randomization tables\n"); /* define randomization tables */ for (i=0; i<randomizationGroups; i++) { if (!(randomizationData[i].order= SDDS_Malloc(sizeof(*randomizationData[i].order)*samples))) SDDS_Bomb("memory allocation failure"); for (j=0; j<samples; j++) randomizationData[i].order[j] = j; randomizeOrder((char*)randomizationData[i].order, sizeof(*randomizationData[i].order), samples, 0, random_1); } if (verbose) fprintf(stderr, "Beginning loop over sequence requests\n"); for (i=0; i<seqRequests; i++) { if (verbose) fprintf(stderr, "Processing sequence request %ld\n", i); if (seqRequest[i].flags&SEQ_DIRECT_GAUSSIAN) { if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean)) || (seqRequest[i].sigmaPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].sigmaPar, &seqRequest[i].sigma))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = gauss_rn_lim(seqRequest[i].mean, seqRequest[i].sigma, -1, random_1); } else if (seqRequest[i].flags&SEQ_DIRECT_UNIFORM) { if ((seqRequest[i].minPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].minPar, &seqRequest[i].min)) || (seqRequest[i].maxPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].maxPar, &seqRequest[i].max))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<samples; j++) sample[j] = seqRequest[i].min + (seqRequest[i].max - seqRequest[i].min) * random_1(1); } else if (seqRequest[i].flags&SEQ_DIRECT_POISSON) { double *pos_x, *pos_cdf, CDF; long pos_points, code; pos_x = pos_cdf = NULL; if ((seqRequest[i].meanPar && !SDDS_GetParameterAsDouble(&SDDSin, seqRequest[i].meanPar, &seqRequest[i].mean))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); pos_points = CreatePoissonDistributionTable(&pos_x, &pos_cdf, seqRequest[i].mean); for (j=0; j<samples; j++) { CDF = random_1(1); sample[j]= (int)(interp(pos_x, pos_cdf, pos_points, CDF, 0, 1, &code)); /* fprintf(stderr, "%d, cdf=%f, sample=%f\n", j, CDF, sample[j]);*/ } free(pos_x); free(pos_cdf); } else { if (input && !(seqRequest[i].flags&SEQ_DATAFILE)) SDDSptr = &SDDSin; else SDDSptr = &seqRequest[i].SDDSin; if ((values = SDDS_CountRowsOfInterest(SDDSptr))) { if (!(IVValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].indepName)) || (seqRequest[i].flags&SEQ_CDFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].CDFName))) || (seqRequest[i].flags&SEQ_DFNAME && !(CDFValue = SDDS_GetColumnInDoubles(SDDSptr, seqRequest[i].DFName)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } else { sprintf(msgBuffer, "empty page for file %s\n", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (verbose) fprintf(stderr, "Checking and converting CDF/DF values\n"); /* check/convert CDF/DF values */ for (j=1; j<values; j++) { if (IVValue[j-1]>IVValue[j]) { sprintf(msgBuffer, "random variate values not monotonically increasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } if (seqRequest[i].flags&SEQ_DFNAME) /* convert DF to CDF */ CDFValue[j] += CDFValue[j-1]; if (CDFValue[j] < CDFValue[j-1]) { sprintf(msgBuffer, "CDF values decreasing for %s", seqRequest[i].flags&SEQ_DATAFILE? seqRequest[i].dataFileName:input); SDDS_Bomb(msgBuffer); } } if (verbose) fprintf(stderr, "Normalizing CDF\n"); /* normalize the CDF */ if (CDFValue[values-1]<=0) { sprintf(msgBuffer, "CDF not valid for %s\n", seqRequest[i].dataFileName); SDDS_Bomb(msgBuffer); } for (j=0; j<values; j++) CDFValue[j] /= CDFValue[values-1]; if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (verbose) fprintf(stderr, "Starting halton sequence, offset=%" PRId32 "\n", seqRequest[i].haltonOffset); if (!optimalHalton) haltonID = startHaltonSequence(&seqRequest[i].haltonRadix, 0.5); else haltonID = startModHaltonSequence(&seqRequest[i].haltonRadix, 0); while (seqRequest[i].haltonOffset-- >0) { if (!optimalHalton) nextHaltonSequencePoint(haltonID); else nextModHaltonSequencePoint(haltonID); } } if (verbose) fprintf(stderr, "Generating samples\n"); for (j=0; j<samples; j++) { double CDF; long code; while (1) { if (seqRequest[i].flags&SEQ_HALTONRADIX) { if (!optimalHalton) CDF = nextHaltonSequencePoint(haltonID); else CDF = nextModHaltonSequencePoint(haltonID); } else CDF = random_1(1); if (CDF<=CDFValue[values-1] && CDF>=CDFValue[0]) break; } sample[j] = seqRequest[i].factor*interp(IVValue, CDFValue, values, CDF, 0, 1, &code) + seqRequest[i].offset; } if (seqRequest[i].flags&SEQ_RANDOMIZE) { long k, l; double *sample1; if (verbose) fprintf(stderr, "Randomizing order of values\n"); if (!(sample1 = malloc(sizeof(*sample1)*samples))) SDDS_Bomb("memory allocation failure"); for (l=0; l<randomizationGroups; l++) if (randomizationData[l].group==seqRequest[i].randomizationGroup) break; if (l==randomizationGroups) SDDS_Bomb("problem with construction of randomization groups!"); for (k=0; k<samples; k++) sample1[k] = sample[randomizationData[l].order[k]]; free(sample); sample = sample1; } free(IVValue); free(CDFValue); } if (verbose) fprintf(stderr, "Setting SDDS column values\n"); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_SET_BY_NAME, sample, samples, seqRequest[i].outputName? seqRequest[i].outputName: seqRequest[i].indepName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (verbose) fprintf(stderr, "Writing data page\n"); if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!requireInput) break; } if (verbose) fprintf(stderr, "Exited read loop\n"); free(sample); if ((input && !SDDS_Terminate(&SDDSin)) || !SDDS_Terminate(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<seqRequests; i++) { if (seqRequest[i].dataFileName) free(seqRequest[i].dataFileName); if (seqRequest[i].indepName) free(seqRequest[i].indepName); if (seqRequest[i].outputName) free(seqRequest[i].outputName); if (seqRequest[i].DFName) free(seqRequest[i].DFName); if (seqRequest[i].CDFName) free(seqRequest[i].CDFName); if (seqRequest[i].units) free(seqRequest[i].units); if (seqRequest[i].meanPar) free(seqRequest[i].meanPar); if (seqRequest[i].sigmaPar) free(seqRequest[i].sigmaPar); if (seqRequest[i].minPar) free(seqRequest[i].minPar); if (seqRequest[i].maxPar) free(seqRequest[i].maxPar); if (seqRequest[i].flags&SEQ_DATAFILE && !SDDS_Terminate(&(seqRequest[i].SDDSin))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(seqRequest); for (i=0; i<randomizationGroups; i++) free(randomizationData[i].order); if (randomizationData) free(randomizationData); free_scanargs(&scanned, argc); return(0); }
void ReadPointFile(char *inputFile, char *xCol, char *yCol, long *pages, OUT_POINT **out_point) { SDDS_DATASET SDDS_in; int32_t rows1=0; long pages0=0, std_exist=0; double *x, *y, dx, dy, xmin, xmax, firstx, firsty, ymin, ymax; int nx, ny, i; if (!SDDS_InitializeInput(&SDDS_in, inputFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDS_in, xCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"x column - %s does not exist!\n", xCol); exit(1); } if (SDDS_CheckColumn(&SDDS_in, yCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"y column - %s does not exist!\n", yCol); exit(1); } while (SDDS_ReadPage(&SDDS_in)>0) { rows1 = 0; rows1=SDDS_CountRowsOfInterest(&SDDS_in); if (!rows1) continue; *out_point = SDDS_Realloc(*out_point, sizeof(**out_point)*(pages0+1)); x = y = NULL; (*out_point)[pages0].pout = malloc(sizeof(point)*rows1); (*out_point)[pages0].nout = rows1; if (!(x=(double*)SDDS_GetColumnInDoubles(&SDDS_in, xCol)) || !(y=(double*)SDDS_GetColumnInDoubles(&SDDS_in, yCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); xmin = xmax = firstx = x[0]; ymin = ymax = firsty = y[0]; nx = ny = 0; for (i=0; i<rows1; i++) { (*out_point)[pages0].pout[i].x = x[i]; (*out_point)[pages0].pout[i].y = y[i]; if (x[i]<xmin) xmin = x[i]; else if (x[i]>xmax) xmax = x[i]; if (x[i]==firstx) ny ++; if (y[i]<ymin) ymin = y[i]; else if (y[i]>ymax) ymax = y[i]; if (y[i]==firsty) nx ++; } free(x); free(y); (*out_point)[pages0].xmin = xmin; (*out_point)[pages0].xmax = xmax; (*out_point)[pages0].ymin = ymin; (*out_point)[pages0].ymax = ymax; (*out_point)[pages0].nx = nx; (*out_point)[pages0].ny = ny; if (nx>1) (*out_point)[pages0].dx = (xmax - xmin)/(nx-1); else (*out_point)[pages0].dx = 0; if (ny>1) (*out_point)[pages0].dx = (ymax - ymin)/(ny-1); else (*out_point)[pages0].dy = 0; pages0 ++; } if (!SDDS_Terminate(&SDDS_in)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!pages0) { fprintf(stderr, "No data found in the points file.\n"); exit(1); } *pages = pages0; return; }
long SetupNAFFOutput(SDDS_DATASET *SDDSout, char *output, SDDS_DATASET *SDDSin, char *indepQuantity, long depenQuantities, char **depenQuantity, long **frequencyIndex, long **amplitudeIndex, long **phaseIndex, long **significanceIndex, char **depenQuantityPair, long **amplitudeIndex1, long **phaseIndex1, long **significanceIndex1) { char *freqUnits, *buffer, *ampUnits; long i, maxBuffer, bufferNeeded; if (!(*frequencyIndex = SDDS_Malloc(sizeof(**frequencyIndex)*depenQuantities)) || !(*amplitudeIndex = SDDS_Malloc(sizeof(**amplitudeIndex)*depenQuantities)) || !(*phaseIndex = SDDS_Malloc(sizeof(**phaseIndex)*depenQuantities)) || !(*significanceIndex = SDDS_Malloc(sizeof(**significanceIndex)*depenQuantities)) || !(buffer = SDDS_Malloc(sizeof(*buffer)*(maxBuffer=1024)))) { SDDS_SetError("memory allocation failure"); return 0; } if (!(freqUnits = makeFrequencyUnits(SDDSin, indepQuantity)) || !SDDS_InitializeOutput(SDDSout, SDDS_BINARY, 0, NULL, "sddsnaff output", output)) return 0; for (i=0; i<depenQuantities; i++) { if ((bufferNeeded=strlen(depenQuantity[i])+12)>maxBuffer && !(buffer = SDDS_Realloc(buffer, sizeof(*buffer)*bufferNeeded))) SDDS_Bomb("memory allocation failure"); sprintf(buffer, "%sFrequency", depenQuantity[i]); if (((*frequencyIndex)[i]= SDDS_DefineColumn(SDDSout, buffer, NULL, freqUnits, NULL, NULL, SDDS_DOUBLE, 0))<0 || SDDS_GetColumnInformation(SDDSin, "units", &Units, SDDS_GET_BY_NAME, depenQuantity[i])!=SDDS_STRING) return 0; sprintf(buffer, "%sAmplitude", depenQuantity[i]); if (((*amplitudeIndex)[i]= SDDS_DefineColumn(SDDSout, buffer, NULL, ampUnits, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; sprintf(buffer, "%sPhase", depenQuantity[i]); if (((*phaseIndex)[i] = SDDS_DefineColumn(SDDSout, buffer, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; sprintf(buffer, "%sSignificance", depenQuantity[i]); if (((*significanceIndex)[i] = SDDS_DefineColumn(SDDSout, buffer, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; } if (depenQuantityPair) { if (!(*amplitudeIndex1 = SDDS_Malloc(sizeof(**amplitudeIndex1)*depenQuantities)) || !(*phaseIndex1 = SDDS_Malloc(sizeof(**phaseIndex1)*depenQuantities)) || !(*significanceIndex1 = SDDS_Malloc(sizeof(**significanceIndex1)*depenQuantities))) { SDDS_SetError("memory allocation failure"); return 0; } for (i=0; i<depenQuantities; i++) { if ((bufferNeeded=strlen(depenQuantityPair[i])+12)>maxBuffer && !(buffer = SDDS_Realloc(buffer, sizeof(*buffer)*bufferNeeded))) SDDS_Bomb("memory allocation failure"); sprintf(buffer, "%sAmplitude", depenQuantityPair[i]); if (((*amplitudeIndex1)[i]= SDDS_DefineColumn(SDDSout, buffer, NULL, ampUnits, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; sprintf(buffer, "%sPhase", depenQuantityPair[i]); if (((*phaseIndex1)[i] = SDDS_DefineColumn(SDDSout, buffer, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; sprintf(buffer, "%sSignificance", depenQuantityPair[i]); if (((*significanceIndex1)[i] = SDDS_DefineColumn(SDDSout, buffer, NULL, NULL, NULL, NULL, SDDS_DOUBLE, 0))<0) return 0; } } free(ampUnits); free(freqUnits); if (!SDDS_TransferAllParameterDefinitions(SDDSout, SDDSin, SDDS_TRANSFER_KEEPOLD) || !SDDS_WriteLayout(SDDSout)) return 0; free(buffer); return 1; }
void determine_dataset_labels(PLOT_SPEC *plspec, SDDS_TABLE *table, PLOT_DATA *dataset) { PLOT_REQUEST *request; long i; DATA_INFO *info; char printBuffer[SDDS_MAXLINE]; request = plspec->plot_request+dataset->request_index; for (i=0; i<4; i++) { dataset->label[i] = NULL; if (request->label[i].flags&LABEL_STRING_GIVEN) { if (!SDDS_CopyString(&dataset->label[i], request->label[i].label)) SDDS_Bomb("String copy error (determine_dataset_labels)"); } else if (request->label[i].flags&LABEL_PARAMETER_GIVEN) { long type; char *dataBuffer = NULL; char *format =NULL; if ( !(dataBuffer = malloc(sizeof(double)*4))) SDDS_Bomb("Allocation failure in determin_dataset_labels"); if (!SDDS_GetParameter(table,request->label[i].label, dataBuffer)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if(!(type=SDDS_GetParameterType(table,SDDS_GetParameterIndex(table,request->label[i].label)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if(SDDS_GetParameterInformation(table,"format_string",&format,SDDS_GET_BY_NAME,request->label[i].label) !=SDDS_STRING) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ( request->label[i].flags&LABEL_FORMAT_GIVEN) { if(!SDDS_VerifyPrintfFormat(request->label[i].format,type)) { fprintf(stderr, "error: given format (\"%s\") for parameter %s is invalid\n", request->label[i].format, request->label[i].label); exit(1); } if(!(SDDS_SprintTypedValue((void*)dataBuffer,0,type,request->label[i].format,printBuffer,SDDS_PRINT_NOQUOTES))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_CopyString(&dataset->label[i],printBuffer); } else { if(!SDDS_StringIsBlank(format)) { if(!(SDDS_SprintTypedValue((void*)dataBuffer,0,type,format,printBuffer,SDDS_PRINT_NOQUOTES))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_CopyString(&dataset->label[i],printBuffer); } else { if (!(dataset->label[i]=SDDS_GetParameterAsString(table,request->label[i].label,NULL))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } free(format); free(dataBuffer); /*if (SDDS_GetParameterType(table, SDDS_GetParameterIndex(table, request->label[i].label)) != SDDS_STRING) { fprintf(stderr, "labels taken from parameters must be strings\n"); exit(1); } if (!SDDS_GetParameter(table, request->label[i].label, &s)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); dataset->label[i] = s; */ } else if (request->label[i].flags&(LABEL_USE_NAME+LABEL_USE_SYMBOL+LABEL_USE_DESCRIPTION)) { if (i==0) info = &dataset->info[0]; else info = &dataset->info[1]; if (request->label[i].flags&LABEL_USE_NAME) SDDS_CopyString(&dataset->label[i], i==0?request->xname[dataset->dataname_index]:request->yname[dataset->dataname_index]); else if (request->label[i].flags&LABEL_USE_SYMBOL && info->symbol) SDDS_CopyString(&dataset->label[i], info->symbol); else if (request->label[i].flags&LABEL_USE_DESCRIPTION && info->description) SDDS_CopyString(&dataset->label[i], info->description); if (dataset->label[i] && request->label[i].flags&LABEL_INCLUDE_UNITS && info->units && !SDDS_StringIsBlank(info->units)) { if (!(dataset->label[i] = SDDS_Realloc(dataset->label[i], sizeof(*dataset->label[i])*(strlen(dataset->label[i])+strlen(info->units)+4)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); strcat(dataset->label[i], " ("); strcat(dataset->label[i], info->units); strcat(dataset->label[i], ")"); } } } }
void run_rpn_load(NAMELIST_TEXT *nltext, RUN *run) { SDDS_DATASET SDDSin; long code, foundPage, iColumn, matchRow, rows, iParameter; int32_t columns, parameters; char *parameterValue = NULL; double *data, data1; char **columnName, **matchColumnData, *memName = NULL; char **parameterName; /* process the namelist text */ set_namelist_processing_flags(STICKY_NAMELIST_DEFAULTS); set_print_namelist_flags(0); if (processNamelist(&rpn_load, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (echoNamelists) print_namelist(stdout, &rpn_load); if (match_column && strlen(match_column)) { if (use_row!=-1) { fprintf(stdout, "Error: you asked to match a column and also gave use_row.\n"); exitElegant(1); } if (!match_column_value || !strlen(match_column_value)) { fprintf(stdout, "Error: you must give match_column_value with match_column\n"); exitElegant(1); } } if (match_parameter && strlen(match_parameter)) { if (use_page!=-1) { fprintf(stdout, "Error: you asked to match a parameter and also gave use_page.\n"); exitElegant(1); } if (!match_parameter_value || !strlen(match_parameter_value)) { fprintf(stdout, "Error: you must give match_parameter_value with match_parameter\n"); exitElegant(1); } } if (!filename || !strlen(filename)) { fprintf(stdout, "Error: no filename given for rpn_load.\n"); exitElegant(1); } filename = compose_filename(filename, run->rootname); if (!SDDS_InitializeInputFromSearchPath(&SDDSin, filename)) { fprintf(stdout, "Error: couldn't initialize SDDS input for %s\n", filename); exitElegant(1); } foundPage = 0; while ((code=SDDS_ReadPage(&SDDSin))>0) { if (use_page>0) { if (code==use_page) { foundPage = 1; break; } continue; } if (match_parameter && strlen(match_parameter)) { if (!(parameterValue=SDDS_GetParameterAsString(&SDDSin, match_parameter, NULL))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!wild_match(parameterValue, match_parameter_value)) continue; foundPage = 1; break; } if (use_page==-1 && SDDS_CheckEndOfFile(&SDDSin)==1) { foundPage = 1; break; } } if (!foundPage) { fprintf(stdout, "Error: no appropriate page found\n"); exitElegant(1); } if (!load_parameters) { if ((columnName = SDDS_GetColumnNames(&SDDSin, &columns))==NULL) { fprintf(stdout, "Warning: No columns in file!\n"); return; } rows = SDDS_RowCount(&SDDSin); matchRow = rows-1; if (use_row!=-1) { if (use_row>=rows) { fprintf(stdout, "Error: number of rows in file (%ld) less than needed for use_row=%ld\n", rows, use_row); exitElegant(1); } matchRow = use_row; } if (match_column) { if (SDDS_GetNamedColumnType(&SDDSin, match_column)!=SDDS_STRING) { fprintf(stdout, "Error: column %s nonexistent or not string type.\n", match_column); exitElegant(1); } if (!(matchColumnData=SDDS_GetColumn(&SDDSin, match_column))) { fprintf(stdout, "Error: unable to get data for column %s\n", match_column); exitElegant(1); } if (matching_row_number<0) { /* use last match */ for (matchRow=rows-1; matchRow>=0; matchRow--) if (wild_match(matchColumnData[matchRow], match_column_value)) break; } else { /* use nth match */ for (matchRow=0; matchRow<rows; matchRow++) if (wild_match(matchColumnData[matchRow], match_column_value) && matching_row_number-- == 0) break; } if (matchRow<0 || matchRow>=rows) { fprintf(stdout, "Error: unable to find match for %s in column %s\n", match_column_value, match_column); exitElegant(1); } SDDS_FreeStringArray(matchColumnData, rows); } for (iColumn=0; iColumn<columns; iColumn++) { switch (SDDS_GetNamedColumnType(&SDDSin, columnName[iColumn])) { case SDDS_CHARACTER: case SDDS_STRING: break; default: if (!(data=SDDS_GetColumnInDoubles(&SDDSin, columnName[iColumn]))) { fprintf(stdout, "Error: unable to get data for column %s as numerical data.\n", columnName[iColumn]); exitElegant(1); } if (!(memName=SDDS_Realloc(memName, sizeof(*memName)*((tag?strlen(tag):0)+strlen(columnName[iColumn])+2)))) { fprintf(stdout, "Memory allocation failure trying to create memory name for loaded data\n"); exitElegant(1); } if (tag && strlen(tag)) sprintf(memName, "%s.%s", tag, columnName[iColumn]); else sprintf(memName, "%s", columnName[iColumn]); rpn_store(data[matchRow], NULL, rpn_create_mem(memName, 0)); fprintf(stdout, "%le --> %s\n", data[matchRow], memName); free(columnName[iColumn]); free(data); } } if (memName) free(memName); if (columnName) free(columnName); } else { /* load data from parameters */ if ((parameterName = SDDS_GetParameterNames(&SDDSin, ¶meters))==NULL) { fprintf(stdout, "Warning: No parameters in file!\n"); return; } for (iParameter=0; iParameter<parameters; iParameter++) { switch (SDDS_GetNamedParameterType(&SDDSin, parameterName[iParameter])) { case SDDS_CHARACTER: case SDDS_STRING: break; default: if (!SDDS_GetParameterAsDouble(&SDDSin, parameterName[iParameter], &data1)) { fprintf(stdout, "Error: unable to get data for parameter %s as numerical data.\n", parameterName[iParameter]); exitElegant(1); } if (!(memName=SDDS_Realloc(memName, sizeof(*memName)*((tag?strlen(tag):0)+strlen(parameterName[iParameter])+2)))) { fprintf(stdout, "Memory allocation failure trying to create memory name for loaded data\n"); exitElegant(1); } if (tag && strlen(tag)) sprintf(memName, "%s.%s", tag, parameterName[iParameter]); else sprintf(memName, "%s", parameterName[iParameter]); rpn_store(data1, NULL, rpn_create_mem(memName, 0)); fprintf(stdout, "%le --> %s\n", data1, memName); free(parameterName[iParameter]); } } if (memName) free(memName); if (parameterName) free(parameterName); } }
void addFilter(FILTER_STAGE *filterStage, long optionCode, SCANNED_ARG *scanned) { THRESHOLD_FILTER *thresholdFilter; HILO_FILTER *hiloFilter; NHBP_FILTER *nhbpFilter; FILE_FILTER *fileFilter; CLIP_FILTER *clipFilter; FILTER *filter; long items, filters; char **item; if (!(filterStage->filter = SDDS_Realloc(filterStage->filter, sizeof(*filterStage->filter)*(filterStage->filters+1)))) SDDS_Bomb("allocation failure adding filter slot"); filter = filterStage->filter; filters = filterStage->filters; filterStage->filters ++; items = scanned->n_items - 1; item = scanned->list + 1; switch (filter[filters].filterType = optionCode) { case SET_THRESHOLD: if (!(thresholdFilter = filter[filters].filter = calloc(1, sizeof(THRESHOLD_FILTER)))) SDDS_Bomb("allocation failure allocating threshold filter"); if (items<1) SDDS_Bomb("invalid -threshold syntax"); if (!scanItemList(&thresholdFilter->flags, item, &items, 0, "level", SDDS_DOUBLE, &thresholdFilter->level, 1, FILT_LEVEL_GIVEN, "fractional", -1, NULL, 0, FILT_FRACTHRES_GIVEN, "start", SDDS_DOUBLE, &thresholdFilter->start, 1, FILT_START_GIVEN, "end", SDDS_DOUBLE, &thresholdFilter->end, 1, FILT_START_GIVEN, NULL)) SDDS_Bomb("invalid -threshold syntax/values"); if (!(thresholdFilter->flags&FILT_LEVEL_GIVEN)) SDDS_Bomb("supply level=<value> qualifier for -threshold"); if ((thresholdFilter->flags&FILT_START_GIVEN && thresholdFilter->flags&FILT_END_GIVEN) && thresholdFilter->start>thresholdFilter->end) SDDS_Bomb("start>end for -threshold filter"); break; case SET_HIGHPASS: case SET_LOWPASS: if (!(hiloFilter = filter[filters].filter = calloc(1, sizeof(HILO_FILTER)))) SDDS_Bomb("allocation failure allocating low-pass or high-pass filter"); if (!scanItemList(&hiloFilter->flags, item, &items, 0, "start", SDDS_DOUBLE, &hiloFilter->start, 1, FILT_START_GIVEN, "end", SDDS_DOUBLE, &hiloFilter->end, 1, FILT_END_GIVEN, NULL)) SDDS_Bomb("invalid -highpass or -lowpass syntax"); if (!(hiloFilter->flags&FILT_START_GIVEN) || !(hiloFilter->flags&FILT_END_GIVEN)) SDDS_Bomb("supply start=<value> and end=<value> qualifiers with -highpass and -lowpass"); break; case SET_NOTCH: case SET_BANDPASS: if (!(nhbpFilter = filter[filters].filter = calloc(1, sizeof(NHBP_FILTER)))) SDDS_Bomb("allocation failure allocating notch or bandpass filter"); if (!scanItemList(&nhbpFilter->flags, item, &items, 0, "center", SDDS_DOUBLE, &nhbpFilter->center, 1, FILT_CENTER_GIVEN, "fullwidth", SDDS_DOUBLE, &nhbpFilter->fullwidth, 1, FILT_FULLWIDTH_GIVEN, "flatwidth", SDDS_DOUBLE, &nhbpFilter->flatwidth, 1, FILT_FLATWIDTH_GIVEN, NULL)) SDDS_Bomb("invalid -notch or -bandpass syntax"); if (!(nhbpFilter->flags&FILT_CENTER_GIVEN) || !(nhbpFilter->flags&FILT_FLATWIDTH_GIVEN) ) SDDS_Bomb("supply center=<value> and flatWidth=<value> qualifiers with -notch and -bandpass"); if (!(nhbpFilter->flags&FILT_FULLWIDTH_GIVEN)) nhbpFilter->fullwidth = nhbpFilter->flatwidth; if (nhbpFilter->fullwidth<nhbpFilter->flatwidth) SDDS_Bomb("full width may not be less that flat width for notch/bandpass filter"); break; case SET_FILTERFILE: if (!(fileFilter = filter[filters].filter = calloc(1, sizeof(FILE_FILTER)))) SDDS_Bomb("allocation failure allocating file filter"); if (!scanItemList(&fileFilter->flags, item, &items, 0, "filename", SDDS_STRING, &fileFilter->file, 1, FILT_FILENAME_GIVEN, "frequency", SDDS_STRING, &fileFilter->freqName, 1, FILT_FREQNAME_GIVEN, "real", SDDS_STRING, &fileFilter->realName, 1, FILT_REALNAME_GIVEN, "imaginary", SDDS_STRING, &fileFilter->imagName, 1, FILT_IMAGNAME_GIVEN, "magnitude", SDDS_STRING, &fileFilter->magName, 1, FILT_MAGNAME_GIVEN, NULL)) SDDS_Bomb("invalid -fileFilter syntax"); if (!(fileFilter->flags&FILT_FILENAME_GIVEN)) SDDS_Bomb("supply filename=<string> with -fileFilter"); if (!(fileFilter->flags&FILT_FREQNAME_GIVEN)) SDDS_Bomb("supply frequency=<columnName> with -fileFilter"); if (!(fileFilter->flags&FILT_MAGNAME_GIVEN) && !(fileFilter->flags&FILT_REALNAME_GIVEN && fileFilter->flags&FILT_IMAGNAME_GIVEN)) SDDS_Bomb("supply either magnitude=<columnName>, or real=<columnName> and imag=<columnName>, with -fileFilter"); if (fileFilter->flags&FILT_MAGNAME_GIVEN && (fileFilter->flags&FILT_REALNAME_GIVEN || fileFilter->flags&FILT_IMAGNAME_GIVEN)) SDDS_Bomb("magnitude=<columnName> is incompatible with real=<columnName> and imag=<columnName> for -fileFilter"); fileFilter->freqData = NULL; /* indicates no data present */ break; case SET_CLIPFREQ: if (!(clipFilter = filter[filters].filter = calloc(1, sizeof(CLIP_FILTER)))) SDDS_Bomb("allocation failure allocating clip filter"); if (!scanItemList(&clipFilter->flags, item, &items, 0, "high", SDDS_LONG, &clipFilter->high, 1, FILT_HIGH_GIVEN, "low", SDDS_LONG, &clipFilter->low, 1, FILT_LOW_GIVEN, NULL)) SDDS_Bomb("invalid -clipFrequencies syntax"); if (!(clipFilter->flags&FILT_HIGH_GIVEN) && !(clipFilter->flags&FILT_LOW_GIVEN)) SDDS_Bomb("supply at least one of high=<number> or low=<number> with -clipFrequencies"); break; default: SDDS_Bomb("invalid filter code---this shouldn't happen"); break; } }
int main(int argc, char **argv) { SDDS_DATASET SDDS_orig, SDDS_out1, SDDS_out2; int32_t i, j, k, row, str_len; SCANNED_ARG *s_arg; char *inputFile, *outputRoot, output1[1024], output2[1024], tmpcol[256]; double xmin, xmax, ymin, ymax, zmin, zmax, xinterval, yinterval, zinterval; int32_t xdim, ydim, zdim, columnNames, outputColumns, page=0; char **columnName, **outputColumn; double ****data; inputFile = outputRoot = NULL; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2) bomb(NULL, USAGE); columnNames = outputColumns = 0; columnName = outputColumn = NULL; inputFile = s_arg[1].list[0]; if (argc==3) outputRoot = s_arg[2].list[0]; else outputRoot = inputFile; sprintf(output1, "%s.yz", outputRoot); sprintf(output2, "%s.xz", outputRoot); data = NULL; if (!SDDS_InitializeInput(&SDDS_orig, inputFile)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } columnName = (char**)SDDS_GetColumnNames(&SDDS_orig, &columnNames); for (i=0; i<columnNames; i++) { if (wild_match(columnName[i], "*_1")) { outputColumn = SDDS_Realloc(outputColumn, (outputColumns+1)*sizeof(*outputColumn)); outputColumn[outputColumns] = malloc(sizeof(**outputColumn)*strlen(columnName[i])); str_len = strlen(columnName[i]); strncpy(outputColumn[outputColumns], columnName[i], str_len-2); outputColumn[outputColumns][str_len-2]=0; outputColumns ++; } } data = malloc(sizeof(*data)*outputColumns); while (SDDS_ReadPage(&SDDS_orig)>0) { if (page==0) { if (!SDDS_GetParameterAsDouble(&SDDS_orig, "origin1", &xmin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext1", &xmax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta1", &xinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells1", &xdim) || !SDDS_GetParameterAsDouble(&SDDS_orig, "origin2", &ymin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext2", &ymax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta2", &yinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells2", &ydim) || !SDDS_GetParameterAsDouble(&SDDS_orig, "origin3", &zmin) || !SDDS_GetParameterAsDouble(&SDDS_orig, "max_ext3", &zmax) || !SDDS_GetParameterAsDouble(&SDDS_orig, "delta3", &zinterval) || !SDDS_GetParameterAsLong(&SDDS_orig, "numPhysCells3", &zdim)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } SetupOutputFile(&SDDS_out1, output1, 1, outputColumns, outputColumn); SetupOutputFile(&SDDS_out2, output2, 0, outputColumns, outputColumn); for (i=0; i<outputColumns; i++) { data[i] = malloc(sizeof(**data)*zdim); for (j=0; j<zdim; j++) data[i][j] = malloc(sizeof(***data)*ydim); } } for (i=0; i<outputColumns; i++) { for (j=1; j<=ydim; j++) { sprintf(tmpcol, "%s_%d", outputColumn[i], j); data[i][page][j-1] = NULL; if (!(data[i][page][j-1]=SDDS_GetColumnInDoubles(&SDDS_orig, tmpcol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } page ++; } if (!SDDS_Terminate(&SDDS_orig)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (page!=zdim) { free_data_memory(data, outputColumns, zdim, ydim); fprintf(stderr, "Error, the page number does not equal to zdim size.\n"); exit(1); } /* write to yz output, each page has the same x */ for (page=0; page<xdim; page++) { if (!SDDS_StartPage(&SDDS_out1, ydim*zdim) || !SDDS_SetParameters(&SDDS_out1,SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin1", xmin, "origin2", ymin, "origin3", zmin, "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax, "delta1", xinterval, "delta2", yinterval, "delta3", zinterval, "numPhysCells1", xdim, "numPhysCells2", ydim, "numPhysCells3", zdim, "Variable1Name", "Y", "Variable2Name", "Z", "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", zinterval, "ZDimension", zdim, "YMinimum", ymin, "YMaximum", ymax, "YInterval", yinterval, "YDimension", ydim, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<outputColumns; i++) { row = 0; for (j=0; j<ydim; j++) { for (k=0; k<zdim; k++) { if (!SDDS_SetRowValues(&SDDS_out1, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, outputColumn[i], data[i][k][j][page], "z", k*zinterval + zmin, "y", j*yinterval + ymin, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row ++; } } } if (!SDDS_WritePage(&SDDS_out1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDS_out1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* write to xz output, each page has the same y */ for (page=0; page<ydim; page++) { if (!SDDS_StartPage(&SDDS_out2, xdim*zdim) || !SDDS_SetParameters(&SDDS_out2, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "origin1", xmin, "origin2", ymin, "origin3", zmin, "max_ext1", xmax, "max_ext2", ymax, "max_ext3", zmax, "delta1", xinterval, "delta2", yinterval, "delta3", zinterval, "numPhysCells1", xdim, "numPhysCells2", ydim, "numPhysCells3", zdim, "Variable1Name", "X", "Variable2Name", "Z", "ZMinimum", zmin, "ZMaximum", zmax, "ZInterval", zinterval, "ZDimension", zdim, "XMinimum", xmin, "XMaximum", xmax, "XInterval", xinterval, "XDimension", xdim, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<outputColumns; i++) { row = 0; for (j=0; j<xdim; j++) { for (k=0; k<zdim; k++) { if (!SDDS_SetRowValues(&SDDS_out2, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, row, outputColumn[i], data[i][k][page][j], "z", k*zinterval + zmin, "x", j*xinterval+xmin, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); row ++; } } } if (!SDDS_WritePage(&SDDS_out2)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free_data_memory(data, outputColumns, zdim, ydim); if (!SDDS_Terminate(&SDDS_out2)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); return 0; }
int main(int argc, char **argv) { POLYNOMIAL *poly; long nPoly, iPoly, row, rows, iInput; int iArg; char *input, *output; unsigned long pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; double *outputData ; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3) bomb(NULL, USAGE); outputData = NULL; input = output = NULL; pipeFlags = 0; poly = NULL; nPoly = 0; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, CLO_OPTIONS, 0)) { case CLO_EVALUATE: if (!(poly = SDDS_Realloc(poly, sizeof(*poly)*(nPoly+1))) || !(poly[nPoly].inputColumn =SDDS_Malloc(sizeof(*(poly[nPoly].inputColumn))*5)) || !(poly[nPoly].powerColumn =SDDS_Malloc(sizeof(*(poly[nPoly].powerColumn))*5))) SDDS_Bomb("memory allocation failure"); scanned[iArg].n_items -= 1; if (!scanItemList(&poly[nPoly].flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "filename", SDDS_STRING, &(poly[nPoly].filename), 1, POLY_FILE_SEEN, "output", SDDS_STRING, &(poly[nPoly].outputColumn), 1, POLY_OUTPUT_SEEN, "coefficients", SDDS_STRING, &(poly[nPoly].coefColumn), 1, POLY_COEF_SEEN, "input0", SDDS_STRING, poly[nPoly].inputColumn+0, 1, POLY_IN0_SEEN, "power0", SDDS_STRING, poly[nPoly].powerColumn+0, 1, POLY_OUT0_SEEN, "input1", SDDS_STRING, poly[nPoly].inputColumn+1, 1, POLY_IN1_SEEN, "power1", SDDS_STRING, poly[nPoly].powerColumn+1, 1, POLY_OUT1_SEEN, "input2", SDDS_STRING, poly[nPoly].inputColumn+2, 1, POLY_IN2_SEEN, "power2", SDDS_STRING, poly[nPoly].powerColumn+2, 1, POLY_OUT2_SEEN, "input3", SDDS_STRING, poly[nPoly].inputColumn+3, 1, POLY_IN3_SEEN, "power3", SDDS_STRING, poly[nPoly].powerColumn+3, 1, POLY_OUT3_SEEN, "input4", SDDS_STRING, poly[nPoly].inputColumn+4, 1, POLY_IN4_SEEN, "power4", SDDS_STRING, poly[nPoly].powerColumn+4, 1, POLY_OUT4_SEEN, NULL) || !(poly[nPoly].flags&POLY_FILE_SEEN) || !(poly[nPoly].flags&POLY_OUTPUT_SEEN) || !(poly[nPoly].flags&POLY_COEF_SEEN) || !(poly[nPoly].flags&POLY_IN0_SEEN) || !(poly[nPoly].flags&POLY_OUT0_SEEN)) SDDS_Bomb("invalid -evaluate syntax"); nPoly++; break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scanned[iArg].list[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddspoly", &input, &output, pipeFlags, 0, NULL); if (nPoly==0) SDDS_Bomb("give at least one -evaluate option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (iPoly=0; iPoly<nPoly; iPoly++) initializePolynomial(&poly[iPoly], &SDDSin, &SDDSout); if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while (SDDS_ReadPage(&SDDSin)>0) { rows = SDDS_CountRowsOfInterest(&SDDSin); if (!SDDS_CopyPage(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(outputData = SDDS_Realloc(outputData, sizeof(*outputData)*rows))) SDDS_Bomb("memory allocation failure"); for (iPoly=0; iPoly<nPoly; iPoly++) { for (iInput=0; iInput<poly[iPoly].nInputs; iInput++) { if (!(poly[iPoly].inputData[iInput]= SDDS_GetColumnInDoubles(&SDDSin, poly[iPoly].inputColumn[iInput]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (row=0; row<rows; row++) { for (iInput=0; iInput<poly[iPoly].nInputs; iInput++) poly[iPoly].input[iInput] = poly[iPoly].inputData[iInput][row]; outputData[row] = evaluatePoly(poly[iPoly].coef, poly[iPoly].power, poly[iPoly].nTerms, poly[iPoly].input, poly[iPoly].nInputs); } if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, outputData, rows, poly[iPoly].outputColumn)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(outputData); if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free_scanargs(&scanned,argc); FreePolynormialMemory(poly,nPoly); free(poly); return 0; }
int32_t SDDS_RestoreLayout(SDDS_DATASET *SDDS_dataset) { SDDS_LAYOUT *source, *target; if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_RestoreLayout")) return(0); source = &SDDS_dataset->original_layout; target = &SDDS_dataset->layout; /* copy pointer elements of structure into new memory */ if (source->n_columns) { if (target->column_definition==source->column_definition) { SDDS_SetError("Unable to restore layout--column definition pointers are the same (SDDS_RestoreLayout)"); return(0); } if (!(target->column_definition = (COLUMN_DEFINITION*)SDDS_Realloc((void*)target->column_definition, sizeof(COLUMN_DEFINITION)*source->n_columns))) { SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)"); return(0); } memcpy((char*)target->column_definition, (char*)source->column_definition, sizeof(COLUMN_DEFINITION)*source->n_columns); } if (source->n_parameters) { if (target->parameter_definition==source->parameter_definition) { SDDS_SetError("Unable to restore layout--parameter definition pointers are the same (SDDS_RestoreLayout)"); return(0); } if (!(target->parameter_definition = (PARAMETER_DEFINITION*)SDDS_Realloc((void*)target->parameter_definition, sizeof(PARAMETER_DEFINITION)*source->n_parameters))) { SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)"); return(0); } memcpy((char*)target->parameter_definition, (char*)source->parameter_definition, sizeof(PARAMETER_DEFINITION)*source->n_parameters); } if (source->n_arrays) { if (target->array_definition==source->array_definition) { SDDS_SetError("Unable to restore layout--array definition pointers are the same (SDDS_RestoreLayout)"); return(0); } if (!(target->array_definition = (ARRAY_DEFINITION*)SDDS_Realloc((void*)target->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays))) { SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)"); return(0); } memcpy((char*)target->array_definition, (char*)source->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays); } if (source->n_associates) { if (target->associate_definition==source->associate_definition) { SDDS_SetError("Unable to restore layout--associate definition pointers are the same (SDDS_RestoreLayout)"); return(0); } if (!(target->associate_definition = (ASSOCIATE_DEFINITION*)SDDS_Realloc((void*)target->associate_definition, sizeof(ASSOCIATE_DEFINITION)*source->n_associates))) { SDDS_SetError("Unable to restore layout--allocation failure (SDDS_RestoreLayout)"); return(0); } memcpy((char*)target->associate_definition, (char*)source->associate_definition, sizeof(ASSOCIATE_DEFINITION)*source->n_associates); } target->n_columns = source->n_columns; target->n_parameters = source->n_parameters; target->n_associates = source->n_associates; target->n_arrays = source->n_arrays; target->description = source->description; target->contents = source->contents; target->version = source->version; target->data_mode = source->data_mode; target->filename = source->filename; target->fp = source->fp; return(1); }
int32_t SDDS_SaveLayout(SDDS_DATASET *SDDS_dataset) { SDDS_LAYOUT *source, *target; if (deferSavingLayout) return 1; if (!SDDS_CheckDataset(SDDS_dataset, "SDDS_SaveLayout")) return(0); if ((source = &SDDS_dataset->layout)==(target = &SDDS_dataset->original_layout)) { SDDS_SetError("\"original\" and working page layouts share memory!"); SDDS_PrintErrors(stderr, SDDS_EXIT_PrintErrors|SDDS_VERBOSE_PrintErrors); } /* copy pointer elements of structure into new memory */ if (source->n_columns) { if (!(target->column_definition = (COLUMN_DEFINITION*)SDDS_Realloc((void*)target->column_definition, sizeof(COLUMN_DEFINITION)*source->n_columns)) || !(target->column_index = (SORTED_INDEX**)SDDS_Realloc((void*)target->column_index, sizeof(SORTED_INDEX*)*source->n_columns))) { SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)"); return(0); } memcpy((char*)target->column_definition, (char*)source->column_definition, sizeof(COLUMN_DEFINITION)*source->n_columns); memcpy((char*)target->column_index, (char*)source->column_index, sizeof(SORTED_INDEX*)*source->n_columns); } if (source->n_parameters) { if (!(target->parameter_definition = (PARAMETER_DEFINITION*)SDDS_Realloc((void*)target->parameter_definition, sizeof(PARAMETER_DEFINITION)*source->n_parameters)) || !(target->parameter_index = (SORTED_INDEX**)SDDS_Realloc((void*)target->parameter_index, sizeof(SORTED_INDEX*)*source->n_parameters))) { SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)"); return(0); } memcpy((char*)target->parameter_definition, (char*)source->parameter_definition, sizeof(PARAMETER_DEFINITION)*source->n_parameters); memcpy((char*)target->parameter_index, (char*)source->parameter_index, sizeof(SORTED_INDEX*)*source->n_parameters); } if (source->n_arrays) { if (!(target->array_definition = (ARRAY_DEFINITION*)SDDS_Realloc((void*)target->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays)) || !(target->array_index = (SORTED_INDEX**)SDDS_Realloc((void*)target->array_index, sizeof(SORTED_INDEX*)*source->n_arrays))) { SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)"); return(0); } memcpy((char*)target->array_definition, (char*)source->array_definition, sizeof(ARRAY_DEFINITION)*source->n_arrays); memcpy((char*)target->array_index, (char*)source->array_index, sizeof(SORTED_INDEX*)*source->n_arrays); } if (source->n_associates) { if (!(target->associate_definition = (ASSOCIATE_DEFINITION*)SDDS_Realloc((void*)target->associate_definition, sizeof(ASSOCIATE_DEFINITION)*source->n_associates))) { SDDS_SetError("Unable to save layout--allocation failure (SDDS_SaveLayout)"); return(0); } memcpy((char*)target->associate_definition, (char*)source->associate_definition, sizeof(ASSOCIATE_DEFINITION)*source->n_associates); } target->n_columns = source->n_columns; target->n_parameters = source->n_parameters; target->n_associates = source->n_associates; target->n_arrays = source->n_arrays; target->description = source->description; target->contents = source->contents; target->version = source->version; target->data_mode = source->data_mode; target->filename = source->filename; target->fp = source->fp; target->popenUsed = source->popenUsed; return(1); }
long perform_sddsplot_filtering(SDDS_TABLE *table, FILTER_DEFINITION **filter, long filters) { long i, j, accept, skip_table; long n_left, row_deletion, n_rows; FILTER_TERM *filter_term; FILTER_DEFINITION *filter_ptr; PARAMETER_DEFINITION *pardefptr; double result; static int32_t *rowFlag1 = NULL, *rowFlag2 = NULL; static long rowFlags = 0; skip_table = 0; #if DEBUG fprintf(stderr, "%ld filters being applied\n", filters); #endif if (filters==0) return !skip_table; n_rows = SDDS_RowCount(table); row_deletion = 1; for (i=0; i<filters; i++) { filter_ptr = filter[i]; if (filter_ptr->is_parameter) { accept = 1; filter_term = filter_ptr->filter_term; for (j=0; j<filter_ptr->filter_terms; j++) { if (!(pardefptr=SDDS_GetParameterDefinition(table, filter_term[j].name)) || (pardefptr->type==SDDS_STRING || pardefptr->type==SDDS_CHARACTER)) { fprintf(stderr, "error: unknown or non-numeric parameter %s given for filter\n", filter_term[j].name); exit(1); } accept = SDDS_Logic(accept, SDDS_ItemInsideWindow(SDDS_GetParameter(table, filter_term[j].name, &result), 0, pardefptr->type, filter_term[j].lower, filter_term[j].upper), filter_term[j].logic); } if (!accept) { skip_table = 1; break; } } else if (n_rows) { if (!rowFlag1 || !rowFlag2 || n_rows>rowFlags) { if (!(rowFlag1 = SDDS_Realloc(rowFlag1, sizeof(*rowFlag1)*n_rows)) || !(rowFlag2 = SDDS_Realloc(rowFlag2, sizeof(*rowFlag2)*n_rows))) { fprintf(stderr, "Problem reallocating row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_GetRowFlags(table, rowFlag1, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } filter_term = filter_ptr->filter_term; for (j=0; j<filter_ptr->filter_terms; j++) { #if defined(DEBUG) fprintf(stderr, "Filtering by %s on [%e, %e] with logic %x\n", filter_term[j].name, filter_term[j].lower, filter_term[j].upper, filter_term[j].logic); #endif if ((n_left=SDDS_FilterRowsOfInterest(table, filter_term[j].name, filter_term[j].lower, filter_term[j].upper, filter_term[j].logic))<0) { fprintf(stderr, "error: unable to filter by column %s\n", filter_term[j].name); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } /* fprintf(stderr,"total_rows=%d, filtered left=%d\n",n_rows,n_left);*/ #if defined(DEBUG) fprintf(stderr, "%ld rows of %ld left after filter %ld of set %ld\n", n_left, n_rows, j, i); #endif } if (i) { if (!SDDS_GetRowFlags(table, rowFlag2, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } for (j=0; j<n_rows; j++) { rowFlag1[j] = rowFlag1[j]&rowFlag2[j]; } if (!SDDS_AssertRowFlags(table, SDDS_FLAG_ARRAY, rowFlag1, n_rows)) { fprintf(stderr, "Unable to assert row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } else { if (!SDDS_GetRowFlags(table, rowFlag1, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_filtering)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } row_deletion = 1; } } return !skip_table; }
int main(int argc, char **argv) { double *xData, *yData, *xError, *yError, *derivative, *derivativeError, *derivativePosition; char *input, *output, *xName, *xErrorName, **yName, **yErrorName, **yOutputName, **yOutputErrorName, *ptr; char **yOutputUnits, **yExcludeName; char *mainTemplate[3] = {NULL, NULL, NULL}; char *errorTemplate[3] = {NULL, NULL, NULL}; long i, iArg, yNames, yExcludeNames, rows; int32_t interval; SDDS_DATASET SDDSin, SDDSout; SCANNED_ARG *scanned; unsigned long flags, pipeFlags; long SGLeft, SGRight, SGOrder, SGDerivOrder, intervalGiven, yErrorsSeen; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc==1) bomb(NULL, USAGE); input = output = xName = xErrorName = NULL; yName = yErrorName = yExcludeName = NULL; derivative = derivativeError = derivativePosition = yError = yData = xData = xError = NULL; yNames = yExcludeNames = 0; pipeFlags = 0; interval = 2; SGOrder = -1; SGDerivOrder = 1; intervalGiven = 0; yErrorsSeen = 0; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scanned[iArg].list[0], option, CLO_OPTIONS, 0)) { case CLO_DIFFERENTIATE: if (scanned[iArg].n_items!=2 && scanned[iArg].n_items!=3) SDDS_Bomb("invalid -differentiate syntax"); yName = SDDS_Realloc(yName, sizeof(*yName)*(yNames+1)); yErrorName = SDDS_Realloc(yErrorName, sizeof(*yErrorName)*(yNames+1)); yName[yNames] = scanned[iArg].list[1]; if (scanned[iArg].n_items==3) { yErrorsSeen = 1; yErrorName[yNames] = scanned[iArg].list[2]; } else yErrorName[yNames] = NULL; yNames++; break; case CLO_EXCLUDE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&yExcludeName, &yExcludeNames, scanned[iArg].list+1, scanned[iArg].n_items-1); break; case CLO_VERSUS: if (xName) SDDS_Bomb("give -versus only once"); if (scanned[iArg].n_items!=2) SDDS_Bomb("invalid -versus syntax"); xName = scanned[iArg].list[1]; xErrorName = NULL; break; case CLO_MAINTEMPLATE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -mainTemplate syntax"); scanned[iArg].n_items--; if (!scanItemList(&flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "name", SDDS_STRING, mainTemplate+0, 1, 0, "description", SDDS_STRING, mainTemplate+1, 1, 0, "symbol", SDDS_STRING, mainTemplate+2, 1, 0, NULL)) SDDS_Bomb("invalid -mainTemplate syntax"); break; case CLO_ERRORTEMPLATE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -errorTemplate syntax"); scanned[iArg].n_items--; if (!scanItemList(&flags, scanned[iArg].list+1, &scanned[iArg].n_items, 0, "name", SDDS_STRING, errorTemplate+0, 1, 0, "description", SDDS_STRING, errorTemplate+1, 1, 0, "symbol", SDDS_STRING, errorTemplate+2, 1, 0, NULL)) SDDS_Bomb("invalid -errorTemplate syntax"); break; case CLO_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_INTERVAL: if (scanned[iArg].n_items!=2 || sscanf(scanned[iArg].list[1], "%" SCNd32, &interval)!=1 || interval<=0) SDDS_Bomb("invalid -interval syntax/value"); intervalGiven = 1; break; case CLO_SAVITZKYGOLAY: if ((scanned[iArg].n_items!=4 && scanned[iArg].n_items!=5) || sscanf(scanned[iArg].list[1], "%ld", &SGLeft)!=1 || sscanf(scanned[iArg].list[2], "%ld", &SGRight)!=1 || sscanf(scanned[iArg].list[3], "%ld", &SGOrder)!=1 || (scanned[iArg].n_items==5 && sscanf(scanned[iArg].list[4], "%ld", &SGDerivOrder)!=1) || SGLeft<0 || SGRight<0 || (SGLeft+SGRight)<SGOrder || SGOrder<0 || SGDerivOrder<0) SDDS_Bomb("invalid -SavitzkyGolay syntax/values"); break; default: fprintf(stderr, "invalid option seen: %s\n", scanned[iArg].list[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames"); } } if (intervalGiven && SGOrder>=0) SDDS_Bomb("-interval and -SavitzkyGolay options are incompatible"); if (SGOrder>=0 && (xErrorName || yErrorsSeen)) SDDS_Bomb("Savitzky-Golay method does not support errors in data"); processFilenames("sddsderiv", &input, &output, pipeFlags, 0, NULL); if (!yNames) SDDS_Bomb("-differentiate option must be given at least once"); if (!checkErrorNames(yErrorName, yNames)) SDDS_Bomb("either all -differentiate quantities must have errors, or none"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(ptr=SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xName, NULL))) { fprintf(stderr, "error: column %s doesn't exist\n", xName); exit(1); } free(xName); xName = ptr; if (xErrorName) { if (!(ptr=SDDS_FindColumn(&SDDSin, FIND_NUMERIC_TYPE, xErrorName, NULL))) { fprintf(stderr, "error: column %s doesn't exist\n", xErrorName); exit(1); } else { free(xErrorName); xErrorName = ptr; } } if (!(yNames=expandColumnPairNames(&SDDSin, &yName, &yErrorName, yNames, yExcludeName, yExcludeNames, FIND_NUMERIC_TYPE, 0))) { fprintf(stderr, "error: no quantities to differentiate found in file\n"); exit(1); } setupOutputFile(&SDDSout, &SDDSin, output, &yOutputName, &yOutputErrorName, &yOutputUnits, xName, xErrorName, yName, yErrorName, yNames, mainTemplate, errorTemplate, interval, SGOrder>=0?SGDerivOrder:1); while (SDDS_ReadPage(&SDDSin)>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<2) SDDS_Bomb("Can't compute derivatives: too little data."); derivative = SDDS_Realloc(derivative, sizeof(*derivative)*rows); derivativeError = SDDS_Realloc(derivativeError, sizeof(*derivativeError)*rows); derivativePosition = SDDS_Realloc(derivativePosition, sizeof(*derivativePosition)*rows); if (!SDDS_StartPage(&SDDSout, rows) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); xError = NULL; if (!(xData = SDDS_GetColumnInDoubles(&SDDSin, xName)) || (xErrorName && !(xError=SDDS_GetColumnInDoubles(&SDDSin, xErrorName)))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=0; i<yNames; i++) { yError = NULL; if (!(yData = SDDS_GetColumnInDoubles(&SDDSin, yName[i])) || (yErrorName && yErrorName[i] && !(yError=SDDS_GetColumnInDoubles(&SDDSin, yErrorName[i])))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SGOrder>=0) takeSGDerivative(xData, yData, rows, derivative, derivativePosition, SGLeft, SGRight, SGOrder, SGDerivOrder); else takeDerivative(xData, yData, yError, rows, derivative, derivativeError, derivativePosition, interval); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivative, rows, yOutputName[i]) || (yOutputErrorName && yOutputErrorName[i] && !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivativeError, rows, yOutputErrorName[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (yData) free(yData); if (yError) free(yError); yData = yError = NULL; } if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, derivativePosition, rows, xName) || (xErrorName && !SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, xError, rows, xErrorName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (xData) free(xData); if (xError) free(xError); xData = xError = NULL; } if (!SDDS_Terminate(&SDDSin) || !SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (derivative) free(derivative); if (derivativeError) free(derivativeError); if (derivativePosition) free(derivativePosition); return(0); }
long perform_sddsplot_matching(SDDS_TABLE *table, MATCH_DEFINITION **match, long matches) { long i, j, accept, skip_table; long n_rows, row_deletion; MATCH_TERM *match_term; MATCH_DEFINITION *match_ptr; PARAMETER_DEFINITION *pardefptr; static char s[SDDS_MAXLINE]; static int32_t *rowFlag1 = NULL, *rowFlag2 = NULL; static long rowFlags = 0; skip_table = 0; if (matches==0) return !skip_table; n_rows = SDDS_RowCount(table); for (i=0; i<matches; i++) { match_ptr = match[i]; if (match_ptr->is_parameter) { accept = 1; match_term = match_ptr->match_term; for (j=0; j<match_ptr->match_terms; j++) { if (!(pardefptr=SDDS_GetParameterDefinition(table, match_term[j].name)) || !(pardefptr->type==SDDS_STRING || pardefptr->type==SDDS_CHARACTER)) { fprintf(stderr, "error: unknown or numeric parameter %s given for match\n", match_term[j].name); exit(1); } if (pardefptr->type==SDDS_STRING) { char **ppc; ppc = SDDS_GetParameter(table, match_term[j].name, NULL); strcpy_ss(s, *ppc); } else { char *pc; pc = SDDS_GetParameter(table, match_term[j].name, NULL); sprintf(s, "%c", *pc); } accept = SDDS_Logic(accept, wild_match(s, match_term[j].string), match_term[j].logic); } if (!accept) { skip_table = 1; break; } } else if (n_rows) { if (!rowFlag1 || !rowFlag2 || n_rows>rowFlags) { if (!(rowFlag1 = SDDS_Realloc(rowFlag1, sizeof(*rowFlag1)*n_rows)) || !(rowFlag2 = SDDS_Realloc(rowFlag2, sizeof(*rowFlag2)*n_rows))) { fprintf(stderr, "Unable to reallocate row flag arrays (perform_sddsplot_matching)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } if (!SDDS_GetRowFlags(table, rowFlag1, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_matching-1)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } match_term = match_ptr->match_term; for (j=0; j<match_ptr->match_terms; j++) { #if defined(DEBUG) fprintf(stderr, "Matching %s to %s on with logic %x\n", match_term[j].name, match_term[j].string, match_term[j].logic); #endif if (SDDS_MatchRowsOfInterest(table, match_term[j].name, match_term[j].string, match_term[j].logic)<0) { fprintf(stderr, "Error matching rows (perform_sddsplot_matching)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } #if defined(DEBUG) fprintf(stderr, "%ld rows left after match %ld of set %ld\n", SDDS_CountRowsOfInterest(table), j, i); #endif } if (i) { if (!SDDS_GetRowFlags(table, rowFlag2, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_matching-2)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } for (j=0; j<n_rows; j++) { rowFlag1[j] = rowFlag1[j]&rowFlag2[j]; } if (!SDDS_AssertRowFlags(table, SDDS_FLAG_ARRAY, rowFlag1, n_rows)) { fprintf(stderr, "Unable to assert row flags (perform_sddsplot_matching)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } else { if (!SDDS_GetRowFlags(table, rowFlag1, n_rows)) { fprintf(stderr, "Unable to get row flags (perform_sddsplot_matching-3)\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } } row_deletion = 1; } } return !skip_table; }
int main(int argc, char **argv) { int iArg; char **outputColumn, **difColumn; char *indepColumn, **depenColumn, **exclude; long depenColumns, excludes; char *input, *output; long i, rows, readCode, optionCode; unsigned long flags, pipeFlags; SCANNED_ARG *scanned; SDDS_DATASET SDDSin, SDDSout; double *timeData, *inputData, *outputData; FILTER_STAGE *filterStage; long filterStages, totalFilters; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc<3 || argc>(3+N_OPTIONS)) bomb(NULL, USAGE); output = input = NULL; flags = pipeFlags = 0; indepColumn = NULL; depenColumn = exclude = NULL; depenColumns = excludes = 0; if (!(filterStage = (FILTER_STAGE*)calloc(1, sizeof(*filterStage)))) SDDS_Bomb("allocation failure"); filterStage->filter = NULL; filterStage->filters = 0; filterStages = 1; totalFilters = 0; for (iArg=1; iArg<argc; iArg++) { if (scanned[iArg].arg_type==OPTION) { /* process options here */ switch (optionCode=match_string(scanned[iArg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(scanned[iArg].list+1, scanned[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_COLUMNS: if (indepColumn) SDDS_Bomb("only one -columns option may be given"); if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -columns syntax"); indepColumn = scanned[iArg].list[1]; if (scanned[iArg].n_items>=2) { depenColumn = tmalloc(sizeof(*depenColumn)*(depenColumns=scanned[iArg].n_items-2)); for (i=0; i<depenColumns; i++) depenColumn[i] = scanned[iArg].list[i+2]; } break; case SET_THRESHOLD: case SET_HIGHPASS: case SET_LOWPASS: case SET_NOTCH: case SET_BANDPASS: case SET_FILTERFILE: case SET_CLIPFREQ: addFilter(filterStage+filterStages-1, optionCode, scanned+iArg); totalFilters++; break; case SET_CASCADE: if (filterStages==0) SDDS_Bomb("-cascade option precedes all filter definitions"); if (!(filterStage = SDDS_Realloc(filterStage, (filterStages+1)*sizeof(*filterStage)))) SDDS_Bomb("allocation failure"); filterStage[filterStages].filter = NULL; filterStage[filterStages].filters = 0; filterStages++; break; case SET_NEWCOLUMNS: flags |= FL_NEWCOLUMNS; break; case SET_DIFFERENCECOLUMNS: flags |= FL_DIFCOLUMNS; break; case SET_EXCLUDE: if (scanned[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&exclude, &excludes, scanned[iArg].list+1, scanned[iArg].n_items-1); break; default: fprintf(stderr, "error: unknown/ambiguous option: %s (%s)\n", scanned[iArg].list[0], argv[0]); exit(1); break; } } else { if (!input) input = scanned[iArg].list[0]; else if (!output) output = scanned[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsfdfilter", &input, &output, pipeFlags, 0, NULL); if (!totalFilters) fputs("warning: no filters specified (sddsfdfilter)\n", stderr); if (!indepColumn) SDDS_Bomb("supply the independent column name with the -columns option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSin, indepColumn, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OKAY) exit(1); excludes = appendToStringArray(&exclude, excludes, indepColumn); if (!depenColumns) depenColumns = appendToStringArray(&depenColumn, depenColumns, "*"); if ((depenColumns=expandColumnPairNames(&SDDSin, &depenColumn, NULL, depenColumns, exclude, excludes, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("No quantities selected to filter"); } if (!SDDS_InitializeCopy(&SDDSout, &SDDSin, output, "w")) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (flags&FL_NEWCOLUMNS) { outputColumn = tmalloc(sizeof(*outputColumn)*depenColumns); for (i=0; i<depenColumns; i++) { outputColumn[i] = tmalloc(sizeof(**outputColumn)*(strlen(depenColumn[i])+1+strlen("Filtered"))); sprintf(outputColumn[i], "%sFiltered", depenColumn[i]); if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, depenColumn[i], outputColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } else outputColumn=depenColumn; difColumn = NULL; if (flags&FL_DIFCOLUMNS) { difColumn = tmalloc(sizeof(*difColumn)*depenColumns); for (i=0; i<depenColumns; i++) { difColumn[i] = tmalloc(sizeof(**difColumn)*(strlen(depenColumn[i])+1+strlen("Difference"))); sprintf(difColumn[i], "%sDifference", depenColumn[i]); if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, depenColumn[i], difColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_WriteLayout(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); outputData = NULL; while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if (!SDDS_CopyPage(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rows) { if (!(timeData = SDDS_GetColumnInDoubles(&SDDSin, indepColumn))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(outputData = SDDS_Realloc(outputData, sizeof(*outputData)*rows))) SDDS_Bomb("allocation failure"); for (i=0; i<depenColumns; i++) { if (!(inputData = SDDS_GetColumnInDoubles(&SDDSin, depenColumn[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!applyFilters(outputData, inputData, timeData, rows, filterStage, filterStages)) exit(1); if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, outputData, rows, outputColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (flags&FL_DIFCOLUMNS) { long j; for (j=0; j<rows; j++) outputData[j] = inputData[j] - outputData[j]; if (!SDDS_SetColumnFromDoubles(&SDDSout, SDDS_BY_NAME, outputData, rows, difColumn[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(inputData); } free(timeData); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } free(outputData); for(i=0;i<depenColumns;i++) { free(depenColumn[i]); if (flags&FL_NEWCOLUMNS) free(outputColumn[i]); if (flags&FL_DIFCOLUMNS) free(difColumn[i]); } for(i=0;i<excludes;i++) free(exclude[i]); free(indepColumn); if (flags&FL_NEWCOLUMNS) free(outputColumn); free(depenColumn); if (flags&FL_DIFCOLUMNS) free(difColumn); free(exclude); for(i=0;i<filterStages;i++) { long j; for(j=0;j<filterStage[i].filters;j++) { switch (filterStage[i].filter[j].filterType) { case SET_FILTERFILE : free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->freqData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->magData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->imagData); free( ((FILE_FILTER*) (filterStage[i].filter[j].filter))->realData); break; default : break; } } } if (!SDDS_Terminate(&SDDSout) || !SDDS_Terminate(&SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); return 0; }
int main( int argc, char **argv) { SCANNED_ARG *scanned; char *inputfile, *outputfile; SDDS_DATASET twissPage, resultsPage; double particles, charge, length; long verbosity, noWarning, i, elements, superperiods, growthRatesOnly, force; double pCentral0, I1, I2, I3, I4, I5, taux, tauy, taudelta; double EMeV; double emitx0, emitx, emitxInput, emityInput, emity, coupling, sigmaz0, sigmaz; double sigmaDelta0, sigmaDelta, sigmaDeltaInput, xGrowthRate, yGrowthRate, zGrowthRate; double xGrowthRateInitial, yGrowthRateInitial, zGrowthRateInitial; double emitxOld, sigmaDeltaOld; long method, converged; /* used in simplex minimization */ double yReturn, *xGuess, *dxGuess, *xLowerLimit, *xUpperLimit; short *disable; long dimensions = 15, maxEvaluations = 500, maxPasses = 2; double target = 1e-6; int32_t integrationTurns, integrationStepSize; long integrationPoints = 0; double *exInteg=NULL, *eyInteg=NULL, *elInteg=NULL, *xRateInteg=NULL, *yRateInteg=NULL, *zRateInteg=NULL; double *SdeltaInteg=NULL, *SzInteg=NULL; int32_t *passInteg=NULL; unsigned long dummyFlags; double rfVoltage, rfHarmonic; double alphac, U0, circumference, energy; double *xRateVsS, *yRateVsS, *zRateVsS; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scanned, argc, argv); if (argc == 1) bomb(NULL, USAGE); xRateVsS = yRateVsS = zRateVsS = NULL; inputfile = NULL; outputfile = NULL; energy = 0; verbosity = 0; isRing = 1; particles = 0; charge = 0; coupling = emityInput = 0; force = 1; length = 0; superperiods=1; method = 0; emitxInput = 0; sigmaDeltaInput = 0; growthRatesOnly = 0; integrationTurns = 0; rfVoltage = rfHarmonic = 0; noWarning = 0; for (i = 1; i<argc; i++) { if (scanned[i].arg_type == OPTION) { delete_chars(scanned[i].list[0], "_"); switch(match_string(scanned[i].list[0], option, N_OPTIONS, UNIQUE_MATCH)) { case VERBOSE: if(scanned[i].n_items > 1 ) { get_long(&verbosity, scanned[i].list[1]); } else { verbosity=1; } break; case ISRING: if(scanned[i].n_items > 1 ) { get_long(&isRing, scanned[i].list[1]); } else { isRing=1; } break; case CHARGE: get_double(&charge, scanned[i].list[1]); break; case EMITXINPUT: /* This is really the emitx+emity, not emitx */ get_double(&emitxInput, scanned[i].list[1]); break; case EMITINPUT: get_double(&emitxInput, scanned[i].list[1]); break; case DELTAINPUT: get_double(&sigmaDeltaInput, scanned[i].list[1]); break; case LENGTH: get_double(&length, scanned[i].list[1]); length /= 1000; /* convert input length from mm to m */ break; case COUPLING: get_double(&coupling, scanned[i].list[1]); break; case EMITYINPUT: get_double(&emityInput, scanned[i].list[1]); break; case FORCECOUPLING: get_long(&force, scanned[i].list[1]); break; case PARTICLES: get_double(&particles, scanned[i].list[1]); break; case SUPERPERIOD: get_long(&superperiods, scanned[i].list[1]); break; case METHOD: get_long(&method, scanned[i].list[1]); break; case GROWTHRATESONLY: growthRatesOnly = 1; break; case SET_TARGET: if (scanned[i].n_items!=2 || !get_double(&target, scanned[i].list[1]) || target<0) bomb("invalid -target syntax", NULL); break; case RF: if (scanned[i].n_items<2) bomb("invalid -rf syntax", NULL); scanned[i].n_items--; rfVoltage = rfHarmonic = 0; if (!scanItemList(&dummyFlags, scanned[i].list+1, &scanned[i].n_items, 0, "voltage", SDDS_DOUBLE, &rfVoltage, 1, 0, "harmonic", SDDS_DOUBLE, &rfHarmonic, 1, 0, NULL) || rfVoltage<=0 || rfHarmonic<=0) bomb("invalid -rf syntax/values", "-rf=voltage=MV,harmonic=<value>"); break; case SET_ENERGY: if (scanned[i].n_items!=2) bomb("invalid -energy syntax", NULL); if (!sscanf(scanned[i].list[1], "%lf", &energy) || energy<=0) bomb("invalid -energy syntax/values", "-energy=<MeV>"); break; case SET_INTEGRATE: if (scanned[i].n_items<2) bomb("invalid -integrate syntax", NULL); integrationTurns = 0; integrationStepSize = 1; scanned[i].n_items--; if (!scanItemList(&dummyFlags, scanned[i].list+1, &scanned[i].n_items, 0, "turns", SDDS_LONG, &integrationTurns, 1, 0, "stepsize", SDDS_LONG, &integrationStepSize, 1, 0, NULL) || integrationTurns<=0 || integrationStepSize<1) bomb("invalid -integrate syntax", NULL); break; case NO_WARNING: noWarning = 1; break; default: fprintf(stderr, "Unknown option %s given", scanned[i].list[0]); exit(1); break; } } else { if (!inputfile) inputfile = scanned[i].list[0]; else if (!outputfile) outputfile = scanned[i].list[0]; else bomb("too many filenames given", NULL); } } if (charge && particles) { bomb("Options charge and particles cannot be both specified.",NULL); } if (!charge) charge = particles * e_mks; if (!particles) { /* command line input value is in units of nC */ charge /= 1e9; particles = charge/ e_mks; } if ((!coupling && !emityInput) || (coupling && emityInput)) bomb("Give -coupling or -emityInput (but not both)", NULL); if (!length && !rfVoltage) bomb("Specify either the bunch length or the rf voltage.", NULL); if (growthRatesOnly && integrationTurns) { growthRatesOnly = 0; if (!noWarning) fprintf( stdout, "*Warning* -growthRatesOnly option is incompatiable with -integrate option. The -growthRatesOnly will be disabled.\n"); } if (!growthRatesOnly && !integrationTurns && !noWarning) fprintf( stdout, "*Warning* The growth rate contribution columns in the results file will be those calculated from the equilibrium (or final) condition.\n"); if (integrationTurns && !isRing) { integrationTurns = 0; fprintf( stdout, "*Warning* -isRing=0 is incompatiable with -integrate option. The -integrate will be disabled.\n"); } if (energy && !isRing) { energy = 0; fprintf( stdout, "*Warning* you can not scale energy for linac beam. Scaling will be disabled.\n"); } /***************************************************\ * get parameter information from first input file * \***************************************************/ if (verbosity) fprintf( stdout, "Opening \"%s\" for checking presence of parameters.\n", inputfile); if (!SDDS_InitializeInput(&twissPage, inputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* read first page of input file to get parameters I1 I2 I3 I4 I5. Check presence of first radiation integral. */ SDDS_ReadPage(&twissPage); /* parameter Type */ switch(SDDS_CheckParameter(&twissPage, "I1", NULL, SDDS_DOUBLE, verbosity?stdout:NULL)) { case SDDS_CHECK_NONEXISTENT: if (verbosity) fprintf( stdout, "\tParameter I1 not found in input file.\n"); break; case SDDS_CHECK_WRONGTYPE: SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exitElegant(1); break; case SDDS_CHECK_OKAY: break; default: fprintf( stdout, "Unexpected result from SDDS_CheckParameter routine while checking parameter Type.\n"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); exitElegant(1); break; } if (verbosity) fprintf( stdout, "Opening \"%s\" for writing...\n", outputfile); if (!SDDS_InitializeOutput(&resultsPage, SDDS_BINARY, 1, "Intra-beam scattering rates", "Intra-beam scattering rates", outputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I1", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I2", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I3", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I4", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "I5", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "pCentral", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "taux", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "tauy", NULL) || !SDDS_TransferParameterDefinition(&resultsPage, &twissPage, "taudelta", NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (0>SDDS_DefineParameter(&resultsPage, "Superperiods", NULL, NULL, "Superperiods", NULL, SDDS_LONG, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Energy", "E", "MeV", "Total Energy", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Particles", NULL, NULL, "Particles", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "Charge", NULL, "nC", "Charge", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "PeakCurrent", "I$bp$n", "A", "Peak Current", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "RfVoltage", NULL, "MV", "Rf Voltage", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "xGrowthRateInitial", "g$bIBS,x$n", "1/s", "Initial IBS emittance growth rate in the horizontal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "yGrowthRateInitial", "g$bIBS,y$n", "1/s", "Initial IBS emittance growth rate in the vertical plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "zGrowthRateInitial", "g$bIBS,z$n", "1/s", "Initial IBS emittance growth rate in the longitudinal plane", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (0>SDDS_DefineParameter(&resultsPage, "Convergence", NULL, NULL, "Convergence state of emittance calculations", NULL, SDDS_STRING, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitx0", "$ge$r$bx,0$n", "$gp$rm", "Equilibrium horizontal emittance with no coupling and no IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitxInput", "$ge$r$bx,Input$n", "$gp$rm", "Initial horizontal emittance with coupling", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emityInput", "$ge$r$by,Input$n", "$gp$rm", "Initial vertical emittance with coupling", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); /* requested equilibrium emittances or integrate emittances turn-by-turn*/ if (!growthRatesOnly) { if (0>SDDS_DefineParameter(&resultsPage, "xGrowthRate", "g$bIBS,x$n", "1/s", "IBS emittance growth rate in the horizontal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "yGrowthRate", "g$bIBS,y$n", "1/s", "IBS emittance growth rate in the vertical plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "zGrowthRate", "g$bIBS,z$n", "1/s", "IBS emittance growth rate in the longitudinal plane", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emitx", "$ge$r$bx$n", "$gp$rm", "Horizontal emittance with coupling and with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "emity", "$ge$r$by$n", "$gp$rm", "Vertical emittance with coupling and with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaDelta", "$gs$r$bd$n", NULL, "Relative momentum spread with IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaz", "$gs$r$bz$n", "m", "Bunch length with IBS", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (0>SDDS_DefineParameter(&resultsPage, "sigmaDelta0", "$gs$r$bd,0$n", NULL, "Equilibrium relative momentum spread without IBS", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaDeltaInput", "$gs$r$bd,0$n", NULL, "Initial relative momentum spread", NULL, SDDS_DOUBLE, NULL) || 0>SDDS_DefineParameter(&resultsPage, "sigmaz0", "$gs$r$bz,0$n", "m", "Bunch length without IBS", NULL, SDDS_DOUBLE, NULL)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (verbosity) fprintf( stdout, "Opening for reading \"%s\"\n", inputfile); if (!SDDS_InitializeInput(&twissPage, inputfile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (integrationTurns) { if (SDDS_DefineColumn(&resultsPage, "ex", "$ge$r$bx$n", "$gp$rm", "Horizontal Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "ey", "$ge$r$by$n", "$gp$rm", "Vertical Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "el", "$ge$r$bl$n", "s", "Longitudinal Emittance", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Sdelta", "$gs$bd$n$r", "", "Fractional RMS Energy Spread", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Sz", "$gs$r$bz$n", "m", "RMS Bunch Length", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatex", NULL, "1/s", "Horizontal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatey", NULL, "1/s", "Vertical IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "IBSRatel", NULL, "1/s", "Longitudinal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "Pass", NULL, NULL, NULL, NULL, SDDS_LONG, 0)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); integrationPoints = integrationTurns/integrationStepSize+1; if (!(exInteg = SDDS_Malloc(sizeof(*exInteg)*integrationPoints)) || !(eyInteg = SDDS_Malloc(sizeof(*eyInteg)*integrationPoints)) || !(elInteg = SDDS_Malloc(sizeof(*elInteg)*integrationPoints)) || !(SdeltaInteg = SDDS_Malloc(sizeof(*SdeltaInteg)*integrationPoints)) || !(SzInteg = SDDS_Malloc(sizeof(*SzInteg)*integrationPoints)) || !(xRateInteg = SDDS_Malloc(sizeof(*xRateInteg)*integrationPoints)) || !(yRateInteg = SDDS_Malloc(sizeof(*yRateInteg)*integrationPoints)) || !(zRateInteg = SDDS_Malloc(sizeof(*zRateInteg)*integrationPoints)) || !(passInteg = SDDS_Malloc(sizeof(*passInteg)*integrationPoints))) bomb("memory allocation failure (integration arrays)", NULL); } else { if (SDDS_DefineColumn(&resultsPage, "s", NULL, "m", "Position", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatex", NULL, "1/s", "Local Horizontal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatey", NULL, "1/s", "Local Vertical IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0 || SDDS_DefineColumn(&resultsPage, "dIBSRatel", NULL, "1/s", "Local Longitudinal IBS Emittance Growth Rate", NULL, SDDS_DOUBLE, 0)<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&resultsPage) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while(SDDS_ReadPage(&twissPage)>0) { if (!SDDS_GetParameters(&twissPage, "pCentral", &pCentral0, "I1", &I1, "I2", &I2, "I3", &I3, "I4", &I4, "I5", &I5, "taux", &taux, "tauy", &tauy, "taudelta", &taudelta, "alphac", &alphac, "U0", &U0, NULL) ) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); EMeV = sqrt(sqr(pCentral0) + 1) * me_mev; elements = SDDS_CountRowsOfInterest(&twissPage); s = SDDS_GetColumnInDoubles(&twissPage, "s"); pCentral = SDDS_GetColumnInDoubles(&twissPage, "pCentral0"); circumference = s[elements-1]*superperiods; U0 *= superperiods; if (energy!=0) { /* scale to new energy */ pCentral0 = sqrt(sqr(energy/me_mev)-1); taux /= ipow(energy/EMeV, 3); tauy /= ipow(energy/EMeV, 3); taudelta /= ipow(energy/EMeV, 3); U0 *= ipow(energy/EMeV, 4); for (i=0; i<elements; i++) pCentral[i] = pCentral0; EMeV = energy; } if (!length && U0>rfVoltage) bomb("energy loss per turn is greater than rf voltage", NULL); betax = SDDS_GetColumnInDoubles(&twissPage, "betax"); betay = SDDS_GetColumnInDoubles(&twissPage, "betay"); alphax = SDDS_GetColumnInDoubles(&twissPage, "alphax"); alphay = SDDS_GetColumnInDoubles(&twissPage, "alphay"); etax = SDDS_GetColumnInDoubles(&twissPage, "etax"); etaxp = SDDS_GetColumnInDoubles(&twissPage, "etaxp"); etay = SDDS_GetColumnInDoubles(&twissPage, "etay"); etayp = SDDS_GetColumnInDoubles(&twissPage, "etayp"); /* emitx0 and sigmaDelta0 are unperturbed quantities (i.e. no coupling and no IBS) that zibs requires to internally calculate the quantum excitation. (zibs doesn't use the radiation integrals but should!) */ emitx0 = 55.0/ (32.*sqrt(3.)) * hbar_mks * sqr(pCentral0)/ (me_mks * c_mks) * I5 / (I2 - I4); sigmaDelta0 = sqrt(55.0/ (32.*sqrt(3.)) * hbar_mks * sqr(pCentral0)/ (me_mks * c_mks) * I3 / (2 * I2 + I4)); /* use unperturbed quantities in no input supplied. */ if (!sigmaDeltaInput) sigmaDeltaInput = sigmaDelta0; if (!emitxInput) emitxInput = emitx0/ ( 1 + coupling); else /* The emitxInput value is really emit=emitx+emity */ emitxInput = emitxInput/ ( 1 + coupling); if (!emityInput) emityInput = emitxInput * coupling; else coupling = emityInput/emityInput; sigmaDelta = sigmaDeltaInput; if (length) sigmaz0 = length; else { /* compute length in m from rf voltage, energy spread, etc */ sigmaz0 = circumference*sigmaDelta* sqrt(alphac*EMeV/(PIx2*rfHarmonic*sqrt(sqr(rfVoltage)-sqr(U0)))); } sigmaz = sigmaz0; emity = emityInput; emitx = emitxInput; if (integrationPoints) { IBSIntegrate(exInteg, eyInteg, elInteg, passInteg, SdeltaInteg, SzInteg, xRateInteg, yRateInteg, zRateInteg, integrationTurns, integrationStepSize, pCentral0, emitx, emity, sigmaDelta, sigmaz, particles, emitx0, sigmaDelta0, 2./taux, 2./tauy, 2./taudelta, coupling, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, elements, superperiods, verbosity, isRing, force); } else { if (!(xRateVsS = SDDS_Realloc(xRateVsS, sizeof(*xRateVsS)*elements)) || !(yRateVsS = SDDS_Realloc(yRateVsS, sizeof(*yRateVsS)*elements)) || !(zRateVsS = SDDS_Realloc(zRateVsS, sizeof(*zRateVsS)*elements)) ) bomb("memory allocation failure", NULL); } /* This call is to get the initial growth rates for writing to results file. This applies for any running option selected in the commandline */ IBSRate(particles, elements, superperiods, verbosity, isRing, emitx, emity, sigmaDelta, sigmaz, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, NULL, NULL, NULL, &xGrowthRateInitial, &yGrowthRateInitial, &zGrowthRateInitial, 0); /* iterating for equilibrium emittances and final growth rates */ if (!integrationTurns && !growthRatesOnly && isRing) { if (verbosity > 1) { fprintf (stdout, "Starting values:\nemitx: %10.5g sigmaDelta %10.5g.\n", emitx, sigmaDelta); } emitxOld = emitx; sigmaDeltaOld = sigmaDelta; xGuess = (double*) malloc(sizeof(double)*dimensions); dxGuess = (double*) malloc(sizeof(double)*dimensions); xLowerLimit = (double*) malloc(sizeof(double)*dimensions); xUpperLimit = (double*) malloc(sizeof(double)*dimensions); disable = (short*) malloc(sizeof(short)*dimensions); xGuess[0] = MAX(emitx, emitx0/ (1 + coupling)); xGuess[1] = MAX(sigmaDelta, sigmaDelta0); dxGuess[0] = emitx * 0.1; dxGuess[1] = sigmaDelta * 0.1; xLowerLimit[0] = emitx0/ (1 + coupling); xLowerLimit[1] = sigmaDelta0; xUpperLimit[0] = emitx0/ (1 + coupling) * 200; xUpperLimit[1] = MIN(sigmaDelta0 * 100, 1.0); /* assign other variables to array which are not supoosed to be varied by simplex minimization */ xGuess[2] = pCentral0; xGuess[3] = emity; xGuess[4] = sigmaz0; xGuess[5] = particles; xGuess[6] = emitx0; xGuess[7] = sigmaDelta0; xGuess[8] = taux; xGuess[9] = tauy; xGuess[10] = taudelta; xGuess[11] = coupling; xGuess[12] = elements; xGuess[13] = superperiods; xGuess[14] = verbosity; xLowerLimit[2] = pCentral0; xLowerLimit[3] = emity; xLowerLimit[4] = sigmaz0; xLowerLimit[5] = particles; xLowerLimit[6] = emitx0; xLowerLimit[7] = sigmaDelta0; xLowerLimit[8] = taux; xLowerLimit[9] = tauy; xLowerLimit[10] = taudelta; xLowerLimit[11] = coupling; xLowerLimit[12] = elements; xLowerLimit[13] = superperiods; xLowerLimit[14] = verbosity; xUpperLimit[2] = pCentral0; xUpperLimit[3] = emity; xUpperLimit[4] = sigmaz0; xUpperLimit[5] = particles; xUpperLimit[6] = emitx0; xUpperLimit[7] = sigmaDelta0; xUpperLimit[8] = taux; xUpperLimit[9] = tauy; xUpperLimit[10] = taudelta; xUpperLimit[11] = coupling; xUpperLimit[12] = elements; xUpperLimit[13] = superperiods; xUpperLimit[14] = verbosity; disable[0] = 0; disable[1] = 0; for (i=2 ; i<dimensions ; i++) { dxGuess[i] = 0.0; disable[i] = 1; } if (verbosity) { fprintf( stdout, "Doing simplex minimization...\n"); } simplexMin( &yReturn, xGuess, dxGuess, xLowerLimit, xUpperLimit, disable, dimensions, target, target/100.0, IBSequations, verbosity?IBSsimplexReport:NULL, maxEvaluations, maxPasses, 12, 3.0, 1.0, 0); /* final answers */ emitx = xGuess[0]; sigmaDelta = xGuess[1]; emity = emitx * coupling; sigmaz = sigmaz0 * (sigmaDelta/ sigmaDelta0); } /* calculate growth rates contributions at equilibrium or just one time (-growthRateOnly option) */ if (!integrationPoints) { IBSRate(particles, elements, superperiods, verbosity, isRing, emitx, emity, sigmaDelta, sigmaz, s, pCentral, betax, alphax, betay, alphay, etax, etaxp, etay, etayp, xRateVsS, yRateVsS, zRateVsS, &xGrowthRate, &yGrowthRate, &zGrowthRate, 0); } else { /* final growth rates and emittances after integration */ xGrowthRate = xRateInteg[integrationPoints - 1] ; yGrowthRate = yRateInteg[integrationPoints - 1] ; zGrowthRate = zRateInteg[integrationPoints - 1] ; emitx = exInteg[integrationPoints - 1] ; emity = eyInteg[integrationPoints - 1] ; sigmaDelta = SdeltaInteg[integrationPoints - 1] ; sigmaz = SzInteg[integrationPoints - 1] ; } converged = 1; if (0>SDDS_StartPage(&resultsPage, integrationPoints?integrationPoints:elements) || !SDDS_SetParameters(&resultsPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "Convergence", converged?"Emittance converged":"Emittance did not converge", "pCentral", pCentral0, "RfVoltage", rfVoltage, "I1", I1, "I2", I2, "I3", I3, "I4", I4, "I5", I5, "taux", taux, "tauy", tauy, "taudelta", taudelta, "Energy", EMeV, "Particles", particles, "Charge", (1e9 * charge), "PeakCurrent", (charge*c_mks/(sqrt(2*PI)*sigmaz)), "Superperiods", superperiods, "emitx0", emitx0, "emitxInput", emitxInput, "emityInput", emityInput, "xGrowthRateInitial", xGrowthRateInitial, "yGrowthRateInitial", yGrowthRateInitial, "zGrowthRateInitial", zGrowthRateInitial, "sigmaDeltaInput", sigmaDeltaInput, "sigmaDelta0", sigmaDelta0, "sigmaz0", sigmaz0, NULL) || (!growthRatesOnly && !SDDS_SetParameters(&resultsPage, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, "xGrowthRate", xGrowthRate, "yGrowthRate", yGrowthRate, "zGrowthRate", zGrowthRate, "emitx", emitx, "emity", emity, "sigmaDelta", sigmaDelta, "sigmaz", sigmaz, NULL)) || (integrationPoints && (!SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, exInteg, integrationPoints, "ex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, eyInteg, integrationPoints, "ey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, elInteg, integrationPoints, "el") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, SdeltaInteg, integrationPoints, "Sdelta") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, SzInteg, integrationPoints, "Sz") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, xRateInteg, integrationPoints, "IBSRatex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, yRateInteg, integrationPoints, "IBSRatey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, zRateInteg, integrationPoints, "IBSRatel") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, passInteg, integrationPoints, "Pass"))) || (!integrationPoints && (!SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, s, elements, "s") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, xRateVsS, elements, "dIBSRatex") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, yRateVsS, elements, "dIBSRatey") || !SDDS_SetColumn(&resultsPage, SDDS_SET_BY_NAME, zRateVsS, elements, "dIBSRatel"))) || !SDDS_WritePage(&resultsPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&twissPage) || !SDDS_Terminate(&resultsPage)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); return(0); }
int main(int argc, char **argv) { SDDS_DATASET inSet, outSet; SCANNED_ARG *s_arg; long i_arg, pageReturned, rows, row; int32_t *rowFlag; char *input, *output, *columnName, *par_thresholdName; double *data; unsigned long pipeFlags, flags; double threshold, ezoneFraction, changeThreshold; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&s_arg, argc, argv); if (argc<2 || argc>(2+N_OPTIONS)) bomb(NULL, USAGE); flags = pipeFlags = 0; input = output = NULL; columnName = NULL; ezoneFraction = changeThreshold = 0; rowFlag = NULL; par_thresholdName=NULL; for (i_arg=1; i_arg<argc; i_arg++) { if (s_arg[i_arg].arg_type==OPTION) { switch (match_string(s_arg[i_arg].list[0], option, N_OPTIONS, 0)) { case CLO_THRESHOLD: if (s_arg[i_arg].n_items==2) { if (s_arg[i_arg].list[1][0]=='@' ) { SDDS_CopyString(&par_thresholdName, s_arg[i_arg].list[1]+1); flags |= PAR_THRESHOLD; } else { if (sscanf(s_arg[i_arg].list[1], "%lf", &threshold)!=1) SDDS_Bomb("incorrect -threshold syntax"); flags |= THRESHOLD; } } else SDDS_Bomb("incorrect -threshold syntax"); break; case CLO_FIVEPOINT: flags |= FIVEPOINT; break; case CLO_CHANGETHRESHOLD: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%lf", &changeThreshold)!=1 || changeThreshold<=0) SDDS_Bomb("incorrect -changeThreshold syntax or values"); flags |= CHANGETHRES; break; case CLO_PIPE: if (!processPipeOption(s_arg[i_arg].list+1, s_arg[i_arg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case CLO_COLUMN: if (s_arg[i_arg].n_items!=2) SDDS_Bomb("invalid -column syntax"); columnName = s_arg[i_arg].list[1]; break; case CLO_EXCLUSIONZONE: if (s_arg[i_arg].n_items!=2 || sscanf(s_arg[i_arg].list[1], "%lf", &ezoneFraction)!=1 || ezoneFraction<=0) SDDS_Bomb("invalid -exclusionZone syntax or value"); flags |= EZONEFRAC; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", s_arg[i_arg].list[0]); exit(1); break; } } else { if (input==NULL) input = s_arg[i_arg].list[0]; else if (output==NULL) output = s_arg[i_arg].list[0]; else SDDS_Bomb("too many filenames"); } } processFilenames("sddspeakfind", &input, &output, pipeFlags, 0, NULL); if (!columnName) SDDS_Bomb("-column option must be given"); if (!SDDS_InitializeInput(&inSet, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!SDDS_FindColumn(&inSet, FIND_NUMERIC_TYPE, columnName, NULL)) SDDS_Bomb("the given column is nonexistent or nonnumeric"); if (!SDDS_InitializeCopy(&outSet, &inSet, output, "w") || !SDDS_WriteLayout(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); while ((pageReturned=SDDS_ReadPage(&inSet))>0) { if (!SDDS_CopyPage(&outSet, &inSet)) { SDDS_SetError("Problem copying data for output file"); SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if ((rows=SDDS_CountRowsOfInterest(&outSet))>1) { if (!(data = SDDS_GetColumnInDoubles(&inSet, columnName))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); rowFlag = SDDS_Realloc(rowFlag, sizeof(*rowFlag)*rows); for (row=0; row<rows; row++) rowFlag[row] = 0; markPeaks(data, rowFlag, rows, flags&FIVEPOINT); if (flags&PAR_THRESHOLD) { if (!SDDS_GetParameter(&inSet,par_thresholdName,&threshold)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (flags&THRESHOLD || flags&PAR_THRESHOLD ) { for (row=0; row<rows; row++) if (rowFlag[row] && data[row]<threshold) rowFlag[row] = 0; } if (flags&CHANGETHRES) unmarkFlatPeaks(data, rowFlag, rows, changeThreshold, flags&FIVEPOINT); if (flags&EZONEFRAC) unmarkExcludedPeaks(data, rowFlag, rows, ezoneFraction); if (!SDDS_AssertRowFlags(&outSet, SDDS_FLAG_ARRAY, rowFlag, rows)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); free(data); } if (!SDDS_WritePage(&outSet)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&inSet) || !SDDS_Terminate(&outSet)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free_scanargs(&s_arg,argc); if (par_thresholdName) free(par_thresholdName); if (rowFlag) free(rowFlag); return(0); }
int main(int argc, char **argv) { char *input, *output; char **copyColumnName, **usersCopyColumnName; GROUPS *group; int32_t copyColumns; long usersCopyColumns, groups; long iArg, i, rows, readCode, items; unsigned long flags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; SDDS_RegisterProgramName(argv[0]); argc = scanargs(&scArg, argc, argv); if (argc<2) bomb(USAGE, NULL); output = input = NULL; flags = pipeFlags = 0; group = NULL; copyColumnName = usersCopyColumnName = NULL; usersCopyColumns = copyColumns = groups = 0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_GROUP: if ((items = scArg[iArg].n_items-1)<2) SDDS_Bomb("invalid -group syntax"); if (!(group = SDDS_Realloc(group, sizeof(*group)*(groups+1))) || !SDDS_CopyString(&group[groups].newName, scArg[iArg].list[1]) || !(group[groups].usersOldName = SDDS_Malloc(sizeof(*group[groups].usersOldName)* (group[groups].usersOldNames=items-1))) || !SDDS_CopyStringArray(group[groups].usersOldName, scArg[iArg].list+2, group[groups].usersOldNames)) SDDS_Bomb("memory allocation failure"); group[groups].oldName = NULL; group[groups].oldNames = 0; groups++; break; case SET_COPY: if (usersCopyColumns) SDDS_Bomb("give -copy only once"); if ((usersCopyColumns=scArg[iArg].n_items-1)<1) SDDS_Bomb("invalid -copy syntax"); if (!(usersCopyColumnName = SDDS_Malloc(sizeof(*usersCopyColumnName)*usersCopyColumns)) || !SDDS_CopyStringArray(usersCopyColumnName, scArg[iArg].list+1, usersCopyColumns)) SDDS_Bomb("memory allocation failure"); break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } if (groups==0) SDDS_Bomb("no groups defined"); processFilenames("sddsseparate", &input, &output, pipeFlags, 0, NULL); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (usersCopyColumns) { SDDS_SetColumnFlags(&SDDSin, 0); for (i=0; i<usersCopyColumns; i++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, usersCopyColumnName[i], SDDS_OR); if (!(copyColumnName = SDDS_GetColumnNames(&SDDSin, ©Columns)) || copyColumns==0) SDDS_Bomb("no match for copy columns"); } for (i=0; i<groups; i++) { long j, type=0; SDDS_SetColumnFlags(&SDDSin, 0); for (j=0; j<group[i].usersOldNames; j++) SDDS_SetColumnsOfInterest(&SDDSin, SDDS_MATCH_STRING, group[i].usersOldName[j], SDDS_OR); if (!(group[i].oldName = SDDS_GetColumnNames(&SDDSin, &group[i].oldNames))) { fprintf(stderr, "No match for group %s (sddsseparate)\n", group[i].newName); exit(1); } if (i && group[i-1].oldNames!=group[i].oldNames) { fprintf(stderr, "Group %s comprises %" PRId32 " columns, whereas the last group comprises %" PRId32 " (sddsseparate)\n", group[i].newName, group[i].oldNames, group[i-1].oldNames); exit(1); } type = SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[0])); for (j=1; j<group[i].oldNames; j++) { if (type != SDDS_GetColumnType(&SDDSin, SDDS_GetColumnIndex(&SDDSin, group[i].oldName[j]))) { fprintf(stderr, "Inconsistent data types in group %s (sddsseparate)\n", group[i].newName); fprintf(stderr, "First inconsistent column is %s\n", group[i].oldName[j]); exit(1); } } } if (!SDDS_InitializeOutput(&SDDSout, SDDS_BINARY, 0, NULL, NULL, output) || !SDDS_TransferAllParameterDefinitions(&SDDSout, &SDDSin, 0)) SDDS_Bomb("problem initializing output file"); for (i=0; i<copyColumns; i++) if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, copyColumnName[i], NULL)) SDDS_Bomb("problem transferring copy column definitions to output file"); for (i=0; i<groups; i++) { char *name; if (!SDDS_TransferColumnDefinition(&SDDSout, &SDDSin, group[i].oldName[0], group[i].newName)) { fprintf(stderr, "Problem transferring column %s as %s to output file (sddsseparate)\n", group[i].oldName[0], group[i].newName); exit(1); } if (!(group[i].parameterName = SDDS_Malloc(sizeof(*name)*(strlen(group[i].newName)+100)))) SDDS_Bomb("memory allocation failure"); sprintf(group[i].parameterName, "%sSourceColumn", group[i].newName); if (!SDDS_DefineSimpleParameter(&SDDSout, group[i].parameterName, NULL, SDDS_STRING)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WriteLayout(&SDDSout)) SDDS_Bomb("problem writing layout to output file"); while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!rows) continue; for (i=0; i<group[0].oldNames; i++) { long ic, ig; if (!SDDS_StartPage(&SDDSout, rows) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (ic=0; ic<copyColumns; ic++) { void *data; if (!(data = SDDS_GetInternalColumn(&SDDSin, copyColumnName[ic])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, copyColumnName[ic])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } for (ig=0; ig<groups; ig++) { void *data; if (!SDDS_SetParameters(&SDDSout, SDDS_SET_BY_NAME|SDDS_PASS_BY_VALUE, group[ig].parameterName, group[ig].oldName[i], NULL) || !(data = SDDS_GetInternalColumn(&SDDSin, group[ig].oldName[i])) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_NAME, data, rows, group[ig].newName)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } return 0; }
char *makeNameUnitsLabel(PLOT_SPEC *plspec, long panel, long plane, long group) { long set, datasets; PLOT_DATA **dataset; char **symbolUsed, buffer[LABEL_BUFLEN], *ptr; long symbolsUsed, first, maxLabel, unitsIndex, request, unitsDataSet, addUnits; static long warned[2] = { 0,0 } ; long labelLimit = LABEL_BUFLEN-10; dataset = plspec->panel[panel].dataset; datasets = plspec->panel[panel].datasets; symbolsUsed = maxLabel = 0; symbolUsed = NULL; first = addUnits = 1; buffer[0] = 0; unitsDataSet = -1; for (set=0; set<datasets; set++) { if (dataset[set]->scalesGroupIndex[plane]!=group) continue; if (unitsDataSet<0) unitsDataSet = set; request = dataset[set]->request_index; if (plspec->plot_request[request].overlay.flags) continue; if (!first && ((!dataset[set]->info[plane].units && dataset[set-1]->info[plane].units) || (dataset[set]->info[plane].units && !dataset[set-1]->info[plane].units) || (dataset[set]->info[plane].units && dataset[set-1]->info[plane].units && strcmp(dataset[set]->info[plane].units, dataset[set-1]->info[plane].units)!=0))) { if (!warned[plane]) { fprintf(stderr, "Warning: not all %lc quantities have the same units\n", 'x'+plane); warned[plane] = 1; } addUnits = 0; } if (!dataset[set]->info[plane].symbol) { fprintf(stderr, "internal error: info[%ld] symbol is NULL for dataset %ld (panel %ld)\n", plane, set, panel); exit(1); } unitsIndex = set; if (!maxLabel && (first || !symbolsUsed || match_string(dataset[set]->info[plane].symbol, symbolUsed, symbolsUsed, EXACT_MATCH)<0)) { if (!(symbolUsed= SDDS_Realloc(symbolUsed, sizeof(*symbolUsed)*(symbolsUsed+1)))) SDDS_Bomb("Memory allocation failure (makeNameUnitsLabel)"); symbolUsed[symbolsUsed] = dataset[set]->info[plane].symbol; if ((long)(strlen(buffer)+strlen(symbolUsed[symbolsUsed]))<labelLimit) { if (!first) strcat(buffer, ", "); strcat(buffer, symbolUsed[symbolsUsed]); } else { if (first) strncpy(buffer, symbolUsed[symbolsUsed], labelLimit); buffer[labelLimit] = 0; maxLabel = 1; } symbolsUsed++; } first = 0; } if (symbolUsed) free(symbolUsed); if (maxLabel) strcat(buffer, "..."); if (addUnits && unitsDataSet>=0 && dataset[unitsDataSet]->info[plane].units && strlen(dataset[unitsDataSet]->info[plane].units)) { strcat(buffer, " ("); strcat(buffer, dataset[unitsDataSet]->info[plane].units); strcat(buffer, ")"); } if (strlen(buffer)) { if (!SDDS_CopyString(&ptr, buffer)) SDDS_Bomb("String copying problem (makeNameUnitsLabel)"); return ptr; } return NULL; }
void ReadInputFile(char *inputFile, char *xCol, char *yCol, char *zCol, char *stdCol, long *pages, long **rows, double ***x, double ***y, double ***z, double ***std, SDDS_DATASET *SDDS_in) { int32_t rows1=0; long pages0=0, std_exist=0; *rows = NULL; if (!SDDS_InitializeInput(SDDS_in, inputFile)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(SDDS_in, xCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"x column - %s does not exist!\n", xCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, yCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"y column - %s does not exist!\n", yCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, zCol, NULL, SDDS_ANY_NUMERIC_TYPE, stderr)!=SDDS_CHECK_OK) { fprintf(stderr,"z column - %s does not exist!\n", zCol); exit(1); } if (SDDS_CheckColumn(SDDS_in, stdCol, NULL, SDDS_ANY_NUMERIC_TYPE, NULL)==SDDS_CHECK_OK) std_exist = 1; while (SDDS_ReadPage(SDDS_in)>0) { rows1 = 0; rows1=SDDS_CountRowsOfInterest(SDDS_in); if (!rows1) continue; *rows = SDDS_Realloc(*rows, sizeof(**rows)*(pages0+1)); (*rows)[pages0] = rows1; if (!(*x = SDDS_Realloc(*x, sizeof(**x)*(pages0+1))) || !(*y = SDDS_Realloc(*y, sizeof(**y)*(pages0+1))) || !(*z = SDDS_Realloc(*z, sizeof(**z)*(pages0+1)))) { fprintf(stderr, "Memory allocation error.\n"); exit(1); } if (std_exist) { if (!(*std = SDDS_Realloc(*std, sizeof(**std)*(pages0+1)))) { fprintf(stderr, "Memory allocation error.\n"); exit(1); } (*std)[pages0] = NULL; } (*x)[pages0] = (*y)[pages0] = (*z)[pages0] = NULL; if (!((*x)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, xCol)) || !((*y)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, yCol)) || !((*z)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, zCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (std_exist && !((*std)[pages0]=(double*)SDDS_GetColumnInDoubles(SDDS_in, stdCol))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); pages0 ++; } if (!pages0) { fprintf(stderr, "No data found in the input file.\n"); exit(1); } *pages = pages0; return; }
void add_element_links(ELEMENT_LINKS *links, NAMELIST_TEXT *nltext, LINE_LIST *beamline) { long n_links, src_position_code=0, n_targets, n_sources, mode_code=0; long targets, iTarget, j; char **targetList; ELEMENT_LIST *t_context, *s_context, **eptr, *eptr1; double dz_min, dz; #if DEBUG long i; #endif log_entry("add_element_links"); /* set namelist variables to defaults */ target = item = source = equation = exclude = NULL; /* must initialize these hear rather than in the .nl file * to avoid problems with str_tolower() and other operations */ cp_str(&source_position, "before"); cp_str(&mode, "dynamic"); /* process namelist text */ if (processNamelist(&link_elements, nltext)==NAMELIST_ERROR) bombElegant(NULL, NULL); if (target) str_toupper(target); if (exclude) str_toupper(exclude); if (item) str_toupper(item); if (source) str_toupper(source); if (source_position) str_tolower(source_position); else cp_str(&source_position, "nearest"); if (mode) str_tolower(mode); else cp_str(&mode, "dynamic"); if (echoNamelists) print_namelist(stdout, &link_elements); /* check for valid input */ if (!target) bombElegant("link target not named", NULL); if (!item) bombElegant("link item not named", NULL); if (!source) bombElegant("link source not named", NULL); if (!equation) bombElegant("link equation not given", NULL); if (!source_position || (src_position_code=match_string(source_position, src_position_name, N_SRC_POSITIONS, 0))<0) bombElegant("source_position not given/unknown", NULL); if (!mode || (mode_code=match_string(mode, link_mode, N_LINK_MODES, 0))<0) bombElegant("link mode not known", NULL); if (minimum>maximum) bombElegant("minimum>maximum", NULL); t_context = s_context = NULL; if (has_wildcards(target) && strchr(target, '-')) target = expand_ranges(target); if (exclude && strlen(exclude) && has_wildcards(exclude) && strchr(exclude, '-')) exclude = expand_ranges(exclude); if (!(t_context=wfind_element(target, &t_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with target element %s--not in beamline\n", target); fflush(stdout); exitElegant(1); } if (!(s_context=find_element(source, &s_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with source element %s--not in beamline\n", source); fflush(stdout); exitElegant(1); } targets = 0; targetList = NULL; /* make a list of all the unique element names that match this (possibly wildcard) target */ do { int32_t duplic; if (!exclude || !strlen(exclude) || !wild_match(t_context->name, exclude)) { targetList = SDDS_Realloc(targetList, sizeof(*targetList)*(targets+1)); binaryInsert((void**)targetList, targets, t_context->name, strcmp, &duplic); if (!duplic) targets++; } } while ((t_context=wfind_element(target, &t_context, &(beamline->elem)))); if (!targets) bombElegant("cannot make link--no targets found\n", NULL); /* note that targets==1 if all the targets have the same name ! */ for (iTarget=0; iTarget<targets; iTarget++) { n_links = links->n_links; target = targetList[iTarget]; t_context = NULL; t_context = find_element(target, &t_context, &(beamline->elem)); /* expand the arrays */ links->target_name = trealloc(links->target_name, sizeof(*links->target_name)*(n_links+1)); links->target_elem = trealloc(links->target_elem, sizeof(*links->target_elem)*(n_links+1)); links->item = trealloc(links->item, sizeof(*links->item)*(n_links+1)); links->target_param = trealloc(links->target_param, sizeof(*links->target_param)*(n_links+1)); links->source_name = trealloc(links->source_name, sizeof(*links->source_name)*(n_links+1)); links->source_position = trealloc(links->source_position, sizeof(*links->source_position)*(n_links+1)); links->flags = trealloc(links->flags, sizeof(*links->flags)*(n_links+1)); links->source_elem = trealloc(links->source_elem, sizeof(*links->source_elem)*(n_links+1)); links->equation = trealloc(links->equation, sizeof(*links->equation)*(n_links+1)); links->n_targets = trealloc(links->n_targets, sizeof(*links->n_targets)*(n_links+1)); links->initial_value = trealloc(links->initial_value, sizeof(*links->initial_value)*(n_links+1)); links->baseline_value = trealloc(links->baseline_value, sizeof(*links->baseline_value)*(n_links+1)); links->minimum = trealloc(links->minimum, sizeof(*links->minimum)*(n_links+1)); links->maximum = trealloc(links->maximum, sizeof(*links->maximum)*(n_links+1)); /* copy the basic data */ cp_str(links->target_name+n_links, target); cp_str(links->item+n_links, item); cp_str(links->source_name+n_links, source); cp_str(links->equation+n_links, equation); links->source_position[n_links] = src_position_code; links->flags[n_links] = link_mode_flag[mode_code]; links->minimum[n_links] = minimum; links->maximum[n_links] = maximum; /* make the list of pointers to targets */ eptr = tmalloc(sizeof(*eptr)); eptr[0] = t_context; if ((links->target_param[n_links] = confirm_parameter(item, t_context->type))<0) { fprintf(stdout, "error: element %s does not have a parameter %s\n", target, item); fflush(stdout); exitElegant(1); } n_targets = 1; while ((t_context=find_element(target, &t_context, &(beamline->elem)))) { eptr = trealloc(eptr, sizeof(*eptr)*(n_targets+1)); eptr[n_targets] = t_context; n_targets++; } links->baseline_value[n_links] = tmalloc(sizeof(*links->baseline_value[n_links])*n_targets); links->n_targets[n_links] = n_targets; links->target_elem[n_links] = eptr; t_context = links->target_elem[n_links][0]; switch (entity_description[eptr[0]->type].parameter[links->target_param[n_links]].type) { case IS_DOUBLE: links->initial_value[n_links] = *((double*)(eptr[0]->p_elem+entity_description[eptr[0]->type].parameter[links->target_param[n_links]].offset)); break; case IS_LONG: links->initial_value[n_links] = *((long*)(eptr[0]->p_elem+entity_description[eptr[0]->type].parameter[links->target_param[n_links]].offset)); break; default: bombElegant("invalid type of item for target of link", NULL); break; } for (j=0; j<n_targets; j++) links->baseline_value[n_links][j] = links->initial_value[n_links]; /* make the list of pointers to sources */ if (iTarget) { s_context = NULL; if (!(s_context=find_element(source, &s_context, &(beamline->elem)))) { fprintf(stdout, "error: cannot make link with source element %s--not in beamline\n", source); fflush(stdout); exitElegant(1); } } eptr = tmalloc(sizeof(*eptr)*(n_targets)); if (src_position_code==SRC_POSITION_SAME_OCCURENCE) { n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; s_context = NULL; while (find_element(source, &s_context, &(beamline->elem))) { if (s_context->occurence==links->target_elem[n_links][n_sources]->occurence) { eptr1 = s_context; break; } } if (!eptr1) { fprintf(stdout, "error: no %s element is found with the same occurence number as the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_NEAREST) { n_sources = 0; while (n_sources<n_targets) { dz_min = DBL_MAX; eptr1 = NULL; s_context = NULL; while (find_element(source, &s_context, &(beamline->elem))) { if ((dz = fabs(s_context->end_pos-links->target_elem[n_links][n_sources]->end_pos))<dz_min) { eptr1 = s_context; dz_min = dz; } } if (!eptr1) { fprintf(stdout, "error: no %s element is found near the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_ADJACENT) { n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; if ((eptr1=links->target_elem[n_links][n_sources]->pred)) { if (strcmp(eptr1->name, source)!=0) eptr1 = NULL; } if (!eptr1 && (eptr1=links->target_elem[n_links][n_sources]->succ)) { if (strcmp(eptr1->name, source)!=0) eptr1 = NULL; } if (!eptr1) { fprintf(stdout, "error: no %s element is found adjacent to the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; } } else if (src_position_code==SRC_POSITION_BEFORE) { if (links->target_elem[n_links][0]->end_pos<s_context->end_pos) { fprintf(stdout, "error: there is no %s element before the first %s element--can't link as requested\n", source, target); fflush(stdout); exitElegant(1); } eptr[0] = s_context; n_sources = 0; while (n_sources<n_targets) { eptr1 = NULL; do { if (s_context->end_pos<links->target_elem[n_links][n_sources]->end_pos) eptr1 = s_context; else if (s_context->end_pos==links->target_elem[n_links][n_sources]->end_pos) { eptr1 = s_context; break; } else break; } while (find_element(source, &s_context, &(beamline->elem))); if (!eptr1) { fprintf(stdout, "error: no %s element is found before the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = eptr1; s_context = eptr[n_sources-1]; } } else if (src_position_code==SRC_POSITION_AFTER) { if (links->target_elem[n_links][0]->end_pos>=s_context->end_pos) { /* search for first source element after first target element */ while (find_element(source, &s_context, &(beamline->elem))) { if (links->target_elem[n_links][0]->end_pos<s_context->end_pos) break; } if (!s_context) { fprintf(stdout, "error: no %s element after the first %s element--can't link as requested\n", source, target); fflush(stdout); exitElegant(1); } } eptr[0] = s_context; n_sources = 1; while (n_sources<n_targets) { s_context = links->target_elem[n_links][n_sources-1]; while (find_element(source, &s_context, &(beamline->elem))) { if (s_context->end_pos>links->target_elem[n_links][n_sources]->end_pos) break; } if (!s_context) { fprintf(stdout, "error: no %s element is found after the %ld-th %s element--can't link as requested\n", source, n_sources, target); fflush(stdout); exitElegant(1); } eptr[n_sources++] = s_context; } } links->source_elem[n_links] = eptr; #if DEBUG fprintf(stdout, "list of targets and sources:\n"); fflush(stdout); for (i=0; i<n_targets; i++) fprintf(stdout, "%s at z=%em linked to %s at z=%em\n", links->target_elem[n_links][i]->name, links->target_elem[n_links][i]->end_pos, links->source_elem[n_links][i]->name, links->source_elem[n_links][i]->end_pos); fflush(stdout); #endif links->n_links += 1; } log_exit("add_element_links"); }
int main(int argc, char **argv) { char *indepQuantity, **depenQuantity, **exclude, **depenQuantityPair; long depenQuantities, excludes; char *input, *output; long iArg, i, j, rows, readCode, noWarnings, items; long rowsToUse; unsigned long flags, pairFlags, tmpFlags, pipeFlags; SCANNED_ARG *scArg; SDDS_DATASET SDDSin, SDDSout; double *tdata, *data, t0, dt; double fracRMSChangeLimit, fracFreqAccuracyLimit; int32_t frequenciesDesired, maxFrequencies, freqCycleLimit; short truncate; double *frequency, *amplitude=NULL, *phase=NULL, *significance=NULL, *phase1=NULL, *amplitude1=NULL, *significance1=NULL; long frequenciesFound; long *frequencyIndex, *amplitudeIndex, *phaseIndex, *significanceIndex, pairs; long *amplitudeIndex1, *phaseIndex1, *significanceIndex1; SDDS_RegisterProgramName(argv[0]); #ifdef DEBUG if (1) { long code; double x, y; x = 1.1; code = OneDFunctionOptimize(&y, &x, 0.07, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .9; code = OneDFunctionOptimize(&y, &x, 0.15, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); x = .999; code = OneDFunctionOptimize(&y, &x, 0.11, -4, 4, trialFn, 50, 1e-6, 0, 1); fprintf(stderr, "code: %ld x=%e, y=%e\n", code, x, y); exit(0); } #endif argc = scanargs(&scArg, argc, argv); if (argc<3) { fprintf(stderr, "%s%s", USAGE1, USAGE2); exit(1); } output = input = NULL; flags = pipeFlags = excludes = truncate = pairFlags= 0; indepQuantity = NULL; depenQuantity = exclude = depenQuantityPair=NULL; depenQuantities = 0; noWarnings = 0; fracRMSChangeLimit = 0.0; fracFreqAccuracyLimit = 0.00001; frequenciesDesired = 1; maxFrequencies = 4; freqCycleLimit = 100; pairs=0; for (iArg=1; iArg<argc; iArg++) { if (scArg[iArg].arg_type==OPTION) { /* process options here */ switch (match_string(scArg[iArg].list[0], option, N_OPTIONS, 0)) { case SET_TRUNCATE: truncate = 1; break; case SET_PAIR: if (depenQuantities) SDDS_Bomb("Invalid -pair option, the depent-quantity is provided by -column option already."); if (scArg[iArg].n_items!=3) SDDS_Bomb("invalid -pair syntax"); depenQuantity = SDDS_Realloc(depenQuantity, sizeof(*depenQuantity)*(pairs+1)); depenQuantityPair = SDDS_Realloc(depenQuantityPair, sizeof(*depenQuantityPair)*(pairs+1)); depenQuantity[pairs] = scArg[iArg].list[1]; depenQuantityPair[pairs] = scArg[iArg].list[2]; pairs++; break; case SET_COLUMN: if (indepQuantity) SDDS_Bomb("only one -column option may be given"); if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -column syntax"); indepQuantity = scArg[iArg].list[1]; if (scArg[iArg].n_items>=2) { if (pairs) SDDS_Bomb("Invalid -column syntax, the depent-quantity is provided by -pair option already."); depenQuantity = tmalloc(sizeof(*depenQuantity)*(depenQuantities=scArg[iArg].n_items-2)); for (i=0; i<depenQuantities; i++) depenQuantity[i] = scArg[iArg].list[i+2]; } break; case SET_PIPE: if (!processPipeOption(scArg[iArg].list+1, scArg[iArg].n_items-1, &pipeFlags)) SDDS_Bomb("invalid -pipe syntax"); break; case SET_EXCLUDE: if (scArg[iArg].n_items<2) SDDS_Bomb("invalid -exclude syntax"); moveToStringArray(&exclude, &excludes, scArg[iArg].list+1, scArg[iArg].n_items-1); break; case SET_NOWARNINGS: noWarnings = 1; break; case SET_TERM_SEARCH: items = scArg[iArg].n_items-1; flags &= ~(NAFF_RMS_CHANGE_LIMIT|NAFF_FREQS_DESIRED|NAFF_MAX_FREQUENCIES); fracRMSChangeLimit = 0; frequenciesDesired = 0; maxFrequencies = 10; if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "changelimit", SDDS_DOUBLE, &fracRMSChangeLimit, 1, NAFF_RMS_CHANGE_LIMIT, "maxfrequencies", SDDS_LONG, &maxFrequencies, 1, NAFF_MAX_FREQUENCIES, "frequencies", SDDS_LONG, &frequenciesDesired, 1, NAFF_FREQS_DESIRED, NULL) || (tmpFlags&NAFF_RMS_CHANGE_LIMIT && tmpFlags&NAFF_FREQS_DESIRED) || maxFrequencies<1) SDDS_Bomb("invalid -terminateSearch syntax"); flags |= tmpFlags; if (frequenciesDesired) maxFrequencies = frequenciesDesired; break; case SET_ITERATE_FREQ: items = scArg[iArg].n_items - 1; flags &= ~(NAFF_FREQ_CYCLE_LIMIT|NAFF_FREQ_ACCURACY_LIMIT); if (!scanItemList(&tmpFlags, scArg[iArg].list+1, &items, 0, "cyclelimit", SDDS_LONG, &freqCycleLimit, 1, NAFF_FREQ_CYCLE_LIMIT, "accuracylimit", SDDS_DOUBLE, &fracFreqAccuracyLimit, 1, NAFF_FREQ_ACCURACY_LIMIT, NULL) || !bitsSet(tmpFlags) || freqCycleLimit<2) SDDS_Bomb("invalid -iterateFrequency syntax"); flags |= tmpFlags; break; default: fprintf(stderr, "error: unknown/ambiguous option: %s\n", scArg[iArg].list[0]); exit(1); break; } } else { if (!input) input = scArg[iArg].list[0]; else if (!output) output = scArg[iArg].list[0]; else SDDS_Bomb("too many filenames seen"); } } processFilenames("sddsnaff", &input, &output, pipeFlags, 0, NULL); if (!indepQuantity) SDDS_Bomb("supply the independent quantity name with the -columns option"); if (!SDDS_InitializeInput(&SDDSin, input)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (SDDS_CheckColumn(&SDDSin, indepQuantity, NULL, SDDS_ANY_NUMERIC_TYPE, stderr) !=SDDS_CHECK_OKAY) exit(1); excludes = appendToStringArray(&exclude, excludes, indepQuantity); if (pairs) { pairFlags = flags | NAFF_FREQ_FOUND; depenQuantities = pairs; } if (!depenQuantities) depenQuantities = appendToStringArray(&depenQuantity, depenQuantities, "*"); if (!pairs) { if ((depenQuantities=expandColumnPairNames(&SDDSin, &depenQuantity, NULL, depenQuantities, exclude, excludes, FIND_NUMERIC_TYPE, 0))<=0) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); SDDS_Bomb("No quantities selected to fft"); } } if (!SetupNAFFOutput(&SDDSout, output, &SDDSin, indepQuantity, depenQuantities, depenQuantity, &frequencyIndex, &litudeIndex, &phaseIndex, &significanceIndex, depenQuantityPair,&litudeIndex1, &phaseIndex1,&significanceIndex1)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(frequency = SDDS_Malloc(sizeof(*frequency)*maxFrequencies)) || !(amplitude = SDDS_Malloc(sizeof(*amplitude)*maxFrequencies)) || !(phase = SDDS_Malloc(sizeof(*phase )*maxFrequencies)) || !(significance= SDDS_Malloc(sizeof(*significance)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); if (pairs) { if (!(amplitude1 = SDDS_Malloc(sizeof(*amplitude1)*maxFrequencies)) || !(phase1 = SDDS_Malloc(sizeof(*phase1 )*maxFrequencies)) || !(significance1 = SDDS_Malloc(sizeof(*significance1)*maxFrequencies))) SDDS_Bomb("memory allocation failure"); } while ((readCode=SDDS_ReadPage(&SDDSin))>0) { if ((rows = SDDS_CountRowsOfInterest(&SDDSin))<0) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (rows) { long primeRows; rowsToUse = rows; primeRows = greatestProductOfSmallPrimes(rows); if (rows!=primeRows) { if (truncate) rowsToUse = greatestProductOfSmallPrimes(rows); else if (largest_prime_factor(rows)>100 && !noWarnings) fputs("Warning: number of points has large prime factors.\nThis could take a very long time.\nConsider using the -truncate option.\n", stderr); } if (!SDDS_StartPage(&SDDSout, maxFrequencies) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (!(tdata = SDDS_GetColumnInDoubles(&SDDSin, indepQuantity))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (i=1; i<rowsToUse; i++) if (tdata[i]<=tdata[i-1]) SDDS_Bomb("independent data is not monotonically increasing"); dt = (tdata[rowsToUse-1]-tdata[0])/(rowsToUse-1.0), t0 = tdata[0]; free(tdata); for (i=0; i<depenQuantities; i++) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantity[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); for (j=0; j<maxFrequencies; j++) frequency[j] = amplitude[j] = phase[j] = significance[j] = -1; frequenciesFound = PerformNAFF(frequency, amplitude, phase, significance, t0, dt, data, rowsToUse, flags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); #ifdef DEBUG fprintf(stderr, "Column %s: ", depenQuantity[i]); fprintf(stderr, "f=%10.3e a=%10.3e p=%10.3e s=%10.3e\n", frequency[0], amplitude[0], phase[0], significance[0]); #endif free(data); data = NULL; if (pairs) { if (!(data = SDDS_GetColumnInDoubles(&SDDSin, depenQuantityPair[i]))) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); frequenciesFound = PerformNAFF(frequency, amplitude1, phase1, significance1, t0, dt, data, rowsToUse, pairFlags, fracRMSChangeLimit, maxFrequencies, freqCycleLimit, fracFreqAccuracyLimit, 0, 0); for (j=0; j<maxFrequencies; j++) if (frequency[j]!=-1) frequency[j] = adjustFrequencyHalfPlane(frequency[j], phase[j], phase1[j], dt); free(data); data = NULL; } if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, frequency, maxFrequencies, frequencyIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude, maxFrequencies, amplitudeIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase, maxFrequencies, phaseIndex[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance, maxFrequencies, significanceIndex[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); if (pairs) { if (!SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, amplitude1, maxFrequencies, amplitudeIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, phase1, maxFrequencies, phaseIndex1[i]) || !SDDS_SetColumn(&SDDSout, SDDS_SET_BY_INDEX, significance1, maxFrequencies, significanceIndex1[i])) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } } } else { if (!SDDS_StartPage(&SDDSout, 0) || !SDDS_CopyParameters(&SDDSout, &SDDSin)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_WritePage(&SDDSout)) SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors|SDDS_EXIT_PrintErrors); } if (!SDDS_Terminate(&SDDSin)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } if (!SDDS_Terminate(&SDDSout)) { SDDS_PrintErrors(stderr, SDDS_VERBOSE_PrintErrors); exit(1); } free(frequency); free(amplitude); free(phase); free(significance); if (pairs) { free(amplitude1); free(phase1); free(significance1); free(amplitudeIndex1); free(phaseIndex1); free(significanceIndex1); free(depenQuantityPair); } free(depenQuantity); free(frequencyIndex); free(amplitudeIndex); free(phaseIndex); free(significanceIndex); return 0; }