Skip to content

Commit

Permalink
(Feat) OceanCurrent script for Argo profiles (#1195)
Browse files Browse the repository at this point in the history
* (Feat) OceanCurrent script for Argo profiles

* (Feat) OceanCurrent script for Argo profiles: add README
  • Loading branch information
lbesnard authored Jun 3, 2024
1 parent 19ec04f commit af8697e
Show file tree
Hide file tree
Showing 31 changed files with 304 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ARGO/oceancurrent/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ARGO Profiles JSON Generator for the Ocean Current website

This script processes directories containing `.gif` files and generates a `profiles.json` file per argo platform directory with metadata extracted from the filenames. The filenames follow a specific pattern, and the extracted information includes the date, profile number, and cycle number.

## Directory Structure

The script expects a directory structure as follows:

/mnt/oceancurrent/website/profiles/
├── platform_code1/
│ ├── YYYYMMDD_profileNumber_cycleNumber.gif
│ ├── YYYYMMDD_profileNumber_cycleNumber.html
│ └── ...
├── platform_code2/
│ ├── YYYYMMDD_profileNumber_cycleNumber.gif
│ ├── YYYYMMDD_profileNumber_cycleNumber.html
│ └── ...
└── ...


Where:
- `YYYYMMDD` is the date.
- `profileNumber` is the profile number.
- `cycleNumber` is the cycle number.

## Script Overview

The script performs the following steps:
1. Lists all platform codes in the specified `PROFILES_PATH` directory.
2. For each platform code, lists all files in the corresponding directory.
3. Filters the filenames to include only `.gif` files.
4. Extracts the date, profile number, and cycle number from each filename using a regular expression.
5. Creates a `profiles.json` file per directory containing the extracted information.

## Usage

### Running the Script

1. Confirm the `PROFILES_PATH` variable in the script to point to the base directory containing the platform code directories.
2. Run the script using Python as a cronjob:

```bash
python3 argo_oceancurrent.py
```

## Unittesting
Run the unittests with ```pytest```

47 changes: 47 additions & 0 deletions ARGO/oceancurrent/argo_oceancurrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import os
import re
import json

# Specify the directory path
PROFILES_PATH = "/mnt/oceancurrent/website/profiles/"


def main():

platform_codes = os.listdir(PROFILES_PATH)

for platform_code in platform_codes:
# List all files and directories in the specified directory
profile_path = os.path.join(PROFILES_PATH, platform_code)
all_files = os.listdir(profile_path)

filenames = [f for f in all_files if os.path.isfile(os.path.join(profile_path, f))]

# Regular expression pattern to match the gtif filenames
pattern = re.compile(r'(\d{8})_(\d+)_(\d+)\.gif')

profiles = []
# Iterate over each gtif
for filename in filenames:
match = pattern.match(filename)
if match:
date, profile_number, cycle_number = match.groups()
profiles.append({
"date": date,
"cycle": cycle_number,
"filename": filename
})

profiles_json = json.dumps(profiles, indent=4)

# Write the JSON string to a file in the path of the profile
profile_json_path = os.path.join(profile_path, "profiles.json")

with open(profile_json_path, "w") as f:
f.write(profiles_json)

print(f"{profile_json_path} created successfully.")

if __name__ == '__main__':
main()
85 changes: 85 additions & 0 deletions ARGO/oceancurrent/test_argo_oceancurrent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import json
import shutil
import tempfile
import unittest
from unittest.mock import patch

from argo_oceancurrent import main, PROFILES_PATH


class TestProfilesJsonGeneration(unittest.TestCase):

def setUp(self):
# Create a temporary directory
self.test_dir = tempfile.mkdtemp()
self.profiles_test_dir = os.path.join(self.test_dir, 'mnt/oceancurrent/website/profiles')

# Path to the existing test files
self.existing_test_files_path = os.path.join(os.path.dirname(__file__), 'tests')

# Copy all test files to the temporary directory
for item in os.listdir(self.existing_test_files_path):
s = os.path.join(self.existing_test_files_path, item)
d = os.path.join(self.test_dir, item)
if os.path.isdir(s):
shutil.copytree(s, d, False, None)
else:
shutil.copy2(s, d)

def tearDown(self):
# Remove the temporary directory and all its contents
shutil.rmtree(self.test_dir)

def test_profiles_json_generation(self):
with patch('argo_oceancurrent.PROFILES_PATH', new=self.profiles_test_dir):
# Run the main from argo_oceancurrent to create the profiles.json
main()

# Verify the generated profiles.json for one platform code
platform_code = '7901108'
generated_json_path = os.path.join(self.profiles_test_dir, platform_code, "profiles.json")
self.assertTrue(os.path.exists(generated_json_path), f"{generated_json_path} was not created")

with open(generated_json_path, 'r') as f:
generated_json = json.load(f)

# Define the expected JSON content based on the test files
expected_json = [
{
"date": "20240415",
"cycle": "4",
"filename": "20240415_7901108_4.gif"
},
{
"date": "20240506",
"cycle": "6",
"filename": "20240506_7901108_6.gif"
},
{
"date": "20240516",
"cycle": "7",
"filename": "20240516_7901108_7.gif"
},
{
"date": "20240405",
"cycle": "3",
"filename": "20240405_7901108_3.gif"
},
{
"date": "20240426",
"cycle": "5",
"filename": "20240426_7901108_5.gif"
},
{
"date": "20240526",
"cycle": "8",
"filename": "20240526_7901108_8.gif"
}
]

self.assertEqual(generated_json, expected_json, f"The generated profiles.json content in {platform_code} is incorrect")

if __name__ == '__main__':
unittest.main()

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240407_7901107_3.gif 20240407_7901107_3 20240417_7901107_4 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240407_7901107_3.html>[PREV]</a> <a href=20240417_7901107_4.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240407_7901107_3.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240417_7901107_4.gif 20240407_7901107_3 20240427_7901107_5 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240407_7901107_3.html>[PREV]</a> <a href=20240427_7901107_5.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240417_7901107_4.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240427_7901107_5.gif 20240417_7901107_4 20240508_7901107_6 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240417_7901107_4.html>[PREV]</a> <a href=20240508_7901107_6.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240427_7901107_5.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240508_7901107_6.gif 20240427_7901107_5 20240518_7901107_7 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240427_7901107_5.html>[PREV]</a> <a href=20240518_7901107_7.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240508_7901107_6.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240518_7901107_7.gif 20240508_7901107_6 20240528_7901107_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240508_7901107_6.html>[PREV]</a> <a href=20240528_7901107_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240518_7901107_7.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240528_7901107_8.gif 20240518_7901107_7 20240528_7901107_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240518_7901107_7.html>[PREV]</a> <a href=20240528_7901107_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240528_7901107_8.gif></P></BODY> </html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html> <title> 7901107 </title>
<BODY BGCOLOR = "#FFFFFF">
<a href="../../index.htm">[<em>OceanCurrent</em>]</a> <a href="../map/latest.html">[Argo map]</a><br>
<br><a name=2024><b>----- 2024 -----</b></a>
<br><b> Apr</b>
<a href=20240407_7901107_3.html>07</a>
<a href=20240417_7901107_4.html>17</a>
<a href=20240427_7901107_5.html>27</a>
<br><b> May</b>
<a href=20240508_7901107_6.html>08</a>
<a href=20240518_7901107_7.html>18</a>
<a href=20240528_7901107_8.html>28</a>
</BODY> </html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901107 </title>
<!-- 20240528_7901107_8.gif 20240518_7901107_7 20240528_7901107_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240518_7901107_7.html>[PREV]</a> <a href=20240528_7901107_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240528_7901107_8.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240405_7901108_3.gif 20240405_7901108_3 20240415_7901108_4 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240405_7901108_3.html>[PREV]</a> <a href=20240415_7901108_4.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240405_7901108_3.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240415_7901108_4.gif 20240405_7901108_3 20240426_7901108_5 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240405_7901108_3.html>[PREV]</a> <a href=20240426_7901108_5.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240415_7901108_4.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240426_7901108_5.gif 20240415_7901108_4 20240506_7901108_6 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240415_7901108_4.html>[PREV]</a> <a href=20240506_7901108_6.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240426_7901108_5.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240506_7901108_6.gif 20240426_7901108_5 20240516_7901108_7 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240426_7901108_5.html>[PREV]</a> <a href=20240516_7901108_7.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240506_7901108_6.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240516_7901108_7.gif 20240506_7901108_6 20240526_7901108_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240506_7901108_6.html>[PREV]</a> <a href=20240526_7901108_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240516_7901108_7.gif></P></BODY> </html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240526_7901108_8.gif 20240516_7901108_7 20240526_7901108_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240516_7901108_7.html>[PREV]</a> <a href=20240526_7901108_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240526_7901108_8.gif></P></BODY> </html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html> <title> 7901108 </title>
<BODY BGCOLOR = "#FFFFFF">
<a href="../../index.htm">[<em>OceanCurrent</em>]</a> <a href="../map/latest.html">[Argo map]</a><br>
<br><a name=2024><b>----- 2024 -----</b></a>
<br><b> Apr</b>
<a href=20240405_7901108_3.html>05</a>
<a href=20240415_7901108_4.html>15</a>
<a href=20240426_7901108_5.html>26</a>
<br><b> May</b>
<a href=20240506_7901108_6.html>06</a>
<a href=20240516_7901108_7.html>16</a>
<a href=20240526_7901108_8.html>26</a>
</BODY> </html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html> <title> 7901108 </title>
<!-- 20240526_7901108_8.gif 20240516_7901108_7 20240526_7901108_8 -->
<BODY BGCOLOR = "#FFFFFF">
<a name="top"></a>
<P align = "left">
<a href=../../index.htm>[<em>OceanCurrent</em>]</a><a href=20240516_7901108_7.html>[PREV]</a> <a href=20240526_7901108_8.html>[NEXT]</a> <a href=./index.html>[DATE INDEX]</a> <a href=../whatsshown.htm>[WHAT'S SHOWN]</a> <a href=http://imos.org.au/imostermsofuse.html>[Conditions of use]</a> <BR>
<IMG src=20240526_7901108_8.gif></P></BODY> </html>

0 comments on commit af8697e

Please sign in to comment.