Skip to content

Commit 7031649

Browse files
committed
Merge branch 'master' of https://github.com/deeplycloudy/xlma-python into io-improv
2 parents fb1a2ca + f392af2 commit 7031649

10 files changed

Lines changed: 30 additions & 34 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ coverage.xml
5050
*.cover
5151
.hypothesis/
5252
.pytest_cache/
53+
tests/mpl-results/
5354

5455
# Translations
5556
*.mo

examples/glueviz/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# return line_no
2222
#
2323
# def readfile(self):
24-
# lmad = pd.read_csv(self.file,delim_whitespace=True,header=None,
24+
# lmad = pd.read_csv(self.file,sep='\\s+',header=None,
2525
# compression='gzip',skiprows=self.datastart()+1)
2626
# columns = ['time','lat','lon','alt','chi','p','mask']
2727
# lmad.columns = columns

pyxlma/coords.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class CoordinateSystem(object):
2727
transformations to/from an ECEF cartesian system, e.g.
2828
>>> self.ERSxyz = proj4.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
2929
>>> self.ERSlla = proj4.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
30-
>>> projectedData = proj4.transform(self.ERSlla, self.ERSxyz, lat, lon, alt )
30+
>>> projectedData = proj4.Transformer.from_crs(self.ERSlla.crs, self.ERSxyz.crs).transform(lon, lat, alt)
3131
The ECEF system has its origin at the center of the earth, with the +Z toward the north pole,
3232
+X toward (lat=0, lon=0), and +Y right-handed orthogonal to +X, +Z
3333
@@ -80,7 +80,7 @@ def toECEF(self, lon, lat, alt):
8080
lon = atleast_1d(lon)
8181
alt = atleast_1d(alt)
8282
if (lat.shape[0] == 0): return lon, lat, alt # proj doesn't like empties
83-
projectedData = array(proj4.transform(self.ERSlla, self.ERSxyz, lon, lat, alt ))
83+
projectedData = array(proj4.Transformer.from_crs(self.ERSlla.crs, self.ERSxyz.crs).transform(lon, lat, alt))
8484
if len(projectedData.shape) == 1:
8585
return projectedData[0], projectedData[1], projectedData[2]
8686
else:
@@ -91,7 +91,7 @@ def fromECEF(self, x, y, z):
9191
y = atleast_1d(y)
9292
z = atleast_1d(z)
9393
if (x.shape[0] == 0): return x, y, z # proj doesn't like empties
94-
projectedData = array(proj4.transform(self.ERSxyz, self.ERSlla, x, y, z ))
94+
projectedData = array(proj4.Transformer.from_crs(self.ERSxyz.crs, self.ERSlla.crs).transform(x, y, z))
9595
if len(projectedData.shape) == 1:
9696
return projectedData[0], projectedData[1], projectedData[2]
9797
else:
@@ -125,15 +125,15 @@ def toECEF(self, x, y, z):
125125
x += self.cx
126126
y += self.cy
127127
z += self.cz
128-
projectedData = array(proj4.transform(self.projection, self.ERSxyz, x, y, z ))
128+
projectedData = array(proj4.Transformer.from_crs(self.projection.crs, self.ERSxyz.crs).transform(x, y, z))
129129
if len(projectedData.shape) == 1:
130130
px, py, pz = projectedData[0], projectedData[1], projectedData[2]
131131
else:
132132
px, py, pz = projectedData[0,:], projectedData[1,:], projectedData[2,:]
133133
return px, py, pz
134134

135135
def fromECEF(self, x, y, z):
136-
projectedData = array(proj4.transform(self.ERSxyz, self.projection, x, y, z ))
136+
projectedData = array(proj4.Transformer.from_crs(self.ERSxyz.crs, self.projection.crs).transform(x, y, z))
137137
if len(projectedData.shape) == 1:
138138
px, py, pz = projectedData[0], projectedData[1], projectedData[2]
139139
else:
@@ -221,10 +221,10 @@ def __init__(self, subsat_lon=0.0, subsat_lat=0.0, sweep_axis='y',
221221

222222
def toECEF(self, x, y, z):
223223
X, Y, Z = x*self.h, y*self.h, z*self.h
224-
return proj4.transform(self.fixedgrid, self.ECEFxyz, X, Y, Z)
224+
return proj4.Transformer.from_crs(self.fixedgrid.crs, self.ECEFxyz.crs).transform(X, Y, Z)
225225

226226
def fromECEF(self, x, y, z):
227-
X, Y, Z = proj4.transform(self.ECEFxyz, self.fixedgrid, x, y, z)
227+
X, Y, Z = proj4.Transformer.from_crs(self.ECEFxyz.crs, self.fixedgrid.crs).transform(x, y, z)
228228
return X/self.h, Y/self.h, Z/self.h
229229

230230
# class AltitudePreservingMapProjection(MapProjection):
@@ -253,8 +253,8 @@ def __init__(self, ctrLat, ctrLon, ctrAlt, datum='WGS84', ellps='WGS84', effecti
253253
self.lla = proj4.Proj(proj='latlong', ellps=self.ellps, datum=self.datum)
254254
self.xyz = proj4.Proj(proj='geocent', ellps=self.ellps, datum=self.datum)
255255

256-
self.Requator, foo1, foo2 = proj4.transform(self.lla,self.xyz,0,0,0) # Equatorial radius - WGS-84 value = 6378137.0
257-
foo1, foo2, self.Rpolar = proj4.transform(self.lla,self.xyz,0,90,0) # Polar radius - WGS-84 value = 6356752.314
256+
self.Requator, _, _ = proj4.Transformer.from_crs(self.lla.crs, self.xyz.crs).transform(0,0,0) # Equatorial radius - WGS-84 value = 6378137.0
257+
_, _, self.Rpolar = proj4.Transformer.from_crs(self.lla.crs, self.xyz.crs).transform(0,90,0) # Polar radius - WGS-84 value = 6356752.314
258258
self.flattening = (self.Requator-self.Rpolar)/self.Requator
259259

260260
self.eccen = (2.0-self.flattening)*self.flattening # First eccentricity squared - WGS-84 value = 0.00669437999013
@@ -374,10 +374,10 @@ def __init__(self, ctrLat=0.0, ctrLon=0.0, ctrAlt=0.0):
374374

375375
ERSlla = proj4.Proj(proj='latlong', ellps='WGS84', datum='WGS84')
376376
ERSxyz = proj4.Proj(proj='geocent', ellps='WGS84', datum='WGS84')
377-
self.centerECEF = array(proj4.transform(ERSlla, ERSxyz, ctrLon, ctrLat, ctrAlt))
377+
self.centerECEF = array(proj4.Transformer.from_crs(ERSlla.crs, ERSxyz.crs).transform(ctrLon, ctrLat, ctrAlt))
378378

379379
#location of point directly above local center
380-
aboveCenterECEF = array(proj4.transform(ERSlla, ERSxyz, ctrLon, ctrLat, self.ctrAlt+1e3))
380+
aboveCenterECEF = array(proj4.Transformer.from_crs(ERSlla.crs, ERSxyz.crs).transform(ctrLon, ctrLat, self.ctrAlt+1e3))
381381

382382
#normal vector to earth's surface at the center is the local z direction
383383
n = aboveCenterECEF - self.centerECEF
@@ -396,7 +396,7 @@ def __init__(self, ctrLat=0.0, ctrLon=0.0, ctrAlt=0.0):
396396
# Point just to the north of the center on earth's surface, projected onto the tangent plane
397397
# This calculation seems like it should only be done with latitude/north since the local x
398398
# direction curves away along a non-straight line when projected onto the plane
399-
northCenterECEF = array(proj4.transform(ERSlla, ERSxyz, self.ctrLon, self.ctrLat+1.01, self.ctrAlt))
399+
northCenterECEF = array(proj4.Transformer.from_crs(ERSlla.crs, ERSxyz.crs).transform(self.ctrLon, self.ctrLat+1.01, self.ctrAlt))
400400
localy = dot(P, northCenterECEF[:,None] )
401401
localy = localy / norm(localy)
402402

pyxlma/lmalib/flash/properties.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import numpy as np
22
import xarray as xr
3-
from scipy.spatial import Delaunay, ConvexHull
3+
from scipy.spatial import Delaunay, ConvexHull, QhullError
44
from scipy.special import factorial
5-
from scipy.spatial.qhull import QhullError
65
from pyxlma.lmalib.traversal import OneToManyTraversal
76

87
def local_cartesian(lon, lat, alt, lonctr, latctr, altctr):
@@ -135,7 +134,7 @@ def event_discharge_energy(z,area):
135134
eta_c = 0.004 #Scale the energy to depict the fraction of energy neutralized by
136135
#each flash in the capacitor model
137136
#Capacitor model:
138-
w = 4 * ((sigma_crit**2. * d * area.iloc[0])/(2* e)) #The quantity for appears when considering image charges (2*sigma)^2=4sigma^2
137+
w = 4 * ((sigma_crit**2. * d * area)/(2* e)) #The quantity for appears when considering image charges (2*sigma)^2=4sigma^2
139138
return(w*eta_c)
140139

141140

@@ -240,12 +239,12 @@ def flash_stats(ds, area_func=None, volume_func=None):
240239
# could only run on those where point counts meet threshold, instead
241240
# testing inside the function
242241
# Index of event_area and event_volume are event_parent_flash_id
243-
event_area = fl_gb.apply(area_func)
244-
event_volume = fl_gb.apply(volume_func)
242+
event_area = fl_gb.apply(area_func, include_groups=False)
243+
event_volume = fl_gb.apply(volume_func, include_groups=False)
245244

246245
#Compute flash discharge energy using parallel plate capacitor
247-
event_energy = fl_gb.apply(lambda df: event_discharge_energy(df['event_z'],
248-
event_area[df['event_parent_flash_id']]))
246+
event_energy = fl_gb.apply(lambda df1: event_discharge_energy(df1['event_z'],
247+
event_area[df1.name]), include_groups=False)
249248

250249

251250
# set the index for the original dataset's flash dimension to the flash_id

pyxlma/lmalib/grid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def assign_regular_bins(dsg, ds, var_to_grid_map, pixel_id_var='pixel_id',
132132
var_dim = ds[var_name].dims[0]
133133
else:
134134
assert var_dim == ds[var_name].dims[0]
135-
if ds.dims[var_dim] < 1:
135+
if ds.sizes[var_dim] < 1:
136136
# No data on the dim, set in_range to nothing and stop checking other vars
137137
in_range = []
138138
have_data = False

pyxlma/lmalib/io/read.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ def nldn(filenames):
413413
filenames = [filenames]
414414
full_df = pd.DataFrame({})
415415
for filename in filenames:
416-
this_file = pd.read_csv(filename, delim_whitespace=True, header=None,
416+
this_file = pd.read_csv(filename, sep='\\s+', header=None,
417417
names=[
418418
'date', 'time', 'latitude', 'longitude', 'peak_current_kA', 'curr_unit', 'multiplicity', 'semimajor',
419419
'semiminor', 'majorminorratio', 'ellipseangle', 'chi2', 'num_stations', 'type'
@@ -629,7 +629,7 @@ def readfile(self):
629629
comp = 'gzip'
630630
else:
631631
comp = None
632-
lmad = pd.read_csv(self.file,compression=comp,delim_whitespace=True,
632+
lmad = pd.read_csv(self.file,compression=comp,sep='\\s+',
633633
header=None,skiprows=self.data_starts+1,on_bad_lines='skip')
634634
lmad.columns = self.names
635635
except pd.errors.EmptyDataError:

pyxlma/lmalib/traversal.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, dataset, entity_id_vars, parent_id_vars):
7777
self.child_to_parent = collections.OrderedDict()
7878
self.parent_to_child = collections.OrderedDict()
7979
for (entity_var, parent_var) in self._descend():
80-
if dataset.dims[dataset[entity_var].dims[0]] == 0:
80+
if dataset.sizes[dataset[entity_var].dims[0]] == 0:
8181
# No data, so groupby will fail in xarray > 0.13
8282
entity_grouper = None
8383
else:
@@ -86,7 +86,7 @@ def __init__(self, dataset, entity_id_vars, parent_id_vars):
8686
if parent_var is None:
8787
parent_grouper = None
8888
else:
89-
if dataset.dims[dataset[parent_var].dims[0]] == 0:
89+
if dataset.sizes[dataset[parent_var].dims[0]] == 0:
9090
# No data, so groupby will fail in xarray > 0.13
9191
parent_grouper = None
9292
else:

pyxlma/plot/xlma_plot_feature.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ def plot_2d_network_points(bk_plot, netw_data, actual_height=None, fake_ic_heigh
161161
else:
162162
raise ValueError("color_by must be 'time' or 'polarity'")
163163

164-
cgs = netw_data[netw_data['type']=='CG']
165-
ics = netw_data[netw_data['type']=='IC']
164+
cgs = netw_data[netw_data['type']=='CG'].copy()
165+
ics = netw_data[netw_data['type']=='IC'].copy()
166166

167167
if actual_height is None:
168168
cgs['height'] = np.full_like(cgs.longitude, fake_cg_height)

tests/test_coords.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def test_geographic():
2020
geosys = GeographicSystem()
2121
ecef_coords = geosys.toECEF(test_lons, test_lats, test_alts)
2222
lons, lats, alts = geosys.fromECEF(*ecef_coords)
23-
2423
assert np.allclose(ecef_coords[0], test_ecef_X)
2524
assert np.allclose(ecef_coords[1], test_ecef_Y)
2625
assert np.allclose(ecef_coords[2], test_ecef_Z)
@@ -31,9 +30,7 @@ def test_geographic():
3130
def test_geographic_one_point():
3231
geosys = GeographicSystem()
3332
ecef_coords = geosys.toECEF(np.atleast_1d(test_lons[-1]), np.atleast_1d(test_lats[-1]), np.atleast_1d(test_alts[-1]))
34-
print(len(np.atleast_1d(test_lons[-1]).shape))
3533
lons, lats, alts = geosys.fromECEF(*ecef_coords)
36-
3734
assert np.allclose(ecef_coords[0], test_ecef_X[-1])
3835
assert np.allclose(ecef_coords[1], test_ecef_Y[-1])
3936
assert np.allclose(ecef_coords[2], test_ecef_Z[-1])
@@ -119,7 +116,6 @@ def test_radar_system_height():
119116
def test_radar_system_elevation():
120117
ADRAD_rcs = RadarCoordinateSystem(30.6177, -96.3365, 114)
121118
tornado_slant_range, radar_elevation = ADRAD_rcs.getSlantRangeElevation(17144.013390611748, 550.2784673999995)
122-
print(tornado_slant_range, radar_elevation)
123119
assert np.allclose(tornado_slant_range, 17150)
124120
assert np.allclose(radar_elevation, 1.4)
125121

tests/test_grid.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def test_create_regular_grid():
1010
grid_h_res = 0.1
1111
grid_height = 20
1212
grid_v_res = 1
13-
lon_range = (dataset.network_center_longitude - grid_range, dataset.network_center_longitude + grid_range, grid_h_res)
14-
lat_range = (dataset.network_center_latitude - grid_range, dataset.network_center_latitude + grid_range, grid_h_res)
13+
lon_range = (dataset.network_center_longitude.data.item() - grid_range, dataset.network_center_longitude.data.item() + grid_range, grid_h_res)
14+
lat_range = (dataset.network_center_latitude.data.item() - grid_range, dataset.network_center_latitude.data.item() + grid_range, grid_h_res)
1515
alt_range = (0, grid_height, grid_v_res)
1616
time_range = (dataset.event_time.data.min(), dataset.event_time.data.max(), np.timedelta64(1, 'm'))
1717
grid_edge_ranges ={
@@ -66,4 +66,4 @@ def test_events_to_grid():
6666
truth = xr.open_dataset('tests/truth/lma_netcdf/gridded_lma.nc')
6767

6868
for var in truth.data_vars:
69-
compare_dataarrays(gridded_lma, truth, var)
69+
compare_dataarrays(gridded_lma, truth, var)

0 commit comments

Comments
 (0)