Esempio n. 1
0
def get3ddata(confM2R, myvar, year, month, day, timecounter):
    varN=confM2R.globalvarnames.index(myvar)
   
    # The variable splitExtract is defined in IOsubset.py and depends on the orientation
    # and indatatype of grid (-180-180 or 0-360). Assumes regular grid.
    if confM2R.useesmf:
        filename = fc.getFilename(confM2R,year,month,confM2R.inputdatavarnames[varN])
        print(filename,confM2R.inputdatavarnames[varN])
        try:
            cdf = Dataset(filename)
        except:
            print("Unable to open input file {}".format(filename))
            return

        if confM2R.indatatype == "SODA":
            data = cdf.variables[confM2R.inputdatavarnames[varN]][0,:,:,:]

        if confM2R.indatatype == "SODA3":
            data = cdf.variables[confM2R.inputdatavarnames[varN]][month-1,:,:,:]

        if confM2R.indatatype == "SODAMONTHLY":
            data = cdf.variables[str(confM2R.inputdatavarnames[varN])][:, :, :]

        if confM2R.indatatype == "WOAMONTHLY":
            data = cdf.variables[str(confM2R.inputdatavarnames[varN])][month-1,:,:,:]

        if confM2R.indatatype == "NORESM":
            # For NorESM data - all data is in one big file so we need the timecounter to access correct data
            myunits = cdf.variables[str(confM2R.inputdatavarnames[varN])].units
            data = np.squeeze(cdf.variables[str(confM2R.inputdatavarnames[varN])][timecounter,:,:,:])
            data = np.where(data.mask, confM2R.grdROMS.fillval, data)

        if confM2R.indatatype == "GLORYS":
            myunits = cdf.variables[str(confM2R.inputdatavarnames[varN])].units
            data = np.squeeze(cdf.variables[str(confM2R.inputdatavarnames[varN])][0,:,:,:])
            data = np.where(data.mask, confM2R.grdROMS.fillval, data)

        cdf.close()

    if myvar == 'temperature' and confM2R.indatatype in ["NS8KMZ", "GLORYS", "NORESM"]:

        if myunits == "degree_Kelvin" or myunits == "K":
            if confM2R.indatatype in ["GLORYS"]:
                data = np.where(data <= -32.767, confM2R.grdROMS.fillval, data)
            data = data - 273.15

    if confM2R.indatatype == "GLORYS":
        data = np.where(data <= -32.767, confM2R.grdROMS.fillval, data)
        data = np.ma.masked_where(data <= confM2R.grdROMS.fillval, data)

    if __debug__:
        print("Data range of {} just after extracting from netcdf file: {:3.3f}-{:3.3f}".format(str(confM2R.inputdatavarnames[varN]),
                                                                                    float(data.min()), float(data.max())))
    return data
Esempio n. 2
0
def get2ddata(confM2R, myvar, year, month, day, timecounter):
    
    varN=confM2R.globalvarnames.index(myvar)

    if confM2R.useesmf:
        
        if not confM2R.set2DvarsToZero:
            filename = fc.getFilename(confM2R,year,month,confM2R.inputdatavarnames[varN])
            try:
                cdf = Dataset(filename)
            except:
                print("Unable to open input file {}".format(filename))
                return

            if confM2R.indatatype == "SODA":
                data = cdf.variables[confM2R.inputdatavarnames[varN]][0,:,:]

            if confM2R.indatatype == "SODA3":
                if myvar == 'aice':
                    # We only extract the first thickness concentration. Need to fix this so all 5 classes can be extracted.
                    # http://www.atmos.umd.edu/~ocean/index_files/soda3_readme.htm
                    # hi: sea ice thickness [m ice]
                    # mi: sea ice mass [kg/m^2]
                    # hs: snow thickness [m snow]
                    # {cn1,cn2,cn3,cn4,cn5}: sea ice concentration [0:1] in five ice thickness classes
                    data = cdf.variables[confM2R.inputdatavarnames[varN]][int(month-1),0,:,:]
                else:
                    data = cdf.variables[confM2R.inputdatavarnames[varN]][int(month-1),:,:]

            if confM2R.indatatype == "SODAMONTHLY":
                data = cdf.variables[str(confM2R.inputdatavarnames[varN])][:, :]

            if confM2R.indatatype == "WOAMONTHLY":
                data = cdf.variables[str(confM2R.inputdatavarnames[varN])][month-1,:,:]

            if (confM2R.indatatype == "NORESM" and confM2R.set2DvarsToZero is False):
                # myunits = cdf.variables[str(grdROMS.varNames[varN])].units
                # For NORESM data are 12 months of data stored in ice files. Use ID as month indicator to get data.
                data = np.squeeze(cdf.variables[str(confM2R.inputdatavarnames[varN])][timecounter,:,:])
                data = np.where(data.mask, confM2R.grdROMS.fillval, data)

            if confM2R.indatatype == "GLORYS":
                data = np.squeeze(cdf.variables[str(confM2R.inputdatavarnames[varN])][0,:,:])
                data = np.where(data.mask, confM2R.grdROMS.fillval, data)

            if not confM2R.set2DvarsToZero: cdf.close()

            if __debug__ and not confM2R.set2DvarsToZero:
                print("Data range of {} just after extracting from netcdf file: {:3.3f}-{:3.3f}".format(str(confM2R.inputdatavarnames[varN]),
                                                                                            float(data.min()), float(data.max())))
    if confM2R.set2DvarsToZero:
        return np.zeros((np.shape(confM2R.grdMODEL.lon)))
    return data
Esempio n. 3
0
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