Ignore:
Timestamp:
Jan 30, 2023, 9:27:31 PM (20 months ago)
Author:
dcugnet
Message:
  • fix for isotopic tagging tracers
  • modify strHead/strTail: last argument (optional) has the same convention as INDEX: if lBackward==.TRUE., search the pattern starting from the end of the string (default=.FALSE.)
  • consolidate and simplify the new2oldH2O routine
File:
1 edited

Legend:

Unmodified
Added
Removed
  • LMDZ6/trunk/libf/misc/strings_mod.F90

    r4363 r4403  
    194194
    195195!==============================================================================================================================
    196 !=== Extract the substring in front of the last (first if lFirst==TRUE) occurrence of separator "sep" in "str" ================
     196!=== Extract the substring in front of the first (last if lBackward==TRUE) occurrence of "sep" in "str"        ================
    197197!=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect:                           ================
    198198!===    * strHead(..,.FALSE.) = 'a'           ${str%%$sep*}                                                    ================
    199199!===    * strHead(..,.TRUE.)  = 'a_b'         ${str%$sep*}                                                     ================
    200200!==============================================================================================================================
    201 CHARACTER(LEN=maxlen) FUNCTION strHead_1(str,sep,lFirst) RESULT(out)
     201CHARACTER(LEN=maxlen) FUNCTION strHead_1(str, sep, lBackward) RESULT(out)
    202202  CHARACTER(LEN=*),           INTENT(IN) :: str
    203203  CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep
    204   LOGICAL,          OPTIONAL, INTENT(IN) :: lFirst
    205 !------------------------------------------------------------------------------------------------------------------------------
    206   LOGICAL :: lf
    207   lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst
     204  LOGICAL,          OPTIONAL, INTENT(IN) :: lBackward
     205!------------------------------------------------------------------------------------------------------------------------------
    208206  IF(PRESENT(sep)) THEN
    209     out = str(1:INDEX(str,sep,.NOT.lf)-1)
     207    IF(     PRESENT(lBackWard)) out = str(1:INDEX(str,sep,lBackWard)-1)
     208    IF(.NOT.PRESENT(lBackWard)) out = str(1:INDEX(str,sep)-1)
    210209  ELSE
    211     out = str(1:INDEX(str,'/',.NOT.lf)-1)
     210    IF(     PRESENT(lBackWard)) out = str(1:INDEX(str,'/',lBackWard)-1)
     211    IF(.NOT.PRESENT(lBackWard)) out = str(1:INDEX(str,'/')-1)
    212212  END IF
    213213  IF(out == '') out = str
    214214END FUNCTION strHead_1
    215215!==============================================================================================================================
    216 FUNCTION strHead_m(str,sep,lFirst) RESULT(out)
     216FUNCTION strHead_m(str, sep, lBackward) RESULT(out)
    217217  CHARACTER(LEN=maxlen),     ALLOCATABLE :: out(:)
    218218  CHARACTER(LEN=*),           INTENT(IN) :: str(:)
    219219  CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep
    220   LOGICAL,          OPTIONAL, INTENT(IN) :: lFirst
    221 !------------------------------------------------------------------------------------------------------------------------------
    222   LOGICAL :: lf
     220  LOGICAL,          OPTIONAL, INTENT(IN) :: lBackward
     221!------------------------------------------------------------------------------------------------------------------------------
    223222  INTEGER :: k
    224   lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst
    225223  IF(PRESENT(sep)) THEN
    226     out = [(strHead_1(str(k), sep,   lf), k=1, SIZE(str))]
     224    IF(     PRESENT(lBackWard)) out = [(strHead_1(str(k), sep, lBackWard), k=1, SIZE(str))]
     225    IF(.NOT.PRESENT(lBackWard)) out = [(strHead_1(str(k), sep),            k=1, SIZE(str))]
    227226  ELSE
    228     out = [(strHead_1(str(k), lFirst=lf), k=1, SIZE(str))]
     227    IF(     PRESENT(lBackWard)) out = [(strHead_1(str(k), '/', lBackWard), k=1, SIZE(str))]
     228    IF(.NOT.PRESENT(lBackWard)) out = [(strHead_1(str(k), '/'),            k=1, SIZE(str))]
    229229  END IF
    230230END FUNCTION strHead_m
    231231!==============================================================================================================================
    232 !=== Extract the substring following the last (first if lFirst==TRUE) occurrence of separator "sep" in "str"   ================
     232!=== Extract the substring following the first (last if lBackward==TRUE) occurrence of "sep" in "str"          ================
    233233!=== Examples for str='a_b_c' and sep='_' and the bash command with the same effect:                           ================
    234 !===    * strTail(..,.FALSE.) = 'c'           ${str#*$sep}                                                     ================
    235 !===    * strTail(..,.TRUE.)  = 'b_c'         ${str##*$sep}                                                    ================
    236 !==============================================================================================================================
    237 CHARACTER(LEN=maxlen) FUNCTION strTail_1(str,sep,lFirst) RESULT(out)
     234!===    * strTail(str, '_', .FALSE.) = 'b_c'         ${str#*$sep}                                              ================
     235!===    * strTail(str, '_', .TRUE.)  = 'c'           ${str##*$sep}                                             ================
     236!==============================================================================================================================
     237CHARACTER(LEN=maxlen) FUNCTION strTail_1(str, sep, lBackWard) RESULT(out)
    238238  CHARACTER(LEN=*),           INTENT(IN) :: str
    239239  CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep
    240   LOGICAL,          OPTIONAL, INTENT(IN) :: lFirst
    241 !------------------------------------------------------------------------------------------------------------------------------
    242   LOGICAL :: lf
    243   lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst
     240  LOGICAL,          OPTIONAL, INTENT(IN) :: lBackWard
     241!------------------------------------------------------------------------------------------------------------------------------
    244242  IF(PRESENT(sep)) THEN
    245     out = str(INDEX(str,sep,.NOT.lf)+LEN(sep):LEN_TRIM(str))
     243    IF(     PRESENT(lBackWard)) out = str(INDEX(str,sep,lBackWard)+LEN(sep):LEN_TRIM(str))
     244    IF(.NOT.PRESENT(lBackWard)) out = str(INDEX(str,sep)          +LEN(sep):LEN_TRIM(str))
    246245  ELSE
    247     out = str(INDEX(str,'/',.NOT.lf)+1:LEN_TRIM(str))
     246    IF(     PRESENT(lBackWard)) out = str(INDEX(str,'/',lBackWard)+1:LEN_TRIM(str))
     247    IF(.NOT.PRESENT(lBackWard)) out = str(INDEX(str,'/')          +1:LEN_TRIM(str))
    248248  END IF
    249249  IF(out == '') out = str
    250250END FUNCTION strTail_1
    251251!==============================================================================================================================
    252 FUNCTION strTail_m(str,sep,lFirst) RESULT(out)
     252FUNCTION strTail_m(str, sep, lBackWard) RESULT(out)
    253253  CHARACTER(LEN=maxlen),     ALLOCATABLE :: out(:)
    254254  CHARACTER(LEN=*),           INTENT(IN) :: str(:)
    255255  CHARACTER(LEN=*), OPTIONAL, INTENT(IN) :: sep
    256   LOGICAL,          OPTIONAL, INTENT(IN) :: lFirst
    257 !------------------------------------------------------------------------------------------------------------------------------
    258   LOGICAL :: lf
     256  LOGICAL,          OPTIONAL, INTENT(IN) :: lBackWard
     257!------------------------------------------------------------------------------------------------------------------------------
    259258  INTEGER :: k
    260   lf = .FALSE.; IF(PRESENT(lFirst)) lf=lFirst
    261259  IF(PRESENT(sep)) THEN
    262     out = [(strTail_1(str(k), sep,   lf), k=1, SIZE(str))]
     260    IF(     PRESENT(lBackWard)) out = [(strTail_1(str(k), sep, lBackWard), k=1, SIZE(str))]
     261    IF(.NOT.PRESENT(lBackWard)) out = [(strTail_1(str(k), sep),            k=1, SIZE(str))]
    263262  ELSE
    264     out = [(strTail_1(str(k), lFirst=lf), k=1, SIZE(str))]
     263    IF(     PRESENT(lBackWard)) out = [(strTail_1(str(k), '/', lBackWard), k=1, SIZE(str))]
     264    IF(.NOT.PRESENT(lBackWard)) out = [(strTail_1(str(k), '/'),            k=1, SIZE(str))]
    265265  END IF
    266266END FUNCTION strTail_m
Note: See TracChangeset for help on using the changeset viewer.