ファイル作成用内部サブルーチン.
655 use dc_date,
only: dcdifftimecreate, evalbyunit
661 & historycreate, historyaddvariable, historyaddattr, &
662 & historyinitialized, historyput, historyputaxismpi, &
663 & historyaxiscreate, historyaxisinquire, historyaxiscopy, &
664 & historyvarinfoinquire, historyvarinfocreate, &
665 & historyvarinfocopy, historyvarinfoinitialized, &
666 & historyvarinfoclear
667
668 implicit none
669 type(GT_HISTORY), intent(inout):: gthist
670
671
672 character(*), intent(in):: varname
673
674
675 real(DP), intent(in):: time
676
677
678 character(TOKEN):: interval_unit
679
680
681 real(DP):: origin_value
682
683
684 character(TOKEN):: origin_unit
685
686
687
688 real(DP):: origin_sec
689 integer:: newfile_intvalue
690 real(DP):: newfile_intvalued
691
692
693 character(TOKEN):: newfile_intunit
694
695
696
697 character(STRING):: file, file_base, file_suffix, file_newfile_time, file_rank
698
699
700 integer:: stat, i, j, k, vnum, numdims_slice, dim_size, cnt
701 character(STRING):: name, units, longname, cause_c, wgt_name
702 character(TOKEN):: xtype
703 type(GT_HISTORY_AXIS):: gthst_axes_time
704 type(GT_HISTORY_AXIS), pointer:: gthst_axes_slices(:) =>null()
705 type(GT_HISTORY_AXIS_DATA), pointer:: data_axes_slices(:) =>null()
706 type(GT_HISTORY_AXIS_DATA), pointer:: data_weights_slices(:) =>null()
707 real(DP):: wgt_sum, wgt_sum_s
708 logical:: slice_valid
709 integer:: slice_start(1:numdims-1)
710
711
712 integer:: slice_end(1:numdims-1)
713
714
715 integer:: slice_stride(1:numdims-1)
716
717
718
719 character(*), parameter:: subname = "HstFileCreate"
720 continue
721 call beginsub(subname,
'varname=%c', c1 = trim(varname) )
723 cause_c = ""
724
725
726
727
728 vnum = 0
729 do i = 1, numvars
730 call historyvarinfoinquire( &
731 & varinfo = gthst_vars(i), &
732 & name = name )
733 if ( trim(varname) == trim(name) ) vnum = i
734 end do
735
736 if ( vnum == 0 ) then
738 cause_c = varname
739 goto 999
740 end if
741
742
743
744
746 goto 999
747 end if
748
749
750
751
753 & gthstnml = gthstnml, &
754 & name = varname, &
755 & file = file, &
756 & interval_unit = interval_unit )
757
758 call historyaxiscopy( &
759 & gthst_axes_time, &
760 & gthst_axes(numdims), &
761 & units = trim(interval_unit) // ' ' // &
762 & trim(time_unit_suffix) )
763
764
765
766
768 & gthstnml = gthstnml, &
769 & name = varname, &
770 & slice_start = slice_start, &
771 & slice_end = slice_end, &
772 & slice_stride = slice_stride )
773
774
775
776
777 if ( .not. historyinitialized( gthist ) ) then
778
779 if ( all( slice_start == (/ ( 1, i = 1, numdims -1 ) /) ) &
780 & .and. all( slice_end < (/ ( 1, i = 1, numdims -1 ) /) ) &
781 & .and. all( slice_stride == (/ ( 1, i = 1, numdims -1 ) /) ) ) then
782
783 allocate( gthst_axes_slices(1:numdims) )
784 gthst_axes_slices(1:numdims-1) = gthst_axes(1:numdims-1)
785 gthst_axes_slices(numdims:numdims) = gthst_axes_time
786
787 data_axes_slices => data_axes
788 data_weights_slices => data_weights
789 slice_valid = .false.
790
791 else
792 allocate( gthst_axes_slices(1:numdims) )
793 allocate( data_axes_slices(1:numdims) )
794 allocate( data_weights_slices(1:numdims) )
795
796 do i = 1, numdims-1
797
798
799
800
801 if ( slice_start(i) < 1 ) then
803 cause_c =
cprintf(
'slice_start=%d', &
804 & i = (/ slice_start(i) /) )
805 goto 999
806 end if
807
808 if ( slice_stride(i) < 1 ) then
810 cause_c =
cprintf(
'slice_stride=%d', &
811 & i = (/ slice_stride(i) /) )
812 goto 999
813 end if
814
815
816
817
818 if ( ( slice_start(i) == 1 ) &
819 & .and. ( slice_end(i) < 1 ) &
820 & .and. ( slice_stride(i) == 1 ) ) then
821
822 call historyaxiscopy( &
823 & axis_dest = gthst_axes_slices(i) , &
824 & axis_src = gthst_axes(i) )
825
826 data_axes_slices(i) = data_axes(i)
827
828 cycle
829 end if
830
831
832
833
834 call historyaxisinquire( &
835 & axis = gthst_axes(i), &
836 & name = name, &
837 & size = dim_size, &
838 & longname = longname, &
839 & units = units, &
840 & xtype = xtype )
841
842
843 if ( slice_end(i) < 1 ) slice_end(i) = dim_size
844 if ( slice_end(i) > dim_size ) then
846 & 'slice options to (%c) are undesirable ' // &
847 & '(@slice_end=%d). @slice_end is corrected forcibly to (%d) ', &
848 & c1 = trim(name), &
849 & i = (/ slice_end(i), dim_size /) )
850
851 slice_end(i) = dim_size
852 end if
853
854
855 if ( slice_start(i) > slice_end(i) ) then
857 cause_c =
cprintf(
'slice_start=%d, slice_end=%d', &
858 & i = (/ slice_start(i), slice_end(i) /) )
859 goto 999
860 end if
861
862 numdims_slice = int( ( slice_end(i) - slice_start(i) + 1 ) / slice_stride(i) )
863
864
865 if ( numdims_slice < 1 ) then
867 & 'slice options to (%c) are invalid. ' // &
868 & '(@slice_start=%d @slice_end=%d @slice_stride=%d)', &
869 & c1 = trim(name), &
870 & i = (/ slice_start(i), slice_end(i), slice_stride(i) /) )
872 cause_c =
cprintf(
'slice_start=%d, slice_end=%d, slice_stride=%d', &
873 & i = (/ slice_start(i), slice_end(i), slice_stride(i) /) )
874 goto 999
875 end if
876
877 call historyaxiscreate( &
878 & axis = gthst_axes_slices(i), &
879 & name = name, &
880 & size = numdims_slice, &
881 & longname = longname, &
882 & units = units, &
883 & xtype = xtype )
884
885
886
887
888
889 allocate( data_axes_slices(i) % a_axis( numdims_slice ) )
890 cnt = 1
891 do j = slice_start(i), slice_end(i), slice_stride(i)
892 data_axes_slices(i) % a_axis( cnt ) = data_axes(i) % a_axis( j )
893 cnt = cnt + 1
894 end do
895
896
897
898
899 do j = 1, numwgts
900 call historyvarinfoinquire( &
901 & varinfo = gthst_weights(j), &
902 & name = wgt_name )
903
904 if ( trim(name) // wgtsuf == trim(wgt_name) ) then
905
906
907
908
909 wgt_sum = sum( data_weights(j) % a_axis )
910
911 allocate( data_weights_slices(j) % a_axis( numdims_slice ) )
912 cnt = 1
913 do k = slice_start(i), slice_end(i), slice_stride(i)
914 data_weights_slices(j) % a_axis( cnt ) = data_weights(j) % a_axis( k )
915 cnt = cnt + 1
916 end do
917
918 wgt_sum_s = sum( data_weights_slices(j) % a_axis )
919 data_weights_slices(j) % a_axis = data_weights_slices(j) % a_axis * ( wgt_sum / wgt_sum_s )
920
921 end if
922
923 end do
924
925 end do
926
927
928
929
930 do i = 1, numwgts
931 if ( .not. associated( data_weights_slices(i) % a_axis ) ) then
932 allocate( data_weights_slices(i) % a_axis( size(data_weights(i) % a_axis ) ) )
933 data_weights_slices(i) % a_axis = data_weights(i) % a_axis
934 end if
935 end do
936
937
938
939
940 gthst_axes_slices(numdims) = gthst_axes_time
941
942 slice_valid = .true.
943 end if
944
945
946
947
949 & gthstnml = gthstnml, &
950 & name = varname, &
951 & file = file, &
952 & origin_value = origin_value, &
953 & origin_unit = origin_unit, &
954 & interval_unit = interval_unit, &
955 & newfile_intvalue = newfile_intvalue, &
956 & newfile_intunit = newfile_intunit )
957
958
959
960
961 origin_sec = &
962 & dccalconvertbyunit( &
963 & real( origin_value, dp ), origin_unit, 'sec', cal_save )
964
965
966
967
968
969
970
971 if ( newfile_intvalue < 1 ) then
972
973 origin_value = dccalconvertbyunit( &
974 & origin_sec, 'sec', interval_unit, cal_save )
975
976
977 else
978
979 origin_value = &
980 & dccalconvertbyunit( time, 'sec', interval_unit, cal_save )
981
982
983 end if
984
985
986
987
988 if ( len_trim( file ) - index(file, '.nc', .true.) == 2 ) then
989 file_base = file(1:len_trim( file ) - 3)
990 file_suffix = '.nc'
991 else
992 file_base = file
993 file_suffix = ''
994 end if
995 if ( trim(rank_save) == '' ) then
996 file_rank = ''
997 else
998 file_rank = '_rank' // trim( adjustl(rank_save) )
999 end if
1000 if ( newfile_intvalue > 0 ) then
1001 newfile_intvalued = &
1002 & dccalconvertbyunit( time, 'sec', newfile_intunit, cal_save )
1003
1004 file_newfile_time = &
1005 &
cprintf(
'_time%08d', i = (/ int( newfile_intvalued ) /) )
1006
1007 else
1008 file_newfile_time = ''
1009 end if
1010
1011 file = trim(file_base) // trim(file_rank) // trim(file_newfile_time) // trim(file_suffix)
1012
1013
1014
1015
1016 call historycreate( &
1017 & history = gthist, &
1018 & file = file, title = title_save, &
1019 & source = source_save, institution = institution_save, &
1020 & axes = gthst_axes_slices(1:numdims), &
1021 & origind = origin_value, &
1022 & conventions = conventions_save, &
1023 & gt_version = gt_version_save, &
1024 & flag_mpi_split = save_mpi_split, &
1025 & flag_mpi_gather = save_mpi_gather )
1026
1027
1028
1029
1030 do i = 1, numdims - 1
1031 call historyaxisinquire( &
1032 & axis = gthst_axes_slices(i), &
1033 & name = name )
1034 call historyput( &
1035 & history = gthist, &
1036 & varname = name, &
1037 & array = data_axes_slices(i) % a_axis )
1038 end do
1039
1040
1041
1042
1043 if ( save_mpi_gather ) then
1044 do i = 1, numdims - 1
1045 call historyaxisinquire( &
1046 & axis = gthst_axes_slices(i), &
1047 & name = name )
1048
1049 if ( .not. associated( data_axes_whole(i) % a_axis ) ) then
1051 & 'data of axis (%c) in whole area is lack. ' // &
1052 & 'Specify the data by "HistoryAutoPutAxisMPI" explicitly.', &
1053 & c1 = trim(name) )
1055 cause_c = name
1056 end if
1057
1058 call historyputaxismpi( &
1059 & history = gthist, &
1060 & varname = name, &
1061 & array = data_axes_whole(i) % a_axis )
1062 end do
1063 end if
1064
1065
1066
1067
1068 if ( slice_valid ) then
1069 deallocate( gthst_axes_slices )
1070 deallocate( data_axes_slices )
1071 else
1072 deallocate( gthst_axes_slices )
1073 nullify( data_axes_slices )
1074 end if
1075
1076
1077
1078
1079 do i = 1, numwgts
1080 call historyaddvariable( &
1081 & history = gthist, &
1082 & varinfo = gthst_weights(i) )
1083 call historyvarinfoinquire( &
1084 & varinfo = gthst_weights(i), &
1085 & name = name )
1086 call historyput( &
1087 & history = gthist, &
1088 & varname = name, &
1089 & array = data_weights_slices(i) % a_axis )
1090 end do
1091
1092 if ( slice_valid ) then
1093 deallocate( data_weights_slices )
1094 else
1095 nullify( data_weights_slices )
1096 end if
1097
1098
1099 end if
1100
1101
1102
1103
1104
1105 call historyaddvariable( &
1106 & varinfo = gthst_vars(vnum), &
1107 & history = gthist )
1108
1109999 continue
1110 call storeerror(stat, subname, cause_c = cause_c)
subroutine, public storeerror(number, where, err, cause_c, cause_i)
integer, parameter, public hst_ebadslice
integer, parameter, public hst_ebadvarname
integer, parameter, public dc_noerr
エラー等を保持
integer, parameter, public hst_empinoaxisdata
character(string) function, public joinchar(carray, expr)
subroutine, public beginsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca, version)
subroutine, public endsub(name, fmt, i, r, d, l, n, c1, c2, c3, ca)