Skip to content

Struggling with write_file() function #257

@rba27

Description

@rba27

Hello! Thank you so much for this lovely Python package. It has been very useful in my biological oceanography research.

I am having a problem saving my Eddies Observations object to a usable file. When I try to save the object to a NetCDF file, I get the following error:

Traceback (most recent call last):
[...]
TypeError: illegal data type for attribute b'rotation_type', must be one of dict_keys(['S1', 'i1', 'u1', 'i2', 'u2', 'i4', 'u4', 'i8', 'u8', 'f4', 'f8']), got O

I am able to save the file to zarr format, but when I then attempt to open it again in a different script using load_from_zarr(), I get this error:

"/Users/miniconda3/envs/eddy_tracker/lib/python3.10/site-packages/py_eddy_tracker/observations/observation.py", line 819, in load_from_zarr
   track_array_variables = h_zarr.attrs["track_array_variables"]
 File "/Users/miniconda3/envs/eddy_tracker/lib/python3.10/site-packages/zarr/attrs.py", line 72, in __getitem__
   return self.asdict()[item]
KeyError: 'track_array_variables' 

Here is the code I am using to produce the Eddy Observations:

from datetime import datetime
import numpy as np
import pandas as pd
from py_eddy_tracker import data
from py_eddy_tracker.dataset.grid import RegularGridDataset
from py_eddy_tracker.observations.observation import EddiesObservations
from netCDF4 import Dataset
from py_eddy_tracker.eddy_feature import Contours
from matplotlib import pyplot as plt

def start_axes(title):
   fig = plt.figure(figsize=(13, 8))
   ax = fig.add_axes([0.03, 0.03, 0.90, 0.94])
   ax.set_xlim(279, 304), ax.set_ylim(29, 44)
   ax.set_aspect("equal")
   ax.set_title(title, weight="bold")
   return ax


def update_axes(ax, mappable=None):
   ax.grid()
   if mappable:
       plt.colorbar(mappable, cax=ax.figure.add_axes([0.94, 0.05, 0.01, 0.9]))

file_path = "/Users/Desktop/EddyTracking/new_adt_22_23_360.nc"

output_path = "./Eddies_GS/"
output_file = "gs_eddies_observations.nc"
nc_file = Dataset(file_path, 'r')
time_var = nc_file.variables['time']
time_values = time_var[:] 
nc_file.close()

base_date = datetime(2022, 1, 11)

a_eddy_observations = EddiesObservations()
c_eddy_observations = EddiesObservations()

for t, time in enumerate(time_values):
   print(f"starting analysis for time {t}")
   grid = RegularGridDataset(
       file_path,
       "longitude",
       "latitude",
       indexs=dict(
           time = t
       ),
   )

   grid.bessel_high_filter("adt", 700)

   date = base_date + pd.Timedelta(days=t) 
   a, c = grid.eddy_identification(
       "adt",  
       "ugos", "vgos",  
       date,
       0.002,
       shape_error=55
   )

   a_eddy_observations = a_eddy_observations.merge(a)
   c_eddy_observations = c_eddy_observations.merge(c)

a_eddy_observations.write_file(
   output_path,
   "a_test_eddies_observations.nc",
   zarr_flag=False

)

c_eddy_observations.write_file(
   output_path,
   "c_test_eddies_observations.nc",
   zarr_flag=False

)

print("All eddy observations have been processed and saved.")

Do you know what may be causing this issue? Thank you so much for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions