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

Support for Creating and Setting NetCDF Attributes in Fortran #42

Merged
merged 6 commits into from
Feb 14, 2025

Conversation

amstokely
Copy link
Collaborator

@amstokely amstokely commented Feb 11, 2025

Description:

This PR adds support for creating and writing attributes in NetCDF files from Fortran using the NetCDF C++ API. It simplifies defining attributes, specifying data types, and assigning values, handling global, group-scoped, and variable-specific attributes.

Key Features:

  • Attribute Creation: Supports defining attributes in global, group, and variable contexts.
  • Data Writing: Enables assigning integer, real, and string values with automatic C/Fortran string conversion.
  • Fortran-Friendly Interface: Abstracts low-level C++ API details, ensuring easy integration.

Motivation:

This enhancement provides a streamlined Fortran interface for managing NetCDF attributes, eliminating manual memory handling and C interoperability complexities.

@amstokely amstokely changed the title Feature/netcdf cxx attributes Support for Creating and Setting NetCDF Attributes in Fortran Feb 11, 2025
@amstokely amstokely requested review from mgduda and jim-p-w February 11, 2025 22:01
@amstokely amstokely self-assigned this Feb 11, 2025
@mgduda
Copy link
Collaborator

mgduda commented Feb 12, 2025

With #39 merged, can the base branch for this PR be changed to main?

@amstokely amstokely changed the base branch from feature/netcdf_cxx_variables to main February 12, 2025 18:21
@amstokely amstokely changed the base branch from main to feature/netcdf_cxx_variables February 12, 2025 18:22
@amstokely amstokely force-pushed the feature/netcdf_cxx_attributes branch from e87ebbe to 3e67cbe Compare February 12, 2025 18:42
@amstokely amstokely changed the base branch from feature/netcdf_cxx_variables to main February 12, 2025 18:42
@amstokely amstokely force-pushed the feature/netcdf_cxx_attributes branch from 3e67cbe to 3c1e423 Compare February 12, 2025 18:47
@amstokely amstokely requested a review from mgduda February 12, 2025 22:54
@jim-p-w
Copy link
Collaborator

jim-p-w commented Feb 13, 2025

As a general observation, putting every parameter to a function call on it's own line, and the closing ); on it's own line makes the code harder to read. I can't see the entire definition of netcdfPutAtt without scrolling. This seems more legible:

            if (varName) {
                auto var = group->getVar(varName);

                if (netcdfDataType == netCDF::ncString) {
                  var.putAtt(attName, std::string(reinterpret_cast<const char *>(values)));
                } else {
                    var.putAtt( attName, netcdfDataType, len, values);
                }
            } else {
                if (netcdfDataType == netCDF::ncString) {
                  group->putAtt(attName, std::string( reinterpret_cast<const char *>(values)));
                } else {
                    group->putAtt( attName, netcdfDataType, len, values);
                }
            }

obs2ioda-v2/src/cxx/netcdf_attribute.h Outdated Show resolved Hide resolved
obs2ioda-v2/src/cxx/netcdf_attribute.h Show resolved Hide resolved
obs2ioda-v2/src/netcdf_cxx_mod.f90 Outdated Show resolved Hide resolved
obs2ioda-v2/src/cxx/netcdf_attribute.cc Outdated Show resolved Hide resolved
@amstokely
Copy link
Collaborator Author

As a general observation, putting every parameter to a function call on it's own line, and the closing ); on it's own line makes the code harder to read. I can't see the entire definition of netcdfPutAtt without scrolling. This seems more legible:

            if (varName) {
                auto var = group->getVar(varName);

                if (netcdfDataType == netCDF::ncString) {
                  var.putAtt(attName, std::string(reinterpret_cast<const char *>(values)));
                } else {
                    var.putAtt( attName, netcdfDataType, len, values);
                }
            } else {
                if (netcdfDataType == netCDF::ncString) {
                  group->putAtt(attName, std::string( reinterpret_cast<const char *>(values)));
                } else {
                    group->putAtt( attName, netcdfDataType, len, values);
                }
            }

I adjusted my linter to only wrap function calls/declarations if they exceed 72 characters.

@amstokely amstokely requested a review from jim-p-w February 13, 2025 22:31
@amstokely amstokely merged commit 67fe7b9 into main Feb 14, 2025
amstokely added a commit that referenced this pull request Feb 18, 2025
* Added support for writing netcdf attributes using the netcdf-cxx interface.

* Updated PutAtt functions so that they all use the same templated function.

* Removed duplicate defs of setFill interface functions.

* Changed signatures for putAtt functions.

* Generalized template docstrings.

* Updated order of putAtt args in fortran docstring.

Removed template declarations in netcdf_variable.h, updated docs, and removed all getVar related netcdf functions.

Renamed netcdfPutVar data argument to values.

Removed @tparam from netcdf variable docstrings and added a default class to setFill/putVar select type statements.

Added support for writing netcdf attributes.

Removed unused c_f_pointer import

Replaced netcdf fortran io ops with netcdf-cxx ops in ncio_mod.f90
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

Successfully merging this pull request may close these issues.

3 participants