<commit_before># -*- coding: utf-8 -*-

# plot the grid area using basemap

import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import gridmap


# Define plotting function in basemap

def plot_basegrid(gmap):
    """Plot filled coastline for grid defined by gmap"""

    # Bør Lm og Mm være med i PolarGridMap-objektet?
    # Må nå inn som separate argument.

    if gmap.ellipsoid.name == "WGS84":
        rsphere=(gmap.ellipsoid.a, gmap.ellipsoid.b)
    else:
        rsphere = gmap.ellipsoid.a

    # Compute lon/lat of grid corners (needed by basemap=
    llcrnrlon, llcrnrlat = gmap.grid2ll(-0.5, -0.5)
    Lm, Mm = gmap.shape
    urcrnrlon, urcrnrlat = gmap.grid2ll(Lm+1.5, Mm+1.5)

    p = Basemap(llcrnrlon=llcrnrlon, llcrnrlat=llcrnrlat,
                urcrnrlon=urcrnrlon, urcrnrlat=urcrnrlat,
                projection='stere',
                resolution='i',
                area_thresh=0.5*(gmap.dx/1000.0)**2, # km^2 
                rsphere=rsphere,
                lat_0 = 90.0,
                lon_0 = gmap.ylon,
                lat_ts = gmap.lat_ts)

    p.drawcoastlines()
    p.fillcontinents(color='grey', lake_color='grey')
    # Finne på noe glupt slik at disse settes automatisk
    # ut fra lon/lat i hjørnene
    #p.drawparallels(range(50, 90, 5))
    #p.drawmeridians(range(0, 360, 15), latmax=90)
    return p

# ----------------------------

#f = Dataset('b0.nc')
#gmap = gridmap.gridmap_fromfile(f)
#Lp, Mp = len(f.dimensions['xi_rho']), len(f.dimensions['eta_rho'])
#Lm, Mm = Lp-2, Mp-2
# Grid parameters
xp    = 418.25        # x grid coordinate of north pole
yp    = 257.25        # y grid coordinate of north pole
dx    = 10000         # grid resolution (at lat_ts)      [m]
ylon  = 58.0          # angle of y-axis                  [deg]

Lm, Mm = 200, 100

gmap = gridmap.PolarStereographic(xp, yp, dx, ylon,
                                  shape=(Lm,Mm),
                                  ellipsoid=gridmap.WGS84)


p = plot_basegrid(gmap)

plt.show()






<commit_msg>Updated the plot_basegrid.py example<commit_after># -*- coding: utf-8 -*-

"""
Plot the grid area using basemap

Does not require the gridmap module
"""

# ----------------------------------
# <NAME> <<EMAIL>>
# Institute of Marine Research
# 2012-06-23
# ----------------------------------


import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
#import gridmap

# --------------------------------------
# Define the polar stereographic grid
# --------------------------------------

xp    = 418.25        # x grid coordinate of north pole
yp    = 257.25        # y grid coordinate of north pole
dx    = 10000         # grid resolution (at 60 )      [m]
ylon  = 58.0          # angle of y-axis                  [deg]

Lm, Mm = 200, 100     # Number of internal grid cells

ellipsoid = "WGS84"   # alternatives "sphere" or "WGS84"

# --------------------
# Other settings
# --------------------

resolution = "i" # GSHHS resolution code, "c", "l", "i", "h", or "f"
parallels = range(50, 65, 5)     # Graticule longitudes
meridians = range(-10, 20, 10)   # Graticule latitudes

# ----------------------------
# Set up the Basemap object
# ----------------------------

lat_ts = 60.0  # Latitude of true scale

if ellipsoid == "WGS84":
    a = 6378137.0
    f = 1/298.257223563
    b = (1-f)*a
    rsphere = (a, b)
else:
    rsphere = 6371000.0
    
# Basemap needs longitude, latitude of grid corners
# make a preliminary projection with origin in North Pole
p0 = Basemap(llcrnrlon=ylon, llcrnrlat=90.0,
             urcrnrlon=ylon+1, urcrnrlat=89.0,  # dummy
             projection='stere', rsphere=rsphere,
             lat_0=90.0, lon_0=ylon, lat_ts=lat_ts)

# Use full grid, including boundary cells
# extent from -0.5 to Lm+1.5 (resp. Mm+1.5)
lon_ll, lat_ll = p0((-0.5-xp)*dx, (-0.5-yp)*dx, inverse=True)
lon_ur, lat_ur = p0((Lm+1.5-xp)*dx, (Mm+1.5-yp)*dx, inverse=True)

area0 = 0.5*dx*dx/1.0e6  # Do not use islands < half grid cell

p = Basemap(llcrnrlon=lon_ll, llcrnrlat=lat_ll,
            urcrnrlon=lon_ur, urcrnrlat=lat_ur,
            projection='stere', rsphere=rsphere,
            lat_0=90.0, lon_0=ylon, lat_ts=lat_ts,
            resolution=resolution,
            area_thresh = area0)

# -------------
# Make the plot
# -------------

color='green'
p.fillcontinents(color=color, lake_color=color)

p.drawparallels(parallels)
p.drawmeridians(meridians, latmax=90)

plt.show()








{'fraction_non_alphanumeric': 0.1748790145182578, 'fraction_numerical': 0.04377474703035636, 'mean_word_length': 2.0192563081009296, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5970033', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1622, 'n_words': 407}