Skip to content

Commit

Permalink
Update mesan unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Mar 31, 2024
1 parent 10602b4 commit 52d8c58
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 208 deletions.
26 changes: 8 additions & 18 deletions src/smhi/mesan.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def parameters(self) -> Parameters:
Returns:
parameter model
"""
return self._parameters.parameter
return self._parameters

def _get_parameters(self) -> Parameters:
"""Get parameters from SMHI.
Expand Down Expand Up @@ -140,8 +140,9 @@ def get_geo_multipoint(self, downsample: int = 2) -> Polygon:
multipoint polygon model
"""
downsample = self._check_downsample(downsample)
multipoint_url = f"geotype/multipoint.json?downsample={downsample}"
data, headers, status = self._get_data(self._base_url + multipoint_url)
data, headers, status = self._get_data(
self._base_url + f"geotype/multipoint.json?downsample={downsample}"
)

return self.__polygon_model(
status=status,
Expand All @@ -164,8 +165,9 @@ def get_point(
Returns:
point data model
"""
url = self._build_point_url(longitude, latitude)
data, headers, status = self._get_data(url)
data, headers, status = self._get_data(
self._base_url + f"geotype/point/lon/{longitude}/lat/{latitude}/data.json"
)
data_table, info_table = self._format_data_point(data)

return self.__point_data_model(
Expand Down Expand Up @@ -314,18 +316,6 @@ def _format_data_multipoint(self, data: dict) -> Optional[pd.DataFrame]:

return pd.DataFrame(formatted_data)

def _build_point_url(self, lon: float, lat: float) -> str:
"""Build point url.
Args:
lon: longitude
lat: latitude
Returns:
valid point url
"""
return self._base_url + f"geotype/point/lon/{lon}/lat/{lat}/data.json"

def _build_multipoint_url(
self,
validtime: str,
Expand Down Expand Up @@ -382,4 +372,4 @@ def _check_valid_time(self, test_time: str) -> bool:
true if valid and false if not valid
"""
valid_time = self._format_datetime(test_time)
return -1 < (arrow.now().shift(hours=-1) - arrow.get(valid_time)).days < 1
return -1 < (arrow.now("Z").shift(hours=-1) - arrow.get(valid_time)).days < 1
2 changes: 1 addition & 1 deletion src/smhi/metfcts.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ def _check_valid_time(self, test_time: str) -> bool:
true if valid and false if not valid
"""
valid_time = self._format_datetime(test_time)
return -1 < (arrow.get(valid_time) - arrow.now()).days < 10
return -1 < (arrow.get(valid_time) - arrow.now("Z").shift(hours=-1)).days < 10
7 changes: 6 additions & 1 deletion src/smhi/models/mesan_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class MesanMultiPointDataSchema(pa.DataFrameModel):
value: Series[float]


class MesanGeometry(BaseModel):
type_: str = Field(..., alias="type")
coordinates: List[List[float]]


class MesanPointData(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)

Expand All @@ -68,7 +73,7 @@ class MesanPointData(BaseModel):
approved_time: str
reference_time: str
level_unit: str

geometry: MesanGeometry
df: pd.DataFrame
df_info: DataFrame[MesanPointDataInfoSchema]

Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/mesan/multipoint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Accept-Ranges: bytes
Access-Control-Allow-Headers: X-Requested-With, Content-Type
Access-Control-Allow-Origin: *
Age: 0
Cache-Control: max-age=3600,public
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Sun, 31 Mar 2024 09:31:35 GMT
Expires: Sun, 31 Mar 2024 10:31:35 GMT
Last-Modified: Sun, 31 Mar 2024 09:31:35 GMT
Transfer-Encoding: chunked
Vary: Accept-Encoding
Via: 1.1 varnish (Varnish/6.6)
X-Varnish: 681874038
X-hits: 0

{"approvedTime":"2024-03-31T08:39:25Z","referenceTime":"2024-03-31T08:00:00Z","geometry":{"type":"MultiPoint","coordinates":[[2.250475,52.500440]]},"timeSeries":[{"validTime":"2024-03-31T08:00:00Z","parameters":[{"name":"t","levelType":"hl","level":2,"unit":"Cel","values":[8.3]}]}]}
2 changes: 2 additions & 0 deletions tests/fixtures/mesan/multipoint_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
,value,lat,lon
0,8.3,52.50044,2.250475
19 changes: 19 additions & 0 deletions tests/fixtures/mesan/point.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Date: Sun, 31 Mar 2024 09:24:00 GMT
Expires: Sun, 31 Mar 2024 10:24:00 GMT
Access-Control-Allow-Headers: X-Requested-With, Content-Type
Cache-Control: max-age=3600,public
Content-Type: application/json
Content-Encoding: gzip
Vary: Accept-Encoding
X-Varnish: 657956746
Age: 0
Via: 1.1 varnish (Varnish/6.6)
X-hits: 0
Access-Control-Allow-Origin: *
Last-Modified: Sun, 31 Mar 2024 09:24:00 GMT
Accept-Ranges: bytes
Connection: keep-alive
Transfer-Encoding: chunked

{"approvedTime":"2024-03-31T08:39:25Z","referenceTime":"2024-03-31T08:00:00Z","geometry":{"type":"Point","coordinates":[[16.150350,58.570784]]},"timeSeries":[{"validTime":"2024-03-31T08:00:00Z","parameters":[{"name":"t","levelType":"hl","level":2,"unit":"Cel","values":[5.5]},{"name":"gust","levelType":"hl","level":10,"unit":"m/s","values":[5.7]},{"name":"r","levelType":"hl","level":2,"unit":"percent","values":[90]},{"name":"msl","levelType":"hmsl","level":0,"unit":"hPa","values":[1002.5]},{"name":"Tiw","levelType":"hl","level":2,"unit":"Cel","values":[4.8]},{"name":"cb_sig","levelType":"hmsl","level":0,"unit":"m","values":[178]},{"name":"cb_sig","levelType":"hl","level":0,"unit":"m","values":[143]},{"name":"c_sigfr","levelType":"hl","level":0,"unit":"percent","values":[80]},{"name":"tcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"ct_sig","levelType":"hl","level":0,"unit":"m","values":[9792]},{"name":"lcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"hcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"mcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"prtype","levelType":"hl","level":0,"unit":"code","values":[-9]},{"name":"pmax","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmin","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmedian","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmean","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"prec1h","levelType":"hl","level":0,"unit":"mm","values":[0.0]},{"name":"prec3h","levelType":"hl","level":0,"unit":"mm","values":[0.0]},{"name":"frsn1h","levelType":"hl","level":0,"unit":"cm","values":[0.0]},{"name":"vis","levelType":"hl","level":2,"unit":"km","values":[43.9]},{"name":"spp","levelType":"hl","level":0,"unit":"percent","values":[-9]},{"name":"prsort","levelType":"hl","level":0,"unit":"code","values":[0]},{"name":"wd","levelType":"hl","level":10,"unit":"degree","values":[86]},{"name":"ws","levelType":"hl","level":10,"unit":"m/s","values":[2.9]},{"name":"Wsymb2","levelType":"hl","level":0,"unit":"category","values":[6]}]},{"validTime":"2024-03-31T07:00:00Z","parameters":[{"name":"t","levelType":"hl","level":2,"unit":"Cel","values":[4.7]},{"name":"gust","levelType":"hl","level":10,"unit":"m/s","values":[5.1]},{"name":"r","levelType":"hl","level":2,"unit":"percent","values":[93]},{"name":"msl","levelType":"hmsl","level":0,"unit":"hPa","values":[1002.6]},{"name":"Tiw","levelType":"hl","level":2,"unit":"Cel","values":[4.2]},{"name":"cb_sig","levelType":"hmsl","level":0,"unit":"m","values":[178]},{"name":"cb_sig","levelType":"hl","level":0,"unit":"m","values":[139]},{"name":"c_sigfr","levelType":"hl","level":0,"unit":"percent","values":[80]},{"name":"tcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"ct_sig","levelType":"hl","level":0,"unit":"m","values":[6728]},{"name":"lcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"hcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"mcc","levelType":"hl","level":0,"unit":"octas","values":[8]},{"name":"prtype","levelType":"hl","level":0,"unit":"code","values":[-9]},{"name":"pmax","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmin","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmedian","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"pmean","levelType":"hl","level":0,"unit":"kg/m2/h","values":[0.0]},{"name":"prec1h","levelType":"hl","level":0,"unit":"mm","values":[0.0]},{"name":"prec3h","levelType":"hl","level":0,"unit":"mm","values":[0.0]},{"name":"frsn1h","levelType":"hl","level":0,"unit":"cm","values":[0.0]},{"name":"vis","levelType":"hl","level":2,"unit":"km","values":[38.0]},{"name":"spp","levelType":"hl","level":0,"unit":"percent","values":[-9]},{"name":"prsort","levelType":"hl","level":0,"unit":"code","values":[0]},{"name":"wd","levelType":"hl","level":10,"unit":"degree","values":[83]},{"name":"ws","levelType":"hl","level":10,"unit":"m/s","values":[2.4]},{"name":"Wsymb2","levelType":"hl","level":0,"unit":"category","values":[6]}]}]}
3 changes: 3 additions & 0 deletions tests/fixtures/mesan/point_data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
valid_time,Tiw,Wsymb2,c_sigfr,cb_sig,ct_sig,frsn1h,gust,hcc,lcc,mcc,msl,pmax,pmean,pmedian,pmin,prec1h,prec3h,prsort,prtype,r,spp,t,tcc,vis,wd,ws
2024-03-31 07:00:00+00:00,4.2,6,80,178,6728,0.0,5.1,8,8,8,1002.6,0.0,0.0,0.0,0.0,0.0,0.0,0,-9,93,-9,4.7,8,38.0,83,2.4
2024-03-31 08:00:00+00:00,4.8,6,80,178,9792,0.0,5.7,8,8,8,1002.5,0.0,0.0,0.0,0.0,0.0,0.0,0,-9,90,-9,5.5,8,43.9,86,2.9
27 changes: 27 additions & 0 deletions tests/fixtures/mesan/point_data_info.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name,level,level_type,unit
Tiw,2,hl,Cel
Wsymb2,0,hl,category
c_sigfr,0,hl,percent
cb_sig,0,hmsl,m
ct_sig,0,hl,m
frsn1h,0,hl,cm
gust,10,hl,m/s
hcc,0,hl,octas
lcc,0,hl,octas
mcc,0,hl,octas
msl,0,hmsl,hPa
pmax,0,hl,kg/m2/h
pmean,0,hl,kg/m2/h
pmedian,0,hl,kg/m2/h
pmin,0,hl,kg/m2/h
prec1h,0,hl,mm
prec3h,0,hl,mm
prsort,0,hl,code
prtype,0,hl,code
r,2,hl,percent
spp,0,hl,percent
t,2,hl,Cel
tcc,0,hl,octas
vis,2,hl,km
wd,10,hl,degree
ws,10,hl,m/s
Loading

0 comments on commit 52d8c58

Please sign in to comment.