	program read_mfi_cdf_h0
c
c       read_mfi_cdf_h0.f
c
c       Purpose: an example to convert cdf files to ascii 
c		with selected columns 
c
c	Functions and subroutines: 
c		Requires CDF (Common Data Format) library v2.5 or above
c		The library can be obtained at the National Space 
c		Science Center, NASA/Goddard Space Flight Center, 
c		Greenbelt, Maryland
c
c		see: http://nssdc.gsfc.nasa.gov/cdf
c
c	CDF input : wind/mfi high res. data in cdf format
c
c	ASCII outputs : 
c
c	    (1) On screen: brief description of the content of the cdf file.
c
c	    (2) The ascii file (e.g., ascii_outputfile) contains:
c
c	integer*4   yr   Year (e.g., 1996)
c       integer*4   mon  Month (e.g. 1-January...12-December)
c       integer*4   day  Day of Month (1...28/29/30/31)
c       integer*4   hr   Hour (1 -24)
c       integer*4   min  Minute (0-59)
c       real*4 sec       second of the minute (0.0-59.9999)
c       real*4 BGSE(1)   X component of B in GSE coordinate in nT
c       real*4 BGSE(2)   Y component of B in GSE coordinate in nT
c       real*4 BGSE(3)   Z component of B in GSE coordinate in nT
c       real*4 BGSM(2)   Y component of B in GSM coordinate in nT
c       real*4 BGSM(3)   Z component of B in GSM coordinate in nT
c       real*4 BF1        Total magnetic flux in nT
c       real*4 BRMSGSM(1) X component of the rms of B in nT
c       real*4 BRMSGSM(2) Y component of the rms of B in nT
c       real*4 BRMSGSM(3) Z component of the rms of B in nT
c       real*4 BRMSF1     Total magnitude of the rms of B in nT
c	integer*4 NUM_PTS Number of points in averaging
c       real*4 PGSE(1)   X component of s/c position in GSE coordinate in Re
c       real*4 PGSE(2)   Y component of s/c position in GSE coordinate in Re
c       real*4 PGSE(3)   Z component of s/c position in GSE coordinate in Re
c       real*4 PGSM(2)   Y component of s/c position in GSM coordinate in Re
c       real*4 PGSM(3)   Z component of s/c position in GSM coordinate in Re
c
c	Format used for ascii output:
c
c 	format(I4,4I3,F5.1,10G13.6,I10,5G13.6)
c
c	Note:
c	    The information of s/c position has only 1-min resolution.
c	Each minute of the position data is duplicated 20 times to fill 
c	the 3-s records.
c
c
c	Created by Sean (Sheng-Hsien) Chen, Hughes STX Corporation, 
c		Greenbelt, Maryland 
c
c	October 24, 1997
c
c**************************************************************************
	integer*4 unit_asc,id,status
	character*80 CDFname,output

	print*,'Input cdf file name (without the extension .cdf)? ' 
	read(*,'(a)') CDFname 
c
	print*,'Output file ascii? ' 
	read(*,'(a)') output 
	open(unit_asc,file=output,status='new',form='formatted')
c
	call WriteAscii(CDFname,unit_asc)
c
	close(unit_asc)
	stop
	end
c
c********************************************************************
c
c WriteAscii(CDFname,unit_asc)
c
c SHC 10/24/97
c********************************************************************
	subroutine WriteAscii(CDFname,unit_asc)
	character*(*) CDFname
	integer*4 id,unit_asc	
	real*4 BF1,BRMSF1,BGSM(3),BRMSGSM(3),BGSE(3),PGSM(3),PGSE(3)
	integer*4 NUM_PTS,Time(3),yr,month,day,hr,min,status
	real*4 sec
	integer*4 i,j,NumRec
	integer*4 version,release,form,encoding,majority,
	1	numDims,dimSizes(10),numRvars,numZvars
c
c 	CDF library (v2.5 or above) has to be set up and linked properly 
c	in order to include cdf.inc
c
	include 'cdf.inc' 
c
c	Open CDF input file
c
	call CDF_open(CDFname,id,status)
	call ErrorHandler(status,'Openning CDF file')
c
c	Get CDF metadata
c
	call CDF_lib(SELECT_, CDF_, id,
	1	   GET_, CDF_ENCODING_, encoding,
	2	         CDF_MAJORITY_, majority,
	3	         CDF_FORMAT_,  format,
	4	         CDF_VERSION_, version,
	5	         CDF_RELEASE_, release,
	6	         CDF_NUMDIMS_, numDims,
	7	         CDF_NUMrVARS_, numRvars,
	8	         CDF_NUMzVARS_, numZvars,
	9	         CDF_DIMSIZES_, dimSizes,
	1	   NULL_,status)
	call ErrorHandler(status,'GET CDF info')
c
c  Print CDF metadata
c
	print*,'CDF Version: ', version, release
	print*,'CDF Format: ',form
	print*,'Data Encoding: ', encoding
	print*,'Variable Majority:',majority
	print*,'Number of dimensions: ', numDims
c
	do i=1,numDims 
		print*,dimSizes(i)
	enddo
	print*,'Number of R variables: ',numRvars
	print*,'Number of Z variables: ',numZvars

	call CDF_lib(SELECT_,CDF_,id, zVAR_NAME_,'Epoch3',
	1	GET_,	zVAR_MAXREC_,NumRec, NULL_,status)
	call ErrorHandler(status,'GET CDF zVar/Number of Records')
	
	print*,'Total number of records for 3-s data: ',NumRec
c
c
c  Note: to increase the conversion speed, read all the records
c  at a time and store in a larger array for each component
c  instead of one record at a time if the memory is not a concern.
c
c  examples;
c  call CDFlib(SELECT_,CDF_,id,
c                  zVAR_NAME_,"B3GSE",
c                  zVARs_RECNUMBER_,i,
c                  zVARs_RECCOUNT_,NumRec,
c          GET_,   zVAR_HYPERDATA_,BGSE, NULL_,status);
c
c   where BGSE has a size of real*4 (3,NumRec)
c
c
	do i=1,NumRec
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'Time3_PB5',
	2		zVARs_RECNUMBER_,i,
	3		zVAR_DIMCOUNTS_,3,
	4	GET_,	zVAR_HYPERDATA_,Time, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3GSE',
	2		zVARs_RECNUMBER_,i,
	3		zVAR_DIMCOUNTS_,3,
	4	GET_,	zVAR_HYPERDATA_,BGSE, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3GSM',
	2		zVARs_RECNUMBER_,i,
	3		zVAR_DIMCOUNTS_,3,
	4	GET_,	zVAR_HYPERDATA_,BGSM, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3F1',
	2		zVARs_RECNUMBER_,i,
	3	GET_,	zVAR_DATA_,BF1, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3RMSGSM',
	2		zVARs_RECNUMBER_,i,
	3		zVAR_DIMCOUNTS_,3,
	4	GET_,	zVAR_HYPERDATA_,BRMSGSM, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3F1',
	2		zVARs_RECNUMBER_,i,
	3	GET_,	zVAR_DATA_,BF1, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'B3RMSF1',
	2		zVARs_RECNUMBER_,i,
	3	GET_,	zVAR_DATA_,BRMSF1, NULL_,status)
	    call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'NUM3_PTS',
	2		zVARs_RECNUMBER_,i,
	3	GET_,	zVAR_DATA_,NUM_PTS, NULL_,status)
	
	    if(mod(i-1,20) .eq. 0) then
	        j=(i-1)/20+1
		call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'PGSE',
	2		zVARs_RECNUMBER_,j,
	3		zVAR_DIMCOUNTS_,3,
	4	   GET_,zVAR_HYPERDATA_,PGSE, NULL_,status)
		call CDF_lib(SELECT_,CDF_,id,
	1		zVAR_NAME_,'PGSM',
	2		zVARs_RECNUMBER_,j,
	3		zVAR_DIMCOUNTS_,3,
	4	   GET_,zVAR_HYPERDATA_,PGSM, NULL_,status)
	    endif

	    yr=Time(1)
	    call doy_month(Time(1),Time(2),month,day,1)
	    sec=Time(3)*0.001
	    hr=sec/3600
	    min=(sec-hr*3600)/60
	    sec=sec-hr*3600-min*60

	    write(unit_asc,9999) yr,month,day,hr,min,sec,
	1	BGSE(1),BGSE(2),BGSE(3),BGSM(2),BGSM(3),BF1,
	2	BRMSGSM(1),BRMSGSM(2),BRMSGSM(3),BRMSF1,NUM_PTS,
	3	PGSE(1),PGSE(2),PGSE(3),PGSM(2),PGSM(3)
	enddo	
c
c	Close CDF
c
	call CDF_close(id,status)
	return
9999 	format(I4,4I3,F5.1,10G13.6,I8,5G13.6)
	end
c********************************************************************
c
c ErrorHandler(status,message)
c
c SHC 10/23/97
c********************************************************************
	subroutine ErrorHandler(status,message)
	integer*4 status
	character*(*) message
	character*80 text
	integer*4 status_buf
c
c 	CDF library (v2.5 or above) has to be set up and linked properly 
c	in order to include cdf.inc
c
	include 'cdf.inc'

	if(status .ge. CDF_OK) return

	call CDF_lib(SELECT_,CDF_STATUS_, status,
	1	GET_, STATUS_TEXT_, text,
	2	NULL_,status_buf)

	if(status .ge. CDF_WARN) then
	    print*,'CDF warning: ',text
	    return
	endif

	print*,'Aborting at ',message
	print*,test
	print*,'Program aborted.'
	stop
	end
c
c********************************************************************
c
c convert day of year to month and date or vice versa
c
c SHC 10/23/97
c********************************************************************
	subroutine doy_month(year,doy,month,day,index)
	integer*4 year,doy,month,day,index
	integer*4 i,dom(13),yr	

	yr=year
	dom(1)=0
	dom(2)=31
	dom(3)=59
	dom(4)=90
	dom(5)=120
	dom(6)=151
	dom(7)=181
	dom(8)=212
	dom(9)=243
	dom(10)=273
	dom(11)=304
	dom(12)=334
	dom(13)=365

	if((mod(yr,4).eq.0 .and. mod(yr,100).ne.0) .or.
	1	(mod(yr,400).eq.0)) then 
	    do i=3,13 
		dom(i)=dom(i)+1
	    enddo
	endif

	if(index .gt. 0) then
	    do i=1,12
		if(doy .gt. dom(i) .and. doy .le. dom(i+1)) then
		    month=i
		    day=doy-dom(i)
		    return 
	        endif
	    enddo
	    print*,' wrong parameter doy given in doy_month'
	else if(month .ge. 1 .and. month .le. 12) then
		doy=dom(month)+day
        	return
	endif
	end
