From 78ee0b670bda8debf078c399f304b781c7de7f65 Mon Sep 17 00:00:00 2001 From: raychew Date: Mon, 20 May 2024 14:57:00 +0200 Subject: [PATCH] made all imports relative; package now works with pip install I finally have a reason to move away from the iPython environment, and so now all imports should be done properly --- .gitignore | 2 +- inputs/icon_regional_run.py | 8 ++++--- inputs/local_paths_example.py | 2 +- pyproject.toml | 27 +++++++++++++++++++++++ src/io.py | 40 +++++++++++++++++++++++------------ src/utils.py | 14 +++++++++++- src/var.py | 2 +- wrappers/interface.py | 4 ++-- 8 files changed, 76 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 56fa884..aa67871 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,7 @@ *.json *.bat *.log - +*.egg-info /docs/build/* .VSCodeCounter/* diff --git a/inputs/icon_regional_run.py b/inputs/icon_regional_run.py index 222bcac..5589946 100644 --- a/inputs/icon_regional_run.py +++ b/inputs/icon_regional_run.py @@ -1,6 +1,6 @@ import numpy as np -from src import var, utils -from inputs import local_paths +from ..src import var, utils +from ..inputs import local_paths params = var.params() @@ -23,6 +23,8 @@ params.tri_set = [13, 104, 105, 106] +params.merit_cg = 20 + # Setup the Fourier parameters and object. params.nhi = 24 params.nhj = 48 @@ -34,7 +36,7 @@ params.rect = True params.debug = False -params.dfft_first_guess = True +params.dfft_first_guess = False params.refine = False params.verbose = False diff --git a/inputs/local_paths_example.py b/inputs/local_paths_example.py index 7251182..db100c2 100644 --- a/inputs/local_paths_example.py +++ b/inputs/local_paths_example.py @@ -1,4 +1,4 @@ -from src import var +from ..src import var paths = var.obj() diff --git a/pyproject.toml b/pyproject.toml index 966dee6..804171b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,3 +1,30 @@ +[project] +name = "pyCSAM" +version = "0.95.1" + +dependencies = [ + "Cartopy==0.21.1", + "h5py==3.9.0", + "ipython==8.12.3", + "matplotlib==3.7.2", + "netCDF4==1.6.5", + "noise==1.2.2", + "numba==0.57.1", + "numpy==1.24.3", + "pandas==2.0.3", + "scikit_learn==1.3.0", + "scipy==1.12.0", +] + +# Packaging +[build-system] +requires = ["setuptools"] +build-backend = "setuptools.build_meta" + +[tool.setuptools] +package-dir = {"pycsam" = ""} + + [tool.towncrier] directory = "changelog.d" filename = "CHANGELOG.rst" diff --git a/src/io.py b/src/io.py index cdb2f6f..022f86c 100644 --- a/src/io.py +++ b/src/io.py @@ -8,7 +8,7 @@ import os from datetime import datetime -from src import utils +from ..src import utils class ncdata(object): @@ -570,40 +570,52 @@ def __init__(self, params): rootgrp = nc.Dataset(self.path + self.fn, "w", format="NETCDF4") + for key, value in vars(params).items(): + + # if params attribute is None but check passed, then the attribute is not necessary for the run; skip it + if value is None: + continue + # NetCDF does not accept Boolean types; convert to int + if type(value) is bool: + value = int(value) + # Else, write attribute + setattr(rootgrp, key, value) + _ = rootgrp.createDimension("nspec", params.n_modes) self.n_modes = params.n_modes rootgrp.close() - def output(self, id, clat, clon, analysis): + def output(self, id, clat, clon, is_land, analysis=None): rootgrp = nc.Dataset(self.path + self.fn, "a", format="NETCDF4") grp = rootgrp.createGroup(str(id)) is_land_var = grp.createVariable("is_land","i4") - is_land_var[:] = 1 + is_land_var[:] = is_land clat_var = grp.createVariable("clat","f8") clat_var[:] = clat clon_var = grp.createVariable("clon","f8") clon_var[:] = clon - dk_var = grp.createVariable("dk","f8") - dk_var[:] = analysis.dk - dl_var = grp.createVariable("dl","f8") - dl_var[:] = analysis.dl + if analysis is not None: + dk_var = grp.createVariable("dk","f8") + dk_var[:] = analysis.dk + dl_var = grp.createVariable("dl","f8") + dl_var[:] = analysis.dl - pick_idx = np.where(analysis.ampls > 0) + pick_idx = np.where(analysis.ampls > 0) - H_spec_var = grp.createVariable("H_spec","f8", ("nspec",)) - H_spec_var[:] = self.__pad_zeros(analysis.ampls[pick_idx], self.n_modes) + H_spec_var = grp.createVariable("H_spec","f8", ("nspec",)) + H_spec_var[:] = self.__pad_zeros(analysis.ampls[pick_idx], self.n_modes) - kks_var = grp.createVariable("kks","f8", ("nspec",)) - kks_var[:] = self.__pad_zeros(analysis.kks[pick_idx], self.n_modes) + kks_var = grp.createVariable("kks","f8", ("nspec",)) + kks_var[:] = self.__pad_zeros(analysis.kks[pick_idx], self.n_modes) - lls_var = grp.createVariable("lls","f8", ("nspec",)) - lls_var[:] = self.__pad_zeros(analysis.lls[pick_idx], self.n_modes) + lls_var = grp.createVariable("lls","f8", ("nspec",)) + lls_var[:] = self.__pad_zeros(analysis.lls[pick_idx], self.n_modes) rootgrp.close() diff --git a/src/utils.py b/src/utils.py index 08a34de..62e275f 100644 --- a/src/utils.py +++ b/src/utils.py @@ -818,4 +818,16 @@ def transfer_attributes(params, cls, prefix=""): if not hasattr(params, key): setattr(params, key, value) elif getattr(params, key) == None: - setattr(params, key, value) \ No newline at end of file + setattr(params, key, value) + + +def is_land(cell, simplex_lat, simplex_lon, topo, height_tol=0.5, percent_tol=0.95): + + get_lat_lon_segments( + simplex_lat, simplex_lon, cell, topo, load_topo=True, filtered=False + ) + + if not (((cell.topo <= height_tol).sum() / cell.topo.size) > percent_tol): + return False + else: + return True \ No newline at end of file diff --git a/src/var.py b/src/var.py index 5b1fd90..a310062 100644 --- a/src/var.py +++ b/src/var.py @@ -3,7 +3,7 @@ """ import numpy as np -from src import utils, io +from ..src import utils, io class grid(object): diff --git a/wrappers/interface.py b/wrappers/interface.py index 39d2186..a092357 100644 --- a/wrappers/interface.py +++ b/wrappers/interface.py @@ -3,8 +3,8 @@ """ -from src import fourier, lin_reg, physics, reconstruction -from src import utils, var +from ..src import fourier, lin_reg, physics, reconstruction +from ..src import utils, var from copy import deepcopy import numpy as np