function writebov3dvector(name, lower, upper, tnow, data, varname)
    f=fopen([name,'.bov'],'w');
    datafile=[name,'.dat'];
    fprintf(f, 'TIME: %f\n', tnow);
    fprintf(f, 'DATA_FILE: %s\n', datafile);
    fprintf(f, 'DATA_SIZE: %d %d %d\n', size(data,1), size(data, 2), size(data,3));
    fprintf(f, 'DATA_FORMAT: DOUBLE\n');
    fprintf(f, 'VARIABLE: %s\n',varname);
    fprintf(f, 'DATA_ENDIAN: LITTLE\n');
    fprintf(f, 'CENTERING: zonal\n');
    fprintf(f, 'BRICK_ORIGIN: %e %e %e\n',lower);
    fprintf(f, 'BRICK_SIZE: %e %E %e\n', upper-lower);
    fprintf(f, 'BYTE_OFFSET: 0\n');
    fprintf(f, 'DATA_COMPONENTS: %d', size(data, 4));
    fclose(f);
    f=fopen(datafile, 'wb');
    fwrite(f, permute(data,[4,1,2,3]),'double');
    fclose(f);
end


%    SUBROUTINE WriteBOV3DVector(name, lower, upper, tnow, data, varname)
%       CHARACTER(LEN=*) :: name, varname
%       CHARACTER(LEN=100) :: Filename
%       REAL(KIND=qPREC) :: lower(3), upper(3), tnow
%       REAL(KIND=qPREC), DIMENSION(:,:,:,:) :: data
%       WRITE(FileName,'(A4,A,A4)') 'out/',TRIM(name),".dat"
%       OPEN(UNIT=BOV_DATA_HANDLE, FILE=TRIM(FileName), status="replace", FORM="unformatted")
%       WRITE(BOV_DATA_HANDLE) reshape(data,(/size(data,4),size(data)/size(data,4)/),(/0d0/),(/2,1/))
%       CLOSE(BOV_DATA_HANDLE)
%       WRITE(FileName,'(A4,A,A4)') 'out/',TRIM(name),".bov"
%       OPEN(UNIT=BOV_DATA_HANDLE, FILE=Filename)
%       WRITE(BOV_DATA_HANDLE,'(A6,E26.16)')  "TIME: ", tnow
%       WRITE(BOV_DATA_HANDLE,'(A11,A,A4)') "DATA_FILE: ", TRIM(name), ".dat"
%       WRITE(BOV_DATA_HANDLE,'(A11,3I12)')  "DATA_SIZE: ", size(data,1),size(data,2),size(data,3)
%       WRITE(BOV_DATA_HANDLE,'(A19)')  "DATA_FORMAT: DOUBLE"
%       WRITE(BOV_DATA_HANDLE,'(A10,A)')  "VARIABLE: ", TRIM(varname)
% # if defined _BIGENDIAN
%       WRITE(BOV_DATA_HANDLE,'(A19)')  "DATA_ENDIAN: BIG"
% # else
%       WRITE(BOV_DATA_HANDLE,'(A19)')  "DATA_ENDIAN: LITTLE"
% # endif
%       WRITE(BOV_DATA_HANDLE,'(A16)')  "CENTERING: zonal"
%       WRITE(BOV_DATA_HANDLE,'(A14,3E26.16)')  "BRICK_ORIGIN: ", lower
%       WRITE(BOV_DATA_HANDLE,'(A12,3E26.16)')  "BRICK_SIZE: ", upper-lower
%       WRITE(BOV_DATA_HANDLE,'(A14)')  "BYTE_OFFSET: 4"
%       WRITE(BOV_DATA_HANDLE,'(A17,I4)')  "DATA_COMPONENTS: ", size(data,4)
%       CLOSE(BOV_DATA_HANDLE)
%    END SUBROUTINE WriteBOV3DVector
