next up previous contents FITSIO Home
Next: About this document ... Up: 3. Utility Subroutines Previous: 3.1 PRINTERROR - print   Contents

3.2 DELETEFILE - delete a FITS file

This subroutine simply deletes a specified FITS file if it exists. Some of the programs in this cookbook which create a test FITS file use this routine to delete the file if it already exists from a previous run of the program.

Programmers may wish to use a similar technique to delete a partially written FITS file if an error occurs that prevents the file from being completed successfully.

  1. FTGIOU returns an unused Fortran logical unit number for use when opening the file. This routine must only be used if ALL unit numbers used by the program are allocated though it, because it will have no knowledge of any other unit numbers that the program may have otherwise used.

  2. FTOPEN attempts to open the FITS file. There are 3 possible outcomes: a returned status = 0 means the FITS file was opened successfully; status = 103 means the file doesn't exist; any other status value indicates that the file exists but it could not be read successfully, probably because the format does not conform to the FITS standard.

  3. FTDELT deletes the file associated with the particular unit number. In more normal circumstances, one would use the FTCLOS subroutine to close a file without deleting it.

  4. FTCMSG clears the internal FITSIO error message stack. In this case the previous failure to open the FITS file will have generated a message on the stack which is of no importance.

  5. In this case there was some sort of problem reading the FITS file, probably because the format does not conform to the FITS standard. Ignore this error and delete the file anyway after clearing the error message stack of any superfluous messages.

  6. FTFIOU frees the logical unit number previously allocated with ftgiou.

      subroutine deletefile(filename,status)

C     A simple little routine to delete a FITS file

      integer status,unit,blocksize
      character*(*) filename

C     simply return if status is greater than zero
      if (status .gt. 0)return

C     Get an unused Logical Unit Number to use to open the FITS file
 1    call ftgiou(unit,status)

C     try to open the file, to see if it exists
 2    call ftopen(unit,filename,1,blocksize,status)

      if (status .eq. 0)then
C         file was opened;  so now delete it 
 3        call ftdelt(unit,status)
      else if (status .eq. 103)then
C         file doesn't exist, so just reset status to zero and clear errors
          status=0
 4        call ftcmsg
      else
C         there was some other error opening the file; delete the file anyway
          status=0
 5        call ftcmsg
          call ftdelt(unit,status)
      end if

C     free the unit number for later reuse
 6    call ftfiou(unit, status)
      end

next up previous contents FITSIO Home
Next: About this document ... Up: 3. Utility Subroutines Previous: 3.1 PRINTERROR - print   Contents