SuperDARN / pydarn

Python library for visualizing SuperDARN Data
GNU Lesser General Public License v3.0
31 stars 11 forks source link

EHN: Coastlines without Cartopy #350

Closed carleyjmartin closed 11 months ago

carleyjmartin commented 1 year ago

Scope

This pull request allows users who are plotting spatial plots in magnetic coordinates to be able to plot coastlines from a given file instead of needing to install Cartopy. In some cases, it is very difficult or not possible to install cartopy on linux systems so this is some help for that.

This does not affect geographic plots as we use cartopy to set up the axes to begin with. The new file with coastline shapes in is produced using cartopy so it should be completely identical to the cartopy produced plots.

Initially I said I would maybe make a monthly version of the coastlines in magnetic coordinates, however, with the date dependency and the shifting in longitude that we do, it was not possible as we would need a position for every second all the time or we would need to convert from geographic coords each time like we do now - so I chose the latter but reading from file instead of from Cartopy.

We can definitely try to remove the dependency for geographic plots, but as they are used less and it will require a new axes to be developed (rather than using one straight from Cartopy) it would be it's own project and PR.

We are still retaining Cartopy as default to produce coastlines as we do not have to maintain it, and it gives us more flexibility to expand to different coordiante systems and projections in the future

issue: #326

Approval

Number of approvals: 2 with code review please

Test

matplotlib version: 3.7.1 Note testers: please indicate what version of matplotlib you are using

import matplotlib.pyplot as plt
import pydarn
file='/Users/carley/Documents/data/20221220.2200.00.rkn.a.fitacf'
#file = '/Users/carley/Documents/data/20220108.0801.00.dcn.fitacf'
SDarn_read = pydarn.SuperDARNRead(file)
fitacf_data = SDarn_read.read_fitacf()

# Fan
pydarn.Fan.plot_fan(fitacf_data, scan_index=55, lowlat=50,
                    radar_label=True, groundscatter=True,
                    coastline=True, zmin=-500, zmax=500,
                    colorbar_label='Velocity m/s')
plt.show()

map_file = "/Users/carley/Documents/data/maps/20150310.n.map"
#map_file = "/Users/carley/Documents/data/maps/20190421.s.map2"
map_data = pydarn.SuperDARNRead().read_dmap(map_file)

#print(map_data)
pydarn.Maps.plot_mapdata(map_data, record=500, coastline=True, lowlat=60)
plt.show()

This tests fans and plots for both north and south. You will have to run without cartopy installed to test, as the code detects if cartopy is there or not and acts accordingly. The following plots were produced with cartopy on the right and not cartopy on the left.

Screenshot 2023-08-01 at 3 11 13 PM Screenshot 2023-08-01 at 3 10 10 PM Screenshot 2023-08-01 at 3 11 46 PM Screenshot 2023-08-01 at 3 10 44 PM

Note: I know I need to sort some merge conflicts out on here - fixed!

Shirling-VT commented 11 months ago

Reviewed the changes, all look good to me. Tested for with and without cartopy installed, the generated fan and map plots for both hemispheres look identical to me. Here are the testing environment, code, and results:

MacOS Monterey 12.5.1

matplotlib 3.7.3

python 3.8.0

`import pydarn import datetime as dt import matplotlib.pyplot as plt

cly_file = '/Users/xueling/data/superdarn/2015/fitacf/cly/20150308.1400.03.cly.fitacf.bz2' cly_data = pydarn.SuperDARNRead().read_dmap(cly_file)

fanplot = pydarn.Fan.plot_fan(cly_data, scan_index=27,radar_label=True, lowlat=10, groundscatter=True,coastline=True, zmin=-500, zmax=500, colorbar_label='Velocity m/s') plt.show()

zho_file = '/Users/xueling/data/superdarn/2015/fitacf/zho/20150308.1401.00.zho.fitacf.bz2' zho_data = pydarn.SuperDARNRead().read_dmap(zho_file)

a = pydarn.Fan.plot_fan(zho_data, scan_index=3, colorbar_label='Velocity [m/s]', coastline=True, fov_color='grey',line_color='red', radar_label=True, grid = True,channel=1) plt.show()

map_file = '/Users/xueling/data/superdarn/2015/map2/north/20150310.north.map2.bz2' map_data = pydarn.SuperDARNRead().read_dmap(map_file) pydarn.Maps.plot_mapdata(map_data,record=360,zmin=-100,zmax=1000, coastline=True, parameter=pydarn.MapParams.FITTED_VELOCITY, lowlat=50) plt.show()

map_file = "/Users/xueling/data/superdarn/2019/map2/south/20190421.south.map2.bz2" map_data = pydarn.SuperDARNRead().read_dmap(map_file) pydarn.Maps.plot_mapdata(map_data,record=360, parameter=pydarn.MapParams.FITTED_VELOCITY, coastline=True, lowlat=50, color_vectors=True, reference_vector=1000) plt.show()`

Left are figures with cartopy installed, right are without cartopy. image image

KhanalKrishna commented 11 months ago

Matplotlib version: 3.8.0 Figures below are generated without installing cartopy.

import matplotlib.pyplot as plt from datetime import datetime import pydarn

Read in fitACF file using SuperDARDRead, then read_fitacf

file = "20130202.2001.00.inv.fitacf" SDarn_read = pydarn.SuperDARNRead(file) fitacf_data = SDarn_read.read_fitacf()

pydarn.Fan.plot_fan(fitacf_data, scan_index=27, coastline= True,colorbar_label='Velocity [m/s]') plt.show()

Read map file

file = "20150419.north.map2" SDarn_read = pydarn.SuperDARNRead(file) map_data = SDarn_read.read_map()

pydarn.Maps.plot_mapdata(map_data, record=150, coastline= True) plt.show()

image image