Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gwyddion gwy file import/export #5

Open
mpanighel opened this issue Jun 3, 2021 · 1 comment
Open

Add Gwyddion gwy file import/export #5

mpanighel opened this issue Jun 3, 2021 · 1 comment

Comments

@mpanighel
Copy link
Member

Using the gwyfile Python package.

Export example from RHK .sm4:

from gwyfile.objects import GwyContainer, GwyDataField, GwySIUnit
obj = GwyContainer()

filename = 'VT210531_A1_0025.sm4'
f = spym.load(filename)

for i, ch in enumerate(f.data_vars):

    datarr = f[ch]

    si_units_xy = GwySIUnit()
    si_units_z = GwySIUnit()
    meta = GwyContainer()
    df = GwyDataField(datarr.values)

    si_units_xy['unitstr'] = datarr.attrs['RHK_Xunits']
    si_units_z['unitstr'] = datarr.attrs['RHK_Zunits']

    for k,v in datarr.attrs.items():
        meta[k] = str(v)

    df['xres'] = datarr.attrs['RHK_Xsize']
    df['yres'] = datarr.attrs['RHK_Ysize']
    df['xreal'] = abs( datarr.attrs['RHK_Xscale'] * datarr.attrs['RHK_Xsize'])
    df['yreal'] = abs( datarr.attrs['RHK_Yscale'] * datarr.attrs['RHK_Ysize'])
    df['si_unit_xy'] = si_units_xy
    df['si_unit_z'] = si_units_z

    data_path = "/{}/data".format(i)
    meta_path = "/{}/meta".format(i)
    obj[data_path] = df
    obj[data_path+'/title'] = ch
    obj[meta_path] = meta

    if i==0:
        obj['/0/data/visible'] = True

obj.tofile(filename+'.gwy')
@mpanighel
Copy link
Member Author

Import example:

from xarray import Dataset, DataArray
import gwyfile
import numpy as np

filename = 'VT210531_A1_0032.sm4.gwy'
obj = gwyfile.load(filename)

ds = Dataset()

i = 0

while True:
    data_path = "/{}/data".format(i)
    meta_path = "/{}/meta".format(i)

    try:
        name = obj[data_path+"/title"]
    except KeyError:
        break

    xres = obj[data_path]["xres"]
    yres = obj[data_path]["yres"]
    xreal = obj[data_path]["xreal"]
    yreal = obj[data_path]["yreal"]

    data = obj[data_path]["data"].reshape(xres, yres)
    attrs = dict(obj[meta_path])
    for k,v in attrs.items():
        if k in ["scaling_factor", "offset", "bias", "setpoint", "feedback_pgain", "scan_angle", "time_per_point"]:
            attrs[k] = float(v)

    coords = [("x", np.linspace(0.0, xreal, num=xres)),
              ("y", np.linspace(0.0, yreal, num=yres))]

    ds[name] = DataArray(data,
                         coords=coords,
                         attrs=attrs)

    i += 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant