	program rename_raw
c
c	Program to rename raw image files.
c	By Michael Knappertsbusch, 11.8.2004.
c
c	Version 5:
c	Modified from version 3 for raw image files generated on a PC, 13 March 2018
c	Modified from version 4 for raw image files generated on a PC, 22 September 2022
c
c	Length of input files: 19 characters (for example 925011002aK0101.raw).
c	Length of output files: 5 characters (0101r, for direct input to Trace_AMOR1_batch.out).
c
c	The program reads the contents of the image files (640x480 pixel, 256 grey levels)
c	and writes it under a new name to disk.
c
c	Example application: Preparation of images for the Trace program.
c	It renames black-white raw image files, that were generated with AMOR 2.8 and then
c	modified on a PC using Photoshop or ImageJ from grey-level tiff images to black- and
c	white raw images.
c
c
c	Explanations:
c	Raw images have size of 640x480 pixels, 8 bit depth (256 grey levels).
c	A raw image has no header. But it is useful (for programming purpose) to apply
c	"word lengths" that correspond to the length of a Tiff header:
c	By standard the first 768 bytes of a Tiff file belong to the Tiff header (according
c	to Documentation of Nih-Image from Wayne Rasband); the following 640x480 bytes are the
c	contents of the image.
c	The total raw image size is 640*480 = 307200 bytes.
c	The length of the Tiff header is 768 bytes, which is an entire fraction of 307200 bytes
c	(768*400=307200).
c	Therefore, in order to read the image by direct access the record length recl can be 768 bytes.
c	However, because this is a very long word, I choose half of this length, that is recl=384.
c	Then the number n of records needed to read the entire image will be twice as large, e.g. n=800.
c
	integer n,number
	character*25 list	! File list containing names of images to be renamed.
	character*19 input	! Name of image file to be renamed 925011002aK0101.raw
	character*5 output	! Renamed output: 0101r
	character*384 byte	! Variable for the record with length 384.
c
	write(9,*) '**********************************************'
	write(9,*) '                                             *'
	write(9,*) '*           Program Rename                   *'
	write(9,*) '*   Version 5, by Michael Knappertsbusch     *'
	write(9,*) '*         Batch-operation mode               *'
	write(9,*) '*                                            *'
	write(9,*) '* Renames PC files, 19 chars long, incl. txt *'
	write(9,*) '*                                            *'
	write(9,*) '**********************************************'
c
	write(9,*) 'Enter list containing filenames'
	write(9,*) '    (max. 25 chars)'
	write(9,*) ' '
	read(9,2) list
 2	format(a25)
c
	open(15,file=list,status='old')
	number=0
 5	read(15,6,end=99) input
 6	format(a19)		! 19 characters per input file name
 	number=number+1		! counts the number of files, that were renamed
c
c	determine name of output file:
c
	output=input(12:15)//'r'
c
c	now open existing image files and files under new name output:
c
	open(16,file=input,access='direct',
     1  recl=384,status='old')
	open(17,file=output,access='direct',
     1  recl=384,status='new')
        write(9,*) '. . .renaming raw files. . .'
c
c	read contents of existing images and write them to output    
c
	do 10 n=1,800				! n= number of record with recl=384.
	  read(16,'(a384)',rec=n) byte
	  write(17,'(a384)',rec=n) byte
 10	continue
 	close(17)
	close(16)
	goto 5
c
 99	write(9,*) ' '
 	write(9,*) number,' images renamed'
 	pause 100
 100	continue
c
 	stop
	end
	
        
