|
Global Monthly Vegetation CoverSoftware to Read 8-Bit Data |
Introduction Page, Digital Data Information
The 8-bit binary files were generated on a Silicon Graphics Inc. (SGI) Unix workstation using the C language. Each uncompressed image is made up of 2,500 columns and 904 rows of data, with 0.144 degree (nominally 0.15 degree) latitude/longitude resolution and the total size of 2,260,000 bytes per image. The image is read by rows, starting with row 1 at 75 N and ending with row 904 at 55 S. Every row begins at 180 W longitude and each successive map cell is 0.144 degrees east of the previous map cell. Thus, the upper left corner of the image is at 75 N, 180 W, and the lower right corner is at 55 S, 180 E. The data range is between 0 and 255. The ocean map cells are all zeroes.
Since the binary imagery are character data, it is impossible to browse through it. However, these data can be easily converted into integer values. The following is a list of FORTRAN and C programs that can be used as a guide for reading the files. Each of these programs converts the character data into integer values. Reading data is somewhat platform-specific. The coding is broken down by language and platform.
NOTE: The output data is written to UNIT=6 (the screen).
USING FORTRAN
The following is a list of programs that should be used depending on your platform.
A. SGI or SUN Workstations
PROGRAM CONVRT
C
C THIS PROGRAM IS DESIGNED TO READ 8-BIT IMAGE DATA AND
C OUTPUT THE DATA AS ASCII TEXT.
C THE DIMENSIONS: 5000 IS BLOCKSIZE, 452 IS NUMBER OF RECORDS
C ********
DECLARE ALL VARIABLES
C
CHARACTER*1 IMAGE(5000)
CHARACTER*8 HEX(5000)
INTEGER DATA(5000)
C
C ******** OPEN FILES TO READ ********************************
C
OPEN(UNIT=10,FILE='GVI.DATA',ACCESS='DIRECT',
$RECL=1250,FORM='UNFORMATTED',STATUS='UNKNOWN')
DO 10 I=1,452
READ(10,REC=I) IMAGE
DO 15 J=1,5000
WRITE(HEX(J),'(Z2)') IMAGE(J)
READ(HEX(J),'(Z2)') DATA(J)
15 CONTINUE
DO 20 J=1,5000
WRITE(6,'(I4)') DATA(J)
20 CONTINUE
10 CONTINUE
STOP
END
********************************************************************** B. IBM MAINFRAMES
PROGRAM CONVRT
C
C THIS PROGRAM IS DESIGNED TO READ 8-BIT IMAGE DATA AND
C OUTPUT THE DATA AS ASCII TEXT.
C
C ******** DECLARE ALL VARIABLES *******************
C
CHARACTER*1 IMAGE(5000)
INTEGER*2 DATA(5000)
EQUIVALENCE(IMAGE,DATA)
C
C ******** OPEN FILES TO READ ********************************
C
OPEN(UNIT=10,FILE='GVI.DATA')
DO 10 I=1,452
READ(10,'(250(20A1))') IMAGE
DO 20 J=1,5000
WRITE(6,'(I4)') data(J)
20 CONTINUE
10 CONTINUE
STOP
END
********************************************************************** C. CRAY
With the CRAY, the following script (cray.sh) should be used used with the compiled FORTRAN code.
PROGRAM CONVRT
C
C THIS PROGRAM IS DESIGNED TO READ 8-BIT IMAGE DATA AND
C OUTPUT THE DATA AS ASCII TEXT.
C
C ******** DECLARE ALL VARIABLES ****************************
C 2500 BLOCKSIZE, 904 NUMBER OF RECORDS
CHARACTER IMAGE
C
do j = 1,904
Do i = 1 , 2500
1 READ(10) IMAGE
write(6,*) ichar(IMAGE)
enddo
enddo
STOP
END
The shell script to use with the FORTRAN code is the following: #!/bin/sh assign -R assign -a gvi.data -sunblocked u:10 cf77 filename ./a.out exit where, assign -R ==> Reset all assign attributes. assign -a filename -sunblocked u:10 ==> data file "filename" has structure unblocked and has been assigned to unit 10. cf77 filename ==> Use the cray FORTRAN 77 compiler on input file "filename" containing program CONVRT code. ./a.out ==> The executable**********************************************************************
D. VAX
The VAX is a special case, because all files are automatically reblocked into 512 byte sectors. In order to read this data in blocks of 5000, it is necessary to first convert the VAX 512 byte blocked data into a file with 5000 byte blocks. To do this first run the program conv_5000.
PROGRAM CONV_5000
C
C READ A RECORD OF DATA IN 512 RECL AND CONVERT
C TO 5000 RECL FORMAT.
C THE LEAST COMMON MULTPLE FOR BOTH RECORD LENGTHS IS 320000
C
BYTE RD_BUF(320000)
INTEGER IPTR,JPTR
C
C
OPEN(UNIT=20,FILE='FILE_INPUT',ACCESS='SEQUENTIAL',
* STATUS='OLD',FORM='UNFORMATTED',READONLY)
OPEN(UNIT=40,FILE='FILE_OUTPUT',ACCESS='SEQUENTIAL',
* STATUS='NEW',FORM='UNFORMATTED',RECL=1250,RECORDTYPE='FIXED')
C
C
C
100 IPTR = 1
C
C read in portion of 512 recl
DO INDX = 1,625
READ(20,ERR=900,END=55)(RD_BUF(IPTR-1+J),J=1,512)
IPTR = IPTR + 512
END DO
C
C write out in 5000 recl
JPTR = 1
DO INDX = 1,64
WRITE(40,ERR=900)(RD_BUF(JPTR-1+J),J=1,5000)
JPTR = JPTR + 5000
END DO
C
GO TO 100
C
C
C write out last part of buffer
C
55 CONTINUE
JPTR = 1
DO INDX = 1,4
WRITE(40,ERR=900)(RD_BUF(JPTR-1+J),J=1,5000)
JPTR = JPTR + 5000
END DO
200 FORMAT(' JTEMP = ',I5)
C
C
C skip over error processing
GO TO 1000
C
C ERROR CODE
900 CONTINUE
WRITE(6,888)
888 FORMAT(' ERROR')
C
1000 CLOSE(20)
CLOSE(40)
C
STOP
END
Next take the output file "FILE_OUTPUT" and run program vax.f.
program CONVRT
C
C THIS PROGRAM PRINTS OUT ALL BYTES FROM A 5000 RECL DATA SET
C
byte bb(5000)
character*1 cchr(5000)
equivalence (bb,cchr)
C
open(20,file='FILE_OUTPUT',status='old')
C
rewind (20)
do 500 i=1,452
read (20,'(5000a1))')cchr
write(6,*)(bb(indx),indx=1,5000)
500 continue
C
close(20)
stop
end
USING C
The following program can be universally used on all platforms which have a C compiler.
#includeThe output ASCII image will be a sequential file, with each pixel constituting a new record, whereas the entire image has 2,260,000 records or lines.#include #include #include static FILE *fp1, *fp2; void main(int argc, char *argv[]) { char image[2260000]; int xsize=2500, ysize=904; register int i; long data; if (argc != 3) { printf("usage -- image_read inputfile outputfile\n"); exit(EXIT_FAILURE); } if (!(fp1=fopen(argv[1],"rb"))) { printf("Cannot open file %s to read\n",argv[1]); exit(EXIT_FAILURE); } if (!(fp2=fopen(argv[2],"w"))) { printf("Cannot open file %s to write\n",argv[2]); exit(EXIT_FAILURE); } fread(&image[0],1,xsize*ysize,fp1); for(i=0;i<2260000;i++) { data=(long) image[i]; fprintf(fp2,"%d\n",data); } /* Write output to user-defined file */ } To compile the program, use the following command: cc read_image.c -lm -o read_image Then to run the program, use the following: read_image inputfile outputfile where, read_image ==> The executable inputfile ==> The input 8-bit binary image outputfile ==> The output ASCII image
| Top of Page |