Esempio n. 1
0
def main(blkdatfile,
         saltfile,
         lon,
         lat,
         lon2=None,
         lat2=None,
         sectionid=None,
         dpi=180):

    logger.info("Salinity file:%s" % saltfile)
    m = re.match("(.*)_s([0-9]{2})_([0-9a-z]{4}\.nc)", saltfile)
    if m:
        tempfile = m.group(1) + "_t" + m.group(2) + "_" + m.group(3)
    else:
        msg = "Cant deduce temp file from salt file name"
        logger.error(msg)
        raise ValueError, msg
    logger.info("Temp     file:%s" % saltfile)

    sig0 = modeltools.hycom.Sigma(0)
    sig2 = modeltools.hycom.Sigma(2)

    # output filename info from season value
    if int(m.group(2)) > 0 and int(m.group(2)) < 13:
        sinfo = m.group(2)
    elif int(m.group(2)) == 0:
        sinfo = "year"
    elif int(m.group(2)) == 13:
        sinfo = "jfm"
    elif int(m.group(2)) == 14:
        sinfo = "amj"
    elif int(m.group(2)) == 15:
        sinfo = "jas"
    elif int(m.group(2)) == 16:
        sinfo = "ond"

    if sectionid: sinfo = sinfo + "_%s" % sectionid

    s_ncid = netCDF4.Dataset(saltfile, "r")
    t_ncid = netCDF4.Dataset(tempfile, "r")
    s_lon = s_ncid.variables["lon"][:]
    s_lat = s_ncid.variables["lat"][:]
    dprof = s_ncid["depth"][:]
    nz = dprof.size

    #Closest point
    if lon2 is None or lat2 is None:
        section = False
        i = numpy.argmin(numpy.abs(numpy.mod(s_lon - lon + 360., 360.)))
        j = numpy.argmin(numpy.abs(s_lat - lat))

        salprof = s_ncid["s_an"][0, :, j, i]
        temprof = t_ncid["t_an"][0, :, j, i]
        salprof = numpy.expand_dims(salprof, axis=1)
        temprof = numpy.expand_dims(temprof, axis=1)

    # Section
    else:
        section = True
        lon2d, lat2d = numpy.meshgrid(s_lon, s_lat)
        sec = gridxsec.Section([lon, lon2], [lat, lat2], lon2d, lat2d)
        i, j = sec.grid_indexes
        salprof = s_ncid["s_an"][0, :, :, :]
        temprof = t_ncid["t_an"][0, :, :, :]
        salprof = salprof[:, j, i]
        temprof = temprof[:, j, i]

    # MAsk salinity and temperature profiles
    salprof = numpy.ma.masked_where(salprof == s_ncid["s_an"]._FillValue,
                                    salprof)
    temprof = numpy.ma.masked_where(temprof == t_ncid["t_an"]._FillValue,
                                    temprof)

    # Interface values dprofi
    dprofi = numpy.zeros(nz + 1)
    for k in range(dprof.size):
        if k < dprof.size - 1:
            dprofi[k] = 0.5 * (dprof[k] + dprof[k + 1])
        else:
            dprofi[k] = dprof[k]

    # MAx depth in data. TODO. Get from profiles
    maxd = numpy.zeros(salprof[0, :].shape)
    for k in range(nz):
        maxd[~salprof[k, :].mask] = dprof[k]
    logger.info("MAx depth in data is %d m" % numpy.max(maxd))

    # Open blkdat.input
    bp = modeltools.hycom.BlkdatParser(blkdatfile)
    dp0k = bp["dp0k"]
    ds0k = bp["ds0k"]
    sigma = bp["sigma"]
    kdm = bp["kdm"]
    nhybrd = bp["nhybrd"]
    nsigma = bp["nsigma"]
    thflag = bp["thflag"]
    kapref = bp["kapref"]
    isotop = bp["isotop"]
    eqstate = modeltools.hycom.Sigma(thflag)
    #eqstate   = modeltools.hycom.Sigma12Term(thflag)  #Test 12 term sigma
    #eqstate   = modeltools.hycom.Sigma17Term(thflag)  #Test 17 term sigma
    sigprof = eqstate.sig(temprof, salprof)
    sigprof = numpy.ma.masked_where(salprof.mask, sigprof)

    # Thermobaricity
    if kapref <> 0:
        if thflag == 2:
            mykappa = modeltools.hycom.Kappa(kapref, 2000.0e4)
        elif thflag == 0:
            mykappa = modeltools.hycom.Kappa(kapref, 0.)
        else:
            raise ValueError, "Unknown value of thflag"
        tmp = mykappa.kappaf(salprof.transpose(), temprof.transpose(),
                             sigprof.transpose(), dprof * 9806)
        #print tmp.shape,sigprof.shape
        sigprof_tbar = sigprof - tmp.transpose()

    # Min thickness interface values
    intf, masks = bp.intf_min_profile(numpy.array(maxd))
    dp0 = intf[:, 1:] - intf[:, :-1]
    intfmid = (intf[:, 1:] + intf[:, :-1]) * .5
    #print dp0[numpy.argmax(maxd),:],dp0.shape

    # We have interface values, now use designated layer sigma values and actual sigma
    # values to find an approximate vertical coordinate setup.
    newintf, intsig = modeltools.tools.isopycnal_coordinate_layers(
        dprofi, sigprof, dp0, numpy.array(sigma), isotop)

    # TODO: Integrate over temp and sal profiles for plotting these

    # Find segments where its ok to place layer legend
    import scipy.ndimage.measurements
    lab, num_features = scipy.ndimage.measurements.label(
        maxd > numpy.max(maxd) * .25)
    feat_count = {}
    for i in range(num_features):
        feat_count[i + 1] = numpy.count_nonzero(lab == i + 1)
    tmp = sorted(feat_count.items(), key=lambda x: x[1],
                 reverse=True)  # Order by feature count
    main_feature = tmp[0][0]
    ft_ind = []
    for i_enum, i in enumerate(tmp):
        #logger.info( "Feature %03d: %d cells" %(i[0],i[1]))
        tmp = numpy.where(lab == i[0])
        ft_ind.append(int(tmp[0].mean()))
    #print ft_ind
    #tmp=numpy.where(lab==main_feature)
    #print tmp
    #sec_xpind = int (tmp[0].mean())
    #print sec_xpind

    cols = "b"
    colt = "r"
    cold = "m"

    # Plot vertical profile and layers. Two cases: section or not
    if section:
        f, ax = plt.subplots(1, figsize=(10, 5))
        ax.set_title(
            "sigma-%d layers from lon=%6.2f, lat=%6.2f to lon=%6.2f, lat=%6.2f"
            % (thflag, lon, lat, lon2, lat2))
        x = sec.distance / 1000.
        ymult = 1.
        xlim = [x.min(), x.max()]
    else:
        f, ax = plt.subplots(1)
        ax.set_title("Sigma-%d profile and layers at lon=%6.2f, lat=%6.2f" %
                     (thflag, lon, lat))
        x = numpy.array([-100, 100])
        ymult = numpy.ones((2))
        ax.plot(sigprof, -dprof, lw=2, color=cold, label="Sigma-%d" % thflag)
        xlim = ax.get_xlim()
    #print ax.get_position()
    ax.set_position([.1, .1, .7, .7])

    ylim = [-numpy.max(maxd) + 10, 0.]
    for k in range(kdm + 1):

        if k < kdm:
            ax.plot(x,
                    -newintf[:, k] * ymult,
                    lw=.1,
                    color=".5",
                    label="layer %d: %6.3f" % (k + 1, sigma[k]))
            if section:
                ##ind = x.size/2
                #ind = numpy.argmax(maxd)
                #xp = x[ind]
                #yp = -0.5*(newintf[ind,k] + newintf[ind,k+1])
                ##ax.text(xp,yp,"layer %d: %4.2f"%(k+1,sigma[k]),verticalalignment="center",horizontalalignment="left",fontsize=6)
                #ax.text(xp,yp,"%d"%(k+1),verticalalignment="center",horizontalalignment="left",fontsize=6)
                for ind in ft_ind:
                    xp = x[ind]
                    yp = -0.5 * (newintf[ind, k] + newintf[ind, k + 1])
                    #ax.text(xp,yp,"layer %d: %4.2f"%(k+1,sigma[k]),verticalalignment="center",horizontalalignment="left",fontsize=6)
                    ax.text(xp,
                            yp,
                            "%d" % (k + 1),
                            verticalalignment="center",
                            horizontalalignment="left",
                            fontsize=6)
                # TODO
                #ind = x.size-x.size/6
                #xp = x[ind]
                #yp = -0.5*(newintf[ind,k] + newintf[ind,k+1])
                #ax.annotate("layer %d: %4.2f"%(k+1,sigma[k]),
                #    xy=(xp, yp), xycoords='data',
                #    xytext=(1.01,0.0-k*.03), textcoords='axes fraction',
                #    arrowprops=dict(facecolor='b', shrink=0.01,alpha=.5,ec="none",width=1,headwidth=3,frac=.03),
                #    horizontalalignment='left', verticalalignment='center',
                #    fontsize=6
                #    )
            else:
                xp = numpy.sum(xlim) / 2.
                yp = -0.5 * (newintf[0, k] + newintf[0, k + 1])
                #ax.text(xp,yp,"layer %d: %4.2f"%(k+1,sigma[k]),verticalalignment="center",horizontalalignment="left",fontsize=6)
                ax.text(xp,
                        yp,
                        "%d" % (k + 1),
                        verticalalignment="center",
                        horizontalalignment="left",
                        fontsize=6)

            isopyc = numpy.abs(intsig[:, k] - sigma[k]) < 1e-3
            ax.fill_between(x,
                            -newintf[:, k + 1] * ymult,
                            -newintf[:, k] * ymult,
                            color="g",
                            alpha=".5",
                            where=isopyc * ymult)
        else:
            # Plot sea floor
            #ax.plot(x,-newintf[:,k]*ymult,lw=2,color="k",label="Sea floor")
            ax.fill_between(x,
                            -newintf[:, k] * ymult,
                            -10 * numpy.ones(x.shape) * maxd.max(),
                            color=".5")


#   # Some dummy plots for the legend
#   ax.plot([-100,100],[1e5,1e5],lw=.1,color=".5",label="Layer interface")

# Set up final grid
    ax.grid(False)
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    ax.set_ylabel("Depth[m]")
    if section:
        ax.set_xlabel("Distance along section[km]")
    else:
        ax.set_xlabel("Sigma-%d [$kg / m^3$]" % thflag, color=cold)
    for t in ax.get_xticklabels():
        if not section: t.set_color(cold)
        t.set_size(8)
        t.set_rotation(-45)
    #ax.legend(loc="lower left")
    leg = ax.legend(bbox_to_anchor=(1.05, 1),
                    loc=2,
                    borderaxespad=0.,
                    labelspacing=-.5)
    ltext = leg.get_texts()
    plt.setp(ltext, fontsize=4)
    #
    fname = "layers_%s.png" % sinfo
    logger.info("Layers in %s" % fname)
    plt.gcf().savefig(fname, dpi=dpi)
    #
    ax.set_ylim(-750, 0)
    fname = "layers750_%s.png" % sinfo
    logger.info("Layers for top 750m in %s" % fname)
    plt.gcf().savefig("layers750_%s.png" % sinfo, dpi=dpi)
    #
    ax.set_ylim(-100, 0)
    fname = "layers100_%s.png" % sinfo
    logger.info("Layers for top 100m in %s" % fname)
    plt.gcf().savefig("layers100_%s.png" % sinfo, dpi=dpi)

    # Plot density of original data
    f, ax = plt.subplots(1, figsize=(10, 5))
    if section:
        P = ax.pcolormesh(x, -dprof, sigprof, cmap="Paired")
        CS = ax.contour(x, -dprof, sigprof, sigma)
        ax.clabel(CS, inline=1, fontsize=4)
        ax.set_xlabel("x[m]")
        ax.set_ylabel("Depth [m]")
        ax.set_title("Sigma-%d along section [$kg / m^3$]" % (thflag))
        f.colorbar(P, ax=ax)
    else:
        ax.plot(sigprof, -dprof, lw=2, color=cold, label="Sigma-%d" % thflag)
        ax.set_ylabel("Depth[m]")
        ax.set_xlabel("Sigma-%d Density [$kg / m^3$]" % thflag, color=cold)
        ax.set_title("Sigma-%d density profile at %6.2fE,%6.2fN" %
                     (thflag, lon, lat))
        for t in ax.get_xticklabels():
            t.set_color(cold)
            #t.set_size(8)
            #t.set_rotation(-45)
        ax.grid(True)
    f.savefig("dens_data_%s.png" % sinfo, dpi=dpi)

    # Plot density of original data
    if kapref > 0:
        f, ax = plt.subplots(1, figsize=(10, 5))
        if section:
            P = ax.pcolormesh(x, -dprof, sigprof_tbar, cmap="Paired")
            CS = ax.contour(x, -dprof, sigprof_tbar, sigma)
            ax.clabel(CS, inline=1, fontsize=4)
            ax.set_xlabel("x[m]")
            ax.set_ylabel("Depth [m]")
            ax.set_title("In-situ Sigma-%d along section [$kg / m^3$]" %
                         (thflag))
            f.colorbar(P, ax=ax)
        else:
            ax.plot(sigprof_tbar,
                    -dprof,
                    lw=2,
                    color=cold,
                    label="Sigma-%d" % thflag)
            ax.set_ylabel("Depth[m]")
            ax.set_xlabel("Sigma-%d Density [$kg / m^3$]" % thflag, color=cold)
            ax.set_title("In-situ Sigma-%d density profile at %6.2fE,%6.2fN" %
                         (thflag, lon, lat))
            for t in ax.get_xticklabels():
                t.set_color(cold)
                #t.set_size(8)
                #t.set_rotation(-45)
            ax.grid(True)
        f.savefig("dens_data_kappa%d_%s.png" % (kapref, sinfo), dpi=dpi)

    # Plot vertical density gradient of original data
    #print sigprof.shape,dprof.shape
    dsigprof = numpy.zeros(sigprof.shape)
    for k in range(nz):
        #print k,nz
        if k < nz - 1:
            dsigprof[k, :] = (sigprof[k + 1, :] - sigprof[k, :]) / dprof[k]
        else:
            dsigprof[k, :] = dsigprof[k - 1, :]
    dsigprof = numpy.ma.masked_where(numpy.abs(dsigprof) > 1e10, dsigprof)
    dsigprof = dsigprof * 100  # DS per 100 meters
    f, ax = plt.subplots(1, figsize=(10, 5))
    if section:
        P = ax.pcolormesh(x,
                          -dprof,
                          dsigprof,
                          norm=matplotlib.colors.LogNorm(vmin=0.001, vmax=10),
                          cmap="Paired")
        CS = ax.contour(x, -dprof, sigprof, sigma)
        ax.clabel(CS, inline=1, fontsize=4)
        ax.set_title("Delta rho [$kg / m^3$] per 100 meter in the vertical")
        ax.set_ylabel("Depth[m]")
        ax.set_xlabel("Distance along section")
        f.colorbar(P, ax=ax)
    else:
        ax.plot(dsigprof, -dprof, lw=2, color=cold, label="Sigma-%d" % thflag)
        ax.set_ylabel("Depth[m]")
        ax.set_xlabel("Density Difference [$kg / m^3$]", color=cold)
        ax.set_title(
            "Delta rho per 100 meter in the vertical at %8.2fE , %8.2fN" %
            (lon, lat))
        for t in ax.get_xticklabels():
            t.set_color(cold)
            #t.set_size(8)
            #t.set_rotation(-45)
        ax.grid(True)
    f.savefig("densgrad_data_%s.png" % sinfo, dpi=dpi)

    # Plot Buoyancy frequency
    Nsq = dsigprof / 100. * 9.81 / 1000.
    f, ax = plt.subplots(1, figsize=(10, 5))
    if section:
        P = ax.pcolormesh(x,
                          -dprof,
                          Nsq,
                          norm=matplotlib.colors.LogNorm(vmin=1e-9, vmax=1e-4),
                          cmap="Paired")
        #CS=ax.contour(x,-dprof,Nsq,sigma)
        ax.clabel(CS, inline=1, fontsize=4)
        f.colorbar(P, ax=ax)
        ax.set_title("$N^2$ along section")
        ax.set_ylabel("Depth[m]")
        ax.set_xlabel("Distance along section")
    else:
        ax.plot(Nsq, -dprof, lw=2, color=cold, label="Sigma-%d" % thflag)
        ax.set_ylabel("Depth[m]")
        ax.set_xlabel("$N^2$[I always forget the units]", color=cold)
        ax.set_title("$N^2$ at %6.2fE,%6.2fN" % (lon, lat))
        ax.xaxis.set_major_formatter(FormatStrFormatter('%.2g'))

        for t in ax.get_xticklabels():
            t.set_color(cold)
            #t.set_size(8)
            #t.set_rotation(-45)
        ax.grid(True)

    f.savefig("Nsq_data_%s.png" % sinfo, dpi=dpi)
Esempio n. 2
0
def main(region,experiment,tracer,month,axis,layer,cmap,clim,workdir,\
    section,ijspace):
    user = getpass.getuser()

    if region == 'TP0' or region == 'TP2' or region == 'TP5' or region == 'NAT':
        version = 'new'
    elif region == 'TP4' or region == 'NA2':
        version = 'old'
    else:
        print('wrong or not implemented domain is provided')
        quit()

    if axis == 'horizontal' or axis == 'vertical':
        pass
    else:
        print('provide arguments "horizontal" or "vertical"')
        quit()

    if region == "TP0":
        region = "TP0a1.00"
    if region == "TP2":
        region = "TP2a0.10"
    if region == "TP4":
        region = "TP4a0.12"
    if region == "TP5":
        region = "TP5a0.06"
    if region == "NAT":
        region = "NATa1.00"
    if region == "NA2":
        region = "NA2a0.80"

    if tracer == "nitrate" or tracer == 'phosphate' or \
        tracer == 'silicate' or tracer == "oxygen" or \
           tracer == 'dic' or tracer == "alkalinity" or \
              tracer == 'temperature' or tracer == "salinity" :
        pass
    else:
        print('tracer name not correct')
        print('choose: nitrate, phosphate, silicate, \
            oxygen, dic, alkalinity, temperature, salinity')
        quit()

    units = "(mgC m$^{-2}$ s$^{-1}$), tracer converted to C"
    key = "trc"  # trc for tracers,  below tem and sal is set if necessary
    if version == "new":
        if tracer == "nitrate":
            name = "relax.ECO_no3"
        elif tracer == "phosphate":
            name = "relax.ECO_pho"
        elif tracer == "silicate":
            name = "relax.ECO_sil"
        elif tracer == "oxygen":
            name = "relax.ECO_oxy"
            units = "mmol m$^{-3}$"
        elif tracer == "dic":
            name = "relax.CO2_dic"
            units = "mmol m$^{-3}$"
        elif tracer == "alkalinity":
            name = "relax.CO2_alk"
            units = "mEq m$^{-3}$"
        elif tracer == "temperature":
            name = "relax_tem"
            key = "tem"
            units = "$^{o}C$"
        elif tracer == "salinity":
            name = "relax_sal"
            key = "sal"
            units = "psu"

    if version == "old":
        if tracer == "nitrate":
            name = "relax_nit"
        elif tracer == "phosphate":
            name = "relax_pho"
        elif tracer == "silicate":
            name = "relax_sil"
        elif tracer == "oxygen":
            name = "relax_oxy"
            units = "mmol m$^{-3}$"
        elif tracer == "dic":
            name = "relax_dic"
            units = "mmol m$^{-3}$"
        elif tracer == "alkalinity":
            name = "relax_alk"
            units = "mEq m$^{-3}$"
        elif tracer == "temperature":
            name = "relax_tem"
            key = "tem"
            units = "$^{o}C$"
        elif tracer == "salinity":
            name = "relax_sal"
            key = "sal"
            units = "psu"

    abgrid = abfile.ABFileGrid(workdir + user + "/" + \
        region + "/topo/regional.grid","r")
    plon = abgrid.read_field("plon")
    plat = abgrid.read_field("plat")
    jdm, idm = plon.shape

    abdepth = abfile.ABFileBathy(workdir + user + "/" + \
        region + "/relax/" + experiment + "/SCRATCH/regional.depth.b", \
            "r",idm=idm,jdm=jdm)
    depthm = abdepth.read_field("depth")

    abrelax = abfile.ABFileRelax(workdir + user + "/" + \
        region + "/relax/" + experiment + \
            "/" + name + ".a","r")

    # now plot
    fig = plt.figure(figsize=(6, 5), facecolor='w')
    ax = fig.add_subplot(1, 1, 1)
    ax.set_position([0.01, 0.02, 0.865, 0.96])
    cmap = plt.get_cmap(cmap)
    ax.set_facecolor('xkcd:gray')
    if axis == 'horizontal':
        if layer is None:
            print(" ")
            print("provide the layer number to be plotted, e.g. --layer=1")
            print("quitting ...")
            print(" ")
            quit()
        else:
            relax = abrelax.read_field(key, np.int(layer), np.int(month) - 1)
            abrelax.close()
            pmesh = plt.pcolormesh(relax, cmap=cmap)
            cb = ax.figure.colorbar(pmesh)
            if clim is not None: pmesh.set_clim(clim)

    if axis == 'vertical':
        if section is None:
            print(" ")
            print("provide the section to be plotted")
            print("--section='lon1,lon2,lat1,lat2'")
            print("--section='-30.1,2.5,50.0,75.5'")
            print("quitting ...")
            print(" ")
            quit()
        else:
            lon1 = section[0]
            lon2 = section[1]
            lat1 = section[2]
            lat2 = section[3]

        # pick up indexes
        if ijspace:
            sec = gridxsec.SectionIJSpace([lon1, lon2], [lat1, lat2], plon,
                                          plat)
        else:
            sec = gridxsec.Section([lon1, lon2], [lat1, lat2], plon, plat)
        I, J = sec.grid_indexes
        dist = sec.distance
        slon = sec.longitude
        slat = sec.latitude

        dpname = modeltools.hycom.layer_thickness_variable["archive"]
        # get arbitrary relaxation thicknesses
        dummy = sorted(fnmatch.filter(\
            os.listdir(workdir + user + "/" + \
                region + "/relax/" + experiment), \
                    'relax.0000_[012]*_00.a'))

        dummyarch = workdir + user + "/" + \
            region + "/relax/" + experiment + \
               "/" + dummy[np.int(month)-1]
        dummyfile = abfile.ABFileArchv(dummyarch, "r")
        kdm = max(dummyfile.fieldlevels)
        intfsec = np.zeros((kdm + 1, I.size))
        datasec = np.zeros((kdm + 1, I.size))
        for k in range(kdm):
            dp2d = dummyfile.read_field(dpname, k + 1)
            data2d = abrelax.read_field(key, k + 1, np.int(month) - 1)
            dp2d = np.ma.filled(dp2d, 0.)
            dp2d = dp2d / modeltools.hycom.onem
            data2d = np.ma.filled(data2d, 1e30)

            intfsec[k + 1, :] = intfsec[k, :] + dp2d[J, I]
            if k == 0: datasec[k, :] = data2d[J, I]
            datasec[k + 1, :] = data2d[J, I]
        datasec = np.ma.masked_where(datasec > 0.5 * 1e30, datasec)

        x = dist / 1000.  # km
        pmesh = ax.pcolormesh(x, -intfsec, datasec, cmap=cmap)
        cb = ax.figure.colorbar(pmesh)
        if clim is not None: pmesh.set_clim(clim)

        axm = fig.add_subplot(212)
        axm.set_position([0.85, 0.85, 0.15, 0.15])
        axm.set_facecolor('xkcd:gray')
        pmesh2 = plt.pcolormesh(dummyfile.read_field(dpname, 1) * 0.,
                                cmap=cmap)
        pltsec = plt.plot(I, J, 'r', lw=2)
        plt.xticks([])
        plt.yticks([])

    plt.text(0.015,1.05, "%s %s %s" %(tracer, "relaxation month:",\
        month.zfill(2)),transform=ax.transAxes,FontSize=13)
    if axis == 'horizontal':
        plt.text(0.015,
                 1.005,
                 units + " @layer=" + layer,
                 transform=ax.transAxes,
                 FontSize=8)
    else:
        plt.text(0.015, 1.005, units, transform=ax.transAxes, FontSize=8)
    # save figure

    counter = 0
    plottemp = workdir + user + "/" + \
        region + "/relax/" + experiment + \
            "/" + name + "_" + axis + "_%s" + ".png"
    plotname = plottemp % (np.str(counter).zfill(3))

    while os.path.isfile(plotname):
        counter += 1
        plotname = plottemp % (np.str(counter).zfill(3))

    fig.canvas.print_figure(plotname, dpi=180)
    print(" ")
    print("figure: " + plotname)
    print(" ")
def main(lon1,
         lat1,
         lon2,
         lat2,
         variable,
         files,
         filetype="archive",
         clim=None,
         sectionid="",
         ijspace=False,
         xaxis="distance",
         section_map=False,
         dens=False,
         dpi=180):

    logger.info("Filetype is %s" % filetype)
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")

    # Set up section info
    if ijspace:
        sec = gridxsec.SectionIJSpace([lon1, lon2], [lat1, lat2], plon, plat)
    else:
        sec = gridxsec.Section([lon1, lon2], [lat1, lat2], plon, plat)
    I, J = sec.grid_indexes
    dist = sec.distance
    print('dit.shae=', dist.shape)
    slon = sec.longitude
    slat = sec.latitude

    logger.info("Min max I-index (starts from 0):%d %d" % (I.min(), I.max()))
    logger.info("Min max J-index (starts from 0):%d %d" % (J.min(), J.max()))
    #
    #
    if section_map:
        ll_lon = slon.min() - 10.
        ur_lon = slon.max() + 10.
        ll_lat = np.maximum(-90., slat.min() - 10.)
        ur_lat = np.minimum(90., slat.max() + 10.)

        proj = ccrs.Stereographic(central_latitude=90.0,
                                  central_longitude=-40.0)
        pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat)
        px = pxy[:, :, 0]
        py = pxy[:, :, 1]
        x, y = np.meshgrid(np.arange(slon.shape[0]), np.arange(slat.shape[0]))

        figure = plt.figure(figsize=(10, 8))
        ax = figure.add_subplot(111)

        ax = plt.axes(projection=ccrs.PlateCarree())
        ax.set_extent([-179, 179, 53, 85], ccrs.PlateCarree())
        ax.add_feature(cfeature.GSHHSFeature('auto', edgecolor='grey'))
        ax.add_feature(cfeature.GSHHSFeature('auto', facecolor='grey'))
        ax.gridlines()
        ax.plot(slon, slat, "r-", lw=1)

        pos = ax.get_position()
        asp = pos.height / pos.width
        w = figure.get_figwidth()
        h = asp * w
        figure.set_figheight(h)
        if sectionid:
            figure.canvas.print_figure("map_%s.png" % sectionid, dpi=dpi)
        else:
            figure.canvas.print_figure("map.png", dpi=dpi)

    # Get layer thickness variable used in hycom
    dpname = modeltools.hycom.layer_thickness_variable[filetype]
    logger.info("Filetype %s: layer thickness variable is %s" %
                (filetype, dpname))

    if xaxis == "distance":
        x = dist / 1000.
        xlab = "Distance along section[km]"
    elif xaxis == "i":
        x = I
        xlab = "i-index"
    elif xaxis == "j":
        x = J
        xlab = "j-index"
    elif xaxis == "lon":
        x = slon
        xlab = "longitude"
    elif xaxis == "lat":
        x = slat
        xlab = "latitude"
    else:
        logger.warning("xaxis must be i,j,lo,lat or distance")
        x = dist / 1000.
        xlab = "Distance along section[km]"

    # Loop over archive files
    figure = plt.figure()
    ax = figure.add_subplot(111)
    pos = ax.get_position()
    for fcnt, myfile0 in enumerate(files):

        # Remove [ab] ending if present
        m = re.match("(.*)\.[ab]", myfile0)
        if m:
            myfile = m.group(1)
        else:
            myfile = myfile0

        # Add more filetypes if needed. By def we assume archive
        if filetype == "archive":
            i_abfile = abf.ABFileArchv(myfile, "r")
        elif filetype == "restart":
            i_abfile = abf.ABFileRestart(myfile,
                                         "r",
                                         idm=gfile.idm,
                                         jdm=gfile.jdm)
        else:
            raise NotImplementedError("Filetype %s not implemented" % filetype)

        # kdm assumed to be max level in ab file
        kdm = max(i_abfile.fieldlevels)

        # Set up interface and daat arrays
        xx = np.zeros((kdm + 1, I.size))
        intfsec = np.zeros((kdm + 1, I.size))
        datasec = np.zeros((kdm + 1, I.size))
        if dens:
            datasec_sal = np.zeros((kdm + 1, I.size))
            sigma_sec = np.zeros((kdm + 1, I.size))

        # Loop over layers in file.
        logger.info("File %s" % (myfile))
        for k in range(kdm):
            logger.debug("File %s, layer %03d/%03d" % (myfile, k, kdm))

            # Get 2D fields
            dp2d = i_abfile.read_field(dpname, k + 1)
            data2d = i_abfile.read_field(variable, k + 1)
            dp2d = np.ma.filled(dp2d, 0.) / modeltools.hycom.onem
            data2d = np.ma.filled(data2d, 1e30)

            # Place data into section arrays
            intfsec[k + 1, :] = intfsec[k, :] + dp2d[J, I]
            if k == 0: datasec[k, :] = data2d[J, I]
            datasec[k + 1, :] = data2d[J, I]

            if dens:
                data2d_sal = i_abfile.read_field('salin', k + 1)
                data2d_sal = np.ma.filled(data2d_sal, 1e30)
                datasec_sal[k + 1, :] = data2d_sal[J, I]

        i_maxd = np.argmax(np.abs(intfsec[kdm, :]))
        for k in range(kdm + 1):
            xx[k, :] = x[:]

        datasec = np.ma.masked_where(datasec > 0.5 * 1e30, datasec)
        print("datasec min, max=", datasec.min(), datasec.max())
        if dens:
            datasec_sal = np.ma.masked_where(datasec_sal > 0.5 * 1e30,
                                             datasec_sal)
            print("datasec_sal min, max=", datasec_sal.min(),
                  datasec_sal.max())
            sigma_sec = mod_hyc2plot.sig(datasec, datasec_sal)
            sigma_sec = np.ma.masked_where(sigma_sec < 0.0, sigma_sec)
            datasec = sigma_sec
        # Set up section plot
        datasec = np.ma.masked_where(datasec > 0.5 * 1e30, datasec)
        print("min, max=", datasec.min(), datasec.max())
        if clim is None:
            clim = [datasec.min(), datasec.max()]
            #clim=[0.0,13]
        print("clim=", clim[0], clim[1])
        if clim is not None:
            lvls = MaxNLocator(nbins=70).tick_values(clim[0], clim[1])
        mf = 'sawtooth_fc100.txt'
        LinDic = mod_hyc2plot.cmap_dict(mf)
        my_cmap = matplotlib.colors.LinearSegmentedColormap(
            'my_colormap', LinDic)
        cmap = my_cmap
        norm = BoundaryNorm(lvls, ncolors=cmap.N, clip=True)
        P = ax.contourf(xx, -intfsec, datasec, cmap=cmap, levels=lvls)

        # Plot layer interfaces
        for k in range(1, kdm + 1):
            if k % 100 == 0:
                PL = ax.plot(x, -intfsec[k, :], "-", color="k")
                textx = x[i_maxd]
                texty = -0.5 * (intfsec[k - 1, i_maxd] + intfsec[k, i_maxd])
                ax.text(textx,
                        texty,
                        str(k),
                        verticalalignment="center",
                        horizontalalignment="center",
                        fontsize=6)
            elif k % 5 == 0:
                PL = ax.plot(x, -intfsec[k, :], "--", color="k", linewidth=0.5)
                textx = x[i_maxd]
                texty = -0.5 * (intfsec[k - 1, i_maxd] + intfsec[k, i_maxd])
                ax.text(textx,
                        texty,
                        str(k),
                        verticalalignment="center",
                        horizontalalignment="center",
                        fontsize=6)
            else:
                if k > 2 and k % 2 == 0:
                    PL = ax.plot(x,
                                 -intfsec[k, :],
                                 "-",
                                 color=".5",
                                 linewidth=0.5)
                    textx = x[i_maxd]
                    texty = -0.5 * (intfsec[k - 1, i_maxd] +
                                    intfsec[k, i_maxd])
                    ax.text(textx,
                            texty,
                            str(k),
                            verticalalignment="center",
                            horizontalalignment="center",
                            fontsize=6)
                else:
                    continue
        # Print figure
        ax.set_facecolor('xkcd:gray')
        aspect = 90
        pad_fraction = 0.25
        divider = make_axes_locatable(ax)
        width = axes_size.AxesY(ax, aspect=1. / aspect)
        pad = axes_size.Fraction(pad_fraction, width)
        cax = divider.append_axes("right", size=width, pad=pad)
        cb = ax.figure.colorbar(P, cax=cax)
        if clim is not None: P.set_clim(clim)
        if dens:
            ax.set_title('[P. density ]: ' + myfile)
        else:
            ax.set_title('[' + variable + ']: ' + myfile)

        ax.set_ylabel('Depth [m]')
        ax.set_xlabel(xlab)

        # Print in different y-lims
        suff = os.path.basename(myfile)
        if sectionid: suff = suff + "_" + sectionid
        if dens: variable = "dens"
        figure.canvas.print_figure("sec_%s_full_%s.png" % (variable, suff),
                                   dpi=dpi)
        ax.set_ylim(-1000, 0)
        figure.canvas.print_figure("sec_%s_1000m_%s.png" % (variable, suff),
                                   dpi=dpi)

        # Close input file
        i_abfile.close()

        #
        ax.clear()
        cb.remove()
def main(lon1,lat1,lon2,lat2,variable,files,filetype="archive",clim=None,sectionid="",
      ijspace=False,xaxis="distance",section_map=False,ncfiles="",dpi=180) :
   #TP4Grd='/cluster/work/users/aal069/TP4a0.12/mfile/'
   logger.info("Filetype is %s"% filetype)
   gfile = abf.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")


   # Set up section info
   if ijspace :
      sec = gridxsec.SectionIJSpace([lon1,lon2],[lat1,lat2],plon,plat)
   else  :
      sec = gridxsec.Section([lon1,lon2],[lat1,lat2],plon,plat)
   I,J=sec.grid_indexes
   dist=sec.distance
   print('dit.shae=',dist.shape)
   slon=sec.longitude
   slat=sec.latitude
   # In testing
   #J,I,slon,slat,case,dist=sec.find_intersection(qlon,qlat)
   #print I,J
   #raise NameError,"test"

   logger.info("Min max I-index (starts from 0):%d %d"%(I.min(),I.max()))
   logger.info("Min max J-index (starts from 0):%d %d"%(J.min(),J.max()))
   #
   #
   if section_map :
      ll_lon=slon.min()-10.
      ur_lon=slon.max()+10.
      ll_lat=np.maximum(-90.,slat.min()-10.)
      ur_lat=np.minimum(90. ,slat.max()+10.)

      proj=ccrs.Stereographic(central_latitude=90.0,central_longitude=-40.0)
      #pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat)
      #px=pxy[:,:,0]
      #py=pxy[:,:,1]
      #x,y=np.meshgrid(np.arange(slon.shape[0]),np.arange(slat.shape[0]))
        
      figure =plt.figure(figsize=(8,8))
      ax=figure.add_subplot(111,projection=proj)
      #ax = plt.axes(projection=ccrs.PlateCarree())
      ax.set_extent([-179, 179, 53, 85],ccrs.PlateCarree())
      #ax = plt.axes(projection=ccrs.Stereographic())
      ax.add_feature(cfeature.GSHHSFeature('auto', edgecolor='grey'))
      ax.add_feature(cfeature.GSHHSFeature('auto', facecolor='grey'))
      ax.gridlines()
      #ax.coastlines(resolution='110m')
      ax.plot(slon,slat,"r-",lw=1,transform=ccrs.PlateCarree())
       
      pos = ax.get_position()
      asp=pos.height/pos.width
      w=figure.get_figwidth()
      h=asp*w
      figure.set_figheight(h)
      if sectionid :
         figure.canvas.print_figure("map_%s.png"%sectionid,dpi=dpi,bbox_inches='tight')
      else :
         figure.canvas.print_figure("map.png",dpi=dpi,bbox_inches='tight')

   # Get layer thickness variable used in hycom
   dpname = modeltools.hycom.layer_thickness_variable[filetype]
   logger.info("Filetype %s: layer thickness variable is %s"%(filetype,dpname))


   if xaxis == "distance" :
      x=dist/1000.
      xlab="Distance along section[km]"
   elif xaxis == "i" :
      x=I
      xlab="i-index"
   elif xaxis == "j" :
      x=J
      xlab="j-index"
   elif xaxis == "lon" :
      x=slon
      xlab="longitude"
   elif xaxis == "lat" :
      x=slat
      xlab="latitude"
   else :
      logger.warning("xaxis must be i,j,lo,lat or distance")
      x=dist/1000.
      xlab="Distance along section[km]"

   # get kdm from the first file:
   # Remove [ab] ending if present
   print('firstfilw', files[0])
   m=re.match("(.*)\.[ab]",files[0])
   print('m=',m.group(1))
   myf=m.group(1)
   fi_abfile = abf.ABFileArchv(myf,"r")
   kdm=max(fi_abfile.fieldlevels)

   # Loop over archive files
   figure = plt.figure()
   ax=figure.add_subplot(111)
   pos = ax.get_position()
   count_sum=0
   intfsec_sum=np.zeros((kdm+1,I.size))
   datasec_sum=np.zeros((kdm+1,I.size))
   for fcnt,myfile0 in enumerate(files) :
      count_sum=count_sum+1
      print('count_sum==', count_sum)
      print('fcnt=', fcnt)
      print('mfile0=', myfile0)
      # Remove [ab] ending if present
      m=re.match("(.*)\.[ab]",myfile0)
      if m :
         myfile=m.group(1)
      else :
         myfile=myfile0

      # Add more filetypes if needed. By def we assume archive
      if filetype == "archive" :
         i_abfile = abf.ABFileArchv(myfile,"r")
      elif filetype == "restart" :
         i_abfile = abf.ABFileRestart(myfile,"r",idm=gfile.idm,jdm=gfile.jdm)
      else :
         raise NotImplementedError("Filetype %s not implemented"%filetype)
      # kdm assumed to be max level in ab file
      kdm=max(i_abfile.fieldlevels)

      # Set up interface and daat arrays
      
      xx=np.zeros((kdm+1,I.size))
      intfsec=np.zeros((kdm+1,I.size))
      datasec=np.zeros((kdm+1,I.size))
      # Loop over layers in file. 
      logger.info("File %s"%(myfile))
      for k in range(kdm) :
         logger.debug("File %s, layer %03d/%03d"%(myfile,k,kdm))

         # Get 2D fields
         dp2d=i_abfile.read_field(dpname,k+1)
         data2d=i_abfile.read_field(variable,k+1)
         #print('---mn,mx  data=',  data2d.min(),data2d.max())
         if (k%kdm==49):
            print("---Reach bottom layer" )
         dp2d=np.ma.filled(dp2d,0.)/modeltools.hycom.onem
         data2d=np.ma.filled(data2d,1e30)
         # Place data into section arrays
         intfsec[k+1,:] = intfsec[k,:] + dp2d[J,I]
         if k==0 : datasec[k,:] = data2d[J,I]
         datasec[k+1,:] = data2d[J,I]
      

      intfsec_sum=intfsec_sum + intfsec
      datasec_sum=datasec_sum + datasec
      #print 'prs_intafce=', np.transpose(intfsec[:,15]) 
      i_abfile.close()

      # end loop over files
      
   intfsec_avg=intfsec_sum/count_sum
   datasec_avg=datasec_sum/count_sum

   if ncfiles :
      MLDGS_sum=np.zeros((1,I.size))
      count_sum=0
      for fcnt,ncfile in enumerate(ncfiles) :
         count_sum=count_sum+1
         print('ncfile count_sum==', count_sum)
         print('ncfile fcnt=', fcnt)
         print('ncfilefile=', ncfile)
         MLDGS=np.zeros((1,I.size))
         ncfile0 = netCDF4.Dataset(ncfile,'r')
         MLD_2D  = ncfile0.variables['GS_MLD'][:]
         #MLD_2D  = ncfile0.variables['mlp'][:]
         MLDGS[0,:]=MLD_2D[0,J,I]
         MLDGS_sum= MLDGS_sum + MLDGS
         ncfile0.close()
      # end loop over files
      MLDGS_avg=MLDGS_sum/count_sum
   #
   #-----------------------------------------------------------------
   # read from clim mld TP5netcdf
   if ncfiles :
      if 'TP2' in files[0]:
         fh=netCDF4.Dataset('mld_dr003_l3_modif_Interp_TP2grd.nc')
      else:
         fh=netCDF4.Dataset('mld_dr003_l3_modif_Interp_TP5grd.nc')
      fhmldintrp = fh.variables['TP5mld'][:]
      fh.close()
      #fhMLDintrp_sum=np.zeros((760,800))
      MLDclim_sum=np.zeros((1,I.size))
      cunt_sum=0
      for ii in range(12) :
          cunt_sum=cunt_sum +1
          MLDclim=np.zeros((1,I.size))
          MLDclim[0,:]=fhmldintrp[ii,J,I]

          MLDclim_sum= MLDclim_sum + MLDclim
          print('clim count_sum==', cunt_sum)
      MLDclim_avg=MLDclim_sum/cunt_sum
   #-----------------------------------------------------------------   
   i_maxd=np.argmax(np.abs(intfsec_avg[kdm,:]))
   #print i_maxd
   for k in range(kdm+1) :
      xx[k,:] = x[:]
   # Set up section plot
   #datasec = np.ma.masked_where(datasec==1e30,datasec)
   datasec_avg = np.ma.masked_where(datasec_avg>0.5*1e30,datasec_avg)
   #print datasec.min(),datasec.max()
   #P=ax.pcolormesh(dist/1000.,-intfsec,datasec)
   #print i_maxd
   for k in range(kdm+1) :
      xx[k,:] = x[:]
   
   if clim is not None : lvls = MaxNLocator(nbins=30).tick_values(clim[0], clim[1])
   #print 'levels=', lvls
   mf='sawtooth_0-1.txt'
   LinDic=mod_hyc2plot.cmap_dict(mf)
   my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',LinDic)
   cmap=my_cmap
   #cmap = matplotlib.pyplot.get_cmap('gist_rainbow_r')
   norm = BoundaryNorm(lvls, ncolors=cmap.N, clip=True)
   print('x.shape=' ,      x.shape)
   print('x.min,xmax=' ,  x.min(),x.max())
   print('xx.shape=' ,      xx.shape)
   print('xx.min,xxmax=' ,  xx.min(),xx.max())
   print('intfsec_avg.shape=', intfsec_avg.shape)
   print('datasec_avg.shape=', datasec_avg.shape)
   #P=ax.pcolormesh(x,-intfsec,datasec,cmap=cmap)
   P=ax.contourf(xx,-intfsec_avg,datasec_avg,extend='both',cmap=cmap,levels=lvls)
   if 'sal' in variable:
      P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[32.0,33.0,34.0,35.0,35.5],
          colors=('k',),linestyles=('-',),linewidths=(1.5,))
   else:
      P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[-1,0.0,2.0],
          colors=('k',),linestyles=('-',),linewidths=(1.5,))
   matplotlib.pyplot.clabel(P1, fmt = '%2.1d', colors = 'k', fontsize=10) #contour line labels
   # Plot layer interfaces
   for k in range(1,kdm+1) :
      if k%100 == 0 : 
         PL=ax.plot(x,-intfsec_avg[k,:],"-",color="k")
      elif k%5 == 0 and k <= 10: 
         PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5)
         textx = x[i_maxd]
         texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
      elif k%2 and k > 10 : 
         PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5)
         textx = x[i_maxd]
         texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
   if ncfiles :
      PL=ax.plot(x,-MLDGS_avg[0,:],"-",color="w", linewidth=1.50)
      PL=ax.plot(x,-MLDclim_avg[0,:],"--",color="r", linewidth=1.50)
###    else :
###       PL=ax.plot(x,-intfsec_avg[k,:],"-",color=".5")
  # Print figure and remove wite space.
   aspect = 50
   pad_fraction = 0.25
   divider = make_axes_locatable(ax)
   width = axes_size.AxesY(ax, aspect=1./aspect)
   pad = axes_size.Fraction(pad_fraction, width)
   cax = divider.append_axes("right", size=width, pad=pad)
   cb=ax.figure.colorbar(P,cax=cax,extend='both')
   #cb=ax.figure.colorbar(P,extend='both')
   if clim is not None : P.set_clim(clim)
   #cb=ax.figure.colorbar(P,extend='both')
   ax.set_title(variable+':'+myfile+'AVG-')
   ax.set_ylabel('Depth [m]')
   ax.set_xlabel(xlab)
   #ax.set_position(pos)
   #matplotlib.pyplot.tight_layout()

   # Print in different y-lims 
   suff=os.path.basename(myfile)
   if sectionid : suff=suff+"_"+sectionid
   figure.canvas.print_figure("sec_AVG_%s_full_%s.png"%(variable,suff),dpi=dpi)
   #ax.set_ylim(-1000,0)
   if 'Fram' in sectionid or 'Svin' in sectionid:
      print('sectionid=', sectionid)
      ax.set_ylim(-600,0)
      figure.canvas.print_figure("sec_AVG_%s_600m_%s.png"%(variable,suff),dpi=dpi)
   else:
      #ax.set_ylim(-2500,0)
      #figure.canvas.print_figure("sec_AVG_%s_2500m_%s.png"%(variable,suff),dpi=dpi)
      ax.set_ylim(-3000,0)
      figure.canvas.print_figure("sec_AVG_%s_3000m_%s.png"%(variable,suff),dpi=dpi)

   # Close input file
   #i_abfile.close()
   #
   ax.clear()
   cb.remove()
def main(lon1,lat1,lon2,lat2,variable,files,filetype="archive",clim=None,sectionid="",
      ijspace=False,xaxis="distance",section_map=False,dpi=180) :
   TP4Grd='/cluster/work/users/aal069/TP4a0.12/mfile/'
   logger.info("Filetype is %s"% filetype)
   gfile = abf.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")


   # Set up section info
   if ijspace :
      sec = gridxsec.SectionIJSpace([lon1,lon2],[lat1,lat2],plon,plat)
   else  :
      sec = gridxsec.Section([lon1,lon2],[lat1,lat2],plon,plat)
   I,J=sec.grid_indexes
   dist=sec.distance
   print('dit.shae=',dist.shape)
   slon=sec.longitude
   slat=sec.latitude
   # In testing
   #J,I,slon,slat,case,dist=sec.find_intersection(qlon,qlat)
   #print I,J
   #raise NameError,"test"

   logger.info("Min max I-index (starts from 0):%d %d"%(I.min(),I.max()))
   logger.info("Min max J-index (starts from 0):%d %d"%(J.min(),J.max()))
   #
   #
   if section_map :
      ll_lon=slon.min()-10.
      ur_lon=slon.max()+10.
      ll_lat=np.maximum(-90.,slat.min()-10.)
      ur_lat=np.minimum(90. ,slat.max()+10.)

      proj=ccrs.Stereographic(central_latitude=90.0,central_longitude=-40.0)
      #pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat)
      #px=pxy[:,:,0]
      #py=pxy[:,:,1]
      #x,y=np.meshgrid(np.arange(slon.shape[0]),np.arange(slat.shape[0]))
        
      figure =plt.figure(figsize=(8,8))
      ax=figure.add_subplot(111,projection=proj)
      #ax = plt.axes(projection=ccrs.PlateCarree())
      ax.set_extent([-179, 179, 53, 85],ccrs.PlateCarree())
      #ax = plt.axes(projection=ccrs.Stereographic())
      ax.add_feature(cfeature.GSHHSFeature('auto', edgecolor='grey'))
      ax.add_feature(cfeature.GSHHSFeature('auto', facecolor='grey'))
      ax.gridlines()
      #ax.coastlines(resolution='110m')
      ax.plot(slon,slat,"r-",lw=1,transform=ccrs.PlateCarree())
      
      pos = ax.get_position()
      asp=pos.height/pos.width
      w=figure.get_figwidth()
      h=asp*w
      figure.set_figheight(h)
      if sectionid :
         figure.canvas.print_figure("map_%s.png"%sectionid,dpi=dpi,bbox_inches='tight')
      else :
         figure.canvas.print_figure("map.png",dpi=dpi,bbox_inches='tight')

   # Get layer thickness variable used in hycom
   dpname = modeltools.hycom.layer_thickness_variable[filetype]
   logger.info("Filetype %s: layer thickness variable is %s"%(filetype,dpname))


   if xaxis == "distance" :
      x=dist/1000.
      xlab="Distance along section[km]"
   elif xaxis == "i" :
      x=I
      xlab="i-index"
   elif xaxis == "j" :
      x=J
      xlab="j-index"
   elif xaxis == "lon" :
      x=slon
      xlab="longitude"
   elif xaxis == "lat" :
      x=slat
      xlab="latitude"
   else :
      logger.warning("xaxis must be i,j,lo,lat or distance")
      x=dist/1000.
      xlab="Distance along section[km]"

   # get kdm from the first file:
   # Remove [ab] ending if present
   myfile0=files[0]
   print( 'myfile0', myfile0)

   m=re.match("(.*)\.[ab]",myfile0)
   print('m=',m.group(1))
   if m :
      myfile=m.group(1)
   else :
      myfile=myfile0
   dta_afile = abf.AFile(gfile.idm,gfile.jdm,myfile0,"r")
   #intfl='../../../relax/010/relax_int.a'
   intfl=myfile[:-3] + 'int.a'
   int_afile = abf.AFile(gfile.idm,gfile.jdm,intfl,"r")
   #
   lyr=1
   #record_num,xmn,xmx=dta_afile.get_record_number(variable,lyr)
   #print 'record_num, variable, layer ===', record_num-1, variable, lyr
   #print 'mn,mx=',xmn,xmx
   # for record in record_num :
   record_num=1
   record_var=record_num-1 
   fld = dta_afile.read_record(record_var)
   print('mn,mx  data=',fld.min(),fld.max())
   #np.testing.assert_approx_equal(xmn,fld.min(),significant=8)
   #np.testing.assert_approx_equal(xmx,fld.max(),significant=8)
   # presure interfc
   #record_num,xmn,xmx=int_afile.get_record_number('int',lyr)
   #print 'record_num, variable, layer ===', record_num-1, 'int', lyr
   #print 'mn,mx=',xmn,xmx
   # for record in record_num :
   record_prs=record_num-1 
   fld = int_afile.read_record(record_prs)
   print('mn,mx  intface=',fld.min(),fld.max())
   #np.testing.assert_approx_equal(xmn,fld.min(),significant=8)
   #np.testing.assert_approx_equal(xmx,fld.max(),significant=8)
   #
   #kdm=max(fi_abfile.fieldlevels)
   kdm=50
   # Loop over archive files
   figure = plt.figure()
   ax=figure.add_subplot(111)
   pos = ax.get_position()
   count_sum=0
   intfsec_sum=np.zeros((kdm+1,I.size))
   datasec_sum=np.zeros((kdm+1,I.size))
   #
   for mnth in range(12) :
      count_sum=count_sum+1

      logger.info("Reading data and presure interface for month %s"%(mnth+1))
      record_var_pnt=record_var + mnth*kdm
      record_prs_pnt=record_prs + mnth*kdm
      print('pointing at record num:  record_var_pnt', record_var_pnt)
      print('pointing at record num:  record_prs_pnt', record_prs_pnt)
      # Set up interface and daat arrays
      xx=np.zeros((kdm+1,I.size))
      intfsec=np.zeros((kdm+1,I.size))
      datasec=np.zeros((kdm+1,I.size))
      # Loop over layers in file. 
      logger.info("File %s"%(myfile))
      logger.info("intfac_File %s"%(intfl))
      #loop over 50 records
      for k in range(kdm) :
         logger.debug("File %s, layer %03d/%03d"%(myfile,k,kdm))
         r_var=record_var_pnt+k
         r_prs=record_prs_pnt+k
         # Get 2D fields
         dp2d = int_afile.read_record(r_prs)
         data2d=dta_afile.read_record(r_var)
         #print('reading rec num: r_var,r_dp=', r_var, r_prs,' ','data.min,data.max=',data2d.min(),data2d.max())
         print('data: month,layer, range', (mnth+1),'',(r_var)%kdm,data2d.min(),data2d.max())
         #dp2d=i_abfile.read_field(dpname,k+1)
         #data2d=i_abfile.read_field(variable,k+1)
         if ((r_var)%kdm==49):
            print('mn,mx  intface=',dp2d.min(),dp2d.max())
            print('mn,mx  data=',  data2d.min(),data2d.max())
            print( "Reach bottom layer" )
         dp2d=np.ma.filled(dp2d,0.)/modeltools.hycom.onem
         data2d=np.ma.filled(data2d,1e30)

         # Place data into section arrays
         #intfsec[k+1,:] = intfsec[k,:] + dp2d[J,I]
         #print("data2d.shape=",data2d.shape)
         #print("data2d[J,I].size=",data2d[J,I].size)
         intfsec[k+1,:] = dp2d[J,I]
         if k==0 : datasec[k,:] = data2d[J,I]
         datasec[k+1,:] = data2d[J,I]
      

      intfsec_sum=intfsec_sum + intfsec
      datasec_sum=datasec_sum + datasec
      #print 'prs_intafce=', np.transpose(intfsec[:,15]) 
     
   dta_afile.close()
   int_afile.close()
      # end loop over files

   print ('count_sum=',count_sum)
   intfsec_avg=intfsec_sum/count_sum
   datasec_avg=datasec_sum/count_sum
   #
   i_maxd=np.argmax(np.abs(intfsec_avg[kdm,:]))
   #print i_maxd
   for k in range(kdm+1) :
      xx[k,:] = x[:]
   # Set up section plot
   #datasec = np.ma.masked_where(datasec==1e30,datasec)
   datasec_avg = np.ma.masked_where(datasec_avg>0.5*1e30,datasec_avg)
   #print datasec.min(),datasec.max()
   #P=ax.pcolormesh(dist/1000.,-intfsec,datasec)
   #print i_maxd
   for k in range(kdm+1) :
      xx[k,:] = x[:]
   
   if clim is not None : lvls = MaxNLocator(nbins=30).tick_values(clim[0], clim[1])
   #print 'levels=', lvls
   mf='sawtooth_0-1.txt'
   LinDic=mod_hyc2plot.cmap_dict(mf)
   my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap',LinDic)
   cmap=my_cmap
   #cmap = plt.get_cmap('gist_rainbow_r')
   norm = BoundaryNorm(lvls, ncolors=cmap.N, clip=True)
   print('x.shape=' ,      x.shape)
   print('x.min,xmax=' ,  x.min(),x.max())
   print('xx.shape=' ,      xx.shape)
   print('xx.min,xxmax=' ,  xx.min(),xx.max())
   print('intfsec_avg.shape=', intfsec_avg.shape)
   print('datasec_avg.shape=', datasec_avg.shape)
   #P=ax.pcolormesh(x,-intfsec,datasec,cmap=cmap)
   P=ax.contourf(xx,-intfsec_avg,datasec_avg,extend='both',cmap=cmap,levels=lvls)
   if 'sal' in variable:
      P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[32.0,33.0,34.0,35.0,35.5],
          colors=('k',),linestyles=('-',),linewidths=(1.5,))
   else:
      P1=ax.contour(xx,-intfsec_avg,datasec_avg,levels=[-1.0,0.0,2.0],
          colors=('k',),linestyles=('-',),linewidths=(1.5,))
   plt.clabel(P1, fmt = '%2.1d', colors = 'k', fontsize=10) #contour line labels
   # Plot layer interfaces
   for k in range(1,kdm+1) :
      if k%100 == 0 : 
         PL=ax.plot(x,-intfsec_avg[k,:],"-",color="k")
      elif k%5 == 0 and k <= 10: 
         PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5)
         textx = x[i_maxd]
         texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
      elif k%2 == 0 and k > 10: 
         PL=ax.plot(x,-intfsec_avg[k,:],"--",color="k", linewidth=0.5)
         textx = x[i_maxd]
         texty = -0.5*(intfsec_avg[k-1,i_maxd] + intfsec_avg[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
###    else :
###       PL=ax.plot(x,-intfsec_avg[k,:],"-",color=".5")
  # Print figure and remove wite space.
   aspect = 50
   pad_fraction = 0.25
   divider = make_axes_locatable(ax)
   width = axes_size.AxesY(ax, aspect=1./aspect)
   pad = axes_size.Fraction(pad_fraction, width)
   cax = divider.append_axes("right", size=width, pad=pad)
   cb=ax.figure.colorbar(P,cax=cax,extend='both')
   #cb=ax.figure.colorbar(P,extend='both')
   if clim is not None : P.set_clim(clim)
   #cb=ax.figure.colorbar(P,extend='both')
   ax.set_title(variable+':'+myfile+'AVG-')
   ax.set_ylabel('Depth [m]')
   ax.set_xlabel(xlab)
   #ax.set_position(pos)
   #plt.tight_layout()

   # Print in different y-lims 
   suff=os.path.basename(myfile)
   if sectionid : suff=suff+"_"+sectionid
   figure.canvas.print_figure("sec_AVG_%s_full_%s.png"%(variable,suff),dpi=dpi)
   if 'Fram' in sectionid or 'Svin' in sectionid:
      ax.set_ylim(-600,0)
      figure.canvas.print_figure("sec_AVG_%s_600m_%s.png"%(variable,suff),dpi=dpi)
   else :
      ax.set_ylim(-3000,0)
      figure.canvas.print_figure("sec_AVG_%s_3000m_%s.png"%(variable,suff),dpi=dpi)
      #ax.set_ylim(-600,0)
      #figure.canvas.print_figure("sec_AVG_%s_600m_%s.png"%(variable,suff),dpi=dpi)

   # Close input file
   #i_abfile.close()
   #
   ax.clear()
   cb.remove()
def main(lon1,lat1,lon2,lat2,variable,files,filetype="archive",clim=None,sectionid="",
      ijspace=False,xaxis="distance",section_map=False,dpi=180) :

   logger.info("Filetype is %s"% filetype)
   gfile = abfile.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")
   qlon=gfile.read_field("qlon")
   qlat=gfile.read_field("qlat")

   # Set up section info
   if ijspace :
      sec = gridxsec.SectionIJSpace([lon1,lon2],[lat1,lat2],plon,plat)
   else  :
      sec = gridxsec.Section([lon1,lon2],[lat1,lat2],plon,plat)
   I,J=sec.grid_indexes
   dist=sec.distance
   slon=sec.longitude
   slat=sec.latitude

   # In testing
   #J,I,slon,slat,case,dist=sec.find_intersection(qlon,qlat)
   #print I,J
   #raise NameError,"test"



   logger.info("Min max I-index (starts from 0):%d %d"%(I.min(),I.max()))
   logger.info("Min max J-index (starts from 0):%d %d"%(J.min(),J.max()))


   if section_map :
      ll_lon=slon.min()-10.
      ur_lon=slon.max()+10.
      ll_lat=numpy.maximum(-90.,slat.min()-10.)
      ur_lat=numpy.minimum(90. ,slat.max()+10.)
      m = Basemap(projection='mill', llcrnrlon=ll_lon, llcrnrlat=ll_lat, urcrnrlon=ur_lon, urcrnrlat=ur_lat, resolution='l')
      (x,y) = m(slon,slat)
      figure = matplotlib.pyplot.figure()
      ax=figure.add_subplot(111)
      m.drawcoastlines()
      #m.fillcontinents(color='coral',lake_color='aqua')
      m.drawparallels(numpy.arange(-90.,120.,30.),labels=[1,0,0,0]) # draw parallels
      m.drawmeridians(numpy.arange(0.,420.,60.),labels=[0,0,0,1]) # draw meridians
      m.drawmapboundary() # draw a line around the map region
      m.plot(x,y,"r",lw=3)
      m.etopo()
      #m.scatter(x,y,s=20,c=dist)
      pos = ax.get_position()
      #print pos
      asp=pos.height/pos.width
      #print asp
      w=figure.get_figwidth()
      #print w
      h=asp*w
      figure.set_figheight(h)
      if sectionid :
         figure.canvas.print_figure("map_%s.png"%sectionid,dpi=dpi)
      else :
         figure.canvas.print_figure("map.png",dpi=dpi)

   # Get layer thickness variable used in hycom
   dpname = modeltools.hycom.layer_thickness_variable[filetype]
   logger.info("Filetype %s: layer thickness variable is %s"%(filetype,dpname))


   if xaxis == "distance" :
      x=dist/1000.
      xlab="Distance along section[km]"
   elif xaxis == "i" :
      x=I
      xlab="i-index"
   elif xaxis == "j" :
      x=J
      xlab="j-index"
   elif xaxis == "lon" :
      x=slon
      xlab="longitude"
   elif xaxis == "lat" :
      x=slat
      xlab="latitude"
   else :
      logger.warning("xaxis must be i,j,lo,lat or distance")
      x=dist/1000.
      xlab="Distance along section[km]"

   # Loop over archive files
   figure = matplotlib.pyplot.figure()
   ax=figure.add_subplot(111)
   pos = ax.get_position()
   for fcnt,myfile0 in enumerate(files) :

      # Remove [ab] ending if present
      m=re.match("(.*)\.[ab]",myfile0)
      if m :
         myfile=m.group(1)
      else :
         myfile=myfile0

      # Add more filetypes if needed. By def we assume archive
      if filetype == "archive" :
         i_abfile = abfile.ABFileArchv(myfile,"r")
      elif filetype == "restart" :
         i_abfile = abfile.ABFileRestart(myfile,"r",idm=gfile.idm,jdm=gfile.jdm)
      else :
         raise NotImplementedError,"Filetype %s not implemented"%filetype

      # kdm assumed to be max level in ab file
      kdm=max(i_abfile.fieldlevels)

      # Set up interface and daat arrays
      intfsec=numpy.zeros((kdm+1,I.size))
      datasec=numpy.zeros((kdm+1,I.size))

      # Loop over layers in file. 
      logger.info("File %s"%(myfile))
      for k in range(kdm) :
         logger.debug("File %s, layer %03d/%03d"%(myfile,k,kdm))

         # Get 2D fields
         dp2d=i_abfile.read_field(dpname,k+1)
         data2d=i_abfile.read_field(variable,k+1)
         dp2d=numpy.ma.filled(dp2d,0.)/modeltools.hycom.onem
         data2d=numpy.ma.filled(data2d,1e30)

         # Place data into section arrays
         intfsec[k+1,:] = intfsec[k,:] + dp2d[J,I]
         if k==0 : datasec[k,:] = data2d[J,I]
         datasec[k+1,:] = data2d[J,I]

      i_maxd=numpy.argmax(numpy.abs(intfsec[kdm,:]))
      #print i_maxd
      
      # Set up section plot
      #datasec = numpy.ma.masked_where(datasec==1e30,datasec)
      datasec = numpy.ma.masked_where(datasec>0.5*1e30,datasec)
      #print datasec.min(),datasec.max()
      #figure = matplotlib.pyplot.figure()
      #ax=figure.add_subplot(111)
      #P=ax.pcolormesh(dist/1000.,-intfsec,datasec)
      P=ax.pcolormesh(x,-intfsec,datasec,cmap="jet")
      if clim is not None : P.set_clim(clim)

      # Plot layer interfaces
      for k in range(1,kdm+1) :
         if k%10 == 0 : 
            PL=ax.plot(x,-intfsec[k,:],"--",color="k",lw=.5)
         elif k%5 == 0 : 
            PL=ax.plot(x,-intfsec[k,:],"--",color="k",lw=.5)
         else :
            PL=ax.plot(x,-intfsec[k,:],"--",color=".5",lw=.5)

         textx = x[i_maxd]
         texty = -0.5*(intfsec[k-1,i_maxd] + intfsec[k,i_maxd])
         ax.text(textx,texty,str(k),verticalalignment="center",horizontalalignment="center",fontsize=6)
      cb=ax.figure.colorbar(P)
      ax.set_title(myfile)
      ax.set_ylabel(variable)
      ax.set_xlabel(xlab)
      #ax.set_position(pos)
      #matplotlib.pyplot.tight_layout()

      # Print in different y-lims 
      suff=os.path.basename(myfile)
      if sectionid : suff=suff+"_"+sectionid
      figure.canvas.print_figure("sec_%s_full_%s.png"%(variable,suff),dpi=dpi)
      ax.set_ylim(-1000,0)
      figure.canvas.print_figure("sec_%s_1000m_%s.png"%(variable,suff),dpi=dpi)
      ax.set_ylim(-300,0)
      figure.canvas.print_figure("sec_%s_300m_%s.png"%(variable,suff),dpi=dpi)

      # Close input file
      i_abfile.close()

      #
      ax.clear()
      cb.remove()
def main(lon1,
         lat1,
         lon2,
         lat2,
         variable,
         files,
         filetype="archive",
         clim=None,
         sectionid="",
         ijspace=False,
         xaxis="distance",
         section_map=False,
         dpi=180):

    logger.info("Filetype is %s" % filetype)
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")

    # Set up section info
    if ijspace:
        sec = gridxsec.SectionIJSpace([lon1, lon2], [lat1, lat2], plon, plat)
    else:
        sec = gridxsec.Section([lon1, lon2], [lat1, lat2], plon, plat)
    I, J = sec.grid_indexes
    dist = sec.distance
    slon = sec.longitude
    slat = sec.latitude
    print(slon.shape)
    print(slat.shape)

    logger.info("Min max I-index (starts from 0):%d %d" % (I.min(), I.max()))
    logger.info("Min max J-index (starts from 0):%d %d" % (J.min(), J.max()))

    if section_map:
        ll_lon = slon.min() - 10.
        ur_lon = slon.max() + 10.
        ll_lat = np.maximum(-90., slat.min() - 10.)
        ur_lat = np.minimum(90., slat.max() + 10.)

        proj = ccrs.Stereographic(central_latitude=90.0,
                                  central_longitude=-40.0)
        pxy = proj.transform_points(ccrs.PlateCarree(), plon, plat)
        px = pxy[:, :, 0]
        py = pxy[:, :, 1]
        x, y = np.meshgrid(np.arange(slon.shape[0]), np.arange(slat.shape[0]))

        figure = plt.figure(figsize=(10, 8))
        ax = figure.add_subplot(111)

        ax = plt.axes(projection=ccrs.PlateCarree())
        ax.set_extent([-179, 179, 53, 85], ccrs.PlateCarree())
        ax.add_feature(cfeature.GSHHSFeature('auto', edgecolor='grey'))
        ax.add_feature(cfeature.GSHHSFeature('auto', facecolor='grey'))
        ax.gridlines()
        ax.plot(slon, slat, "r-", lw=1)

        pos = ax.get_position()
        asp = pos.height / pos.width
        w = figure.get_figwidth()
        h = asp * w
        figure.set_figheight(h)
        if sectionid:
            figure.canvas.print_figure("map_%s.png" % sectionid, dpi=dpi)
        else:
            figure.canvas.print_figure("map.png", dpi=dpi)

    # Get layer thickness variable used in hycom
    dpname = modeltools.hycom.layer_thickness_variable[filetype]
    logger.info("Filetype %s: layer thickness variable is %s" %
                (filetype, dpname))

    if xaxis == "distance":
        x = dist / 1000.
        xlab = "Distance along section[km]"
    elif xaxis == "i":
        x = I
        xlab = "i-index"
    elif xaxis == "j":
        x = J
        xlab = "j-index"
    elif xaxis == "lon":
        x = slon
        xlab = "longitude"
    elif xaxis == "lat":
        x = slat
        xlab = "latitude"
    else:
        logger.warning("xaxis must be i,j,lo,lat or distance")
        x = dist / 1000.
        xlab = "Distance along section[km]"

    # Loop over archive files
    figure = plt.figure()
    ax = figure.add_subplot(111)
    pos = ax.get_position()
    for fcnt, myfile0 in enumerate(files):

        # Remove [ab] ending if present
        m = re.match("(.*)\.[ab]", myfile0)
        if m:
            myfile = m.group(1)
        else:
            myfile = myfile0

        # Add more filetypes if needed. By def we assume archive
        if filetype == "archive":
            i_abfile = abf.ABFileArchv(myfile, "r")
        elif filetype == "restart":
            i_abfile = abf.ABFileRestart(myfile,
                                         "r",
                                         idm=gfile.idm,
                                         jdm=gfile.jdm)
        else:
            raise NotImplementedError("Filetype %s not implemented" % filetype)

        # kdm assumed to be max level in ab file
        kdm = max(i_abfile.fieldlevels)

        # Set up interface and daat arrays
        intfsec = np.zeros((kdm + 1, I.size))
        datasec = np.zeros((kdm + 1, I.size))

        # Loop over layers in file.
        logger.info("File %s" % (myfile))
        for k in range(kdm):
            logger.debug("File %s, layer %03d/%03d" % (myfile, k, kdm))

            # Get 2D fields
            dp2d = i_abfile.read_field(dpname, k + 1)
            data2d = i_abfile.read_field(variable, k + 1)
            dp2d = np.ma.filled(dp2d, 0.) / modeltools.hycom.onem
            data2d = np.ma.filled(data2d, 1e30)

            # Place data into section arrays
            intfsec[k + 1, :] = intfsec[k, :] + dp2d[J, I]
            if k == 0: datasec[k, :] = data2d[J, I]
            datasec[k + 1, :] = data2d[J, I]

        i_maxd = np.argmax(np.abs(intfsec[kdm, :]))

        # Set up section plot
        datasec = np.ma.masked_where(datasec > 0.5 * 1e30, datasec)
        P = plt.pcolormesh(x, -intfsec, datasec, cmap="jet", shading='auto')
        if clim is not None: P.set_clim(clim)

        # Plot layer interfaces
        for k in range(1, kdm + 1):
            if k % 10 == 0:
                PL = ax.plot(x, -intfsec[k, :], "--", color="k", lw=.5)
            elif k % 5 == 0:
                PL = ax.plot(x, -intfsec[k, :], "--", color="k", lw=.5)
            else:
                PL = ax.plot(x, -intfsec[k, :], "--", color=".5", lw=.5)

            textx = x[i_maxd]
            texty = -0.5 * (intfsec[k - 1, i_maxd] + intfsec[k, i_maxd])
            ax.text(textx,
                    texty,
                    str(k),
                    verticalalignment="center",
                    horizontalalignment="center",
                    fontsize=6)
        cb = ax.figure.colorbar(P)
        ax.set_title(myfile)
        ax.set_ylabel(variable)
        ax.set_xlabel(xlab)

        # Print in different y-lims
        suff = os.path.basename(myfile)
        if sectionid: suff = suff + "_" + sectionid
        figure.canvas.print_figure("sec_%s_full_%s.png" % (variable, suff),
                                   dpi=dpi)
        ax.set_ylim(-1000, 0)
        figure.canvas.print_figure("sec_%s_1000m_%s.png" % (variable, suff),
                                   dpi=dpi)
        ax.set_ylim(-300, 0)
        figure.canvas.print_figure("sec_%s_300m_%s.png" % (variable, suff),
                                   dpi=dpi)

        # Close input file
        i_abfile.close()

        #
        ax.clear()
        cb.remove()