Page 1 of 1

Problem in con2stn?

Posted: January 20th, 2014, 9:17 pm
by Eslinger
I recently had a problem with abnormal program termination while running the utility program con2stn on a Linux system for a linear interpolation extraction. I don't get the same problem with the nearest neighbor extraction. I think I have tracked the problem to a place in the source code. I have copied the relevant block of code below. The equations for CTOP and CBOT use the variable CSUM. The indices II and JJ are checked for array bounds in the first line of code I have copied, but the equations for CTOP and CBOT reference CSUM(II+1) and CSUM(JJ+1), which have the potential for being out of array bounds even if the first check is satisfied.

IF(II.GE.1.AND.II.LT.NLON.AND.JJ.GE.1.AND.JJ.LE.NLAT)THEN
IF(LINEAR.EQ.0)THEN
! nearest neighbor concentration extraction
II=NINT(XI)
JJ=NINT(YJ)
CONC(KS)=CSUM(II,JJ)*CFACT
ELSE
! linear interpolation extraction
XF=XI-II
YF=YJ-JJ
CTOP=(CSUM(II+1,JJ+1)-CSUM(II,JJ+1))*XF+CSUM(II,JJ+1)
CBOT=(CSUM(II+1,JJ )-CSUM(II,JJ ))*XF+CSUM(II,JJ)
CONC(KS)=((CTOP-CBOT)*YF+CBOT)*CFACT
END IF
ELSE
! at edge or outside of grid
CONC(KS)=-1.0
END IF

Re: Problem in con2stn?

Posted: January 30th, 2014, 12:08 pm
by barbara.stunder
Thanks for tracking this down. Go ahead and make the fix and test it. If you send the fix it to us, we can include it in the next version.

Re: Problem in con2stn?

Posted: March 6th, 2014, 8:42 pm
by Eslinger
I have included below an originakl code block and a suggested revised code block in con2stn.f for the Linux version

Original code block in con2stn.f in lines 386 through 424
DO KS=1,NSTA

! longitude correction to avoid dateline problems
IF(CLON.GE.0.0.AND.SLON(KS).LT.0.0)THEN
GLON=SLON(KS)+360.0
ELSEIF((DLON*NLON.GT.180.0).AND.(SLON(KS)-CLON.LE.0.0))THEN
GLON=SLON(KS)+360.0
ELSE
GLON=SLON(KS)
END IF

! convert to grid point
XI=(GLON -CLON)/DLON+1.0
YJ=(SLAT(KS)-CLAT)/DLAT+1.0
II=INT(XI)
JJ=INT(YJ)

IF(II.GE.1.AND.II.LT.NLON.AND.JJ.GE.1.AND.JJ.LE.NLAT)THEN
IF(LINEAR.EQ.0)THEN
! nearest neighbor concentration extraction
II=NINT(XI)
JJ=NINT(YJ)
CONC(KS)=CSUM(II,JJ)*CFACT
ELSE
! linear interpolation extraction
XF=XI-II
YF=YJ-JJ
CTOP=(CSUM(II+1,JJ+1)-CSUM(II,JJ+1))*XF+CSUM(II,JJ+1)
CBOT=(CSUM(II+1,JJ )-CSUM(II,JJ ))*XF+CSUM(II,JJ)
CONC(KS)=((CTOP-CBOT)*YF+CBOT)*CFACT
END IF

ELSE
! at edge or outside of grid
CONC(KS)=-1.0
END IF

! station loop
END DO


Revised code block. The lines down through YJ=(SLAT(KS)-CLAT)/DLAT+1.0 are not modified.
The index check is different on the upper end (NLAT or NLON) for the different
types of concentration extraction. Thus, this code block reverses the order of the
interpolation type check and the index check. It also moves the
INT(II) and NINT(II) types of conversion next to where the indices are used

DO KS=1,NSTA

! longitude correction to avoid dateline problems
IF(CLON.GE.0.0.AND.SLON(KS).LT.0.0)THEN
GLON=SLON(KS)+360.0
ELSEIF((DLON*NLON.GT.180.0).AND.(SLON(KS)-CLON.LE.0.0))THEN
GLON=SLON(KS)+360.0
ELSE
GLON=SLON(KS)
END IF

! convert to grid point
XI=(GLON -CLON)/DLON+1.0
YJ=(SLAT(KS)-CLAT)/DLAT+1.0

IF(LINEAR.EQ.0)THEN
! nearest neighbor concentration extraction
II=NINT(XI)
JJ=NINT(YJ)
IF(II.GE.1.AND.II.LE.NLON.AND.JJ.GE.1.AND.JJ.LE.NLAT)THEN
CONC(KS)=CSUM(II,JJ)*CFACT
ELSE
! at edge or outside of grid
CONC(KS)=-1.0
END IF
ELSE
! linear interpolation concentration extraction
II=INT(XI)
JJ=INT(YJ)
XF=XI-II
YF=YJ-JJ
IF(II.GE.1.AND.II.LT.NLON.AND.JJ.GE.1.AND.JJ.LT.NLAT)THEN
CTOP=(CSUM(II+1,JJ+1)-CSUM(II,JJ+1))*XF+CSUM(II,JJ+1)
CBOT=(CSUM(II+1,JJ )-CSUM(II,JJ ))*XF+CSUM(II,JJ)
CONC(KS)=((CTOP-CBOT)*YF+CBOT)*CFACT
ELSE
! at edge or outside of grid
CONC(KS)=-1.0
END IF
END IF

! station loop
END DO

Re: Problem in con2stn?

Posted: April 23rd, 2014, 2:47 pm
by barbara.stunder
Thank you for the revised code. It will be included in the next update.