Check character array dimension naming and size and return it.
(strlen, encoding, name)
| 123 | |
| 124 | |
| 125 | def validate_char_dim_name(strlen, encoding, name) -> str: |
| 126 | """Check character array dimension naming and size and return it.""" |
| 127 | |
| 128 | if (char_dim_name := encoding.pop("char_dim_name", None)) is not None: |
| 129 | # 1 - extract all characters up to last number sequence |
| 130 | # 2 - extract last number sequence |
| 131 | match = re.search(r"^(.*?)(\d+)(?!.*\d)", char_dim_name) |
| 132 | if match: |
| 133 | new_dim_name = match.group(1) |
| 134 | if int(match.group(2)) != strlen: |
| 135 | emit_user_level_warning( |
| 136 | f"String dimension naming mismatch on variable {name!r}. {char_dim_name!r} provided by encoding, but data has length of '{strlen}'. Using '{new_dim_name}{strlen}' instead of {char_dim_name!r} to prevent possible naming clash.\n" |
| 137 | "To silence this warning either remove 'char_dim_name' from encoding or provide a fitting name." |
| 138 | ) |
| 139 | char_dim_name = f"{new_dim_name}{strlen}" |
| 140 | elif ( |
| 141 | original_shape := encoding.get("original_shape", [-1])[-1] |
| 142 | ) != -1 and original_shape != strlen: |
| 143 | emit_user_level_warning( |
| 144 | f"String dimension length mismatch on variable {name!r}. '{original_shape}' provided by encoding, but data has length of '{strlen}'. Using '{char_dim_name}{strlen}' instead of {char_dim_name!r} to prevent possible naming clash.\n" |
| 145 | f"To silence this warning remove 'original_shape' from encoding." |
| 146 | ) |
| 147 | char_dim_name = f"{char_dim_name}{strlen}" |
| 148 | else: |
| 149 | char_dim_name = f"string{strlen}" |
| 150 | |
| 151 | return char_dim_name |
| 152 | |
| 153 | |
| 154 | class CharacterArrayCoder(VariableCoder): |
no test coverage detected
searching dependent graphs…