Welcome, Guest
Username: Password: Remember me

TOPIC: read and write selafin files in Python

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

  • taoan
  • taoan's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 52
  • Thank you received: 9
Hello Pat,

Firstly I would like to thank you for your very useful PPUTILS python modules!

I got to write a selafin wind binary file from WRF atmosferic model outputs using PPUTILS, to feed my telemac model. However, I noted that my reference date/time is confused when I load my wind selafin file on bluekenue.
I believe that bluekenue is setting my reference time for some default value as you can see on picture below.

vento.gif


My intention is to set my reference time for the same that from WRF data instead to use default bluekenue. I think this is necessary because I am running my telemac model with TPXO tides which need a date/time reference.

Do you know about some PPUTILS funcion to set my correct reference date/time to write on my selafin wind file?

Thank you for your attention.

Taoan
The administrator has disabled public write access.

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

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

I have to declare that the Selafin writer in PPUTILS initializes the date to (see selafin_io_pp.py, line 83):

# Aug 29, 1997 2:15 am EST (date when skynet became self-aware)
self.DATE = [1997, 8, 29, 2, 15, 0]

This, of course, references 1991 movie Terminator 2 Judgement Day. Simplest way for you to set the date would be to change line 83 and enter the correct date for your simulation.

Thank you for pointing this out. I will add a setDate function, so that the user can override the default with the date of their choice in an upstream script.

Pat
The administrator has disabled public write access.
The following user(s) said Thank You: taoan, josekdiaz

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

  • taoan
  • taoan's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 52
  • Thank you received: 9
Yes Pat, it is working after line 83 adjustment. I thougt this default date was very uncomom, but didn't link with Terminator movie :silly:

Thank you!
The administrator has disabled public write access.

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

  • josekdiaz
  • josekdiaz's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 161
  • Thank you received: 48
Hello Pat,

Offtopic, even after a couple of years this easter egg still makes me smile.
The administrator has disabled public write access.
The following user(s) said Thank You: pprodano

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

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

Just to remind you that some scripts already exists in Telemac.
The date change could be achieve with run_selafin.py (alter function)

Regards
Christophe
The administrator has disabled public write access.

read and write selafin files in Python 3 years 5 months ago #38585

  • nitesh
  • nitesh's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 73
  • Thank you received: 1
Hi,

I have a selafin file with space varying rainfall values for one time step. The shape of the array is (1, 24821)
I want to add values of more time steps. shape: (19,24821)
but while doing that I got the same error as Costas "IndexError: too many indices for array".
Can someone please help me to resolve this issue?

Best Regards,
Nitesh
from pputils_master.ppmodules.selafin_io_pp import *

# read *.slf file
slf = ppSELAFIN('Raini_15_12.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()

vunits.clear()
vunits.append('mm')

times.clear()
times.extend(range(0, 19))

vnames.clear()
vnames.append('RAINI')


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

print (results)
print(type(results))
results.shape
#[[1.61982179 1.60806787 1.63061941 ... 1.87916398 1.87916398 1.87916398]]
#<class 'numpy.ndarray'>
#(1, 24821)

results1 = np.concatenate((results, ar), axis=0)
results2=np.delete(results1,0,0)
results2.shape
#(19, 24821)

# 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()

for i in range (0,19):
    slf2.writeVariables(i, results2[i,:])

---> 14     slf2.writeVariables(i, results2[i,:])
IndexError: too many indices for array
The administrator has disabled public write access.

read and write selafin files in Python 3 years 5 months ago #38586

  • nitesh
  • nitesh's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 73
  • Thank you received: 1
Hi,

The error was resolved when I removed the semicolon and tried the following line of code:
for i in range (0,19):
    slf2.writeVariables(i, results2[i:])

But when I try to print the values for different time steps, why does it shows all the values equal to zero? Please help!
# read the variables for the time step 0
slf2.readVariables(0)
res0 = slf2.getVarValues()
print (res0)

[[0. 0. 0. ... 0. 0. 0.]]

# read the variables for the time step 1
slf2.readVariables(1)
res1 = slf2.getVarValues()
print (res1)

[[0. 0. 0. ... 0. 0. 0.]]

It should show the following values for time step 0 and 1 respectively:

[1.61982179 1.61982179 1.61982179 ... 1.87916398 1.87916398 1.87916398]

[0.23219785 0.23219785 0.23219785 ... 0.17322269 0.17322269 0.17322269]
The administrator has disabled public write access.

read and write selafin files in Python 3 years 5 months ago #38588

  • taoan
  • taoan's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 52
  • Thank you received: 9
Hello nitesh,

I think you can try this

for t in range (0,19):
slf2.writeVariables(t, np.array([results2[t]]))


I hope this helps

With best regards


Taoan
The administrator has disabled public write access.

read and write selafin files in Python 3 years 5 months ago #38587

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

The issue has to do with how you are assigning arguments to the writeVariables method. The first argument is the time index, and the second is a 2d array that has the shape of number of variables by number of mesh nodes ((NBV1, NPOIN)). When adding more time steps, you have to loop over each time step, and generate an ((NBV1,NPOIN)) size array for that time step.

Look at the bottom of shift_sel.py, the code is there. My suggestion is to start with an existing *.slf file, read it, and simply add/modify as necessary.

Hope this helps/ If it still doesn't work, post all the files and I will debug.

Best,

Pat
The administrator has disabled public write access.

read and write selafin files in Python 3 years 5 months ago #38593

  • nitesh
  • nitesh's Avatar
  • OFFLINE
  • Senior Boarder
  • Posts: 73
  • Thank you received: 1
Hi,

Thank you so much for the guidance.
I started with an existing .slf file 'Raini_15_12.slf' which has one variable but only has one time step.
"data_t19.csv" contains all the time steps' values for each node in the mesh.
I guess I followed the same technique as you mentioned but it doesn't seem to work for me.
Attached are all the required files to debug the code.

Thanks and Regards,
Nitesh
Attachments:
The administrator has disabled public write access.
Moderators: borisb

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