include include include include include "nlfit.h" include "colnames.h" include "curfitdef.h" define LINEAR 1 # type of database define NON_LINEAR 2 # CONVERT -- Convert between old database format and SDAS table format. # For each database file in the input list, a new table with the same # name and .tab extension will be created. procedure t_convert () char list[SZ_LINE] char database[SZ_PATHNAME] int imt int i, j, len int imtopen(), imtgetim(), imtlen() begin # Expand the input list call clgstr ("input", list, SZ_LINE) imt = imtopen (list) len = imtlen (imt) # Convert each database do i = 1, len { j = imtgetim (imt, database, SZ_PATHNAME) call do_conversion (database) } call imtclose (imt) end # DO_CONVERSION -- Converts one database to SDAS table format. # Old entry will become new file name. Old creation time will # be lost; time stamps will reflect the table creation date and time. procedure do_conversion (database) char database[ARB] pointer dt, tb, colptr pointer coeff, cerror char function[SZ_LINE], unit[SZ_LINE], str[SZ_LINE], root[SZ_FNAME] int i, row, ncoef, npts, tnpar, type long time real rms, baseline, xmin, xmax pointer dtmap(), opendb() int dtgeti(), tbpsta(), fnroot() long clktime() real dtgetr() bool streq() begin # Open database and table. dt = dtmap (database, READ_ONLY) i = fnroot (database, root, SZ_FNAME) tb = opendb (root) do row = 1, DT_NRECS(dt) { # get number of function coefficients ncoef = dtgeti( dt, row, "ninfo" ) - 4 # gets function type iferr { call dtgstr ( dt, row, "func", function, SZ_LINE ) } then { type = LINEAR } else { type = NON_LINEAR } switch (type) { case NON_LINEAR: # re-find number of function coefficients if ( (streq( function, "Gaussians" )) || (streq( function, "cgauss" ))) ncoef = ncoef - 1 # baseline ncoef = ncoef / 2 # coeffs and errors # allocate space for function coefficients and errors call malloc ( coeff, ncoef, TY_REAL ) call malloc ( cerror, ncoef, TY_REAL) # read physical units, npts, rms and Gaussian baseline call dtgstr ( dt, row, "unit", unit, SZ_LINE ) npts = dtgeti (dt, row, "npts") rms = dtgetr (dt, row, "rms") xmin = INDEF xmax = INDEF if ( (streq( function, "Gaussians" )) || (streq( function, "cgauss" ))) baseline = dtgetr ( dt, row, "baseli") # read coefficients and errors do i = 1, ncoef { call sprintf( str, SZ_LINE, "coeff%d") call pargi( i) Memr[coeff+i-1] = dtgetr( dt, row, str ) call sprintf( str, SZ_LINE, "error%d") call pargi( i) Memr[cerror+i-1] = dtgetr( dt, row, str ) } # modify Gaussians coefficient array mapping if ( (streq( function, "Gaussians" )) || (streq( function, "cgauss" ))) { ncoef = ncoef + 2 call realloc ( coeff, ncoef, TY_REAL ) call realloc ( cerror, ncoef, TY_REAL) do i = ncoef, 3, -1 { Memr[coeff+i-1] = Memr[coeff+i-3] Memr[cerror+i-1] = Memr[cerror+i-3] } Memr[coeff] = baseline Memr[coeff+1] = 0. Memr[cerror] = 0. Memr[cerror+1] = 0. } case LINEAR: call strcpy ("*", unit, SZ_LINE) rms = INDEF # allocate space for function coefficients and errors call malloc ( coeff, ncoef, TY_REAL ) call malloc ( cerror, ncoef, TY_REAL ) # read curve type and npts i = int (dtgetr ( dt, row, "coeff1")) call extnstr (FUNCTIONS, i, function) npts = int (dtgetr (dt, row, "coeff4")) # read coefficients xmin = dtgetr( dt, row, "coeff3" ) xmax = dtgetr( dt, row, "coeff4" ) do i = 1, ncoef { call sprintf( str, SZ_LINE, "coeff%d") call pargi (i+4) Memr[coeff+i-1] = dtgetr( dt, row, str ) Memr[cerror+i-1] = INDEF } } # if necessary, increase number of columns in table tnpar = (tbpsta (tb, TBL_NCOLS) - MIN_COLS) / 2 if (tnpar < ncoef) { do i = tnpar + 1, ncoef { call sprintf (str, SZ_LINE, DB_CCOEF) call pargi (i) call tbcdef (tb, colptr, str, DB_UCOEF, DB_FCOEF, TY_REAL, 1, 1) call sprintf (str, SZ_LINE, DB_CERR) call pargi (i) call tbcdef (tb, colptr, str, DB_UERR, DB_FERR, TY_REAL, 1, 1) } } # output call tbcfnd (tb, DB_CFILE, colptr, 1) # file name call tbeptt (tb, colptr, row, DT_NAME(dt, row)) time = clktime (0) # time call cnvtime (time, str, SZ_LINE) call tbcfnd (tb, DB_CTIME, colptr, 1) call tbeptt (tb, colptr, row, str) call tbcfnd (tb, DB_CFUNC, colptr, 1) # function call tbeptt (tb, colptr, row, function) call tbcfnd (tb, DB_CUNIT, colptr, 1) # unit call tbeptt (tb, colptr, row, unit) call tbcfnd (tb, DB_CDEGR, colptr, 1) # no. of coefficients call tbepti (tb, colptr, row, ncoef) call tbcfnd (tb, DB_CNPTS, colptr, 1) # npts call tbepti (tb, colptr, row, npts) call tbcfnd (tb, DB_CRMS, colptr, 1) # rms call tbeptr (tb, colptr, row, rms) call tbcfnd (tb, DB_CXMIN, colptr, 1) # xmin call tbeptr (tb, colptr, row, xmin) call tbcfnd (tb, DB_CXMAX, colptr, 1) # xmax call tbeptr (tb, colptr, row, xmax) do i = 1, ncoef { # coefficients call sprintf (str, SZ_LINE, DB_CCOEF) call pargi (i) call tbcfnd (tb, str, colptr, 1) call tbeptr (tb, colptr, row, Memr[coeff+i-1]) } do i = 1, ncoef { # errors call sprintf (str, SZ_LINE, DB_CERR) call pargi (i) call tbcfnd (tb, str, colptr, 1) call tbeptr (tb, colptr, row, Memr[cerror+i-1]) } call mfree ( coeff, TY_REAL ) call mfree ( cerror,TY_REAL ) } call dtunmap (dt) call tbtclo (tb) end