def convertMODEL2ROMS(confM2R): # First opening of input file is just for initialization of grid filenamein = fc.getFilename(confM2R, confM2R.start_year, confM2R.start_month, confM2R.start_day, None) # Finalize creating the model grd object now that we know the filename for input data confM2R.grdMODEL.opennetcdf(filenamein) confM2R.grdMODEL.createobject(confM2R) confM2R.grdMODEL.getdims() # Create the ESMF weights used to do all of the horizontal interpolation interp2D.setupESMFInterpolationWeights(confM2R) # Now we want to subset the data to avoid storing more information than we need. # We do this by finding the indices of maximum and minimum latitude and longitude in the matrixes if confM2R.subsetindata: IOsubset.findSubsetIndices(confM2R.grdMODEL, min_lat=confM2R.subset[0], max_lat=confM2R.subset[1], min_lon=confM2R.subset[2], max_lon=confM2R.subset[3]) print('==> Initializing done') print('\n--------------------------') print('==> Starting loop over time') timecounter = 0 firstrun = True for year in confM2R.years: months = datetimeFunctions.createlistofmonths(confM2R, year) for month in months: days = datetimeFunctions.createlistofdays(confM2R, year, month) print("days {}".format(days)) for day in days: # Get the current date for given timestep getTime(confM2R, year, month, day, timecounter) # Each MODEL file consist only of one time step. Get the subset data selected, and # store that time step in a new array: if firstrun: print( "=> NOTE! Make sure that these two arrays are in sequential order:" ) print("==> myvars: %s" % confM2R.inputdatavarnames) print("==> varNames %s" % confM2R.globalvarnames) firstrun = False if confM2R.subsetindata: # The first iteration we want to organize the subset indices we want to extract # from the input data to get the interpolation correct and to function fast IOsubset.organizeSplit(confM2R.grdMODEL, confM2R.grdROMS) for myvar in confM2R.globalvarnames: if myvar in [ 'temperature', 'salinity', 'uvel', 'vvel', 'O3_c', 'O3_TA', 'N1_p', 'N3_n', 'N5_s', 'O2_o' ]: data = get3ddata(confM2R, myvar, year, month, day, timecounter) if myvar in [ 'ssh', 'ageice', 'uice', 'vice', 'aice', 'hice', 'snow_thick' ]: data = get2ddata(confM2R, myvar, year, month, day, timecounter) # Take the input data and horizontally interpolate to your grid array1 = interp2D.dohorinterpolationregulargrid( confM2R, data, myvar) if myvar in [ 'temperature', 'salinity', 'O3_c', 'O3_TA', 'N1_p', 'N3_n', 'N5_s', 'O2_o' ]: STdata = verticalinterpolation(myvar, array1, array1, confM2R.grdROMS, confM2R.grdMODEL) for dd in range(len(STdata[:, 0, 0])): STdata[dd, :, :] = np.where( confM2R.grdROMS.mask_rho == 0, confM2R.grdROMS.fillval, STdata[dd, :, :]) STdata = np.where( abs(STdata) > 1000, confM2R.grdROMS.fillval, STdata) IOwrite.writeclimfile(confM2R, timecounter, myvar, STdata) if timecounter == confM2R.grdROMS.inittime and confM2R.grdROMS.write_init is True: IOinitial.createinitfile(confM2R, timecounter, myvar, STdata) if myvar in [ 'ssh', 'ageice', 'aice', 'hice', 'snow_thick' ]: SSHdata = array1[0, :, :] SSHdata = np.where(confM2R.grdROMS.mask_rho == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) > 100, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == 0, confM2R.grdROMS.fillval, SSHdata) # Specific for ROMs. We set 0 where we should have fillvalue for ice otherwise ROMS blows up. SSHdata = np.where( abs(SSHdata) == confM2R.grdROMS.fillval, 0, SSHdata) IOwrite.writeclimfile(confM2R, timecounter, myvar, SSHdata) if timecounter == confM2R.grdROMS.inittime: IOinitial.createinitfile(confM2R, timecounter, myvar, SSHdata) # The following are special routines used to calculate the u and v velocity # of ice based on the transport, which is divided by snow and ice thickenss # and then multiplied by grid size in dx or dy direction (opposite of transport). if myvar in ['uice', 'vice']: SSHdata = array1[0, :, :] if myvar == "uice": mymask = confM2R.grdROMS.mask_u if myvar == "vice": mymask = confM2R.grdROMS.mask_v SSHdata = np.where(mymask == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) > 100, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == confM2R.grdROMS.fillval, 0, SSHdata) IOwrite.writeclimfile(confM2R, timecounter, myvar, SSHdata) if timecounter == confM2R.grdROMS.inittime: if myvar == 'uice': IOinitial.createinitfile( confM2R, timecounter, myvar, SSHdata) if myvar == 'vice': IOinitial.createinitfile( confM2R, timecounter, myvar, SSHdata) if myvar == 'uvel': array2 = array1 if myvar == 'vvel': urot, vrot = rotate(confM2R.grdROMS, confM2R.grdMODEL, data, array2, array1) u, v = interpolate2uv(confM2R.grdROMS, confM2R.grdMODEL, urot, vrot) Udata, Vdata, UBARdata, VBARdata = verticalinterpolation( myvar, u, v, confM2R.grdROMS, confM2R.grdMODEL) if myvar == 'vvel': Udata = np.where(confM2R.grdROMS.mask_u == 0, confM2R.grdROMS.fillval, Udata) Udata = np.where( abs(Udata) > 1000, confM2R.grdROMS.fillval, Udata) Vdata = np.where(confM2R.grdROMS.mask_v == 0, confM2R.grdROMS.fillval, Vdata) Vdata = np.where( abs(Vdata) > 1000, confM2R.grdROMS.fillval, Vdata) UBARdata = np.where(confM2R.grdROMS.mask_u == 0, confM2R.grdROMS.fillval, UBARdata) UBARdata = np.where( abs(UBARdata) > 1000, confM2R.grdROMS.fillval, UBARdata) VBARdata = np.where(confM2R.grdROMS.mask_v == 0, confM2R.grdROMS.fillval, VBARdata) VBARdata = np.where( abs(VBARdata) > 1000, confM2R.grdROMS.fillval, VBARdata) IOwrite.writeclimfile(confM2R, timecounter, myvar, Udata, Vdata, UBARdata, VBARdata) if timecounter == confM2R.grdROMS.inittime: IOinitial.createinitfile(confM2R, timecounter, myvar, Udata, Vdata, UBARdata, VBARdata) timecounter += 1
def convertMODEL2ROMS(confM2R): # First opening of input file is just for initialization of grid if confM2R.indatatype == 'SODA': filenamein = getSODAfilename(confM2R, confM2R.start_year, confM2R.start_month, "salinity") if confM2R.indatatype == 'SODA3': filenamein = getSODA3filename(confM2R, confM2R.start_year, confM2R.start_month, "salinity") if confM2R.indatatype == 'SODAMONTHLY': filenamein = getSODAfilename(confM2R, confM2R.start_year, confM2R.start_month, "salinity") if confM2R.indatatype == 'NORESM': filenamein = getNORESMfilename(confM2R, confM2R.start_year, confM2R.start_month, "grid") if confM2R.indatatype == 'WOAMONTHLY': filenamein = getWOAMONTHLYfilename(confM2R, confM2R.start_year, confM2R.start_month, "temperature") if confM2R.indatatype == 'GLORYS': filenamein = getGLORYSfilename(confM2R, confM2R.start_year, confM2R.start_month, "S") if confM2R.indatatype == 'GLORYS': filenamein = getGLORYSfilename(confM2R, confM2R.start_year, confM2R.start_month, "S") if confM2R.indatatype == 'NS8KM': filenamein = getNS8KMfilename(confM2R, confM2R.start_year, confM2R.start_month, "S") if confM2R.indatatype == 'NS8KMZ': filenamein, readFromOneFile = getNS8KMZfilename( confM2R, confM2R.start_year, confM2R.start_month, "S") # Finalize creating the model grd object now that we know the filename for input data confM2R.grdMODEL.opennetcdf(filenamein) confM2R.grdMODEL.createobject(confM2R) confM2R.grdMODEL.getdims() if confM2R.useesmf: print( "=>Creating the interpolation weights and indexes using ESMF (this may take some time....):" ) print(" -> regridSrc2Dst at RHO points") confM2R.grdMODEL.fieldSrc = ESMF.Field( confM2R.grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.fieldDst_rho = ESMF.Field( confM2R.grdROMS.esmfgrid, "fieldDst", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.regridSrc2Dst_rho = ESMF.Regrid( confM2R.grdMODEL.fieldSrc, confM2R.grdMODEL.fieldDst_rho, regrid_method=ESMF.RegridMethod.BILINEAR, unmapped_action=ESMF.UnmappedAction.IGNORE) print(" -> regridSrc2Dst at U points") confM2R.grdMODEL.fieldSrc = ESMF.Field( confM2R.grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.fieldDst_u = ESMF.Field( confM2R.grdROMS.esmfgrid_u, "fieldDst_u", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.regridSrc2Dst_u = ESMF.Regrid( confM2R.grdMODEL.fieldSrc, confM2R.grdMODEL.fieldDst_u, regrid_method=ESMF.RegridMethod.BILINEAR, unmapped_action=ESMF.UnmappedAction.IGNORE) print(" -> regridSrc2Dst at V points") confM2R.grdMODEL.fieldSrc = ESMF.Field( confM2R.grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.fieldDst_v = ESMF.Field( confM2R.grdROMS.esmfgrid_v, "fieldDst_v", staggerloc=ESMF.StaggerLoc.CENTER) confM2R.grdMODEL.regridSrc2Dst_v = ESMF.Regrid( confM2R.grdMODEL.fieldSrc, confM2R.grdMODEL.fieldDst_v, regrid_method=ESMF.RegridMethod.BILINEAR, unmapped_action=ESMF.UnmappedAction.IGNORE) # Now we want to subset the data to avoid storing more information than we need. # We do this by finding the indices of maximum and minimum latitude and longitude in the matrixes if confM2R.subsetindata: IOsubset.findSubsetIndices(confM2R.grdMODEL, min_lat=confM2R.subset[0], max_lat=confM2R.subset[1], min_lon=confM2R.subset[2], max_lon=confM2R.subset[3]) print('==> Initializing done') print('\n--------------------------') print('==> Starting loop over time') time = 0 firstrun = True for year in confM2R.years: months = datetimeFunctions.createlistofmonths(confM2R, year) for month in months: days = datetimeFunctions.createlistofdays(confM2R, year, month) for day in days: # Get the current date for given timestep getTime(confM2R, year, month, day) # Each MODEL file consist only of one time step. Get the subset data selected, and # store that time step in a new array: if firstrun: print( "=> NOTE! Make sure that these two arrays are in sequential order:" ) print("==> myvars: %s" % confM2R.inputdatavarnames) print("==> varNames %s" % confM2R.globalvarnames) firstrun = False if confM2R.subsetindata: # The first iteration we want to organize the subset indices we want to extract # from the input data to get the interpolation correct and to function fast IOsubset.organizeSplit(confM2R.grdMODEL, confM2R.grdROMS) for myvar in confM2R.globalvarnames: if myvar in ['temperature', 'salinity', 'uvel', 'vvel']: data = get3ddata(confM2R, myvar, year, month, day) if myvar in [ 'ssh', 'ageice', 'uice', 'vice', 'aice', 'hice', 'snow_thick' ]: data = get2ddata(confM2R, myvar, year, month, day) # Take the input data and horizontally interpolate to your grid array1 = horizontalinterpolation(confM2R, myvar, data) if myvar in ['temperature', 'salinity']: STdata = verticalinterpolation(myvar, array1, array1, confM2R.grdROMS, confM2R.grdMODEL) for dd in range(len(STdata[:, 0, 0])): STdata[dd, :, :] = np.where( confM2R.grdROMS.mask_rho == 0, confM2R.grdROMS.fillval, STdata[dd, :, :]) STdata = np.where( abs(STdata) > 1000, confM2R.grdROMS.fillval, STdata) IOwrite.writeclimfile(confM2R, time, myvar, STdata) if time == confM2R.grdROMS.inittime and confM2R.grdROMS.write_init is True: IOinitial.createinitfile(confM2R, time, myvar, STdata) if myvar in [ 'ssh', 'ageice', 'aice', 'hice', 'snow_thick' ]: SSHdata = array1[0, :, :] SSHdata = np.where(confM2R.grdROMS.mask_rho == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) > 100, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == 0, confM2R.grdROMS.fillval, SSHdata) # Specific for ROMs. We set 0 where we should have fillvalue for ice otherwise ROMS blows up. SSHdata = np.where( abs(SSHdata) == confM2R.grdROMS.fillval, 0, SSHdata) IOwrite.writeclimfile(confM2R, time, myvar, SSHdata) if time == confM2R.grdROMS.inittime: IOinitial.createinitfile(confM2R, time, myvar, SSHdata) # The following are special routines used to calculate the u and v velocity # of ice based on the transport, which is divided by snow and ice thickenss # and then multiplied by grid size in dx or dy direction (opposite of transport). if myvar in ['uice', 'vice']: SSHdata = array1[0, :, :] if myvar == "uice": mymask = confM2R.grdROMS.mask_u if myvar == "vice": mymask = confM2R.grdROMS.mask_v SSHdata = np.where(mymask == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) > 100, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == 0, confM2R.grdROMS.fillval, SSHdata) SSHdata = np.where( abs(SSHdata) == confM2R.grdROMS.fillval, 0, SSHdata) # SSHdata = np.ma.masked_where(abs(SSHdata) > 1000, SSHdata) print( "Data range of %s after interpolation: %3.3f to %3.3f" % (myvar, SSHdata.min(), SSHdata.max())) IOwrite.writeclimfile(confM2R, time, myvar, SSHdata) if time == confM2R.grdROMS.inittime: if myvar == 'uice': IOinitial.createinitfile( confM2R, time, myvar, SSHdata) if myvar == 'vice': IOinitial.createinitfile( confM2R, time, myvar, SSHdata) if myvar == 'uvel': array2 = array1 if myvar == 'vvel': indexROMS_UBAR = (confM2R.grdROMS.eta_u, confM2R.grdROMS.xi_u) indexROMS_VBAR = (confM2R.grdROMS.eta_v, confM2R.grdROMS.xi_v) # UBARdata = np.zeros((indexROMS_UBAR), dtype=np.float64) # VBARdata = np.zeros((indexROMS_VBAR), dtype=np.float64) urot, vrot = rotate(confM2R.grdROMS, confM2R.grdMODEL, data, array2, array1) u, v = interpolate2uv(confM2R.grdROMS, confM2R.grdMODEL, urot, vrot) Udata, Vdata, UBARdata, VBARdata = verticalinterpolation( myvar, u, v, confM2R.grdROMS, confM2R.grdMODEL) if myvar == 'vvel': # print "Data range of U after interpolation: %3.3f to %3.3f - V after scaling: %3.3f to %3.3f" % ( # Udata.min(), Udata.max(), Vdata.min(), Vdata.max()) Udata = np.where(confM2R.grdROMS.mask_u == 0, confM2R.grdROMS.fillval, Udata) Udata = np.where( abs(Udata) > 1000, confM2R.grdROMS.fillval, Udata) Vdata = np.where(confM2R.grdROMS.mask_v == 0, confM2R.grdROMS.fillval, Vdata) Vdata = np.where( abs(Vdata) > 1000, confM2R.grdROMS.fillval, Vdata) UBARdata = np.where(confM2R.grdROMS.mask_u == 0, confM2R.grdROMS.fillval, UBARdata) UBARdata = np.where( abs(UBARdata) > 1000, confM2R.grdROMS.fillval, UBARdata) VBARdata = np.where(confM2R.grdROMS.mask_v == 0, confM2R.grdROMS.fillval, VBARdata) VBARdata = np.where( abs(VBARdata) > 1000, confM2R.grdROMS.fillval, VBARdata) IOwrite.writeclimfile(confM2R, time, myvar, Udata, Vdata, UBARdata, VBARdata) if time == confM2R.grdROMS.inittime: IOinitial.createinitfile(confM2R, time, myvar, Udata, Vdata, UBARdata, VBARdata) time += 1