Welcome, Guest
Username: Password: Remember me

TOPIC: read and write selafin files in Python

read and write selafin files in Python 7 years 7 months ago #25944

  • o.gourgue
  • o.gourgue's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 155
  • Thank you received: 11
Hi everyone,

Using "parserSELAFIN.py", I'm able to read a selafin file and use the information in a Python environment.

Now, I would like to add a new field to my selafin object (?) and write it in another selafin file. Is it possible? Is there an example somewhere that I could use for inspiration?

Thank you for your help!

Olivier
The administrator has disabled public write access.

read and write selafin files in Python 7 years 7 months ago #25946

  • c.coulet
  • c.coulet's Avatar
  • OFFLINE
  • Moderator
  • Posts: 3722
  • Thank you received: 1031
Hi Olivier

Not a specialist but try to look in the python script which manage parallel computation.
I know you could split the model in python so necessarily, there is some part dedicated to write a selafin object...

Kind regards
Christophe
The administrator has disabled public write access.

read and write selafin files in Python 7 years 7 months ago #25949

  • pprodano
  • pprodano's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 96
  • Thank you received: 55
Hello Olivier,

Alternately, you can try to use the SELAFIN reader/writer that is part of my PPUTILS project. Reading and writing is done as follows:

from ppmodules.selafin_io_pp import *

# read *.slf file
slf = ppSELAFIN('file_to_be_read.slf')
slf.readHeader()
slf.readTimes()

# store into arrays
times = slf.getTimes()
vnames = slf.getVarNames()
vunits = slf.getVarUnits()
float_type,float_size = slf.getPrecision()
NELEM, NPOIN, NDP, IKLE, IPOBO, x, y = slf.getMesh()

# read the variables for the time step 0
slf.readVariables(0)
results = slf.getVarValues()

# write *.slf file
slf2 = ppSELAFIN('file_to_be_written.slf')
slf2.setPrecision(float_type,float_size)
slf2.setTitle('created with pputils')
slf2.setVarNames(vnames)
slf2.setVarUnits(vunits)
slf2.setIPARAM([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])
slf2.setMesh(NELEM, NPOIN, NDP, IKLE, IPOBO, x, y)
slf2.writeHeader()

slf2.writeVariables(0, results)

For many time steps, you'll have to loop accordingly. Lot's of my scripts use the syntax above.

If you want to add another variable, and write it as a SELAFIN file, you would have to:

1. Append variable name list (and add the variable name),
2. Append variable unit list (and add the variable unit),
3. Add a column in the result array (for the time step in question), and
4. Write the result as above using method writeVariables().

I hope it helps,

Pat
The administrator has disabled public write access.
The following user(s) said Thank You: cyamin, o.gourgue

read and write selafin files in Python 7 years 7 months ago #25963

  • o.gourgue
  • o.gourgue's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 155
  • Thank you received: 11
Thank you Pat!

Your module works perfectly for what I need to do!

Cheers,

Olivier
The administrator has disabled public write access.

read and write selafin files in Python 7 years 5 months ago #26836

  • cyamin
  • cyamin's Avatar
  • OFFLINE
  • openTELEMAC Guru
  • Posts: 997
  • Thank you received: 234
Hello all,

I am trying to use pputils to split a TOMAWAC spectrum file, that contains spectrum data for multiple points, into separate files containing one spectrum per file. Using the above snippet as a starting point I got this far:
from ppmodules.selafin_io_pp import *

# read *.slf file
slf = ppSELAFIN('tom_global_1_[33.75_56.25)_year1.spe')
slf.readHeader()
slf.readTimes()

# store into arrays
times = slf.getTimes()
vnames = slf.getVarNames()
vunits = slf.getVarUnits()
float_type,float_size = slf.getPrecision()
NELEM, NPOIN, NDP, IKLE, IPOBO, x, y = slf.getMesh()

num_spe = len(vnames)
for i in range(1, num_spe+1):
    print i
    # 'setVarNames' and 'setVarUnits' accept lists. Create single item lists.
    vname = []
    vunit = []
    vname.append(vnames[i-1])
    vunit.append(vunits[i-1])
    #print vname,vunit,times[-1]

    # read the variables for the last time step
    slf.readVariables(times[-1])
    results = slf.getVarValues()[i-1]
    #print results
    
    
    # write *.slf file
    slf2 = ppSELAFIN('file_to_be_written_{}.slf'.format(i))
    slf2.setPrecision(float_type,float_size)
    slf2.setTitle('created with pputils')
    slf2.setVarNames(vname)
    slf2.setVarUnits(vunit)
    slf2.setIPARAM([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])
    slf2.setMesh(NELEM, NPOIN, NDP, IKLE, IPOBO, x, y)
    slf2.writeHeader()
    print slf2.NBV1, slf2.vars
    
    slf2.writeVariables(times[-1], results)

However, I get this error:
slf2.writeVariables(times[-1], results)
  File "\pputils-master\ppmodules\selafin_io_pp.py", line 305, in writeVariables
    self.f.write(pack('>'+self.float_type, self.temp[j,k]))
IndexError: too many indices for array

I have no experience with the SELAFIN format and I don't get the error. Any help would be appreciated.

Best Regards,
Costas
The administrator has disabled public write access.

read and write selafin files in Python 7 years 5 months ago #26843

  • cyamin
  • cyamin's Avatar
  • OFFLINE
  • openTELEMAC Guru
  • Posts: 997
  • Thank you received: 234
I think I have solved the above issue, but haven't verified the results, as BK cannot open spectrum files at the moment.

Here is my solution for anyone interested:
from ppmodules.selafin_io_pp import *

# read *.slf file
slf = ppSELAFIN('tom_global_1_[33.75_56.25)_year1.spe')
slf.readHeader()
slf.readTimes()

# store into arrays
times = slf.getTimes()
vnames = slf.getVarNames()
vunits = slf.getVarUnits()
float_type,float_size = slf.getPrecision()
NELEM, NPOIN, NDP, IKLE, IPOBO, x, y = slf.getMesh()

num_spe     = len(vnames)
last_step   = len(times)-1
for i in range(1, num_spe+1):
    print i
    # 'setVarNames' and 'setVarUnits' accept lists. Create single item lists.
    vname = []
    vunit = []
    vname.append(vnames[i-1])
    vunit.append(vunits[i-1])
    print vname,vunit,last_step

    # read the variables for the last time step
    slf.readVariables(last_step)
    results  = slf.getVarValues()[i-1:i]

    # write *.slf file
    slf2 = ppSELAFIN('file_to_be_written_{}.spe'.format(i))
    slf2.setPrecision(float_type,float_size)
    slf2.setTitle('created with pputils')
    slf2.setVarNames(vname)
    slf2.setVarUnits(vunit)
    slf2.setIPARAM([1, 0, 0, 0, 0, 0, 0, 0, 0, 1])
    slf2.setMesh(NELEM, NPOIN, NDP, IKLE, IPOBO, x, y)
    slf2.writeHeader()
    
    slf2.writeVariables(last_step, results)
    del slf2
Costas
The administrator has disabled public write access.
The following user(s) said Thank You: pprodano

read and write selafin files in Python 7 years 5 months ago #26854

  • cyamin
  • cyamin's Avatar
  • OFFLINE
  • openTELEMAC Guru
  • Posts: 997
  • Thank you received: 234
Hello all,

Unfortunately the split spectrum file proved to be ill written. I fed it to ARTEMIS (that was the purpose after all) and failed to read it.

I did some more code corrections but never managed to make it work by reading and writing from scratch using pptools. Eventually I discovered that there are scripts in the python27 folder that can do what I want by simply removing variables (or better keeping only the variables asked) from the original file:
from parsers.parserFortran import cleanQuotes
from parsers.parserSELAFIN import SELAFIN
from runSELAFIN import scanSPECTRAL,alterSELAFIN

os.chdir(res_dir)

slf    = SELAFIN(res_filename)
names  = slf.VARNAMES
frames = slf.tags['times']
tmax   = max(frames)

for i,var in enumerate(names):
    i += 1
    print i,var
    vars = cleanQuotes(var.replace('_',' '))
    slf1 = alterSELAFIN( res_filename, times = (tmax,0,tmax), vars  = vars, root=None  )

    # Define new name and write *.spe file
    name, ext    = os.path.splitext(res_filename)
    new_filename = '{}_{}{}'.format(name,i,ext)
    slf1.putContent( new_filename )
    
    del slf1
The code was taken from runSELAFIN.py class and adapted to my needs. I also found scanSPECTRAL to be a very useful tool to quickly test if my split spectrum file was OK.

I have yet to master writing SELAFIN files from scratch, but so far I am getting away with it. :laugh:

Many thanks to pprodano for sharing his pptools that provide more explanation to SELAFIN I/O and of course to sebourban and all devs for providing the proven tools (for those who spot them and figure out how to use them :P ).

Best Regards,
Costas
The administrator has disabled public write access.

read and write selafin files in Python 7 years 5 months ago #26867

  • pprodano
  • pprodano's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 96
  • Thank you received: 55
Hello Costas,

When I wrote the SELAFIN reader/writer class I originally meant that it be used for geometry files. I haven't tested it with spectral files (until today).

I tried running the conversion sel2vtk.py script to try and visualize your spectral file with Paraview, but the script didn't manage to produce the correct result. This leads me to believe that the SELAFIN format of the spectral files is slightly different than the SELAFIN format of the geometry files. It could be how the IKLE (element connectivity) arrays is read... I will investigate ...

Pat
The administrator has disabled public write access.

read and write selafin files in Python 7 years 5 months ago #26870

  • cyamin
  • cyamin's Avatar
  • OFFLINE
  • openTELEMAC Guru
  • Posts: 997
  • Thank you received: 234
pprodano wrote:
Hello Costas,
... This leads me to believe that the SELAFIN format of the spectral files is slightly different than the SELAFIN format of the geometry files. It could be how the IKLE (element connectivity) arrays is read...
Pat
Hello Pat,
TOMAWAC spectrum files are lately defined in quadrangles, not triangles that were in the past. This means that the connectivity table has 4 entries not 3. However, if you are reading the mesh structure from an existing file and using it to build another, it shouldn't make any difference, right?
Watermotion.eu wrote:

That's excellent news. Could you perhaps direct me toward a place where the usage syntax of parserSELAFIN.py is explained? Thanks.

I hardly manage to get it going (classical pythonian module import issues), and the result is a "My job is done" without a clue of which job it has done.

Here are some useful links:
PPUTILS User's Manual
Matplotlib (pytel scripts)
There is no manual for pytel scripts!
Regards,
Costas
The administrator has disabled public write access.

read and write selafin files in Python 7 years 5 months ago #26868

  • Watermotion.eu
  • Watermotion.eu's Avatar
o.gourgue wrote:
Hi everyone,

Using "parserSELAFIN.py", I'm able to read a selafin file and use the information in a Python environment.

That's excellent news. Could you perhaps direct me toward a place where the usage syntax of parserSELAFIN.py is explained? Thanks.

I hardly manage to get it going (classical pythonian module import issues), and the result is a "My job is done" without a clue of which job it has done.
The administrator has disabled public write access.
Moderators: borisb

The open TELEMAC-MASCARET template for Joomla!2.5, the HTML 4 version.