NOAA-PMEL / PyFerret

The PyFerret program and Python module from NOAA/PMEL
https://ferret.pmel.noaa.gov/Ferret/
The Unlicense
58 stars 22 forks source link

Get list of variables #43

Closed PBrockmann closed 7 years ago

PBrockmann commented 7 years ago

A new method that returns the list of variables from open datasets or defined aggregations would be very convenient.

For now, I have been able to capture the "show data" information about variables by doing

pyferret.run("set redirect/clobber/file=tyty.txt stdout")
pyferret.showdata(brief=True)
pyferret.run("cancel redirect")

but this needs extra work to parse all the output displayed.

aDict = pyferret.listVariables()     

would be much easier.

This method could return an array as follows:

{'levitus_climatology.cdf': {
                           'vars': ['TEMP', 'SALT'],
                           'dims': [{'I': [1, 360], 'J': [1, 180], 'K': [1, 31], 'L': None, 'M': None, 'N': None},
                                       {'I': [1, 360],  'J': [1, 180], 'K': [1, 31], 'L': None, 'M': None, 'N': None}],
                             },
 'monthly_navy_winds.cdf': {
                           'vars': ['UWND', 'VWND'],
                           'dims': [{'I': [1, 144], 'J': [1, 73], 'K': None, 'L': [1, 132], 'M': None, 'N': None},
                                       {'I': [1, 144], 'J': [1, 73], 'K': None, 'L': [1, 132], 'M': None, 'N': None}],
                            }
}

Then list variables comes from:

list((aDict[object]['vars'] for object in aDict))
[['TEMP', 'SALT'], ['UWND', 'VWND']]
karlmsmith commented 7 years ago

I desperately need to update the PyFerret documentation. There is now a getstrdata method returns a string variable (ie, string array; I still need to add a method to return a single string symbols). In my FerDSet object, I use:

namesdict = pyferret.getstrdata('..varnames')
for varname in namesdict['data'].flatten():
    if sys.version_info[0] > 2:
        # For Python3.x: namesdict['data'] is a NumPy array of bytes; convert to unicode
        varname = str(varname, 'UTF-8')

I could add a method that returns an array of the variable names (pretty much just the code above).

But you might want to look into the FerDSet object

>>> lev = pyferret.FerDSet('levitus_climatology.cdf')
>>> dir(lev)
['SALT', 'TEMP', ..., 'salt', 'temp']
>>> lev.temp
FerVar(varname='TEMP', dsetname='levitus_climatology.cdf', title = '', defn='TEMP[d=levitus_climatology.cdf]')
PBrockmann commented 7 years ago

Indeed, the release note describes exactly what I was looking with the use of pyferret.getstrdata('..varnames') Thanks Karl.

PBrockmann commented 7 years ago

I have just tested this getstrdata method on my old laptop (PyFerret-7.0.0-Ubuntu-14.04-32) and I get different results than with my workstation (pyferret-7.1.0-Ubuntu-14.04-64).

errval, errmsg = pyferret.run('use levitus_climatology')
varnamesdict = pyferret.getstrdata('..varnames')
varnamesdict['data'].flatten()

I get with pyferret-7.1.0-Ubuntu-14.04-64 array(['TEMP', 'SALT'], dtype='|S5') but with "older" PyFerret-7.0.0-Ubuntu-14.04-32 array(['TEMP', '\x04'], dtype='|S5') --> no SALT variable ?

Is the 32 architecture definitely abandoned ?

karlmsmith commented 7 years ago

That is an error I had seen before and fixed, so not surprised this is showing up in the old version. I can build an Ubuntu-14.04 32-bit version; just was waiting to see if it was still needed.

PBrockmann commented 7 years ago

If it is not too painfull to build, I would like to have one for this architecture. Many thanks.

karlmsmith commented 7 years ago

Ends up this was a bug in the 32-bit build that had not been fixed. As I was in the middle to getting the updates to memory management and other changes from Ferret into PyFerret, I added the fix into this latest v7.1.1-beta release and did not try making a one-off update to v7.1.

I will get the Ubuntu pre-built bundles uploaded for v7.1.1-beta. And you can try out the new memory management.

karlmsmith commented 7 years ago

pre-built builds added, and this should now be fixed in the v7.1.1 Ubuntu-14.04-32 build.