Convert unformatted to formatted .dat file - fortran

I have a .dat file in an unformatted format and I would like to convert it to a formatted format as I cannot visualize the values and use them easily in Matlab.
The file is created with this code:
do i=1,nxyz
tzmt(i,1,1)=tzmt(i,1,1)+tz(i,1,1)
enddo
if (mod(itime,isave).eq.0) then
open(75,file='vmt.dat',form='unformatted')
write(75) tzmt
close(75)enter code here
endif
Do you have an idea of what I could do it (I am a beginner in Fortran)?

Related

Converting binary data to readable text using c++

I have a file with .dat extension which contains some binary data.i want to convert this to readable text format in c++.Read data line by line .
You have tried this way?
Read / Write Binary Data.
You can now iterate the data in the buffer and cast them to a char.
Now you can
write the chars.
Depending on your encoding the binary data will be "some char".
Linebreaks mostly are '10 or '13'.

Writing a mixof ascii and binary data in Fortran

I'm trying to write a mix of ASCII and binary data as given below for a vtk file format data.
I understand that the binary or ASCII distinction must be made in a file-OPEN statement (in the FORM='BINARY', preferably: ACCESS='STREAM' ). I don't understand how to write the file for the format I require.
What I'm trying to output:
ascii keyword
ascii keyword
ascii keyword
ascii keyword
ascii keywords "variable value in ascii" ascii keywords
.....SOME BINARY DATA ....
.....................
What I'm using:
write(fl) "# vtk DataFile Version 3.0"//CHAR(13)//CHAR(10)
write(fl)"Flow Field"//CHAR(13)//CHAR(10)
write(fl)"BINARY"//CHAR(13)//CHAR(10)
write(fl)"DATASET UNSTRUCTURED_GRID"//CHAR(13)//CHAR(10)
write(fl)"POINTS",npoints,"float" -------------> gives value of npoints(example:8) in binary format
What the output should be:
# vtk DataFile Version 3.0
Flow Field
BINARY
DATASET UNSTRUCTURED_GRID
POINTS 8 Float
.....SOME BINARY DATA ....
.....................
What the output is:
# vtk DataFile Version 3.0
Flow Field
BINARY
DATASET UNSTRUCTURED_GRID
POINTSÃ’^O^#^#float
.....SOME BINARY DATA ....
...................
Firstly, you will find examples of writing of VTK files on the internet, like in questions binary vtk for Rectilinear_grid from fortran code can not worked by paraview and Binary VTK for RECTILINEAR_GRID from fortran code in various open source research codes, like https://bitbucket.org/LadaF/elmm/src/866794b5f95ec93351b0edea47e52af8eadeceb5/src/simplevtk.f90?at=master&fileviewer=file-view-default (this one is my simplified example, there are many more) or in dedicated libraries, like http://people.sc.fsu.edu/~jburkardt/f_src/vtk_io/vtk_io.html (there is also a VTKFortran library for the XML VTK files).
Socondly, even though you are on Windows, you should not use the Windows line ending conventions in VTK binary files. End your lines just with achar(10) (or the new_line constant from iso_fortran_env). And don't forget that the binary data must be bigendian. There are examples how to deal with that in the links above.
Thirdly, to put an integer number to a string, we have a huge number of duplicates. I mean really huge. Start here Convert integers to strings to create output filenames at run time and I will shamelessly recommend my itoa function there, because it will simplify your code a lot.
write(fl)"POINTS ",itoa(npoints)," float"
I would replace
write(fl)"POINTS",npoints,"float"
with
BLOCK
integer, parameter :: big_enough = 132 ! Or whatever
character(big_enough) line
write(line,'(*(g0))')"POINTS ",npoints," Float"//achar(13)//achar(10)
write(f1) trim(line)
END BLOCK

How to save a text file to a .mat file?

How do I save a '.txt' file as a '.mat' file, using either MATLAB or Python?
I tried using textscan() (in MATLAB), and scipy.io.savemat() (in Python). Both didn't help.
My text file is of the format: value1,value2,value3,valu4 (each row) and has over 1000 rows.
Appreciate any help is appreciated. Thanks in advance.
You can use textscan to read the file and save to save the variables into a .mat file
fid = fopen('yourTextFile.txt');
C = textscan(fid,'%f %f %f %f');
fclose(fid);
% maybe change the cells from `C` to a single matrix
M = cell2mat(C);
save('myMatFile.mat','M');
This works because your file seems to have a fixed format.
Have a look at this and this
I was able to get it to work using csvread() as follows:
file = csvread('yourTextFile.txt');
save('myMatFile.mat','file');
if what you need is to change file format:
mv example.mat example.txt

how to convert a file from data to ASCII format

I have the file /etc/mydata which is in data format
$ file /etc/mydata
/etc/mydata: data
is there any fast way to convert
/etc/mydata: data
from data format to ASCII format to have this
/etc/mydata: ASCII text
thank you!
thank you
strings will filter any ASCII text in the file. But it's probably not what you actually need.

writing integers to a file in sml

Is there a output in textIO signature to write integers to a file? output only writes vectors, outputsstring writes substrings and output1 writes only characters. My problem with not using vectors is I have to write each integer into a line in the output file.
I am having tough time with SML.
Thank You.
To write an int to a file as a string, just convert it to a string using Int.toString and then write the string to the file.
To write a number to a file as a byte, use BinIO instead of TextIO. As the name suggests TextIO is for dealing with text - not binary data.