Welcome, Guest
Username: Password: Remember me

TOPIC: Converting Selafin files to NetCDF format

Converting Selafin files to NetCDF format 10 years 2 months ago #14141

  • Alex Goater
  • Alex Goater's Avatar
Hello all,

Does anyone have or know of a program for converting Selafin files to NetCDF format? I'd like to use the flows from a TELEMAC-2D run as input into another program that takes inputs in NetCDF format.

Thanks,
Alex
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14142

  • Lufia
  • Lufia's Avatar
Hi Alex,

I don't know about such a program. However you can use the python Selafinreader to read the Selafin file and write the data into a NetCDF file with python (e.g. netcdf4-python).

Best regards,

Leo
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14145

  • caiodalaqua
  • caiodalaqua's Avatar
Hi Alex,

I started writing such python script, but never had time to finish it.

I am attaching what I have done so far.


Cheers

Caio
Attachments:
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14156

  • Alex Goater
  • Alex Goater's Avatar
Thanks a lot for this Caio, looks like you've done most of the work for me!

I have a few questions on the script (note I'm fairly new to Python so bear with me!):

1. You call a function 'xytodegrees' to define lat,lon - I assume I need to write my own coordinate transform from whatever the selafin file coordinates are?

2. At the start you seem to do a date conversion from UTM to America/Sao_Paulo - I assume this is particular to the files you're working in and not always essential?

3. Apart from that the script looks complete, just need to close the NetCDF file and we're done I think, is that correct? Have I missed anything?

Thanks,
Alex
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14157

  • Alex Goater
  • Alex Goater's Avatar
**Apologies, ignore point 3 - I've found some other changes that are needed**
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14159

  • caiodalaqua
  • caiodalaqua's Avatar
Hi Alex,

You are wright in almost everything...

I forgot that the xytodegrees() was not in this file, sorry for this. I have my won "telemac tools" module and this function is declared there. It's a fairly simple function:

def xytodegrees(x,y):
	from pyproj import Proj
	p = Proj(proj='utm',zone='22S',ellps='WGS84')
	# minus 10000000.0 because it is in the southern hemisphere
	lons,lats=p(x,y-10000000.0,inverse=True)
	return lons,lats

You only have to be careful with which coordinate system you are using.

The timezone in the begging is not needed in the code, unless you want to convert the selafin dates to a particular timezone, in my case, it was São Paulo.

The script is actually far from being complete. In order to have a proper netCDF file you will have to interpolate the vertical planes. My script only does a horizontal interpolation. I assume the easiest way is to interpolate horizontally every plane first and than interpolate the sigma coordinates to a regular Z scheme.

As far as I remember there are more thinks to be done, such as properly define the netCDF objects. As I said before, you can use this as example, I really doubt the script will work as it is today.

Cheers

Caio
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 10 years 2 months ago #14210

  • Alex Goater
  • Alex Goater's Avatar
Hi all,

I've managed to get this working for my needs, see attached.

It reads a 2-D selafin results file, creates a regular grid, and interpolates the values onto this grid before writing to netcdf. The grid is also converted to lat long for the netcdf file.

I'm sure it can be improved or modified for your own needs, e.g. different coordinate systems, variables, times, 3-D variables, more user firendly etc. Note it has only been tested on one results file so no guarantees it works for everything!

Thanks Ciao for your help, I've mentioned you in the header, hope that's ok. One big difference from your script is that interpolation is performed in BNG coordinates, not lat long - a colleague pointed out to me that linear interpolation in lat long can go wrong as it's a spherical coordinates system!

Enjoy!

Alex
Attachments:
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 9 years 4 months ago #17650

  • caiodalaqua
  • caiodalaqua's Avatar
Hi folks,

I had to revisit the matter recently and ended writing a much improved and clean code. So I decide to share, it may be useful to someone.

Now, the script takes a .slf file and BlueKenue regular grid (.r2s), interpolates and writes the .nc file.

It can't handle 3d files yet, but an workaround is to use Postel to separate the individual planes, convert each one and use a tool such as CDO to merge the files.

Also, BlueKenue rotated grids (angle =/= 0) can't be used at this moment.

The user MUST install numpy, matplotlib, netCDF4 and Progressbar before trying to use the script.

The call is pretty much the same as before: python slf2nc.py Input.slf Outpout.nc RegGrid.r2s

The output netcdfs were tested with Panoply and Ncview and seems to be working fine.

Cheers,

Caio
Attachments:
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 7 years 8 months ago #25763

  • huyquangtran
  • huyquangtran's Avatar
  • OFFLINE
  • Expert Boarder
  • Posts: 271
  • Thank you received: 23
Thanks Python experts for sharing your scripts.

I have tried both scripts given by Alex and Caio, but I got some errors.

Could you help me to get out the error message I got below while converting *slf file:

1. Using sel2ncdf_2014-09-12-2.py:

python sel2ncdf_2014-09-12-2.py 2011 12 03 00 OUT_2D_0039F.slf WINDNC.c

Traceback (most recent call last):
File "sel2ncdf_2014-09-12-2.py", line 43, in <module>
from netCDF4 import Dataset # Writing netcdf files
ImportError: No module named netCDF4

2. Using slf2nc.py
python slf2nc.py 2011 12 03 00 OUT_2D_0039F.slf WINDNC.c

Traceback (most recent call last):
File "slf2nc.py", line 38, in <module>
from parsers.parserSELAFIN import SELAFIN
ImportError: No module named parsers.parserSELAFIN

NB: NetCDF has been installed on my pc (both Netcdf-C and NetCDF-Fortran 4.4.4)

Thanks & Regards
Huy
The administrator has disabled public write access.

Converting Selafin files to NetCDF format 7 years 8 months ago #25767

  • caiodalaqua
  • caiodalaqua's Avatar
Hi Huy,

I haven't used Telemac in few years now, but your errors seems to be all related packages not being properly installed.

Did you install python-NETCDF4 (unidata.github.io/netcdf4-python/)? You need it alongside the C and Fortran versions.

My script is complaining about the Telemac parsers that mostly probably have changed since when I wrote the script. You will need to dig a bit into the parser to fix the imports.

Progressbar may also have changed and may crash as well.

Cheers,

Caio
The administrator has disabled public write access.
Moderators: borisb

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