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()
Ejemplo n.º 3
0
def main(myfiles,
         fieldname,
         fieldlevel,
         idm=None,
         jdm=None,
         clim=None,
         filetype="archive",
         window=None,
         cmap="jet",
         datetime1=None,
         datetime2=None,
         vector="",
         tokml=False,
         exptid="",
         masklim=None,
         dpi=180):

    cmap = plt.get_cmap("jet")
    LinDic = mod_hyc2plot.cmap_dict('sawtooth_fc100.txt')
    my_cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', LinDic)
    cmap = my_cmap

    if vector:
        cmap = cmocean.cm.speed

    if tokml:
        ab = abf.ABFileGrid("regional.grid", "r")
        plon = ab.read_field("plon")
        plat = ab.read_field("plat")
        ab.close()

    ab = abf.ABFileGrid("regional.grid", "r")
    plon = ab.read_field("plon")
    plat = ab.read_field("plat")
    ab.close()

    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(plon.shape[1]), np.arange(plon.shape[0]))

    if vector:
        logger.info("Vector component 1:%s" % fieldname)
        logger.info("Vector component 2:%s" % vector)

    figure = plt.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    ax.set_facecolor('xkcd:gray')
    onemm = 9.806
    counter = 0
    sum_fld = np.zeros(plon.shape)
    sumf1 = np.zeros(plon.shape)
    sumf2 = np.zeros(plon.shape)
    file_count = 0
    for myfile0 in myfiles:

        # Open files, and return some useful stuff.
        # ab2 i used in case of vector
        # rdtimes is used for plotting forcing fields
        n_intloop, ab, ab2, rdtimes = open_file(myfile0,
                                                filetype,
                                                fieldname,
                                                fieldlevel,
                                                datetime1=datetime1,
                                                datetime2=datetime2,
                                                vector=vector,
                                                idm=idm,
                                                jdm=jdm)
        # Intloop used to read more fields in one file. Only for forcing for now
        for i_intloop in range(n_intloop):

            # Read ab file of different types
            if filetype == "archive":
                fld1 = ab.read_field(fieldname, fieldlevel)
                if vector: fld2 = ab.read_field(vector, fieldlevel)
            elif filetype == "regional.depth":
                fld1 = ab.read_field(fieldname)
            elif filetype == "forcing":
                fld1 = ab.read_field(fieldname, rdtimes[i_intloop])
                if vector: fld2 = ab2.read_field(vector, rdtimes[i_intloop])
                logger.info("Processing time %.2f" % rdtimes[i_intloop])
            else:
                raise NotImplementedError("Filetype %s not implemented" %
                                          filetype)

            if not window:
                J, I = np.meshgrid(np.arange(fld1.shape[0]),
                                   np.arange(fld1.shape[1]))
            else:
                J, I = np.meshgrid(np.arange(window[1], window[3]),
                                   np.arange(window[0], window[2]))

            print('mnfld1,mxfld1=', fld1.min(), fld1.max())
            # Create scalar field for vectors
            if vector:
                fld = np.sqrt(fld1**2 + fld2**2)
                print('mnfld2,mxfld2=', fld2.min(), fld2.max())
            else:
                fld = fld1

            # Apply mask if requested
            if masklim:
                fld = np.ma.masked_where(fld <= masklim[0], fld)
                fld = np.ma.masked_where(fld >= masklim[1], fld)

            sum_fld = sum_fld + fld
            counter = counter + 1
            if vector:
                sumf1 = sumf1 + fld1
                sumf2 = sumf2 + fld2
        file_count = file_count + 1
        # End i_intloop
    print('Computing the avearge of file_counter= ', file_count, 'counter=',
          counter)
    if file_count > 0:
        fld_Avg = sum_fld / file_count
        print('mn_Avg_fld,mx_Avg_fld=', fld_Avg.min(), fld_Avg.max())
    if vector:
        f1_avg = sumf1 / file_count
        f2_avg = sumf2 / file_count
        print('mn_avg_fld1,mx_avg_fld1=', f1_avg.min(), f1_avg.max())
        print('mn_avg_fld2,mx_avg_fld2=', f2_avg.min(), f2_avg.max())

    if fieldname == 'k.e.':
        P = plt.pcolormesh(x[J, I],
                           y[J, I],
                           np.log10(fld_Avg[J, I]),
                           cmap=cmap,
                           shading='auto')
    elif fieldname == 'srfhgt':
        P = plt.pcolormesh(x[J, I],
                           y[J, I], (fld_Avg[J, I] / onemm),
                           cmap=cmap,
                           shading='auto')
    else:
        P = plt.pcolormesh(x[J, I],
                           y[J, I],
                           fld_Avg[J, I],
                           cmap=cmap,
                           shading='auto')

    if 'temp' in fieldname:
        P1=plt.contour(x[J,I],y[J,I],fld_Avg[J,I],levels=[-1.,1,4.0,8], \
                   colors=('w',),linestyles=('-',),linewidths=(1.5,))
        plt.clabel(P1, fmt='%2.1d', colors='w',
                   fontsize=10)  #contour line labels

    if vector:
        skip = 10
        logger.info("ploting quiver .......>>> %s" % vector)
        I2 = I[::skip, ::skip]
        J2 = J[::skip, ::skip]
        plt.quiver(x[J2, I2], y[J2, I2], f1_avg[J2, I2], f2_avg[J2, I2])

    ##
    # Print figure and remove wite space.
    ax.set_facecolor('xkcd:gray')
    aspect = 40
    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)
    if vector:
        cb = ax.figure.colorbar(P, cax=cax, extend='max')
    else:
        cb = ax.figure.colorbar(P, cax=cax, extend='both')
    if clim is not None: P.set_clim(clim)
    ax.set_title('Avg' + "%s:%s(%d)" % (myfile0, fieldname, fieldlevel))
    # Print figure.
    fnamepng_template = exptid + "Avg_%s_%d_%03d_Avg.png"
    fnamepng = fnamepng_template % (fieldname, fieldlevel, counter)
    logger.info("output in  %s" % fnamepng)
    figure.canvas.print_figure(fnamepng, bbox_inches='tight', dpi=dpi)
    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(myfiles,
         fieldname,
         fieldlevel,
         idm=None,
         jdm=None,
         clim=None,
         filetype="archive",
         window=None,
         cmap="jet",
         datetime1=None,
         datetime2=None,
         vector="",
         tokml=False,
         masklim=None,
         filename2='',
         filename5='',
         dpi=180):

    LinDic = mod_hyc2plot.cmap_dict('sawtooth_fc100.txt')
    if 'temp' or 'sal' in fieldname:
        cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', LinDic)
    else:
        cmap = matplotlib.colors.LinearSegmentedColormap('my_colormap', LinDic)
    if tokml:
        ab = abf.ABFileGrid("regional.grid", "r")
        plon = ab.read_field("plon")
        plat = ab.read_field("plat")
        ab.close()

    ab = abf.ABFileGrid("regional.grid", "r")
    plon = ab.read_field("plon")
    plat = ab.read_field("plat")
    scpx = ab.read_field("scpx")
    scpy = ab.read_field("scpy")
    target_lonlats = [plon, plat]
    abdpth = abf.ABFileBathy('regional.depth', "r", idm=ab.idm, jdm=ab.jdm)
    mdpth = abdpth.read_field('depth')
    maskd = mdpth.data
    maskd[maskd > 1e29] = np.nan
    Region_mask = True
    Region_mask = False
    if Region_mask:
        maskd[plat > 70] = np.nan
        #maskd[plat<50]=np.nan
        maskd[plon > 20] = np.nan
        maskd[plon < -30] = np.nan

    Nordic_mask = maskd

    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(plon.shape[1]), np.arange(plon.shape[0]))

    if vector:
        logger.info("Vector component 1:%s" % fieldname)
        logger.info("Vector component 2:%s" % vector)

    #---------------first read and compute clim
    Err_map = 1
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12))
    if 'tem' or 'sal' in fieldname:
        counter = 0
        if 'tem' in fieldname:
            rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_tem.a"
        if 'sal' in fieldname:
            rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_sal.a"
        rlx_afile = abf.AFile(ab.idm, ab.jdm, rlxfile0, "r")
        lyr = fieldlevel
        record_num = 1
        record_var = record_num - 1
        fld = rlx_afile.read_record(record_var)
        print('mn,mx  data='), fld.min(), fld.max()
        kdm = 50
        dt_clim = np.zeros(12)
        for mnth in range(12):
            fld1 = rlx_afile.read_record(mnth * kdm + lyr - 1)
            print('record, mn,mx  data='), kdm * mnth, fld1.min(), fld1.max()
            # Intloop used to read more fields in one file. Only for forcing for now
            dt_clim[mnth] = mod_hyc2plot.spatiomean(fld1, maskd)
            sum_fld1 = sum_fld1 + fld1
            Clim_arr[:, :, mnth] = fld1[:, :]
            counter = counter + 1
            print('counter='), counter
            del fld1
        Clim_Avg = sum_fld1 / counter
        del sum_fld1

    #---------------filename
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    onemm = 9.806
    counter = 0
    file_count = 0
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    dt_cnl = np.zeros(len(myfiles))
    diff_dt_cnl = np.zeros(len(myfiles))
    rmse_dt_cnl = np.zeros(len(myfiles))
    Labl1 = "Model: " + fieldname
    if "SPRBAS_0" in myfiles[0]:
        Labl1 = "CNTL: prsbas=0 "
    if filename2:
        dt_2 = np.zeros(len(filename2))
        diff_dt_2 = np.zeros(len(filename2))
        rmse_dt_2 = np.zeros(len(filename2))
        yyyy1 = filename2[0][-9:-5]
        print("filename2[0]="), filename2[0][-9:-5]
        print("filename2[0]="), filename2[0]
        print("yyy1="), yyyy1
        tid_2=np.array([datetime.datetime(int(yyyy1), 1, 15) \
           + relativedelta(months=i) for i in range(len(filename2))])
        Labl2 = "filename2"
        Labl2 = "Corrected"
        if "erai" in filename2[0]:
            Labl2 = "CNTL: prsbas=1e5 "

    yyyy1cnt = myfiles[0][-8:-5]
    print("myfiles[0]="), myfiles[0][-9:-5]
    print("myfiles[0]="), myfiles[0]
    print("yyy1cnt="), print(yyyy1cnt)
    base = datetime.datetime(int(yyyy1cnt), 1, 15)
    tid = np.array(
        [base + relativedelta(months=i) for i in range(len(myfiles))])
    if len(myfiles) == 36:
        base = datetime.datetime(int(yyyy1cnt), 1, 15)
        tid = np.array(
            [base + relativedelta(months=i) for i in range(len(myfiles))])

    nmexp = 1
    if filename2:
        nmexp = nmexp + 1
    print(
        'processing data from No runs ==##############>>>>>>>>>>>>>>>>>>>>>>>'
    ), nmexp
    for iii in range(nmexp):
        counter = 0
        file_count = 0
        sum_fld1 = maskd
        sum_fld1[~np.isnan(sum_fld1)] = 0.0
        if iii == 1 and filename2:
            myfiles = filename2
        else:
            logger.info(
                ">>>>>--------------------------Processing the first files=  %d<<<<"
                % iii)

        logger.info(
            ">>>>>--------------------------Processing the first files=  %d<<<<"
            % iii)
        for myfile0 in myfiles:
            # Open files, and return some useful stuff.
            # ab2 i used in case of vector
            # rdtimes is used for plotting forcing fields
            n_intloop,ab,ab2,rdtimes = open_file(myfile0,filetype,fieldname,fieldlevel,\
                  datetime1=datetime1,datetime2=datetime2,vector=vector,idm=idm,jdm=jdm)
            # Intloop used to read more fields in one file. Only for forcing for now
            for i_intloop in range(n_intloop):
                # Read ab file of different types
                if filetype == "archive":
                    fld1 = ab.read_field(fieldname, fieldlevel)
                elif filetype == "forcing":
                    fld1 = ab.read_field(fieldname, rdtimes[i_intloop])
                    if vector:
                        fld2 = ab2.read_field(vector, rdtimes[i_intloop])
                    logger.info("Processing time %.2f" % rdtimes[i_intloop])
                else:
                    raise NotImplementedError("Filetype %s not implemented" %
                                              filetype)
                if not window:
                    J, I = np.meshgrid(np.arange(fld1.shape[0]),
                                       np.arange(fld1.shape[1]))
                else:
                    J, I = np.meshgrid(np.arange(window[1], window[3]),
                                       np.arange(window[0], window[2]))
                # Create scalar field for vectors
                if vector:
                    fld = np.sqrt(fld1**2 + fld2**2)
                else:
                    fld = fld1
                print('---------mn,mx  data='), fld.min(), fld.max()
                sum_fld1 = sum_fld1 + fld
                cindx = np.remainder(counter, 12)
                print("counter"), counter, print("cindx="), cindx
                if iii == 0:
                    dt_cnl[counter] = mod_hyc2plot.spatiomean(fld, Nordic_mask)
                    diff_dt_cnl[counter] = mod_hyc2plot.spatiomean(
                        fld[:, :] - Clim_arr[:, :, cindx], Nordic_mask)
                    rmse_dt_cnl[counter] = np.sqrt(
                        mod_hyc2plot.spatiomean(
                            (fld[:, :] - Clim_arr[:, :, cindx])**2,
                            Nordic_mask))
                    Labl = Labl1
                if iii == 1 and filename2:
                    dt_2[counter] = mod_hyc2plot.spatiomean(fld, Nordic_mask)
                    diff_dt_2[counter] = mod_hyc2plot.spatiomean(
                        fld[:, :] - Clim_arr[:, :, cindx], Nordic_mask)
                    rmse_dt_2[counter] = np.sqrt(
                        mod_hyc2plot.spatiomean(
                            (fld[:, :] - Clim_arr[:, :, cindx])**2,
                            Nordic_mask))
                    Labl = Labl2
                # Apply mask if requested
                counter = counter + 1
                file_count = file_count + 1
                del fld
            # End i_intloop
        print('Computing the avearge of file_counter= '), print(
            file_count), print('counter='), print(counter), print(
                'cindx='), print(cindx)
        if file_count > 0:
            fld_Avg = sum_fld1 / file_count
        if Err_map:
            cmap = cmocean.cm.balance
            fld_diff = fld_Avg - Clim_Avg

        if fieldname == 'k.e.':
            P = ax.pcolormesh(x[J, I],
                              y[J, I],
                              np.log10(fld_Avg[J, I]),
                              cmap=cmap)
        elif fieldname == 'srfhgt':
            P = ax.pcolormesh(x[J, I],
                              y[J, I], (fld_Avg[J, I] / onemm),
                              cmap=cmap)
        else:
            P = ax.pcolormesh(x[J, I], y[J, I], fld_diff[J, I], cmap=cmap)
        if 'temp' in fieldname:
            P1 = ax.contour(x[J, I],
                            y[J, I],
                            fld_diff[J, I],
                            levels=[-1., 1, 4.0, 8],
                            colors=('w', ),
                            linestyles=('-', ),
                            linewidths=(1.5, ))
            matplotlib.pyplot.clabel(P1, fmt='%2.1d', colors='w',
                                     fontsize=10)  #contour line labels

        # Print figure.
        aspect = 40
        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')
        if clim is not None: P.set_clim(clim)
        ax.set_title("Diff:%s(%d)" % (fieldname, fieldlevel) +
                     ' :( Model - Clim )')
        # Print figure.
        fnamepng_template = myfiles[0][-20:-5].replace(
            "/", '') + "_Avg_TP6_%s_%d_%03d_iii%03d_Avg.png"
        if Region_mask:
            fnamepng_template='Region_'+yyyy1cnt+myfiles[0][1:11].replace("/",'') \
                  +"Avg_TP5_%s_%d_%03d_iii%03d_Avg.png"
        fnamepng = fnamepng_template % (fieldname, fieldlevel, counter, iii)
        logger.info("output in  %s" % fnamepng)
        figure.canvas.print_figure(fnamepng, bbox_inches='tight', dpi=dpi)
        ax.clear()
        cb.remove()
        datmen = np.nanmean(fld_diff)
        spatiodatmen = mod_hyc2plot.spatiomean(fld_diff, Nordic_mask)
        print('-----------mean diff data, spatio='), datmen, spatiodatmen
        del sum_fld1
        #---------------------------------------

    print('tid len='), print(tid.shape)
    if filename2:
        print('dt_2='), print(dt_2.shape)
    tid_clim = np.array([base + relativedelta(months=i) for i in range(12)])
    figure, ax = plt.subplots()
    rpt = len(dt_cnl) / 12
    dt_clim_cat = dt_clim
    for ii in range(int(rpt - 1)):
        print("concatenate ")
        dt_clim_cat = np.concatenate([dt_clim_cat, dt_clim])

    years = YearLocator()  # every year
    months = MonthLocator()  # every month
    yearsFmt = DateFormatter('%Y')
    nplts = 1
    ax.plot_date(tid, dt_cnl, '-o', color='g', ms=3, label=Labl1)
    if filename2:
        ax.plot_date(tid_2, dt_2, '-v', color='orange', ms=3, label=Labl2)
        nplts = nplts + 1
    if filename5:
        ax.plot_date(tid_5, dt_5, '--', color='m', label=Labl5)
        nplts = nplts + 1
    if 'tem' or 'sal' in fieldname:
        ax.plot_date(tid[0:len(dt_cnl)],
                     dt_clim_cat[:],
                     ':',
                     color='black',
                     label='Clim:' + fieldname)
        nplts = nplts + 1
    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.xaxis.set_minor_locator(months)
    ax.autoscale_view()

    # format the coords message box
    def price(x):
        return '$%1.2f' % x

    ax.fmt_xdata = DateFormatter('%Y-%m-%d')
    ax.fmt_ydata = price
    ax.grid(True)
    figure.autofmt_xdate()
    legend = plt.legend(loc='upper left', fontsize=8)
    plt.title("Area-averaged: %s(%d)" % (fieldname, fieldlevel))
    plt.ylabel("%s(%d)" % (fieldname, fieldlevel))
    ts_fil = "time_series_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel,
                                                    nplts)
    if Region_mask:
        ts_fil = 'Region_' + ts_fil
    figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi)
    logger.info("Successfull printing:  %s" % ts_fil)

    #-----------------
    # plot short  mean error
    figure, ax = plt.subplots()
    print("diff_dt_cnl[:]="), diff_dt_cnl[:]
    nplts = 1
    ll = -1 * len(tid)
    if filename2:
        ll = -1 * len(tid)
    ax.plot_date(tid[ll:], diff_dt_cnl[ll:], '-o', color='g', ms=3)
    if filename2:
        ax.plot_date(tid_2, diff_dt_2, '-v', color='orange', ms=3, label=Labl2)
        nplts = nplts + 1

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.xaxis.set_minor_locator(months)
    ax.autoscale_view()

    # format the coords message box
    def price(x):
        return '$%1.2f' % x

    ax.fmt_xdata = DateFormatter('%Y-%m-%d')
    ax.fmt_ydata = price
    ax.grid(True)
    figure.autofmt_xdate()
    legend = plt.legend(loc='upper left', fontsize=8)
    plt.title("Mean diff:Model-Clim: %s(%d)" % (fieldname, fieldlevel))
    plt.ylabel("diff:%s(%d)" % (fieldname, fieldlevel))
    ts_fil = 'Mdiff' + "ST_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel,
                                                     nplts)
    if Region_mask:
        ts_fil = 'Region_Mdiff' + "ST_cntl_flx_%s_%02d_%02d" % (
            fieldname, fieldlevel, nplts)
    figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi)
    logger.info("Successfull printing:  %s" % ts_fil)

    # plot rooot mean square RMSE  error
    figure, ax = plt.subplots()
    nplts = 1
    ll = -1 * len(tid)
    if filename2:
        ll = -1 * len(tid_2)
    ax.plot_date(tid[ll:], rmse_dt_cnl[ll:], '-o', color='g', ms=3)
    if filename2:
        ax.plot_date(tid_2, rmse_dt_2, '-v', color='orange', ms=3, label=Labl2)
        nplts = nplts + 1

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.xaxis.set_minor_locator(months)
    ax.autoscale_view()

    # format the coords message box
    def price(x):
        return '$%1.2f' % x

    ax.fmt_xdata = DateFormatter('%Y-%m-%d')
    ax.fmt_ydata = price
    ax.grid(True)
    figure.autofmt_xdate()
    legend = plt.legend(loc='upper left', fontsize=8)
    plt.title("RMSE: (Model-Clim) %s(%d)" % (fieldname, fieldlevel))
    plt.ylabel("RMSE:%s(%d)" % (fieldname, fieldlevel))
    ts_fil = 'RMSE' + "ST_cntl_flx_%s_%02d_%02d" % (fieldname, fieldlevel,
                                                    nplts)
    if Region_mask:
        ts_fil = 'Region_RMSE' + "ST2007_cntl_flx_%s_%02d_%02d" % (
            fieldname, fieldlevel, nplts)
    figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi)
    logger.info("Successfull printing:  %s" % ts_fil)
    logger.info(
        "End --------------------------------------------------------------- printing:  %s"
        % ts_fil)