diff --git a/diag_manager/fms_diag_field_object.F90 b/diag_manager/fms_diag_field_object.F90 index 69b5fc626..0eb4eff0a 100644 --- a/diag_manager/fms_diag_field_object.F90 +++ b/diag_manager/fms_diag_field_object.F90 @@ -806,18 +806,31 @@ end function get_vartype !> @brief Gets varname !! @return copy of the variable name -pure function get_varname (this, to_write) & +function get_varname (this, filename, to_write) & result(rslt) - class (fmsDiagField_type), intent(in) :: this !< diag object - logical, optional, intent(in) :: to_write !< .true. if getting the varname that will be writen to the file + class (fmsDiagField_type), intent(in) :: this !< diag object + character(len=*), optional, intent(in) :: filename !< Name of the diag_file we are writing to + logical, optional, intent(in) :: to_write !< .true. if getting the varname that will be writen to the file character(len=:), allocatable :: rslt + + integer :: i + rslt = this%varname !< If writing the varname can be the outname which is defined in the yaml if (present(to_write)) then if (to_write) then - !TODO this is wrong - rslt = this%diag_field(1)%get_var_outname() + if (.not. present(filename)) then + call mpp_error(FATAL, "get_varname was called using the to_write optional argument, "//& + "but a filename was not provided!") + endif + ! Loop through all of the file the variable is in and find the outputname of the variable + do i = 1, size(this%diag_field) + if (trim(filename) .eq. trim(this%diag_field(i)%get_var_fname())) then + rslt = this%diag_field(i)%get_var_outname() + return + endif + enddo endif endif @@ -1677,7 +1690,7 @@ function get_default_missing_value(var_type) & !> @brief Determines the diag_obj id corresponding to a module name and field_name !> @return diag_obj id -PURE FUNCTION diag_field_id_from_name(this, module_name, field_name) & +FUNCTION diag_field_id_from_name(this, module_name, field_name) & result(diag_field_id) CLASS(fmsDiagField_type), INTENT(in) :: this !< The field object CHARACTER(len=*), INTENT(in) :: module_name !< Module name that registered the variable @@ -1995,14 +2008,14 @@ subroutine generate_associated_files_att(this, att, start_time) character(len=128) :: start_date !< Start date to append to the begining of the filename integer :: year, month, day, hour, minute, second - field_name = this%get_varname(to_write = .true.) + + file_name = this%get_field_file_name() + field_name = this%get_varname(to_write = .true., filename=file_name) ! Check if the field is already in the associated files attribute (i.e the area can be associated with multiple ! fields in the file, but it only needs to be added once) if (index(att, field_name) .ne. 0) return - file_name = this%get_field_file_name() - if (prepend_date) then call get_date(start_time, year, month, day, hour, minute, second) write (start_date, '(1I20.4, 2I2.2)') year, month, day diff --git a/diag_manager/fms_diag_file_object.F90 b/diag_manager/fms_diag_file_object.F90 index 3f55ac0e4..eac19fe0f 100644 --- a/diag_manager/fms_diag_file_object.F90 +++ b/diag_manager/fms_diag_file_object.F90 @@ -1828,7 +1828,8 @@ subroutine write_field_metadata(this, diag_field, diag_axis) cell_measures = "" if (field_ptr%has_area()) then - cell_measures = "area: "//diag_field(field_ptr%get_area())%get_varname(to_write=.true.) + cell_measures = "area: "//diag_field(field_ptr%get_area())%get_varname(to_write=.true., & + filename=diag_file%get_file_fname()) !! Determine if the area field is already in the file. If it is not create the "associated_files" attribute !! which contains the file name of the file the area field is in. This is needed for PP/fregrid. @@ -1839,7 +1840,8 @@ subroutine write_field_metadata(this, diag_field, diag_axis) endif if (field_ptr%has_volume()) then - cell_measures = trim(cell_measures)//" volume: "//diag_field(field_ptr%get_volume())%get_varname(to_write=.true.) + cell_measures = trim(cell_measures)//" volume: "//diag_field(field_ptr%get_volume())%get_varname(& + to_write=.true., filename=diag_file%get_file_fname()) !! Determine if the volume field is already in the file. If it is not create the "associated_files" attribute !! which contains the file name of the file the volume field is in. This is needed for PP/fregrid. diff --git a/test_fms/diag_manager/test_cell_measures.F90 b/test_fms/diag_manager/test_cell_measures.F90 index 0c1a479a6..f23654645 100644 --- a/test_fms/diag_manager/test_cell_measures.F90 +++ b/test_fms/diag_manager/test_cell_measures.F90 @@ -100,5 +100,16 @@ subroutine check_output() if (trim(buffer) .ne. "area: area") & call mpp_error(FATAL, "The cell_measures attribute is not the expected result! "//trim(buffer)) call close_file(fileobj) + + ! Check that file1.nc exists, that the var1 exists and it contains the cell_measures attributes + ! Here area is in the file, but the output name is area_file2 instead of area + if (.not. open_file(fileobj, "file2.nc", "read")) & + call mpp_error(FATAL, "file1.nc was not created by the diag manager!") + call get_variable_attribute(fileobj, "var1", "cell_measures", buffer) + if (trim(buffer) .ne. "area: area_file2") & + call mpp_error(FATAL, "The cell_measures attribute is not the expected result! ("//trim(buffer)//") vs "//& + "(area: area_file2)") + call close_file(fileobj) + call close_file(fileobj) end subroutine check_output end program diff --git a/test_fms/diag_manager/test_cell_measures.sh b/test_fms/diag_manager/test_cell_measures.sh index b47d11b61..afa5cc769 100755 --- a/test_fms/diag_manager/test_cell_measures.sh +++ b/test_fms/diag_manager/test_cell_measures.sh @@ -31,15 +31,6 @@ title: test_diag_manager base_date: 2 1 1 0 0 0 diag_files: -- file_name: static_file - freq: -1 - time_units: hours - unlimdim: time - varlist: - - module: fun_mod - var_name: area - reduction: none - kind: r4 # Here file 1 does not have the "area" variable so the associated files attribute is expected - file_name: file1 freq: 6 hours @@ -50,6 +41,26 @@ diag_files: var_name: var1 reduction: average kind: r4 +- file_name: file2 + freq: 1 hours + time_units: hours + unlimdim: time + module: fun_mod + reduction: none + kind: r4 + varlist: + - var_name: var1 + - var_name: area + output_name: area_file2 +- file_name: static_file + freq: -1 + time_units: hours + unlimdim: time + varlist: + - module: fun_mod + var_name: area + reduction: none + kind: r4 _EOF # remove any existing files that would result in false passes during checks