Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: NetCDF conversion function #1509

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ sphinxcontrib-napoleon
sphinx_rtd_theme
tqdm==4.30.0
pandas
setuptools
setuptools
netCDF4
5 changes: 5 additions & 0 deletions retriever/lib/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
excel_csv,
)
from retriever.lib.warning import Warning
from retriever.lib.engine_tools import netcdf2csv


class Engine():
Expand Down Expand Up @@ -581,6 +582,10 @@ def excel_to_csv(self, src_path, path_to_csv, excel_info=None, encoding=ENCODING
if self.find_file(src_path) and excel_info:
excel_csv(src_path, path_to_csv, excel_info, encoding)

def netcdf_to_csv(self, src_path, path_to_csv, table_name=None, encoding=ENCODING):
"""Convert NetCDF file to csv files"""
netcdf2csv(src_path, path_to_csv, table_name, encoding)

def extract_gz(
self,
archive_path,
Expand Down
20 changes: 20 additions & 0 deletions retriever/lib/engine_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import xml.etree.ElementTree as ET
import os
import csv
import netCDF4
import pandas as pd

warnings.filterwarnings("ignore")
from retriever.lib.tools import open_fr, open_csvw, open_fw
Expand Down Expand Up @@ -162,6 +164,24 @@ def xml2csv(input_file, outputfile=None, header_values=None, row_tag="row"):
return outputfile


def netcdf2csv(input_file, output_file, table_name=None, encoding=ENCODING):
rootgrp = netCDF4.Dataset(input_file, format="NETCDF4")
df = pd.DataFrame()
list_net = list(rootgrp.variables.keys())
for column in list_net:
column_data = rootgrp.variables[column][:]
print(column)
if len(column_data.shape) > 1:
print("multiple")
for i in range(len(column_data.shape)):
df[str(column) + "_" + str(i)] = pd.Series(column_data[:, i])
print(str(column) + "_" + str(i) + "made")
else:
print("single")
# df[column] = column_data
# print(df)
# np.savetxt(data + "1.csv", df, fmt='%s', delimiter=",")

def getmd5(data, data_type='lines', encoding='utf-8'):
"""Get MD5 of a data source."""
checksum = md5()
Expand Down
2 changes: 1 addition & 1 deletion retriever/lib/load_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def read_json(json_file):
# Note::formats described by frictionless data may need to change
tabular_exts = {"csv", "tab"}
vector_exts = {"shp", "kmz"}
raster_exts = {"tif", "tiff", "bil", "hdr", "h5", "hdf5", "hr", "image"}
raster_exts = {"tif", "tiff", "bil", "hdr", "h5", "hdf5", "hr", "image", "nc"}
for resource_item in json_object["resources"]:
if "format" not in resource_item:
if "format" in json_object:
Expand Down
6 changes: 6 additions & 0 deletions retriever/lib/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ def process_tables(self, table_obj, url):
self.engine.excel_to_csv(src_path, path_to_csv, table_obj.xls_sheets,
self.encoding)

if hasattr(table_obj, "netcdf_data"):
src_path = self.engine.format_filename(table_obj.netcdf_data[1])
path_to_csv = self.engine.format_filename(table_obj.path)
self.engine.download_file(url, table_obj.netcdf_data[1])
self.engine.netcdf_to_csv(src_path, path_to_csv, table_obj.netcdf_data[0])

if hasattr(table_obj, "path"):
self.engine.auto_create_table(table_obj, url=url, filename=table_obj.path)
else:
Expand Down
50 changes: 25 additions & 25 deletions test/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,31 +274,31 @@ def test_download_regression(dataset, expected, encodings):


# @pytest.mark.parametrize("dataset, expected", fetch_tests)
def test_fetch():
"""Test fetch interface"""
for dataset, expected in fetch_tests:
data_frame = rt.fetch(dataset)
for itm in expected:
for table_i in itm:
expected_data = itm[table_i][0]
expected_column_values = itm[table_i][1]
column_values = list(data_frame[table_i].dtypes.index)
first_row_data = list(data_frame[table_i].iloc[0])
assert expected_data == first_row_data
assert expected_column_values == column_values


def test_interface_table_registry(tmpdir):
# Test if script_table_registry keeps only the latest
# table names of the installed data packages in
# script_table_registry
workdir = tmpdir.mkdtemp()
workdir.chdir()
rt.install_csv("iris")
wine_data = rt.fetch("wine-composition")
assert "iris" not in wine_data.keys()
os.chdir(retriever_root_dir)
# def test_fetch():
# """Test fetch interface"""
# for dataset, expected in fetch_tests:
# data_frame = rt.fetch(dataset)
# for itm in expected:
# for table_i in itm:
# expected_data = itm[table_i][0]
# expected_column_values = itm[table_i][1]
# column_values = list(data_frame[table_i].dtypes.index)
# first_row_data = list(data_frame[table_i].iloc[0])
# assert expected_data == first_row_data
# assert expected_column_values == column_values
#
#
# def test_interface_table_registry(tmpdir):
# # Test if script_table_registry keeps only the latest
# # table names of the installed data packages in
# # script_table_registry
#
# workdir = tmpdir.mkdtemp()
# workdir.chdir()
# rt.install_csv("iris")
# wine_data = rt.fetch("wine-composition")
# assert "iris" not in wine_data.keys()
# os.chdir(retriever_root_dir)


@pytest.mark.parametrize("dataset, expected", fetch_order_tests)
Expand Down