Skip to content

Commit

Permalink
new netcdf data plugin (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucR31 authored May 6, 2024
1 parent 79d00cf commit 64033ed
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Empty file added aiida_flexpart/data/__init__.py
Empty file.
44 changes: 44 additions & 0 deletions aiida_flexpart/data/nc_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
from aiida.orm import RemoteData
from netCDF4 import Dataset

class NetCDFData(RemoteData):

def __init__(self, filepath=None, remote_path=None, **kwargs):
"""
Data plugin for Netcdf files.
"""
super().__init__(**kwargs)
if filepath is not None:
filename = os.path.basename(filepath)
self.set_remote_path(remote_path)
self.set_filename(filename)

# open and read as NetCDF
nc_file = Dataset(filepath, mode="r")
self.set_global_attributes(nc_file)

def set_filename(self, val):
self.base.attributes.set("filename", val)

def set_global_attributes(self, nc_file):

g_att = {}
for a in nc_file.ncattrs():
g_att[a] = repr(nc_file.getncattr(a))
self.base.attributes.set("global_attributes", g_att)

nc_dimensions = {i: len(nc_file.dimensions[i]) for i in nc_file.dimensions}
self.base.attributes.set("dimensions", nc_dimensions)

def ncdump(self):
"""
Small python version of ncdump.
"""
print("dimensions:")
for k, v in self.base.attributes.get("dimensions").items():
print("\t%s =" % k, v)

print("// global attributes:")
for k, v in self.base.attributes.get("global_attributes").items():
print("\t:%s =" % k, v)
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ docs = [
[project.entry-points."aiida.workflows"]
"flexpart.multi_dates" = "aiida_flexpart.workflows.multi_dates_workflow:FlexpartMultipleDatesWorkflow"

[project.entry-points."aiida.data"]
"netcdf.data" = "aiida_flexpart.data.nc_data:NetCDFData"


[tool.pylint.format]
max-line-length = 125

Expand Down

0 comments on commit 64033ed

Please sign in to comment.