From dbb2db0d31b4a23b4980c70d93f2f0a5b21774de Mon Sep 17 00:00:00 2001
From: Bjarke Tobias Olsen <btol@dtu.dk>
Date: Fri, 17 Dec 2021 10:03:45 +0100
Subject: [PATCH] Added from_pywasp_pwc constructor to XRSite

---
 py_wake/site/xrsite.py | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/py_wake/site/xrsite.py b/py_wake/site/xrsite.py
index ce23c7845..d962fbc8c 100644
--- a/py_wake/site/xrsite.py
+++ b/py_wake/site/xrsite.py
@@ -303,6 +303,46 @@ class XRSite(Site):
                 xr_site = XRSite(ds)
         return xr_site
 
+    @classmethod
+    def from_pywasp_pwc(cls, pwc, **kwargs):
+        """ Instanciate XRSite from a pywasp predicted wind climate (PWC) xr.Dataset
+
+        Parameters
+        ----------
+        pwc : xr.Dataset
+            pywasp predicted wind climate dataset. At a minimum should contain
+            "A", "k", and "wdfreq".
+
+        """
+        renames = {
+            "wdfreq": "Sector_frequency",
+            "A": "Weibull_A",
+            "k": "Weibull_k",
+            "turbulence_intensity": "TI",
+            "sector": "wd",
+            "point": "i",
+            "west_east": "x",
+            "south_north": "y",
+            "height": "h",
+        }
+
+        pwc_renamed = pwc.rename({k: v for k, v in renames.items() if k in pwc})
+        pwc_renamed = pwc_renamed.transpose("i", "wd", ...)
+
+        ws_mean = xr.apply_ufunc(
+            weibull.mean, pwc_renamed["Weibull_A"], pwc_renamed["Weibull_k"]
+        )
+        speedup = ws_mean / ws_mean.max(dim="i")
+        if "Speedup" not in pwc_renamed.data_vars:
+            pwc_renamed["Speedup"] = speedup
+
+        # Add TI and P if not already present
+        for var in ["P", "TI"]:
+            if var not in pwc_renamed.data_vars:
+                pwc_renamed[var] = pwc_renamed["Weibull_A"] * 0.0
+
+        return cls(pwc_renamed, **kwargs)
+
 
 class UniformSite(XRSite):
     """Site with uniform (same wind over all, i.e. flat uniform terrain) and
-- 
GitLab