Close This Window

Please download official ILL logos here

 

For using on the web or on a screenFor printing in high resolutionWhite version, for dark backgrounds

Download PNG

Download AI

Download white PNG

Download JPG

 

Download white AI

SEQFIT

The Computing for Science (CS) group supports ILL scientists, students and visitors in a number of activities including data analysis, instrument simulation and sample simulation.

Back to ILL Homepage
English French Deutsch 

All Software

SEQFIT - Systematically fitting sequences of data

R. Ghosh, version 2.5, April 2008


Summary - fitfun version 7 onwards


Sequence fitting is included in the newer fitfun library as a standard opton (M).  Where only

a filename is necessary for identifying input data clcickfit includes a file browser to build a list

of files to be treated.  Where a more complex input control is required the user must supply

a control file which tells the program whether to update parameters after each fit (or use

the starting set repeatedly), and give the name of an output (spreadsheet compatible) file,

and then the list of input data for each sequence to be attempted .



Summary - fitfun versions 5-6

SEQFIT is included in and extends the fitfun library with a preprocessor stage which allows starting fit parameters to be initialised and tested on a typical data set using standard commands. It then allows these parameters to be used for a sequence of data, either systematically updating the parameter set, or using the same initial starting values.

Identical with the standard fitfun the programmer has to provide the main calling program which sets up parameter names etc., and must provide routines for reading data and performing the calculation of the model. In addition, here, it is necessary to provide a second read routine, which should not have any dialogue components, and which can use one or two indices to select sequences of data in ascending or descending order (e.g. as run number and subspectrum sequences).

After fitting the sequence of data a routine FTSQOP is called (a dummy routine is included in the library). By providing a personalised replacement the resultant fit information can be written out in a selective manner appropriate to the problem under insvestigation, and can write a data file read for integratio in a spread-sheet analysis program.

These notes should be read in conjunction with the main notes on fitfun.

Analysing sequences

As for the second fitfun layer the seqfit commands consist of one letter which may be followed by one or several numbers. Initially a manual fit must be performed so the typical sequence of use is shown below:

F
Drops down to the standard fitfun routine level. Here one set of data must be read in and fitted. Then on exiting from this level the parameters are available fo start the fitting sequence.

T run1 run2 sub1 sub2 [max-iterations]
U run1 run2 sub1 sub2 [max-iterations]
The T command will fit all data sequentially using the same starting parammeters. The U updates the parameters with those from the last fit. The run and subrun mnumbers may designate either an increasing or decreasing sequence. A maximum of 4000 fits can be performed in a 1 or 2 dimensional set of data designated by the run and subrun numbers. Normally the iteration limit of 100 is used by default.

L par1 par2 par3 par4..
After fitting the results for the sets can be shown by selecting the parameter numbers; up to 5 parameters and their standard deviations can be listed at each L command, and results are also sent to a listing file. Included too is the fit index, which is 0 when no problems have occurred, 1 when no minimum has been found (and the deviations are not reliable), and 2 when the iteration limit has been reached before them minimum.

D 1 (or 2) parameter-number
P 1 (or 2) parameter-number
D gives a summary plot of the selected parameter as a function of run number (1), or subrun number (2) on the screen (more explicitly the device defined in the environment variable PGPLOT_ILL_DEV_1). P outputs the results to a PostScript file (device defined in PGPLOT_ILL_DEV_2) A text string
This adds the text string as annotation to the displayed data.
?
This command lists the numbers and names of parameters which have been fitted.
H lists a summary of the sequence fitting commands.

Call to the fitting routine

        CALL FTFNSQ (PNAM, READone, READn, CALSUB)

Input

PNAM character*4 four characters to be used as the program
name to identify the parameter file "PNAM.ffn"
and any listing file as "PNAMnn.lis"

READone the name of a user supplied routine defined in
a Fortran EXTERNAL statement. This subroutine
will be called after each R command in the
fitfun level to read-in
fresh data. Note COMMONs private to the MAIN
and READIN may also be used to store useful
data for calculations.

READn the name of a user supplied routine defined in
a Fortran EXTERNAL statement. This subroutine
will be called successively after each T or U
command to read in fresh data.
Note COMMONs private to the MAIN, READone
and READn may also be used to store useful
data for calculations. There should be no
requests for control input from the user in
this version of the data read-in routine.

CALSUB the name of a user supplied routine defined in a
Fortran EXTERNAL statement. This routine is
called repeatedly during the fitting procedure
to recalculate the model as the parameters are
varied. As indicated above COMMONs may also
hold local data values.

Routines to be provided by the programmer

Examples in appendix 1

MAIN
This serves primarily to provide titles for the parameters to be used by the fitting routine. The program name PNAM is used to identify the file "PNAM.ffn" containing saved parameters. It should therefore only contain alphabetic or numeric characters. It is also used to generate a systematic listing file name PNAMmm.lis; hence it is useful to use lowercase letters for PNAM in a Unix environment. The remaining text fields label the plotting scales and individual variables. The workspace is set in COMMON/WORK

READone

CALL READone (NPNTS, XOBS, YOBS, YERR, TEXT)

READone returns
NPNTS integer*4 the number of points read-in
XOBS) real*4 arrays of spectrum to be fitted
YOBS) observed values, and errors
YERR)
TEXT character*50 an identifying title
The data arrays are not modified by the fitting routines; subsets of points can be selected ("O" or Only command in the manual fitting stage) and this copy is fed into the fitting routines. Note READone may have questions within it to select a part of a larger data-file, and it may also load up private COMMONs to pass other data, e.g. resolution function arrays to the calculation subroutine. Unless the standard library is recompiled the data arrays XOBS,YOBS,YERR should not exceed 2100 elements each.

READn

CALL READn (IER, IS,IR, NPNTS, XOBS, YOBS, YERR)
IER OUT 0 data read successfully, otherwise non-zero
IR IN integer*4 Run number to be used to identify data source
IS IN integer*4 Run sub-number to identify data source
NPNTS OUT integer*4 the number of points read in
XOBS) OUT real*4 arrays of spectrum to be fitted
YOBS) OUT observed values, and errors
YERR) OUT
With the present library the data arrays should not exceed 2100 elements each. The routine should not read at all from the terminal, since this would disrupt the fitting sequence.

CALSUB


CALL CALSUB( NPAR,PARM,NFIT,XUSE,YUSE,YRUSE,YCALC,F)

Input
NPAR integer*4 the number of parameters named in MAIN
PARM real*4 array of parameters for model
NFIT integer*4 number of data to fit
XUSE,YUSE, YRUSE real*4 arrays of preselected data, x values, y
and errors in y.

Returned
YCALC real*4 array of calculated function at each XUSE value
F real*4 array to be minimised at each XUSE value

The array F will be examined by the fitting routine after each iteration to find a set of paramters which will minimise the SUM(F(x)**2). This array might be simply YCALC-YUSE, a function thereof, or the errors may be introduced as a pre-weighting by the programmer. Additional data may be introduce via private COMMON blocks. The YCALC array is used specifically for displaying the function for direct comparison with the input data. Internal arrays in the calculation routines should permit 200 points to be calculated (for display and output purposes). If errors are available (non-zero) then CHI-squared is calculated.

The parameters can also be used as a simple way of choosing a type of model to be used, by testing it within CALSUB, though clearly the step should then be set always at 0.0!

FTSQOP

The programmer can include his own version of this routine to replace the dummy in the library. The routine is called after each treatment sequence. A template follows:

      SUBROUTINE FTSQOP
c***** subroutine template which could be used to write selected
c output on i/o channel LOUT
parameter (maxpar=40)
parameter (maxpat=4000)
COMMON/SSOUTCC/LOUT
COMMON/SSOUTCH/FNAM
COMMON/SSUSED/KEEP
common/seqtxt/text
common /found/ tab(maxpat,maxpar),etab(maxpat,maxpar),
1idxx(maxpat,3),nsq
character*50 text
character*11 fnam
C text character*50 generic title
C tab real parameters from each fit
C etab real deviations from each fit
C idxx integer run,subrun,fit triplets
C nseq integer number of fits
C LOUT integer i/o stream for file FNAM
C FNAM character*11 output file PROGnnn.sls
C KEEP integer must be changed to 1 if
C FNAM is not be be deleted
C***** users code here
C
C
C***** if data have been written on unit LOUT then set KEEP,
C otherwise the empty result file will be deleted
KEEP=1
RETURN
END
Use of Fortran Unit numbers

Units 1 and 2 are used for internal file-handling.
Unit 16 is used for the listing file output.
Units 5,6 are used for terminal input and output respectively.
Unit 19 is opened as an output file name PROGnnn.sls

Appendix I - Example MAIN
      program sasfitn
c**** Fits up to 4 peaks to SANS data
c f77 -o sasfitn sasfitn.f calskp.f calskpx.f sasin.f sasinn.f \
c libfitfun.a librlib.a libpgplot.a -lX11
c note version of calskp has background at centre of first peak
c (parameter 8), and Gaussian then Lorentzian....
EXTERNAL SASINN,SASIN,CALskp
C calskp calculates sum of up to four peaks
COMMON/WORK/W(160000)
COMMON/TITLES/NAMES(40),TX,TY
COMMON/TITLEP/NPARAS
COMMON/VERSION/VERP
COMMON/TTYIO/INTY,INTTY,IERT
COMMON/FCLOSE/ICLOSE,ISTART
COMMON/PROGCH/PNAMC
CHARACTER*8 NAMES
CHARACTER*4 PNAM,PNAMC
CHARACTER*20 TX,TY
DATA NAMES/ 'FLAT BGD','slope BG','(spare)','FACTOR F',
1'P1 type ','P1 area ','P1 width','P1 posn ','P1 modif',
1'P2 type ','P2 area ','P2 width','P2 posn ','P2 modif',
1'P3 type ','P3 area ','P3 width','P3 posn ','P3 modif',
1'P4 type ','P4 area ','P4 width','P4 posn ','P4 modif',
116*' '/
DATA TX /'Q A-1'/
DATA TY /'Intensity'/
INTY=5
INTTY=6
NPARAS=24
VERP=3.0
WRITE(6,1)
1 FORMAT(/' Fits sequences of up to 4 peaks in SANS datasets '/
1//' Manual peak fitting and parameter initialisation'//
1' Peak type :'/
1' 1 Single Gaussian'/
1' 2 Single Lorenztian'/
1' 3 Voigt - Lorenztian width shown'/
1' 4 Skewed Gaussian'/
1' Modifier parameter for Voigt is Gaussian width '/
1' Modifier parameter for Skewed Gaussian is LHS-width/RHS'/
1' Width values are half width at half height'/
1' Background is value at centre of first peak'/)
PNAM='fit4'
PNAMC=PNAM
C***** OPEN DATA FILES
ICLOSE=0
c***** plotting is closed only here, not by fitfun routines
CALL FTFNSQ(PNAM,SASIN,SASINN,CALskp)
CALL LPENDF
C***** CLOSE ANY LISTING FILE...
CALL SSEND
C***** CLOSE ANY Spreadsheet FILE...
CALL PGNEND(1)
CALL PGNEND(2)
C***** CLOSE OPEN GRAPHICS
end
READn Routine
      subroutine sasinn(ier,ir,is,npnts,xobs,yobs,yerr)
c***** reads in a SANS regrouped spectrum identified by ir and is
parameter (maxdat=2100)
real xobs(maxdat),yobs(maxdat),yrobs(maxdat),yer(maxdat)
real bl(40)
COMMON/PROGCH/PNAMC
character txt*50,pnamc*4
npnts=0
irun=ir
iext=is
call rgio(ier,'R',irun,iext,txt,bl,npnts,
1xobs,yobs,yer,pnamc)
return
end
FTSQOP Routine
      subroutine ftsqop
C***** Output routine to write out peak parameters
parameter (maxpar=40)
parameter (maxpat=4000)
common/seqtxt/text
common /found/ tab(maxpat,maxpar),etab(maxpat,maxpar),
1idxx(maxpat,3),nseq
COMMON/SSOUTCC/LOUT
COMMON/SSOUTCH/FNAM
COMMON/SSUSED/KEEP
real parm(maxpar),sdev(maxpar)
real back(4),backerr(4),ratio(4),raterr(4)
character*50 text
character*11 FNAM
write(6,106)
write(16,106)
106 format(' run ext Backgr % Area % Width %',
1' Position % B-Slope B-Con. fit ')
do 900 k=1,nseq

do n=1,maxpar
parm(n)=tab(k,n)
sdev(n)=etab(k,n)
end do
ir=idxx(k,1)
is=idxx(k,2)
ifited=idxx(k,3)
jjjj = 0
c****** for each peak found then:
do i=5,20,5
jjjj = jjjj + 1
back(jjjj) = 0.0
backerr(jjjj) = 0.0
ratio(jjjj) = 0.0
raterr(jjjj) = 0.0
if(parm(i).gt.0) then
C***** Try to output bgd at centre of each peak as first data column
back(jjjj) = parm(1) + parm(2)*(parm(i+3)-parm(8))
C***** Let's try to combine errors for the background
bslop = parm(2) * parm(i+3)
err1sq = (sdev(1) * parm(1) / 100.0)**2
err2sq = (sdev(2)**2 + sdev(i+3)**2)
1 * bslop*bslop/10000.0
backerr(jjjj) = sqrt(err1sq+err2sq) * 100.0 / abs(back(jjjj))
write(16,20) ir,is,back(jjjj),backerr(jjjj),
1 parm(i+1),sdev(i+1),
1 parm(i+2),sdev(i+2),parm(i+3),sdev(i+3),
1 parm(2),parm(1),iprint
write(6,20) ir,is,back(jjjj),backerr(jjjj),
1 parm(i+1),sdev(i+1),
1 parm(i+2),sdev(i+2),parm(i+3),sdev(i+3),
1 parm(2),parm(1),ifited
20 format(i7,i5,f10.5,f6.1,f10.1,f6.2,f7.4,f6.2,
1 f8.5,f6.2,f9.3,f9.3,i3)
If ( back(jjjj) .ne. 0. ) ratio(jjjj) = parm(i+1)/back(jjjj)
C**** This is an error in counts not per cent
raterr(jjjj) = sqrt(backerr(jjjj)*backerr(jjjj) + sdev(i+1)
1 *sdev(i+1) ) * ratio(jjjj)/100.
end if
end do
C**** the parameters are finally written out in a file with
C**** fixed format
WRITE(LOUT,201)ir,is,(parm(i),sdev(i),i=1,24),
1 (ratio(j),raterr(j),j=1,4)
201 FORMAT(I6,I4,48(2x,E12.4),8(2x,E12.4))

900 continue
KEEP=1
return
end