Beispiel #1
0
def open_file(myfile0,
              filetype,
              fieldname,
              fieldlevel,
              datetime1=None,
              datetime2=None,
              vector="",
              idm=None,
              jdm=None):

    logger.info("Now processing  %s" % myfile0)
    m = re.match("(.*)\.[ab]", myfile0)
    if m:
        myfile = m.group(1)
    else:
        myfile = myfile0

    ab2 = None
    rdtimes = []
    if filetype == "archive":
        ab = abf.ABFileArchv(myfile, "r")
        n_intloop = 1

    elif filetype == "regional.depth":
        ab = abf.ABFileBathy(myfile, "r", idm=idm, jdm=jdm)
        n_intloop = 1
    elif filetype == "forcing":
        ab = abf.ABFileForcing(myfile, "r", idm=idm, jdm=jdm)
        if vector:
            file2 = myfile.replace(fieldname, vector)
            logger.info("Opening file %s for vector component nr 2" % file2)
            ab2 = abf.ABFileForcing(file2, "r", idm=idm, jdm=jdm)
        if datetime1 is None or datetime2 is None:
            raise NameError(
                "datetime1 and datetime2 must be specified when plotting forcing files"
            )
        else:
            iday1, ihour1, isec1 = modeltools.hycom.datetime_to_ordinal(
                datetime1, 3)
            rdtime1 = modeltools.hycom.dayfor(datetime1.year, iday1, ihour1, 3)
            #
            iday2, ihour2, isec2 = modeltools.hycom.datetime_to_ordinal(
                datetime2, 3)
            rdtime2 = modeltools.hycom.dayfor(datetime2.year, iday2, ihour2, 3)
            rdtimes = sorted([
                elem for elem in ab.field_times
                if elem > rdtime1 and elem < rdtime2
            ])
            n_intloop = len(rdtimes)
    else:
        raise NotImplementedError("Filetype %s not implemented" % filetype)
    # Check that fieldname is actually in file
    if fieldname not in ab.fieldnames:
        logger.error("Unknown field %s at level %d" % (fieldname, fieldlevel))
        logger.error("Available fields : %s" % (" ".join(ab.fieldnames)))
        raise ValueError("Unknown field %s at level %d" %
                         (fieldname, fieldlevel))

    return n_intloop, ab, ab2, rdtimes
def main(intopo):
    # Read plon,plat
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")
    gfile.close()

    # Read input bathymetry
    bfile = abf.ABFileBathy(intopo,
                            "r",
                            idm=gfile.idm,
                            jdm=gfile.jdm,
                            mask=True)
    in_depth_m = bfile.read_field("depth")
    bfile.close()

    # Print to CICE mask files
    kmt = numpy.where(~in_depth_m.mask, 1., 0.)
    modeltools.cice.io.write_netcdf_kmt(kmt, "cice_kmt.nc")
def spatiomean(fldin,regi_mask):
  #  fldin=[]
    ab = abf.ABFileGrid("regional.grid","r")
    pplon=ab.read_field("plon")
    pplat=ab.read_field("plat")
    scppx=ab.read_field("scpx")
    scppy=ab.read_field("scpy")
    abdpth = abf.ABFileBathy('regional.depth',"r",idm=ab.idm,jdm=ab.jdm)
    mdpth=abdpth.read_field('depth')
    maskdd=mdpth.data
    maskdd[maskdd>1e29]=np.nan
    #fldin[fldin>1e29]=np.nan
    #scppx[np.isnan(maskdd)]=np.nan
    #scppy[np.isnan(maskdd)]=np.nan
###     # mask for specific region
###     #Nordic=False
###     if Nordmask:
###        print 'Compute for Nordic------>>>>>>-'
###        maskdd[pplat>80]=np.nan
###        maskdd[pplat<55]=np.nan
###        maskdd[pplon>60]=np.nan
###        maskdd[pplon<-60]=np.nan
###        #Norid
###        #fldin[np.isnan(maskdd)]=np.nan
###        #scppx[np.isnan(maskdd)]=np.nan
###        #scppy[np.isnan(maskdd)]=np.nan
###     #--
    numer=fldin*scppx*scppy
    denum=scppx*scppy
    numer[np.isnan(regi_mask)]=np.nan
    denum[np.isnan(regi_mask)]=np.nan
    fldin_avg=np.nansum(numer)/np.nansum(denum)
    print('np.nansum(numer)='), print(np.nansum(numer))
    print('np.nansum(denum)='), print(np.nansum(denum))
    print('fldin_avg='), print(fldin_avg)
    #direct mean
    return fldin_avg
Beispiel #4
0
def main(myfiles,
         fieldname,
         idm=None,
         jdm=None,
         clim=None,
         filetype="archive",
         window=None,
         cmap="jet",
         datetime1=None,
         datetime2=None,
         vector="",
         tokml=False,
         masklim=None,
         filename2='',
         dpi=180):

    cmap = matplotlib.pyplot.get_cmap("jet")
    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 > 80] = np.nan
        maskd[plat < 50] = np.nan
        maskd[plon > 60] = np.nan
        maskd[plon < -50] = 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)

    #---------------
    fieldlevel = 0
    Err_map = 1
    #freezp=-2.5
    freezp = -1.8
    Point_tid = True
    Point_tid = False
    if Point_tid:
        ix = 1394
        jy = 267
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12))
    #---------------
    # compute for TP6 files
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    counter = 0
    file_count = 0
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0

    #-----------------------------------------
    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 = myfiles[0][:28]
    #Labl1="CNTL: New prsbas=0"
    yyyy1 = myfiles[0][-14:-10]
    print("myfiles[0]=", myfiles[0])
    print("yyy1=", yyyy1)
    base = datetime.datetime(int(yyyy1), 1, 15)
    tid = np.array(
        [base + relativedelta(months=i) for i in range(len(myfiles))])
    counter = 0
    file_count = 0
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    logger.info(
        ">>>>>--------------------------Processing the first files=  myfiles")
    if "salin" in fieldname:
        fieldname = "salin01"
    for ncfile0 in myfiles:
        logger.info("Now processing  %s" % ncfile0)
        fh = Dataset(ncfile0, mode='r')
        fld_arr = fh.variables[fieldname][:]
        if "srfhgt" in fieldname:
            #convert to "m"
            fld_arr = fld_arr / 9.806
        print("fld_arr.shpe", fld_arr.shape)
        tot = fld_arr.shape[0]
        fh.close()
        for ii in range(tot):
            fld = fld_arr[ii, :, :]
            print('mn,mx=', fld.min(), fld.max(), 'count=', counter)
            dt_cnl[counter] = np.nanmean(fld)
            if Point_tid:
                dt_cnl[counter] = fld[jy, ix]
            print("fld.shape", fld.shape)
            print("Nordic_mask.shape", Nordic_mask.shape)
            counter = counter + 1
            sum_fld1 = sum_fld1 + fld
            del fld
        # End i_intloop
    print('Computing the avearge of file_counter= ', file_count, 'counter=',
          counter)
    #next experminet
    if filename2:
        dt_2 = np.zeros(len(filename2))
        diff_dt_2 = np.zeros(len(filename2))
        rmse_dt_2 = np.zeros(len(filename2))
        yyyy2 = filename2[0][-14:-10]
        print("filename2[0]=", filename2[0])
        print("yyy1=", yyyy2)
        tid_2 = np.array([
            datetime.datetime(int(yyyy2), 1, 15) + relativedelta(months=i)
            for i in range(len(filename2))
        ])
        Labl2 = filename2[0][:28]
        counter = 0
        file_count = 0
        sum_fld1 = maskd
        sum_fld1[~np.isnan(sum_fld1)] = 0.0
        logger.info(
            ">>>>>--------------------------Processing the first files=  myfiles"
        )
        for ncfil in filename2:
            logger.info("Now processing  %s" % ncfil)
            fh = Dataset(ncfil, mode='r')
            fld_arr = fh.variables[fieldname][:]
            if "srfhgt" in fieldname:
                fld_arr = fld_arr / 9.806
            print("fld_arr.shpe", fld_arr.shape)
            tot = fld_arr.shape[0]
            fh.close()
            for ii in range(tot):
                fld = fld_arr[ii, :, :]
                #fld=np.ma.masked_where(fld<freezp,fld)
                print('mn,mx=', fld.min(), fld.max(), 'count=', counter)
                dt_2[counter] = np.nanmean(fld)
                if Point_tid:
                    dt_2[counter] = fld[jy, ix]
                counter = counter + 1
                sum_fld1 = sum_fld1 + fld
                del fld

    #---------------------------------------
    figure, ax = plt.subplots()
    years = YearLocator()  # every year
    months = MonthLocator()  # every month
    yearsFmt = DateFormatter('%Y')
    #ax=figure.add_subplot(111)
    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='blue', ms=3, label=Labl2)
    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 right', fontsize=8)
    if Point_tid:
        plt.title("Point:(lon,lat)=(" + str(plon[jy, ix]) + ',' +
                  str(plat[jy, ix]) + "): %s(%d)" % (fieldname, fieldlevel))
    else:
        plt.title("Area-averaged: %s(%d)" % (fieldname, fieldlevel))
    #plt.xlabel('dayes')
    if "srfhgt" in fieldname:
        plt.ylabel("%s[m]" % (fieldname))
    else:
        plt.ylabel("%s(%d)" % (fieldname, fieldlevel))
    #plt.title('Pakistan India Population till 2007')
    ts_fil = "Time_series_cntl%s_%02d_%02d" % (fieldname, fieldlevel,
                                               len(myfiles))
    if Region_mask:
        ts_fil = 'Region_' + ts_fil
    if Point_tid:
        ts_fil = 'Point_ix' + str(ix) + 'jy' + str(jy) + ts_fil
    figure.canvas.print_figure(ts_fil, bbox_inches='tight', dpi=dpi)
    logger.info("Successfull printing:  %s" % ts_fil)
Beispiel #5
0
def main(infile_coarse,infile_fine,
      ncells_linear=20,
      ncells_exact=3,
      check_consistency=False,
      bathy_threshold=0.) :

   #bathy_threshold=0. # TODO
   logger.info("Bathy threshold is %12.4f"%bathy_threshold)

   # Read plon,plat
   gfile=abf.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")
   gfile.close()

   # Read input bathymetry - fine version
   m=re.match( "^(.*)(\.[ab])", infile_fine)
   if m : infile_fine=m.group(1)
   bfile=abf.ABFileBathy(infile_fine,"r",idm=gfile.idm,jdm=gfile.jdm)
   fine_depth_m=bfile.read_field("depth")
   fine_depth_m=numpy.ma.masked_where(fine_depth_m<=bathy_threshold,fine_depth_m)
   fine_depth=numpy.ma.filled(fine_depth_m,bathy_threshold)
   bfile.close()

   # Read input bathymetry - coarse version
   m=re.match( "^(.*)(\.[ab])", infile_coarse)
   if m : infile_coarse=m.group(1)
   bfile=abf.ABFileBathy(infile_coarse,"r",idm=gfile.idm,jdm=gfile.jdm)
   coarse_depth_m=bfile.read_field("depth")
   coarse_depth_m=numpy.ma.masked_where(coarse_depth_m<=bathy_threshold,coarse_depth_m)
   coarse_depth=numpy.ma.filled(coarse_depth_m,bathy_threshold)
   bfile.close()

   # create relaxation mask (rmu)
   tmp=numpy.linspace(0.,1.,ncells_linear)
   tmp=numpy.concatenate((numpy.zeros((ncells_exact,)),tmp)) # ie: hree first cells will match outer bathymetry
   ncells=ncells_linear+ncells_exact
   rmu=numpy.ones(coarse_depth.shape)
   rmu[:,0:ncells] = numpy.minimum(tmp,rmu[:,0:ncells])
   rmu[0:ncells,:] = numpy.minimum(tmp,rmu[0:ncells,:].transpose()).transpose()
   rmu[:,-ncells:] = numpy.minimum(tmp[::-1],rmu[:,-ncells:])
   rmu[-ncells:,:] = numpy.minimum(tmp[::-1],rmu[-ncells:,:].transpose()).transpose()

   ## Only allow points where both models are defined in the boundaruy
   rmumask=fine_depth_m.mask
   rmumask[:,0:ncells] = numpy.logical_or(rmumask[:,0:ncells],coarse_depth_m.mask[:,0:ncells])
   rmumask[0:ncells,:] = numpy.logical_or(rmumask[0:ncells,:],coarse_depth_m.mask[0:ncells,:])
   rmumask[:,-ncells:] = numpy.logical_or(rmumask[:,-ncells:],coarse_depth_m.mask[:,-ncells:])
   rmumask[-ncells:,:] = numpy.logical_or(rmumask[-ncells:,:],coarse_depth_m.mask[-ncells:,:])

   
   figure = matplotlib.pyplot.figure(figsize=(8,8))
   ax=figure.add_subplot(111)
   P=ax.pcolormesh(rmu)
   figure.colorbar(P)#,norm=matplotlib.colors.LogNorm(vmin=mask.min(), vmax=mask.max()))
   figure.canvas.print_figure("tst.png")


   # Modify bathy in mask region
   newbathy = (1.-rmu) * coarse_depth + rmu * fine_depth
   newbathy[rmumask] = bathy_threshold
   newbathy[:,0]=bathy_threshold
   newbathy[:,-1]=bathy_threshold
   newbathy[0,:]=bathy_threshold
   newbathy[-1,:]=bathy_threshold
   #print newbathy.min(),newbathy.max()



   # Make call to consistency routine
   if check_consistency :
      logger.info("Passing merged bathymetry to consistency check ")
      import hycom_bathy_consistency # Normally in same dir as this python routine, so ok
      newbathy=hycom_bathy_consistency.main("",[],[],
            remove_isolated_basins=True,
            remove_one_neighbour_cells=True,
            remove_islets=True,
            remove_inconsistent_nesting_zone=True,
            inbathy=numpy.ma.masked_where(newbathy<=bathy_threshold,newbathy),
            write_to_file=False)


   # Mask data where depth below threshold
   newbathy_m=numpy.ma.masked_where(newbathy<=bathy_threshold,newbathy)

   # Create netcdf file with all  stages for analysis
   logger.info("Writing bathymetry to diagnostic file bathy_merged.nc")
   ncid = netCDF4.Dataset("bathy_merged.nc","w")
   ncid.createDimension("idm",newbathy.shape[1])
   ncid.createDimension("jdm",newbathy.shape[0])
   ncid.createVariable("lon","f8",("jdm","idm"))
   ncid.createVariable("lat","f8",("jdm","idm"))
   ncid.createVariable("coarse","f8",("jdm","idm"))
   ncid.createVariable("coarse_masked","f8",("jdm","idm"))
   ncid.createVariable("fine","f8",("jdm","idm"))
   ncid.createVariable("fine_masked","f8",("jdm","idm"))
   ncid.createVariable("final","f8",("jdm","idm"))
   ncid.createVariable("final_masked","f8",("jdm","idm"))
   ncid.createVariable("rmu","f8",("jdm","idm"))
   ncid.createVariable("modified","f8",("jdm","idm"))
   ncid.variables["lon"][:]=plon
   ncid.variables["lat"][:]=plat
   ncid.variables["coarse"][:]=coarse_depth
   ncid.variables["coarse_masked"][:]=coarse_depth_m
   ncid.variables["fine"][:]=fine_depth
   ncid.variables["fine_masked"][:]=fine_depth_m
   ncid.variables["final"][:]=newbathy
   ncid.variables["final_masked"][:]=newbathy_m
   modmask=newbathy-fine_depth
   ncid.variables["modified"][:] = modmask
   ncid.variables["rmu"][:] = rmu
   ncid.close()
   
   logger.info("Writing bathymetry plot to file newbathy.png")
   figure = matplotlib.pyplot.figure(figsize=(8,8))
   ax=figure.add_subplot(111)
   P=ax.pcolormesh(newbathy)
   figure.colorbar(P,norm=matplotlib.colors.LogNorm(vmin=newbathy.min(), vmax=newbathy.max()))
   I,J=numpy.where(numpy.abs(modmask)>.1)
   ax.scatter(J,I,20,"r")
   figure.canvas.print_figure("newbathy.png")



   # Print to HYCOM
   abf.write_bathymetry("MERGED",0,newbathy,bathy_threshold)
       tmp = [int(elem) for elem in tmp[0:4]]
       setattr(args, self.dest, tmp)

   parser = argparse.ArgumentParser(description='')
   parser.add_argument('--filename', help="",nargs='+')
   
   args = parser.parse_args()
   print(args.filename)
   #Example
   #python ./interpolate_sstncof2TP5.py --filename ../ncof_sst/ncof_sst_20*.nc
   
   gfile = abf.ABFileGrid("regional.grid","r")
   plon=gfile.read_field("plon")
   plat=gfile.read_field("plat")
   gfile.close()
   dpfile = abf.ABFileBathy("regional.depth","r",idm=gfile.idm,jdm=gfile.jdm)
   depth=dpfile.read_field("depth")
   dpfile.close()
   target_lon=plon
   target_lat=plat
   target_lonlats = [target_lon,target_lat]

   # compute for TPZ files
   counter=0
   file_count=0
   if args.filename:
      for ncfile0 in args.filename :
            #nci = mr.nc_getinfo(ncfile0)
            ncfile1 = Dataset(ncfile0,'r',format="NETCDF4")
            logger.info("ostia sst data: Now processing  %s"%ncfile0)
            # interpolate ncof_sst in tp5 grid
def main():

    # Read plon,plat
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")
    gfile.close()

    # Read input bathymetry
    bfile = abf.ABFileBathy("regional.depth",
                            "r",
                            idm=gfile.idm,
                            jdm=gfile.jdm,
                            mask=True)
    in_depth_m = bfile.read_field("depth")
    bfile.close()
    #in_depth=numpy.ma.filled(in_depth_m,bathy_threshold)

    # Starting point  (State 1 for Atlantic)
    kapref = numpy.ones(plat.shape) * 1.0
    print kapref.min(), kapref.max()

    # Find regions north of northern limit. Assumes
    for segment in range(len(northern_limit_longitudes)):

        ind1 = segment
        ind2 = (segment + 1) % len(northern_limit_longitudes)

        lo1 = northern_limit_longitudes[ind1]
        la1 = northern_limit_latitudes[ind1]
        lo2 = northern_limit_longitudes[ind2]
        la2 = northern_limit_latitudes[ind2]

        tmp1 = numpy.mod(plon + 360 - lo1, 360.)
        tmp2 = numpy.mod(lo2 + 360 - lo1, 360.)
        J = tmp1 <= tmp2
        #print numpy.count_nonzero(J)

        # Linear weights and latitude in selected points
        w2 = tmp1 / tmp2
        w1 = 1. - w2
        la = la2 * w2 + la1 * w1

        kapref[J] = numpy.where(plat[J] > la[J], 2.0, kapref[J])

    import scipy.ndimage
    kapref = scipy.ndimage.gaussian_filter(kapref, sigma=20)

    #print in_depth_m.min(),type(in_depth_m)
    kaprefplot = numpy.ma.masked_where(in_depth_m.mask, kapref)
    figure = matplotlib.pyplot.figure()
    ax = figure.add_subplot(111)
    P = ax.pcolormesh(kaprefplot)
    figure.colorbar(P, ax=ax)
    figure.canvas.print_figure("kapref.png")

    af = abf.AFile(plon.shape[1], plon.shape[0], "tbaric.a", "w")
    hmin, hmax = af.writerecord(kapref, None, record=0)
    af.close()
    bf = open("tbaric.b", "w")
    bf.write("tbaric.b\n")
    bf.write("\n")
    bf.write("\n")
    bf.write("\n")
    bf.write("i/jdm =  %5d %5d\n" % (plon.shape[1], plon.shape[0]))
    bf.write("tbaric: range = %14.6e%14.6e\n" % (hmin, hmax))
    bf.close()
def main(infile, rmu_width, rmu_efold, dpi=180):

    bathy_threshold = 0.  # TODO

    # Read plon,plat
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")
    gfile.close()

    # Read input bathymetri
    m = re.match("^(.*)(\.[ab])", infile)
    if m: infile = m.group(1)
    bfile = abf.ABFileBathy(infile,
                            "r",
                            idm=gfile.idm,
                            jdm=gfile.jdm,
                            mask=True)
    in_depth_m = bfile.read_field("depth")
    bfile.close()
    in_depth = numpy.ma.filled(in_depth_m, bathy_threshold)

    #print in_depth.min(),in_depth.max()
    ip = ~in_depth_m.mask
    ip = numpy.copy(ip)
    iu = numpy.copy(ip)
    iv = numpy.copy(ip)
    iu[:, 1:] = numpy.logical_and(ip[:, 1:], ip[:, :-1])
    iv[1:, :] = numpy.logical_and(ip[1:, :], ip[:-1, :])

    ifports = []
    ilports = []
    jfports = []
    jlports = []
    kdports = []

    process_south = True
    process_north = True
    process_west = True
    process_east = True

    fatal = False

    rmumask = numpy.zeros(in_depth.shape)
    labels = numpy.zeros(in_depth.shape)

    # Test ocean mask in 2nd grid cell from edge. If ocean, mark as nesting boundary.
    # When written to ports.input, we write
    # NB: All diag output is "Fortran" indexes (Starting from 1) - thats why we add 1 here and there

    for kdport in [1, 2, 3, 4]:
        t_ifports, t_ilports, t_jfports, t_jlports, t_kdports, t_labels, = port_setup(
            kdport, in_depth_m)
        labels[t_labels > 0] = t_labels[t_labels > 0] + labels.max()
        ifports.extend(t_ifports)
        ilports.extend(t_ilports)
        jfports.extend(t_jfports)
        jlports.extend(t_jlports)
        kdports.extend(t_kdports)

    # Build mask
    for i in range(len(ifports)):
        rmumask = relaxation_mask(rmumask, ifports[i], ilports[i], jfports[i],
                                  jlports[i], kdports[i], rmu_width)
    #print rmumask.min(),rmumask.max()
    rmumask = numpy.minimum(rmumask, 1.) * 1. / (rmu_efold * 86400.)
    rmumask_m = numpy.ma.masked_where(in_depth_m.mask, rmumask)

    # Check consistency
    fatal = False
    for i in range(len(ifports)):
        fatal = fatal or check_consistency(ifports[i], ilports[i], jfports[i],
                                           jlports[i], kdports[i], iu, iv,
                                           i + 1)

    # Open port output file
    logger.info("Writing to ports.input.tmp")
    fid = open("ports.input.tmp", "w")
    fid.write("%6d  'nports' = Number of ports \n" % len(kdports))
    for i in range(len(kdports)):
        write_port_location(fid, kdports[i], ifports[i] + 1, ilports[i] + 1,
                            jfports[i] + 1, jlports[i] + 1)
    fid.close()

    # Write rmu file
    rmufile = abf.ABFileRmu(
        "rmu",
        "w",
        cline1="Relaxation mask",
        cline2=
        "Relaxation mask created by topo_ports.py. rel zone width=%d, efold time=%d days"
        % (rmu_width, rmu_efold),
        mask=True)
    rmufile.write_field(rmumask, in_depth_m.mask, "rmu")
    rmufile.close()

    # Plot rmu with pcolormesh
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    cmap = matplotlib.pyplot.get_cmap("Greys_r")
    cmap2 = matplotlib.pyplot.get_cmap("jet")
    ax.add_patch(
        matplotlib.patches.Rectangle((1, 1),
                                     in_depth.shape[1],
                                     in_depth.shape[0],
                                     color=".5",
                                     alpha=.5))
    P = ax.pcolormesh(in_depth_m, cmap=cmap)
    P = ax.pcolormesh(rmumask_m, cmap=cmap2)
    CB = ax.figure.colorbar(P)
    figure.canvas.print_figure("rmu.png", dpi=dpi)

    # Plot ports with pcolormesh
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    cmap = matplotlib.pyplot.get_cmap("Greys_r")
    ax.add_patch(
        matplotlib.patches.Rectangle((1, 1),
                                     in_depth.shape[1],
                                     in_depth.shape[0],
                                     color=".5",
                                     alpha=.5))
    P = ax.pcolormesh(in_depth_m, cmap=cmap)
    I, J = numpy.where(labels > 0)
    S = ax.scatter(J, I, 50, labels[I, J], edgecolor='none')
    CB = ax.figure.colorbar(S)
    ax.set_xlim(0, in_depth.shape[1])
    ax.set_ylim(0, in_depth.shape[0])
    CB.ax.set_title("Port number")
    logger.info("Writing to ports_all.png")
    figure.canvas.print_figure("ports_all.png", dpi=dpi)

    # Port diagnostics plot
    figure2 = matplotlib.pyplot.figure(figsize=(8, 8))
    ax2 = figure2.add_subplot(111)
    cmap = matplotlib.pyplot.get_cmap("Greys_r")
    P = ax2.pcolormesh(in_depth_m,
                       cmap=cmap,
                       edgecolor=".4",
                       alpha=.5,
                       linewidth=.05)
    ax2.hold()
    Ps = []
    Ls = []
    for i in range(len(kdports)):

        iwidth = ilports[i] - ifports[i] + 1
        jwidth = jlports[i] - jfports[i] + 1
        #print ifports[i],jfports[i],iwidth,jwidth
        d = 1
        if kdports[i] == 1:
            xy = (ifports[i], jfports[i])
            jwidth = d
            c = "r"
        elif kdports[i] == 2:
            xy = (ifports[i], jfports[i])
            jwidth = d
            c = "g"
        elif kdports[i] == 3:
            xy = (ifports[i], jfports[i])
            iwidth = d
            c = "b"
        elif kdports[i] == 4:
            xy = (ifports[i], jfports[i])
            iwidth = d
            c = "m"

        figure.clf()
        ax = figure.add_subplot(111)
        P = ax.pcolormesh(in_depth_m,
                          cmap=cmap,
                          edgecolor=".4",
                          alpha=.5,
                          linewidth=.05)
        ax.add_patch(
            matplotlib.patches.Rectangle(xy, iwidth, jwidth, color=c,
                                         alpha=.5))
        ax.grid()
        ax.set_xlim(xy[0] - 20, xy[0] + iwidth + 20)
        ax.set_ylim(xy[1] - 20, xy[1] + jwidth + 20)
        ax.set_title("Port number %d - kdport=%d" % (i + 1, kdports[i]))

        R = ax2.add_patch(
            matplotlib.patches.Rectangle(xy, iwidth, jwidth, color=c,
                                         alpha=.5))
        Ps.append(R)
        Ls.append("Port %d" % (i + 1))

        fname = "port_%03d.png" % (i + 1)
        logger.info("Writing Diagnostics to %s" % fname)
        figure.canvas.print_figure(fname, bbox_inches='tight', dpi=dpi)

    fname = "ports_all_2.png"
    logger.info("Writing Diagnostics to %s" % fname)
    ax2.legend(Ps, Ls)
    figure2.canvas.print_figure(fname, bbox_inches='tight', dpi=dpi)

    if fatal:
        logger.error(
            "Errors were encountered - see errors above, and consult diag files. You may need to modify your topo file"
        )
        raise NameError("fatal exit")
    return rmumask, rmumask_m
def main(infile,
         blo,
         bla,
         remove_isolated_basins=True,
         remove_one_neighbour_cells=True,
         remove_islets=True,
         remove_inconsistent_nesting_zone=True,
         inbathy=None,
         write_to_file=True,
         bathy_threshold=0.):

    logger.info("Bathy threshold is %12.4f" % bathy_threshold)

    # Read plon,plat
    gfile = abf.ABFileGrid("regional.grid", "r")
    plon = gfile.read_field("plon")
    plat = gfile.read_field("plat")
    gfile.close()

    # Read input bathymetri
    if inbathy is not None:
        in_depth_m = inbathy
    else:
        bfile = abf.ABFileBathy(infile,
                                "r",
                                idm=gfile.idm,
                                jdm=gfile.jdm,
                                mask=True)
        in_depth_m = bfile.read_field("depth")
        #print "in_depth_m type, min, max:",type(in_depth_m),in_depth_m.min(),in_depth_m.max()
        bfile.close()

    # Modify basin
    in_depth = numpy.ma.filled(in_depth_m, bathy_threshold)
    depth = numpy.copy(in_depth)
    logger.info("depth min max: %f8.0 %f8.0" % (depth.min(), depth.max()))
    it = 1
    while it == 1 or numpy.count_nonzero(numpy.abs(depth - depth_old)) > 0:
        depth_old = numpy.copy(depth)
        logger.info("Basin modifications ... pass %d" % (it))
        if remove_isolated_basins:
            depth = modeltools.tools.remove_isolated_basins(
                plon, plat, depth, blo, bla, threshold=bathy_threshold)
        if remove_islets:
            depth = modeltools.tools.remove_islets(depth,
                                                   threshold=bathy_threshold)
        if remove_one_neighbour_cells:
            depth = modeltools.tools.remove_one_neighbour_cells(
                depth, threshold=bathy_threshold)
        if remove_inconsistent_nesting_zone:
            depth = modeltools.tools.remove_inconsistent_nesting_zone(
                depth, threshold=bathy_threshold)
        logger.info("Modified %d points " %
                    numpy.count_nonzero(depth - depth_old))
        it += 1
    logger.info("Modifications finished after %d iterations " % (it - 1))
    w5 = numpy.copy(depth)

    w5[:, 0] = bathy_threshold
    w5[:, -1] = bathy_threshold
    w5[0, :] = bathy_threshold
    w5[-1, :] = bathy_threshold
    #print "w5 type min max",type(w5),w5.min(),w5.max()

    # Mask data where depth below threshold
    w5_m = numpy.ma.masked_where(w5 <= bathy_threshold, w5)

    # Create netcdf file with all  stages for analysis
    logger.info("Writing bathymetry to file bathy_consistency.nc")
    ncid = netCDF4.Dataset("bathy_consistency.nc", "w")
    ncid.createDimension("idm", w5.shape[1])
    ncid.createDimension("jdm", w5.shape[0])
    ncid.createVariable("lon", "f8", ("jdm", "idm"))
    ncid.createVariable("lat", "f8", ("jdm", "idm"))
    ncid.createVariable("old", "f8", ("jdm", "idm"))
    ncid.createVariable("old_masked", "f8", ("jdm", "idm"))
    ncid.createVariable("new", "f8", ("jdm", "idm"))
    ncid.createVariable("new_masked", "f8", ("jdm", "idm"))
    ncid.createVariable("modified", "i4", ("jdm", "idm"))
    ncid.variables["lon"][:] = plon
    ncid.variables["lat"][:] = plat
    ncid.variables["old"][:] = in_depth
    ncid.variables["old_masked"][:] = in_depth_m
    ncid.variables["new"][:] = w5
    ncid.variables["new_masked"][:] = w5_m
    modmask = numpy.abs(in_depth - depth) > .1
    ncid.variables["modified"][:] = modmask.astype("i4")
    ncid.close()

    logger.info("Writing bathymetry plot to file newbathy.png")
    figure = matplotlib.pyplot.figure(figsize=(8, 8))
    ax = figure.add_subplot(111)
    P = ax.pcolormesh(w5_m)
    figure.colorbar(P,
                    norm=matplotlib.colors.LogNorm(vmin=w5_m.min(),
                                                   vmax=w5_m.max()))
    I, J = numpy.where(numpy.abs(modmask) > .1)
    ax.scatter(J, I, 20, "r")
    figure.canvas.print_figure("newbathy.png")

    # Print to HYCOM and CICE bathymetry files
    if write_to_file:
        abf.write_bathymetry("CONSISTENT", 0, w5, bathy_threshold)

    return w5
Beispiel #10
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,
         masklim=None,
         filename2='',
         dpi=180):

    cmap = matplotlib.pyplot.get_cmap("jet")

    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 > 80] = np.nan
        maskd[plat < 50] = np.nan
        maskd[plon > 60] = np.nan
        maskd[plon < -50] = 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
    freezp = -1.8
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12))
    #---------------
    # compute for TP6 files
    #-----------------------------------------
    #---------------------------------------------------------------------------------
    #---------------------------------------------------------------------------------
    # filename2
    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 = "CNTL SST"
    Labl1 = myfiles[0][:28]
    yyyy1 = myfiles[0][-9:-5]
    if "archv." in myfiles[0]:
        yyyy1 = myfiles[0][-13:-9]
    print("myfiles[0]=", myfiles[0])
    print("yyy1=", yyyy1)
    if filename2:
        dt_2 = np.zeros(len(filename2))
        diff_dt_2 = np.zeros(len(filename2))
        rmse_dt_2 = np.zeros(len(filename2))
        tid_2=np.array([datetime.datetime(int(yyyy1), 1, 15) \
           + relativedelta(months=i) for i in range(len(filename2))])
        Labl2 = filename2[0][:28]
        counter = 0
        file_count = 0
        sum_fld1 = maskd
        sum_fld1[~np.isnan(sum_fld1)] = 0.0
        if "srfhgt" in fieldname:
            fieldname = "srfhgt"
        elif "temp" in fieldname:
            fieldname = "temp"
        for fil0 in filename2:
            logger.info("Now processing  %s" % fil0)
            n_intloop,ab,ab2,rdtimes = open_file(fil0,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)
                # Create scalar field for vectors
                print('---------mn,mx  data=', fld1.min(), fld1.max())
                #if "srfhgt" in fieldname:
                #   fld1= fld1/9.806
                print("fld1.shpe", fld1.shape)
                print('mn,mx=', fld1.min(), fld1.max(), 'count=', counter)
                dt_2[counter] = np.nanmean(fld1)
                counter = counter + 1
                sum_fld1 = sum_fld1 + fld1
                del fld1
    #---------------------------------------------------------------------------------
    #---------------------------------------------------------------------------------
    base = datetime.datetime(int(yyyy1), 1, 15)
    tid = np.array(
        [base + relativedelta(months=i) for i in range(len(myfiles))])
    if "archv." in myfiles[0]:
        tid = np.array(
            [base + relativedelta(days=i) for i in range(len(myfiles))])
    nmexp = 1
    if filename2:
        nmexp = nmexp + 1
    print(
        'processing data from No runs ==##############>>>>>>>>>>>>>>>>>>>>>>>',
        nmexp)
    whole_domain = True
    whole_domain = False
    #
    counter = 0
    file_count = 0
    sum_fld1 = maskd
    sum_fld1[~np.isnan(sum_fld1)] = 0.0
    logger.info(
        ">>>>>--------------------------Processing the first files=  myfiles")
    for myfile0 in myfiles:
        logger.info("Now processing  %s" % myfile0)
        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 ('temp' in fieldname) and whole_domain:
                    vert_fld_sum = 0
                    for lvl in range(50):
                        print('lvl=', lvl, fieldlevel)
                        fld_lvl = ab.read_field(fieldname, lvl + 1)
                        vert_fld_sum = vert_fld_sum + np.nanmean(fld_lvl)
                    vert_fld_avg = vert_fld_sum / 50.0

            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)
            # Create scalar field for vectors
            print('---------mn,mx  data=', fld1.min(), fld1.max())
            #if "srfhgt" in fieldname:
            #   fld1= fld1/9.806
            print("fld1.shpe", fld1.shape)
            print('mn,mx=', fld1.min(), fld1.max(), 'count=', counter)
            if ('temp' in fieldname) and whole_domain:
                dt_cnl[counter] = vert_fld_avg
            else:
                dt_cnl[counter] = np.nanmean(fld1)
            counter = counter + 1
            sum_fld1 = sum_fld1 + fld1
            del fld1
            # End i_intloop
        print('Computing the avearge of file_counter= ', file_count,
              'counter=', counter)
    #---------------------------------------
    #---------------------------------------
    #plot_climatology
    Clim_arr = np.zeros((plon.shape[0], plon.shape[1], 12))
    if 'tem' in fieldname:
        counter = 0
        rlxfile0 = "/cluster/work/users/achoth/TP5a0.06/relax/050/relax_tem.a"
        rlx_afile = abf.AFile(ab.idm, ab.jdm, rlxfile0, "r")
        lyr = fieldlevel
        record_num = lyr
        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)
            logger.debug("File %s, record_var/mnth*kdm %03d/%03d" %
                         (rlxfile0, record_var, mnth * kdm))
            print('record, mn,mx  data=', kdm * mnth, fld1.min(), fld1.max())
            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] = np.nanmean(fld1)
            #Clim_arr[:,:,mnth]=fld1[:,:]
            counter = counter + 1
            print('counter=', counter)
            del fld1
        #
        tid_clim = np.array(
            [base + relativedelta(months=i) for i in range(12)])
        #figure, ax = matplotlib.pyplot.figure()
        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])
    #
    #---------------------------------------
    #---------------------------------------
    figure, ax = plt.subplots()
    years = YearLocator()  # every year
    months = MonthLocator()  # every month
    yearsFmt = DateFormatter('%Y')
    #ax=figure.add_subplot(111)
    nplts = 1
    ax.plot_date(tid, dt_cnl, '-o', color='g', ms=3, label=Labl1)
    if 'tem' in fieldname:
        ax.plot_date(tid[0:len(dt_cnl)],
                     dt_clim_cat[:],
                     ':',
                     color='black',
                     label='Phc-Clim.')
    if filename2:
        ax.plot_date(tid_2, dt_2, '-v', color='orange', ms=3, label=Labl2)
    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))

    if "k.e" in fieldname:
        fieldname = "KE"
    if "u-vel" in fieldname:
        fieldname = "u-vel"
    ts_fil = "Time_series_cntl%s_%02d_%02d" % (fieldname, fieldlevel, counter)
    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)
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)