Ejemplo n.º 1
0
def get_proj(ifile):
    """
    MAP_PROJ - Model projection [1=Lambert, 2=polar stereographic,
                                 3=mercator, 6=lat-lon]  (required)
    TRUELAT1 - required for MAP_PROJ = 1, 2, 3 (defaults to 0 otherwise)
    TRUELAT2 - required for MAP_PROJ = 6 (defaults to 0 otherwise)
    STAND_LON - Standard longitude used in model projection (required)
    REF_LON, REF_LON - A reference longitude and latitude (required)
    KNOWNI, KNOWNJ - The I and J locations of REF_LON and REF_LAT (required)
    POLE_LAT - optional for MAP_PROJ = 6 (defaults to 90 otherwise)
    POLE_LAT - optional for MAP_PROJ = 6 (defaults to 0 otherwise)
    DX, DY - required for MAP_PROJ = 1, 2, 3 (defaults to 0 otherwise)
    LATINC, LONINC - required for MAP_PROJ = 6 (defaults to 0 otherwise)
    """

    projname = {
        1: "lambert_conformal_conic",
        2: 'polar_stereographic',
        3: 'mercator'
    }[ifile.MAP_PROJ]
    var = ifile.createVariable(projname, 'i', ())
    var.grid_mapping_name = projname
    var.earth_radius = 6370000.
    var.false_easting = 0
    var.false_northing = 0
    x0_from_cen = len(ifile.dimensions['west_east']) / 2 * ifile.DX
    y0_from_cen = len(ifile.dimensions['south_north']) / 2 * ifile.DY
    if projname == 'mercator':
        var.longitude_of_projection_origin = ifile.STAND_LON
        var.standard_parallel = ifile.TRUELAT1
    elif projname == "lambert_conformal_conic":
        var.standard_parallel = np.array([ifile.TRUELAT1, ifile.TRUELAT2])
        var.longitude_of_central_meridian = ifile.STAND_LON
        var.latitude_of_projection_origin = ifile.MOAD_CEN_LAT
    elif projname == 'polar_stereographic':
        var.straight_vertical_longitude_from_pole = ifile.STAND_LON
        var.latitude_of_projection_origin = ifile.TRUELAT1
        var.standard_parallel = ifile.MOAD_CEN_LAT

    from PseudoNetCDF.coordutil import getproj4_from_cf_var
    from mpl_toolkits.basemap import pyproj
    projstr = getproj4_from_cf_var(var)
    proj = pyproj.Proj(projstr)
    gcx, gcy = proj(ifile.CEN_LON, ifile.CEN_LAT)
    glx = gcx - x0_from_cen
    gly = gcy - y0_from_cen
    var.false_easting = -glx
    var.false_northing = -gly
    return projname
Ejemplo n.º 2
0
 def getproj(self, withgrid=False, projformat='pyproj'):
     from PseudoNetCDF.coordutil import getproj4_from_cf_var
     gridmapping = self.variables['crs']
     proj4str = getproj4_from_cf_var(gridmapping, withgrid=withgrid)
     preserve_units = withgrid
     if projformat == 'proj4':
         return proj4str
     elif projformat == 'pyproj':
         import pyproj
         # pyproj adds +units=m, which is not right for latlon/lonlat
         if '+proj=lonlat' in proj4str or '+proj=latlon' in proj4str:
             preserve_units = True
         return pyproj.Proj(proj4str, preserve_units=preserve_units)
     elif projformat == 'wkt':
         import osr
         srs = osr.SpatialReference()
         # Imports WKT to Spatial Reference Object
         srs.ImportFromProj4(proj4str)
         srs.ExportToWkt()  # converts the WKT to an ESRI-compatible format
         return srs.ExportToWkt()
     else:
         raise ValueError('projformat must be pyproj, proj4 or wkt')