Esempio n. 1
0
def get_ESMF_inputs(lon, lat, ODLmap):
    X, Y = ODLmap(lon, lat)

    # inner centres
    centres = lon[1:-1, 1:-1], lat[1:-1, 1:-1]
    nx, ny = centres[0].shape  # shape of centres

    # corners
    # - take mean in stereographic projection
    cX = np.zeros((nx + 1, ny + 1))
    cY = np.zeros((nx + 1, ny + 1))
    X, Y = ODLmap(lon, lat)

    # mean X
    X1 = X[0:-1, 0:-1]
    X2 = X[0:-1, 1:]
    X3 = X[1:, 1:]
    X4 = X[1:, 0:-1]
    cX = .25 * (X1 + X2 + X3 + X4)

    # mean Y
    Y1 = Y[0:-1, 0:-1]
    Y2 = Y[0:-1, 1:]
    Y3 = Y[1:, 1:]
    Y4 = Y[1:, 0:-1]
    cY = .25 * (Y1 + Y2 + Y3 + Y4)

    # reverse to get lon,lat
    corners = ODLmap(cX, cY, inverse=True)
    ell_mat = wgs84_ellipsoid_mat()

    areas = np.zeros(centres[0].shape)
    latC = np.zeros((4, ))
    lonC = np.zeros((4, ))
    for i in range(nx):
        for j in range(ny):
            latC[0] = lat[i, j]
            latC[1] = lat[i, j + 1]
            latC[2] = lat[i + 1, j + 1]
            latC[3] = lat[i + 1, j]
            lonC[0] = lon[i, j]
            lonC[1] = lon[i, j + 1]
            lonC[2] = lon[i + 1, j + 1]
            lonC[3] = lon[i + 1, j]
            areas[i, j] = GS.area_polygon_ellipsoid(lonC, latC, ell_mat)
            print(lonC)
            print(latC)
            print(ell_mat)
            sys.exit()

    return centres, corners, areas
Esempio n. 2
0
def average_area_model(cdate, vertices, fcdir0, outdir='.'):
    # average MIZ area for a given date
    # use binary files since more likely to be there
    # returns None if nothing there

    fcdir = fcdir0 + '/' + cdate + '/'
    if not os.path.exists(fcdir):
        return
    lst = os.listdir(fcdir)
    if cdate in lst:
        # check if need to add another cdate
        fcdir += cdate + '/'

    lst = os.listdir(fcdir)
    if 'bin' in lst:
        bindir = fcdir + 'bin'
    else:
        bindir = fcdir + 'binaries'

    # make file_list object from binary files
    # - treat in same way as multi-record netcdf
    fli = mr.file_list(bindir, 'archv_wav', '.a')
    if fli.number_of_time_records == 0:
        return

    # loop over 6-h intervals:
    daily_areas = []
    for hr in range(0, 24, 6):
        dto = datetime.datetime(int(cdate[:4]), int(cdate[4:6]),
                                int(cdate[6:8]), hr)

        #print(dto)
        if dto in fli.datetimes:
            idx = fli.datetimes.index(dto)
            out = fli.MIZmap(no_width=True,
                             vertices=vertices,
                             time_index=idx,
                             outdir=outdir)
            # out   = fli.MIZmap(vertices=vertices,time_index=idx,outdir=outdir)
            # sys.exit()
            #
            tfil = out[out.keys()[0]]
            pil = mc.single_file(tfil)

            tot_area = 0
            for pio in pil.poly_info_objects:
                #pio.area		# approximate area (Euclidean after projection using NP as center)
                #pio.ll_coords #list of coords of boundaries
                lon, lat = np.array(pio.ll_coords).transpose()
                area = GS.area_polygon_ellipsoid(lon, lat)
                # print(area)
                tot_area += area

            print('\nTot area: ' + str(tot_area) + '\n')
            daily_areas.append(tot_area)

    if len(daily_areas) == 0:
        return
    else:
        # take daily average
        TotArea = np.mean(daily_areas)
        print('\nAvg tot area: ' + str(TotArea) + '\n')
        return TotArea
Esempio n. 3
0
        # chart area
        pil = mc.single_file(chartdir + '/' + day.strftime("%Y%m%d") +
                             '1200_Greenland_WA_MIZpolys.txt')
        #po=pil.plot_all(show=False,latlon=True)
        #plt.savefig('testfig.png')

        #po.fig.show()

        pil_reduced = pil.reduce_area(vertices)
        #- closing polygon
        #po0=pil_reduced.plot_all(show=False,latlon=True)
        #po0.fig.show()
        #po0=pil_reduced.plot_all(show=False,latlon=True,check_flags=True)

        lon, lat = np.array(vertices).transpose()
        print('rectangle area\t' + str(GS.area_polygon_ellipsoid(lon, lat)))

        chart_area = 0
        for pio in pil_reduced.poly_info_objects:
            #pio.area		# approximate area (Euclidean after projection using NP as center)
            #pio.ll_coords #list of coords of boundaries
            lon, lat = np.array(pio.ll_coords).transpose()
            area = GS.area_polygon_ellipsoid(lon, lat)
            print(area)
            chart_area += area
        # ========================================================================================================

        # ========================================================================================================
        # max swh
        DT, idx = nci.nearestDate(day)
        swh_max = []
    ellipsoid_mat = [a, ecc]  # ellipsoid formatted ala matlab

    if 1:
        lons = np.array([40, 50, 50, 40])
        lats = np.array([60, 60, 70, 70])

        if 0:
            print('lons')
            print(lons)
            print(lons * np.pi / 180.)
            #
            print('lats')
            print(lats)
            print(lats * np.pi / 180.)

    area = GS.area_polygon_ellipsoid(lons, lats, ellipsoid_mat=ellipsoid_mat)
    print('Area: ' + str(area) + ' m^2')

if 1:
    # test great circle dist
    a = 6378273.  # semi-major axis (m)
    if 1:
        lat1, lon1 = 0., 0.
        # lat2,lon2   = 0.,1.
        lat2, lon2 = 0., 1.e-6
        dist2 = a * lon2 * np.pi / 180.

    if 1:
        lat1 = np.array([lat1])
        lon1 = np.array([lon1])
        lat2 = np.array([lat2])
Esempio n. 5
0
   def get_contour_lengths(self,bmap=None,pobj=None,show=True,test_function=None,\
                              func_vals_orig=None):

      import numpy as np
      ###########################################################################################
      class area_info:

         ########################################################################################
         def __init__(self,xy_bdy_coords,xy_conts,\
                        area,perimeter,lengths,\
                        spherical_geometry=False,\
                        ll_bdy_coords=None,ll_contours=None,\
                        func_vals=None,stream_func=None):
         
            import MIZchar as mizc
            # NB use "1*" to remove pointers to the arrays outside the function
            # - like copy, but works for lists also
            self.xy_bdy_coords   = 1*xy_bdy_coords # (x,y) coordinates of boundary (ie in projected space)
            self.xy_contours     = 1*xy_conts      # (x,y) coordinates of each contour
            self.lengths         = 1*lengths	   # lengths of each contour
            self.area	         = area		   # area of polygon
            self.perimeter       = perimeter	   # perimeter of polygon
            self.FDI             = mizc.frac_dim_index([self.area,self.perimeter])
            
            # lon-lat info if present
            self.spherical_geometry = spherical_geometry
            if spherical_geometry:
               if ll_contours is None:
                  raise ValueError('"ll_contours" not given')
               if ll_bdy_coords is None:
                  raise ValueError('"ll_bdy_coords" not given')
               self.lonlat_contours = 1*ll_contours   # (lon,lat) coordinates of each contour
               self.ll_bdy_coords   = 1*ll_bdy_coords # (lon,lat) coordinates of boundary

            if func_vals is not None:
               self.func_vals	 = 1*func_vals	   # value of function used by Laplace's equation
            if stream_func is not None:
               self.stream_func	 = 1*stream_func   # value of stream function produced by Laplace's equation

            # some summarising info about "lengths"
            lens                       = np.array(lengths)
            self.length_mean           = np.mean(lens)
            self.length_median         = np.median(lens)
            self.length_percentile05   = np.percentile(lens,5)
            self.length_percentile95   = np.percentile(lens,95)

            return
      ###########################################################################################

      if bmap is not None:
         # boundary/area information
         # - use routines for sphere
         import geometry_sphere as GS

         print('getting contour lengths on sphere...\n')

         x,y            = np.array(self.coords).transpose()
         lons,lats      = bmap(x,y,inverse=True)
         lst            = list(np.array([lons,lats]).transpose())
         ll_bdy_coords  = [(lo,la) for lo,la in lst]
         area           = GS.area_polygon_ellipsoid(lons,lats,radians=False)
         arclen         = GS.arc_length(lons,lats,radians=False,closed=True)
         perimeter      = arclen[-1]

         # get isolines of function (plot if pobj is not None)
         contours = self.get_isolines(pobj=pobj,show=show,test_function=test_function,\
                                       func_vals_orig=func_vals_orig)

         ll_conts = []
         lengths  = []

         for cont in contours:
            # list of coords (tuples)
            x,y         = np.array(cont).transpose()
            lons,lats	= bmap(x,y,inverse=True)
            arclen      = GS.arc_length(lons,lats,radians=False,closed=False)
            #
            lengths.append(arclen[-1])	#perimeter
            lst	  = list(np.array([lons,lats]).transpose())
            tups  = [(lo,la) for lo,la in lst]
            ll_conts.append(tups)

         # output object with all the info
         AI = area_info(self.coords,contours,\
                        area,perimeter,lengths,\
                        func_vals=self.func_vals,stream_func=self.stream_func_bdy,\
                        ll_bdy_coords=ll_bdy_coords,ll_contours=ll_conts,\
                        spherical_geometry=True)
      else:
         # boundary/area information
         # - just Euclidean routines
         import geometry_planar as GP

         print('getting contour lengths in the plane...\n')

         x,y         = np.array(self.coords).transpose()
         area        = self.shapely_polygon.area
         perimeter   = self.shapely_polygon.length

         # get isolines of function (plot if pobj is not None)
         contours = self.get_isolines(pobj=pobj,show=show,\
                                       test_function=test_function,\
                                       func_vals_orig=func_vals_orig)
         lengths  = []

         for cont in contours:
            # list of coords (tuples)
            P  = GP.curve_info(cont,closed=False)[0]	# perimeter
            lengths.append(P)

         # output object with all the info
         # area_info(xy_bdy_coords,xy_conts,\
         #			area,perimeter,lengths,\
         #			ll_bdy_coords=None,ll_conts=None,\
         #			func_vals=None,stream_func=None,):
         AI = area_info(self.coords,contours,\
                        area,perimeter,lengths,\
                        func_vals=self.func_vals,stream_func=self.stream_func_bdy,\
                        spherical_geometry=False)

      return AI