def horizontalinterpolation(confM2R, myvar, data): print('Start %s horizontal interpolation for %s' % (confM2R.grdtype, myvar)) try: if myvar in ['temperature', 'salinity']: return interp2D.dohorinterpolationregulargrid(confM2R, data) elif myvar in [ 'ssh', 'ageice', 'uice', 'vice', 'aice', 'hice', 'snow_thick' ]: return interp2D.dohorinterpolationsshregulargrid(confM2R, data) elif myvar in ['uvel', 'vvel']: return interp2D.dohorinterpolationregulargrid(confM2R, data) except IOError as error: print("An error occurred in horizontalinterpolation: {}".format(error)) raise
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