Batch renaming of HGS output binaries for HSPLOT

This post covers a useful workflow for renaming large groups of sequentially titled files, such as HydroGeoSphere’s binary PHGS output files. This process is particularly helpful when working with HSPLOT, which requires input files to be numbered sequentially starting from 1 (e.g., 0001, 0002, 0003, …n).


This week we will once again be reviewing a small workflow that many users may find useful, although it’s not really a HydroGeoSphere command. This little power-user tip will help you to rename large groups of sequentially titled files like the binary PHGS output files (e.g. prefixo.head_pm.0001, prefixo.head_pm.0002, prefixo.head_pm.0003, etc.). This is extremely helpful if you need to process only a portion of HGS outputs via HSPLOT.

For example, you may have a long spin-up period at the beginning of your model (e.g. 1000 timesteps) and you are only interested in visualizing the later timesteps (e.g. timesteps 1000-2000). Or perhaps your model crashed half-way through; so, you restarted the model from the crash point and now you need to stitch two sets of binary output files together.

This workflow will be helpful in either scenario (and many others), since the HSPLOT executable requires all input files to be in sequence started from 1 (e.g. 0001, 0002, 0003, …n).

To illustrate this workflow in action, you can download an example project (a variation of Abdul) here which includes all necessary input files:
0_1619530184588_Abdul_Batch_Rename.zip

Figure 1: Warning issues due to missing file

This project contains the results of a completed simulation, but for some reason the results from the first output time (*.0001) are not included. If HSPLOT.exe were to be executed at this stage it would fail to reformat the majority of the model outputs, since they don’t start sequentially at *.0001.

Figure 1 illustrates the results of HSPLOT.exe if executed at this stage.

In order to use HSPLOT on these results, all input files will need to be renamed. With 99 binary output files, this would be a very time-consuming task (even with a very small/simple model such as this one).

Therefore, you may consider using the rename.bat batch file within the ‘Batch rename file’ folder.

Before executing the batch file, let’s investigate what it actually does. Batch files are very simple script files which contain a series of commands to be executed in the command prompt. To review the contents of rename.bat you can right-click and select ‘Edit’ to open the file in a text editor (see Figure 2). The file contains 12 lines of text, each of which contains a simple ‘rename’ command. The ‘rename’ command includes two arguments; an argument to identify a file to rename (i.e. the ‘original file type’), and a new/updated file name.

Figure 2: Batch file contents

In the example project we fortunately only have 12 timesteps worth of files that need to be renamed. This means that it would be quite easy to write this ‘rename.bat’ file manually. However, it’s common to run simulations with hundreds or thousands of output times. In these instances, it’s easiest and fastest to use a spreadsheet to automate the scripting of the batch file.

If you open the ‘Batch file renaming.xlsx’ file you will see a simple spreadsheet which you can use to help create a ‘rename.bat’ file of your own. Simply update column C with the original file types/extensions, and then copy the results from column F into a new text file and save it with the ‘.bat’ extension (to identify it as a batch file).

Batch files are very simple but powerful script files that can be executed in the Windows environment.

WARNING: Batch files such as this rename.bat script can have unintended consequences of overwriting files if executed in the wrong folder or if they contain a mistake. If overwriting your data is a concern, it is best practice to make a back up of your model folder prior to testing out this method.

DISCLAIMER: This tip is provided at the end user’s own risk and no presumption of responsibility is implied for the person posting this tip or Aquanty.

To use the rename.bat file simply copy it into the main project folder and double-click it. Alternatively, you can open the command prompt and type "rename.bat" and hit enter. Either way, all output files in the project folder will be renamed:

Notes:

  • Make sure to update column D if you need to rename more than 9 input files. The input files for timesteps 1-1000 should always contain four digits.

  • The * portions of the rename.bat file are known as ‘wildcard’ characters. By using the *character each rename command will cause ALL files in the folder with the specified extension to be renamed.

Figure 3: Using the ‘Batch file renaming.xlsx’ file

Figure 4: Results of the rename.bat script

Previous
Previous

Aquanty Hosts Machine Learning Challenge: Enhancing Satellite Image Mosaics

Next
Next

Research Highlight - Ice Roughness Estimation via Remotely Piloted Aircraft and Photogrammetry