rewrite ensure_cpg_file function to prevent frequent file writing #3673
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Describe the PR
The original
ensure_cpg_file
function was called very frequently wheneverget_zone_geometry
is used. Particularly, when using the energy potential - photovoltaic-thermal panels functionality. In each separate PVT calculation, this function was called around 12 times.The original implementation of
ensure_cpg_file
is to just writezone.cpg
file to desired content: "ISO-8859-1". When using a cloud drive, for example, OneDrive, to sync the scenarios, such frequent writing will cause OneDrive to sync this .cpg file constantly. Sometimes this cloud drive sync will block CEA to write it, causing PermissionError when trying to write this file again, forcing CEA to exit the current batch simulation. For example, my batch of PVT simulation stops at 1728/1752, and it's hard to find out which one has not been simulated yet...A straightforward solution will be first open the file with read access, check if the content is already there. If so, there's no need to change anything and just end the function; if not, then the file is re-opened using
with
, and this time the correct content is written into the file.To Test
I have tested it on my own computer and make sure that this function worked correctly with both correct and wrong content in
zone.cpg
after several runs. However, there's not yet a systematic testing on all cases.Steps to test this PR:
zone.cpg
in/inputs/geometry
folder;get_zone_geometry
;zone.cpg
file again, confirm that the content isISO-8859-1
.Expected behaviour
Behaviour should not be affected.
Extra Data Needed
None.