      subroutine mdserr(lun, zstr, stat)
 
      implicit none
 
      character*(*) zstr        ! context message string
      integer stat              ! mdsplus status code
      integer lun               ! lun
 
      integer MDS_ERRSTR
C
      integer il, istat
      character*128 mds_msg
C
      write(lun,*) zstr,stat,':'
      istat=mds_errstr(stat,mds_msg)
      il=len_trim(mds_msg)
      write(lun,*) mds_msg(1:il)
C
      return
      end
