Skip to content

Commit

Permalink
extend the Fortran interface of MultiField to return its FieldSet; ex…
Browse files Browse the repository at this point in the history
…tend FieldSet to concatenate two FieldSets
  • Loading branch information
sbrdar committed Sep 6, 2023
1 parent 2f0a600 commit 67600f3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/atlas/field/FieldSet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ void atlas__FieldSet__add_field(FieldSetImpl* This, FieldImpl* field) {
This->add(field);
}

void atlas__FieldSet__add_fieldset(FieldSetImpl* This, FieldSetImpl* fieldset) {
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
ATLAS_ASSERT(fieldset != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
for(int i = 0; i < fieldset->size(); i++) {
This->add(fieldset->operator[](i));
}
}

int atlas__FieldSet__has_field(const FieldSetImpl* This, char* name) {
ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
return This->has(std::string(name));
Expand Down
1 change: 1 addition & 0 deletions src/atlas/field/FieldSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ extern "C" {
FieldSetImpl* atlas__FieldSet__new(char* name);
void atlas__FieldSet__delete(FieldSetImpl* This);
void atlas__FieldSet__add_field(FieldSetImpl* This, FieldImpl* field);
void atlas__FieldSet__add_fieldset(FieldSetImpl* This, FieldSetImpl* fieldset);
int atlas__FieldSet__has_field(const FieldSetImpl* This, char* name);
const char* atlas__FieldSet__name(FieldSetImpl* This);
idx_t atlas__FieldSet__size(const FieldSetImpl* This);
Expand Down
10 changes: 7 additions & 3 deletions src/atlas/field/detail/MultiFieldInterface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace atlas {
namespace field {

extern "C" {
::atlas::field::MultiFieldImpl* atlas__MultiField__create(eckit::Configuration* config) {
MultiFieldImpl* atlas__MultiField__create(eckit::Configuration* config) {
ATLAS_ASSERT(config != nullptr);
long nproma = config->getLong("nproma");
long nlev = config->getLong("nlev");
Expand Down Expand Up @@ -59,7 +59,7 @@ ::atlas::field::MultiFieldImpl* atlas__MultiField__create(eckit::Configuration*
}
auto multiarray_shape = array::make_shape(nblk, nfld, nlev, nproma);

::atlas::field::MultiFieldImpl* multifield = new ::atlas::field::MultiFieldImpl{array::ArraySpec{datatype, multiarray_shape}};
MultiFieldImpl* multifield = new MultiFieldImpl{array::ArraySpec{datatype, multiarray_shape}};
auto& multiarray = multifield->array();

size_t multiarray_field_idx = 0;
Expand Down Expand Up @@ -119,9 +119,13 @@ ::atlas::field::MultiFieldImpl* multifield = new ::atlas::field::MultiFieldImpl{
return multifield;
}

void atlas__MultiField__delete(::atlas::field::MultiFieldImpl* This) {
void atlas__MultiField__delete(MultiFieldImpl* This) {
delete This;
}

FieldSet* atlas__MultiField__fieldset(MultiFieldImpl* This) {
return &This->fieldset();
}
}

// ------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions src/atlas/field/detail/MultiFieldInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#pragma once

#include "atlas/field/FieldSet.h"
#include "atlas/field/MultiField.h"

namespace atlas {
Expand All @@ -30,6 +31,7 @@ namespace field {
extern "C" {
MultiFieldImpl* atlas__MultiField__create(eckit::Configuration* config);
void atlas__MultiField__delete(MultiFieldImpl* This);
FieldSet* atlas__MultiField__fieldset(MultiFieldImpl* This);
}

//----------------------------------------------------------------------------------------------------------------------
Expand Down
13 changes: 11 additions & 2 deletions src/atlas_f/field/atlas_FieldSet_module.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ contains
procedure, private :: field_by_name
procedure, private :: field_by_idx_int
procedure, private :: field_by_idx_long
procedure, public :: add
procedure, public :: add_field
procedure, public :: add_fset
generic :: add => add_field, add_fset
generic :: field => field_by_name, field_by_idx_int, field_by_idx_long

procedure, public :: set_dirty
Expand Down Expand Up @@ -213,14 +215,21 @@ function FieldSet__name(this) result(fieldset_name)
fieldset_name = c_ptr_to_string(fieldset_name_c_str)
end function FieldSet__name

subroutine add(this,field)
subroutine add_field(this,field)
use atlas_fieldset_c_binding
use atlas_Field_module, only: atlas_Field
class(atlas_FieldSet), intent(in) :: this
type(atlas_Field), intent(in) :: field
call atlas__FieldSet__add_field(this%CPTR_PGIBUG_A, field%CPTR_PGIBUG_A)
end subroutine

subroutine add_fset(this,fset)
use atlas_fieldset_c_binding
class(atlas_FieldSet), intent(inout) :: this
class(atlas_FieldSet), intent(in) :: fset
call atlas__FieldSet__add_fieldset(this%CPTR_PGIBUG_A, fset%CPTR_PGIBUG_A)
end subroutine

function has(this,name) result(flag)
use, intrinsic :: iso_c_binding, only: c_int
use fckit_c_interop_module, only: c_str
Expand Down
16 changes: 16 additions & 0 deletions src/atlas_f/field/atlas_MultiField_module.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module atlas_multifield_module

use fckit_owned_object_module, only : fckit_owned_object
use atlas_Config_module, only: atlas_Config
use atlas_fieldset_module, only: atlas_fieldset

implicit none

Expand Down Expand Up @@ -42,6 +43,8 @@ TYPE, extends(fckit_owned_object) :: atlas_MultiField

!------------------------------------------------------------------------------
contains
procedure, public :: MultiField__fieldset
generic :: fieldset => MultiField__fieldset

#if FCKIT_FINAL_NOT_INHERITING
final :: atlas_MultiField__final_auto
Expand Down Expand Up @@ -83,6 +86,19 @@ end function

!-------------------------------------------------------------------------------

function MultiField__fieldset(this) result(fset)
use, intrinsic :: iso_c_binding, only : c_ptr
use atlas_multifield_c_binding
class(atlas_MultiField), intent(in) :: this
type(c_ptr) :: fset_cptr
type(atlas_FieldSet) :: fset
fset_cptr = atlas__MultiField__fieldset(this%CPTR_PGIBUG_B)
fset = atlas_FieldSet( fset_cptr )
call fset%return()
end function

!-------------------------------------------------------------------------------

#if FCKIT_FINAL_NOT_INHERITING
ATLAS_FINAL subroutine atlas_MultiField__final_auto(this)
type(atlas_MultiField), intent(inout) :: this
Expand Down

0 comments on commit 67600f3

Please sign in to comment.