コード例 #1
0
def createAtmosFileUV(grdROMS, modelpath, atmospath, startdate, enddate,
                      useESMF, myformat, abbreviation, mytype, gridtype):

    # Setup
    years = [(int(startdate.year) + kk)
             for kk in range(1 + int(enddate.year) - int(startdate.year))]

    # Create the objects for source and destination grids

    # Get the "Fraction of sfc area covered by ocean
    nor = atmospath + "NRCP45AERCN_f19_g16_CLE_02.cam2.h0.2006-01.nc"
    cdf = Dataset(nor, "r")
    OCENFRAC = cdf.variables["OCNFRAC"][:]
    cdf.close()
    Fill = -999.0

    grdMODEL = grd.grdClass(nor, mytype, mytype, useESMF, 'atmos')

    # Create the outputfile
    outfilename = abbreviation + '_windUV_' + str(mytype) + '_' + str(
        startdate.year) + '_to_' + str(enddate.year) + '.nc'
    IOatmos.createNetCDFFileUV(grdROMS, outfilename, myformat, mytype)

    # Setup ESMF for interpolation (calculates weights)
    grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid,
                                   "fieldSrc",
                                   staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.fieldDst_rho = ESMF.Field(grdROMS.esmfgrid,
                                       "fieldDst",
                                       staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.regridSrc2Dst_rho = ESMF.Regrid(
        grdMODEL.fieldSrc,
        grdMODEL.fieldDst_rho,
        regrid_method=ESMF.RegridMethod.BILINEAR)
コード例 #2
0
def getStationData(years, IDS, sodapath, latlist, lonlist, stationNames):

    fileNameIn = sodapath + 'SODA_2.0.2_' + str(years[0]) + '_' + str(
        IDS[0]) + '.cdf'
    """First time in loop, get the essential old grid information"""
    """SODA data already at Z-levels. No need to interpolate to fixed depths, but we use the one we have"""

    grdMODEL = grd.grdClass(fileNameIn, "SODA")
    IOverticalGrid.get_z_levels(grdMODEL)

    station = 0
    numberOfPoints = 4

    for lat, lon in zip(latlist, lonlist):
        print '\n----------------NEW STATION==> %s ------------------------------------------' % (
            stationNames[station])
        """Now we want to find the indices for our longitude, latitude station pairs in the lat-long list"""
        gridIndexes, dis = getStationIndices(grdMODEL, lon, lat, 'SODA',
                                             numberOfPoints)

        stTime = []
        stDate = []
        time = 0
        counter = 0
        t = 0
        total = float(len(years) * len(IDS))
        import progressbar
        progress = progressbar.ProgressBar(widgets=[Percentage(),
                                                    Bar()],
                                           maxval=total).start()

        for year in years:
            for ID in IDS:
                file = "SODA_2.0.2_" + str(year) + "_" + str(ID) + ".cdf"
                filename = sodapath + file

                jdsoda, yyyymmdd, message = getStationTime(grdMODEL, year, ID)

                stTime.append(jdsoda)
                stDate.append(yyyymmdd)

                cdf = Dataset(filename, 'r')
                """Each SODA file consist only of one time step. Get the subset data selected, and
                store that time step in a new array:"""
                if year == years[0] and ID == IDS[0]:
                    validIndex, validDis = testValidStation(
                        cdf, dis, numberOfPoints, gridIndexes)
                    deepest = testValidDepth(cdf, numberOfPoints, gridIndexes,
                                             grdMODEL.depth)
                    stTemp, stSalt, stSSH, stUvel, stVvel, stTauX, stTauY = initArrays(
                        years, IDS, deepest, stationNames[station], lon, lat)

                for i in validIndex:
                    wgt = float(validDis[i]) / sum(validDis)
                    latindex = int(gridIndexes[i][0])
                    lonindex = int(gridIndexes[i][1])
                    """The values at a station is calculated by interpolating from the
                    numberOfPoints around the station uysing weights (wgt)
                    """
                    stTemp[time, :] = stTemp[time, :] + (cdf.variables["TEMP"][
                        t, 0:deepest, latindex, lonindex]) * wgt
                    stSalt[time, :] = stSalt[time, :] + (cdf.variables["SALT"][
                        t, 0:deepest, latindex, lonindex]) * wgt
                    stSSH[time] = stSSH[time] + (
                        cdf.variables["SSH"][t, latindex, lonindex]) * wgt
                    stTauX[time] = stTauX[time] + (
                        cdf.variables["TAUX"][t, latindex, lonindex]) * wgt
                    stTauY[time] = stTauY[time] + (
                        cdf.variables["TAUY"][t, latindex, lonindex]) * wgt
                    stUvel[time, :] = stUvel[time, :] + (cdf.variables["U"][
                        t, 0:deepest, latindex, lonindex]) * wgt
                    stVvel[time, :] = stVvel[time, :] + (cdf.variables["V"][
                        t, 0:deepest, latindex, lonindex]) * wgt

                cdf.close()
                counter += 1

                progress.update(time)
                time += 1

        print 'Total time steps saved to file %s for station %s' % (time,
                                                                    station)
        #plotData.contourStationData(stTemp,stTime,stDate,-grdMODEL.depth[0:deepest],stationNames[station])

        outfilename = 'station_' + str(stationNames[station]) + '.nc'
        print 'Results saved to file %s' % (outfilename)
        writeStationNETCDF4(stTemp, stSalt, stUvel, stVvel, stSSH, stTauX,
                            stTauY, stTime, grdMODEL.depth[0:deepest], lat,
                            lon, outfilename)
        station += 1
コード例 #3
0
def createAtmosFileUV(grdROMS, modelpath, atmospath, startdate, enddate,
                      useESMF, myformat, abbreviation, mytype, gridtype,
                      show_progress):

    # Setup
    print "\nGenerating atmospheric forcing for: %s to %s\n" % (startdate,
                                                                enddate)
    years = [(int(startdate.year) + kk)
             for kk in range(1 + int(enddate.year) - int(startdate.year))]

    if show_progress is True:
        import progressbar
        progress = progressbar.ProgressBar(
            widgets=[progressbar.Percentage(),
                     progressbar.Bar()],
            maxval=len(years)).start()

    # Create the objects for source and destination grids

    # Get the "Fraction of sfc area covered by ocean
    nor = atmospath + "NRCP45AERCN_f19_g16_CLE_02.cam2.h0.2006-01.nc"
    cdf = Dataset(nor, "r")
    OCENFRAC = cdf.variables["OCNFRAC"][:]
    cdf.close()
    Fill = -999.0

    grdMODEL = grd.grdClass(nor, mytype, mytype, useESMF, 'atmos')

    # Create the outputfile
    outfilename = abbreviation + '_windUV_' + str(mytype) + '_' + str(
        startdate.year) + '_to_' + str(enddate.year) + '.nc'
    IOatmos.createNetCDFFileUV(grdROMS, outfilename, myformat, mytype)

    # Setup ESMF for interpolation (calculates weights)
    print "  -> regridSrc2Dst at RHO points"
    grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid,
                                   "fieldSrc",
                                   staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.fieldDst_rho = ESMF.Field(grdROMS.esmfgrid,
                                       "fieldDst",
                                       staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.regridSrc2Dst_rho = ESMF.Regrid(
        grdMODEL.fieldSrc,
        grdMODEL.fieldDst_rho,
        regrid_method=ESMF.RegridMethod.BILINEAR)

    # Loop over each year and do the interpolations and write to file
    year = 2050
    month = 1
    day = 1
    if mytype == "NORESM":

        filename = getNORESMfilename(year, month, day, "TAUX", atmospath)
        cdf = Dataset(filename, "r")
        U10 = cdf.variables["U10"][:]
        TAUX = -(cdf.variables["TAUX"][:])
        TAUY = -(cdf.variables["TAUY"][:])

        magstr = np.sqrt(TAUX * TAUX + TAUY * TAUY)
        magstr = np.where(magstr < 1.e-8, 1.e-8, magstr)

        windE = (TAUX / magstr) * U10
        windN = (TAUY / magstr) * U10

        time_in = cdf.variables["time"][:]
        time_calendar = cdf.variables['time'].calendar
        time_units = cdf.variables['time'].units

        scru = np.zeros((len(time_in), np.shape(grdROMS.lat_rho)[0],
                         np.shape(grdROMS.lat_rho)[1]))
        scrv = np.zeros((len(time_in), np.shape(grdROMS.lat_rho)[0],
                         np.shape(grdROMS.lat_rho)[1]))

        # Loop over each time-step in current file
        for t in xrange(len(time_in)):
            currentdate = num2date(time_in[t],
                                   units=time_units,
                                   calendar=time_calendar)
            print "Interpolating date: ", currentdate

            # Eastward wind
            grdMODEL.fieldSrc[:, :] = np.flipud(
                np.rot90(np.squeeze(windE[t, :, :])))
            fieldE = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc,
                                                grdMODEL.fieldDst_rho)

            # Since ESMF uses coordinates (x,y) we need to rotate and flip to get back to (y,x) order.
            fieldE = np.fliplr(np.rot90(fieldE.data, 3))
            fieldE = laplaceFilter(fieldE, 1000, grdROMS.xi_rho,
                                   grdROMS.eta_rho)
            fieldE = fieldE * grdROMS.mask_rho

            # Northward wind
            grdMODEL.fieldSrc[:, :] = np.flipud(
                np.rot90(np.squeeze(windN[t, :, :])))
            fieldN = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc,
                                                grdMODEL.fieldDst_rho)

            fieldN = np.fliplr(np.rot90(fieldN.data, 3))
            fieldN = laplaceFilter(fieldN, 1000, grdROMS.xi_rho,
                                   grdROMS.eta_rho)
            fieldN = fieldN * grdROMS.mask_rho

            # Magnitude
            grdMODEL.fieldSrc[:, :] = np.flipud(
                np.rot90(np.squeeze(magstr[t, :, :])))
            magnitude = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc,
                                                   grdMODEL.fieldDst_rho)

            magnitude = np.fliplr(np.rot90(magnitude.data, 3))
            magnitude = laplaceFilter(magnitude, 1000, grdROMS.xi_rho,
                                      grdROMS.eta_rho)
            magnitude = magnitude * grdROMS.mask_rho

            import plotAtmos
            print "Interpolated range: ", np.min(magnitude), np.max(magnitude)
            print "Original range: ", np.min(magstr), np.max(magstr)

            grdROMS.time += 1
            print np.shape(windE), np.shape(grdMODEL.lon), np.shape(
                grdMODEL.lat)
            plotAtmos.contourMap(grdROMS, grdROMS.lon_rho, grdROMS.lat_rho,
                                 fieldE, fieldN, magnitude, 'wind', 'REGSCEN',
                                 currentdate)
            plotAtmos.contourMap(grdMODEL, grdMODEL.lon, grdMODEL.lat,
                                 np.squeeze(windE[t, :, :]),
                                 np.squeeze(windN[t, :, :]),
                                 np.squeeze(magstr[t, :, :]), 'wind', 'NORESM',
                                 currentdate)

            # Rotate to ROMS grid structure
            scru[t, :, :] = (fieldE * np.cos(grdROMS.angle)) + (
                fieldN * np.sin(grdROMS.angle))
            scrv[t, :, :] = (fieldN * np.cos(grdROMS.angle)) - (
                fieldE * np.sin(grdROMS.angle))
コード例 #4
0
def main():
    print '\n--------------------------\n'
    print 'Started ' + time.ctime(time.time())

    # EDIT ===================================================================
    # Set show_progress to "False" if you do not want to see the progress
    # indicator for horizontal interpolation.
    show_progress = True
    # Set compileAll to True if you want automatic re-compilation of all the
    # fortran files necessary to run model2roms. Options are "gfortran" or "ifort". Edit
    # compile.py to add other Fortran compilers.
    compileAll = False
    if compileAll is True:
        import compile
        compile.compileAll("ifort")

    # Extract time-series of data for given longitude/latitude
    extractStations = False
    # Define a set of longitude/latitude positions with names to extract into
    # station files (using extractStations)
    if (extractStations):
        stationNames = [
            'NorthSea', 'Iceland', 'EastandWestGreenland', 'Lofoten',
            'Georges Bank'
        ]
        lonlist = [2.4301, -22.6001, -47.0801, 13.3801, -67.2001]
        latlist = [54.5601, 63.7010, 60.4201, 67.5001, 41.6423]

    # Create the bry, init, and clim files for a given grid and input data
    createOceanForcing = True
    # Create atmospheric forcing for the given grid
    createAtmosForcing = False
    # Create a smaller resolution grid based on your original. Decimates every second for
    # each time run
    decimateGridfile = False
    # Write ice values to file (for Arctic regions)
    writeIce = True
    # Use ESMF for the interpolation. This requires that you have ESMF and ESMPy installed (import ESMF)
    useESMF = True
    # Apply filter to smooth the 2D fields after interpolation (time consuming)
    useFilter = True
    # Format to write the ouput to: 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_64BIT', or 'NETCDF3_CLASSIC'
    # Using NETCDF4 automatically turns on compression of files (ZLIB)
    myformat = 'NETCDF4'
    # Frequency of the input data: usually monthly
    timeFrequencyOfInputData = "month"  #, "month", "hour"

    # Subset input data. If you have global data you may want to seubset these to speed up reading. Make
    # sure that your input data are cartesian (0-360 or -180:180, -90:90)
    subsetIndata = False
    if subsetIndata:
        subset = defineSubsetForIndata()

    # Set the input data MODEL indatatype
    indatatype = 'SODA'
    indatatype = 'SODAMONTHLY'
    indatatype = 'WOAMONTHLY'
    indatatype = 'NORESM'
    indatatype = 'GLORYS'
    #indatatype = 'NS8KM'
    #indatatype = 'NS8KMZ'

    # GRIDTYPES ------------------------------------------------------------------------------
    # Define what grid type you wnat to interpolate to
    outgrid = "NS8KM"
    #outgrid = "REGSCEN"
    #outgrid = "KINO"
    #    outgrid= "AA10KM"

    # Define what grid type you wnat to interpolate from: Can be Z for SIGMA for ROMS
    # vertical coordinate system or ZLEVEL
    ingridtype = "SIGMA"
    ingridtype = "ZLEVEL"

    outgridtype = "ROMS"

    # PATH TO DATA ----------------------------------------------------------------------------
    # Define the path to the input data
    if indatatype == 'SODA':
        modelpath = "/Volumes/MacintoshHD2/Datasets/SODA/"

    if indatatype == 'SODAMONTHLY':
        modelpath = "/Volumes/MacintoshHD2/Datasets/SODAMonthly/"

    if indatatype == 'GLORYS':
        modelpath = "/Volumes/MacintoshHD2/Datasets/GLOBAL_REANALYSIS_PHYS_001_009/"
        modelpath = "/Users/trondkr/Projects/is4dvar/GLORYS2V3/"
        modelpath = "/work/shared/imr/NS8KM/FORCING/GLORYS2V3/ftp.myocean.mercator-ocean.fr/Core/GLOBAL_REANALYSIS_PHYS_001_009/"

    if indatatype == 'NORESM':
        modelpath = "/Users/trondkr/Projects/RegScen/NRCP45AERCN_f19_g16_CLE_01/"
        #modelpath = "/work/users/trondk/REGSCEN/NRCP45AERCN_f19_g16_CLE_01/"
        #modelpath = "/work/users/trondk/REGSCEN/N20TRAERCN/"
        if createAtmosForcing:
            atmospath = "/Users/trondkr/Projects/RegScen/model2roms/TESTFILES/"

    if indatatype == 'NS8KM':
        modelpath = "/Users/trondkr/Projects/is4dvar/grid2lonlat/RESULTS/"

    if indatatype == 'NS8KMZ':
        modelpath = "/Users/trondkr/Projects/NOWMAPS/delivery-COPERNICUS/"
        #modelpath = "/work/shared/imr/NS8KM/Z-LEVEL-ASSIMILATION2010-2013/"

    if indatatype == 'WOAMONTHLY':
        modelpath = "/Users/trondkr/Projects/is4dvar/createSSS/"

    # PATH TO GRID -----------------------------------------------------------------------------
    # Define the path to the grid file
    if outgrid == "NS8KM":
        romsgridpath = "/Users/trondkr/Projects/is4dvar/Grid/nordsjoen_8km_smoothed02022015.nc"
        romsgridpath = "/work/shared/imr/NS8KM/FORCING/GRID/nordsjoen_8km_grid_hmax20m_v4.nc"

    if outgrid == "KINO":
        romsgridpath = "/work/users/trondk/KINO/GRID/kino_1600m_19022016_vf20.nc"
        #romsgridpath = "/Users/trondkr/Projects/KINO/GRID/kino_1600m_07082015_vf20.nc"

    if outgrid == "REGSCEN":
        romsgridpath = "/Users/trondkr/Projects/RegScen/Grid/AA_10km_grid_noest.nc"
        #romsgridpath = "/Users/trondkr/Projects/is4dvar/Grid/nordsjoen_8km_grid_hmax20m_v3.nc"
        #romsgridpath = "/work/users/trondk/REGSCEN/GRID/AA_10km_grid_noest.nc"

    if outgrid == "GREENLAND":
        romsgridpath = "/Users/trondkr/Projects/RegScen/Grid/Sermilik_grid_4000m.nc"
        romsgridpath = "/Users/trondkr/Projects/RegScen/model2roms/polarlowr_grid.nc"

    if outgrid == "AA10KM":
        romsgridpath = "/work-common/shared/imr/AA10KM/GRID/AA_10km_grid.nc"

    if indatatype == 'WOAMONTHLY': isClimatology = True
    else: isClimatology = False

    # DETAILS -----------------------------------------------------------------------------------
    # Define the period to create forcing for
    start_year = 2013
    end_year = 2014
    start_month = 11
    end_month = 12
    start_day = 15
    end_day = 15

    if (int(calendar.monthrange(start_year, start_month)[1]) < start_day):
        start_day = int(calendar.monthrange(start_year, start_month)[1])

    if (int(calendar.monthrange(end_year, end_month)[1]) < end_day):
        end_day = int(calendar.monthrange(end_year, end_month)[1])

    startdate = datetime(start_year, start_month, start_day)
    enddate = datetime(end_year, end_month, end_day)
    years = [start_year + year for year in xrange(end_year + 1 - start_year)]

    # Define what and name of variables to include in the forcing files
    # -> myvars is the name model2roms uses to identify variables
    # -> varNames is the name of the variable found in the NetCDF input files
    if indatatype == 'SODA':
        myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
        varNames = ['TEMP', 'SALT', 'SSH', 'U', 'V']

    if indatatype == 'SODAMONTHLY':
        myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
        varNames = ['temp', 'salt', 'ssh', 'u', 'v']

    if indatatype == 'NS8KM':
        myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
        varNames = ['votemper', 'vosaline', 'zeta', 'vozocrtx', 'vomecrty']

    if indatatype == 'NS8KMZ':
        fileNameIn, readFromOneFile = model2roms.getNS8KMZfilename(
            startdate.year, startdate.month, startdate.day, "S", modelpath)

        myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']

        if (readFromOneFile):
            varNames = ['temp', 'salt', 'zeta', 'u_eastward', 'v_northward']
        else:
            varNames = ['votemper', 'vosaline', 'zeta', 'vozocrtx', 'vomecrty']

    if indatatype == 'GLORYS':
        if (writeIce):
            myvars = [
                'temperature', 'salinity', 'ssh', 'uvel', 'vvel', 'uice',
                'vice', 'aice', 'hice'
            ]
            varNames = [
                'votemper', 'vosaline', 'sossheig', 'vozocrtx', 'vomecrty',
                'iicevelu', 'iicevelv', 'ileadfra', 'iicethic'
            ]
        else:
            myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
            varNames = [
                'votemper', 'vosaline', 'sossheig', 'vozocrtx', 'vomecrty'
            ]

    if indatatype == 'WOAMONTHLY':
        myvars = ['temperature', 'salinity']
        varNames = ['t_an', 's_an']

    if indatatype == 'NORESM':
        myvars = [
            'temperature', 'salinity', 'ssh', 'uvel', 'vvel', 'ageice', 'uice',
            'vice', 'aice', 'hice', 'snow_thick'
        ]
        varNames = [
            'templvl', 'salnlvl', 'sealv', 'uvellvl', 'vvellvl', 'iage',
            'uvel', 'vvel', 'aice', 'hi', 'hs'
        ]

    # NO EDIT BELOW ====================================================================================================
    abbreviation = defineAbbreviation(outgrid)
    climName, initName, bryName = defineOutputFilenames(
        abbreviation, start_year, end_year, start_month, end_month, start_day,
        end_day, indatatype)

    if isClimatology is True:
        climName = abbreviation + '_' + str(indatatype) + '_climatology.nc'

    # Create the grid object for the output grid
    grdROMS = grd.grdClass(romsgridpath, "ROMS", outgridtype, useESMF, 'ocean',
                           outgrid)
    grdROMS.vars = myvars

    if (useESMF):
        import ESMF
        manager = ESMF.Manager(logkind=ESMF.LogKind.MULTI, debug=True)

    if createOceanForcing:

        showInfo(myvars, romsgridpath, climName, initName, bryName, start_year,
                 end_year, isClimatology, useESMF, myformat)

        model2roms.convertMODEL2ROMS(years,
                                     startdate,
                                     enddate,
                                     timeFrequencyOfInputData,
                                     climName,
                                     initName,
                                     modelpath,
                                     romsgridpath,
                                     myvars,
                                     varNames,
                                     show_progress,
                                     indatatype,
                                     outgridtype,
                                     isClimatology,
                                     writeIce,
                                     useESMF,
                                     useFilter,
                                     myformat,
                                     subsetIndata,
                                     outgrid,
                                     subset=None)

        clim2bry.writeBry(grdROMS, start_year, bryName, climName, writeIce,
                          indatatype, myformat)

    if createAtmosForcing:
        atmosForcing.createAtmosFileUV(grdROMS, modelpath, atmospath,
                                       startdate, enddate, useESMF, myformat,
                                       abbreviation, indatatype, gridtype,
                                       show_progress)

    #if decimateGridfile:
    #decimateGrid.createGrid(grdROMS, "/Users/trondkr/Projects/KINO/GRID/kino_1600m_18072015.nc", "/Users/trondkr/Projects/KINO/GRID/kino_1600m_18072015v2.nc", 2)

    if extractStations:
        print "Running in station mode and extracting pre-defined station locations"
        IOstation.getStationData(years, IDS, modelpath, latlist, lonlist,
                                 stationNames)

    print 'Finished ' + time.ctime(time.time())
コード例 #5
0
ファイル: atmosForcing.py プロジェクト: raytl/model2roms
def createAtmosFileUV(grdROMS,modelpath,atmospath,startdate,enddate,useESMF,myformat,abbreviation,mytype,gridtype,show_progress):

    # Setup 
    print "\nGenerating atmospheric forcing for: %s to %s\n"%(startdate, enddate)
    years = [(int(startdate.year) + kk) for kk in range(1 + int(enddate.year) - int(startdate.year))]
   
    if show_progress is True:
        import progressbar
        progress = progressbar.ProgressBar(widgets=[progressbar.Percentage(), progressbar.Bar()], maxval=len(years)).start()
    
    # Create the objects for source and destination grids
   
    # Get the "Fraction of sfc area covered by ocean
    nor = atmospath + "NRCP45AERCN_f19_g16_CLE_02.cam2.h0.2006-01.nc"
    cdf = Dataset(nor,"r")
    OCENFRAC = cdf.variables["OCNFRAC"][:]
    cdf.close()
    Fill = -999.0

    grdMODEL = grd.grdClass(nor, mytype, mytype, useESMF,'atmos')
    
    # Create the outputfile
    outfilename=  abbreviation + '_windUV_' + str(mytype) + '_' + str(startdate.year) + '_to_' + str(enddate.year) + '.nc'
    IOatmos.createNetCDFFileUV(grdROMS, outfilename, myformat, mytype)
    
    # Setup ESMF for interpolation (calculates weights)
    print "  -> regridSrc2Dst at RHO points"
    grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.fieldDst_rho = ESMF.Field(grdROMS.esmfgrid, "fieldDst", staggerloc=ESMF.StaggerLoc.CENTER)
    grdMODEL.regridSrc2Dst_rho = ESMF.Regrid(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho, regrid_method=ESMF.RegridMethod.BILINEAR)

    # Loop over each year and do the interpolations and write to file
    year=2050; month=1; day=1
    if mytype == "NORESM":
        
        filename = getNORESMfilename(year,month,day,"TAUX",atmospath)
        cdf = Dataset(filename,"r")
        U10 = cdf.variables["U10"][:]
        TAUX = -(cdf.variables["TAUX"][:])
        TAUY = -(cdf.variables["TAUY"][:])

        magstr = np.sqrt(TAUX*TAUX + TAUY*TAUY)
        magstr = np.where(magstr < 1.e-8,1.e-8,magstr)

        windE = (TAUX/magstr)*U10
        windN = (TAUY/magstr)*U10
        
        time_in = cdf.variables["time"][:]
        time_calendar = cdf.variables['time'].calendar
        time_units = cdf.variables['time'].units
       
        scru = np.zeros((len(time_in),np.shape(grdROMS.lat_rho)[0],np.shape(grdROMS.lat_rho)[1]))
        scrv = np.zeros((len(time_in),np.shape(grdROMS.lat_rho)[0],np.shape(grdROMS.lat_rho)[1]))
  
        # Loop over each time-step in current file
        for t in xrange(len(time_in)):
            currentdate=num2date(time_in[t], units=time_units,calendar=time_calendar)
            print "Interpolating date: ",currentdate
            
            # Eastward wind
            grdMODEL.fieldSrc[:,:]=np.flipud(np.rot90(np.squeeze(windE[t,:,:])))
            fieldE = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho)

            # Since ESMF uses coordinates (x,y) we need to rotate and flip to get back to (y,x) order.
            fieldE = np.fliplr(np.rot90(fieldE.data,3))
            fieldE = laplaceFilter(fieldE, 1000, grdROMS.xi_rho, grdROMS.eta_rho)
            fieldE = fieldE*grdROMS.mask_rho
           

            # Northward wind
            grdMODEL.fieldSrc[:,:]=np.flipud(np.rot90(np.squeeze(windN[t,:,:])))
            fieldN = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho)

            fieldN = np.fliplr(np.rot90(fieldN.data,3))
            fieldN = laplaceFilter(fieldN, 1000, grdROMS.xi_rho, grdROMS.eta_rho)
            fieldN = fieldN*grdROMS.mask_rho
        
            # Magnitude
            grdMODEL.fieldSrc[:,:]=np.flipud(np.rot90(np.squeeze(magstr[t,:,:])))
            magnitude = grdMODEL.regridSrc2Dst_rho(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho)

            magnitude = np.fliplr(np.rot90(magnitude.data,3))
            magnitude = laplaceFilter(magnitude, 1000, grdROMS.xi_rho, grdROMS.eta_rho)
            magnitude = magnitude*grdROMS.mask_rho
           
            import plotAtmos
            print "Interpolated range: ", np.min(magnitude), np.max(magnitude)
            print "Original range: ", np.min(magstr), np.max(magstr)
            
            grdROMS.time+=1
            print np.shape(windE), np.shape(grdMODEL.lon), np.shape(grdMODEL.lat)
            plotAtmos.contourMap(grdROMS, grdROMS.lon_rho, grdROMS.lat_rho, fieldE, fieldN, magnitude, 
                'wind','REGSCEN',currentdate)
            plotAtmos.contourMap(grdMODEL, 
                grdMODEL.lon, 
                grdMODEL.lat, 
                np.squeeze(windE[t,:,:]), 
                np.squeeze(windN[t,:,:]), 
                np.squeeze(magstr[t,:,:]), 
                'wind','NORESM',currentdate)

            # Rotate to ROMS grid structure
            scru[t,:,:]=(fieldE*np.cos(grdROMS.angle)) + (fieldN*np.sin(grdROMS.angle))
            scrv[t,:,:]=(fieldN*np.cos(grdROMS.angle)) - (fieldE*np.sin(grdROMS.angle))
コード例 #6
0
ファイル: main.py プロジェクト: raytl/model2roms
def main():
    print '\n--------------------------\n'
    print 'Started ' + time.ctime(time.time())

    # EDIT ===================================================================
    # Set show_progress to "False" if you do not want to see the progress
    # indicator for horizontal interpolation.
    show_progress = True
    # Set compileAll to True if you want automatic re-compilation of all the
    # fortran files necessary to run model2roms. Options are "gfortran" or "ifort". Edit
    # compile.py to add other Fortran compilers.
    compileAll = False
    if compileAll is True:
        import compile; compile.compileAll("ifort")

    # Extract time-series of data for given longitude/latitude
    extractStations = False
     # Define a set of longitude/latitude positions with names to extract into
    # station files (using extractStations)
    if (extractStations):
        stationNames = ['NorthSea', 'Iceland', 'EastandWestGreenland', 'Lofoten', 'Georges Bank']
        lonlist = [2.4301, -22.6001, -47.0801, 13.3801, -67.2001]
        latlist = [54.5601, 63.7010, 60.4201, 67.5001, 41.6423]

    # Create the bry, init, and clim files for a given grid and input data
    createOceanForcing = True
    # Create atmospheric forcing for the given grid
    createAtmosForcing = False
    # Create a smaller resolution grid based on your original. Decimates every second for
    # each time run
    decimateGridfile = False
        # Write ice values to file (for Arctic regions)
    writeIce = False
    # Use ESMF for the interpolation. This requires that you have ESMF and ESMPy installed (import ESMF)
    useESMF = True
    # Apply filter to smooth the 2D fields after interpolation (time consuming)
    useFilter = True
    # Format to write the ouput to: 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_64BIT', or 'NETCDF3_CLASSIC'
    # Using NETCDF4 automatically turns on compression of files (ZLIB)
    myformat='NETCDF4'
    # Frequency of the input data: usually monthly 
    timeFrequencyOfInputData = "day" #, "month", "hour"

    # Subset input data. If you have global data you may want to seubset these to speed up reading. Make 
    # sure that your input data are cartesian (0-360 or -180:180, -90:90)
    subsetIndata = False
    if subsetIndata:
        subset = defineSubsetForIndata()

    # Set the input data MODEL indatatype
    indatatype = 'SODA'
    indatatype = 'SODAMONTHLY'
    indatatype = 'WOAMONTHLY'
    indatatype = 'NORESM'
    indatatype = 'GLORYS'
    #indatatype = 'NS8KM'
    #indatatype = 'NS8KMZ'

    # GRIDTYPES ------------------------------------------------------------------------------
    # Define what grid type you wnat to interpolate to 
    outgrid  = "NS8KM"
    #outgrid = "REGSCEN"
    #outgrid = "KINO"

    # Define what grid type you wnat to interpolate from: Can be Z for SIGMA for ROMS
    # vertical coordinate system or ZLEVEL
    ingridtype = "SIGMA"
    ingridtype = "ZLEVEL"

    outgridtype="ROMS"

    # PATH TO DATA ----------------------------------------------------------------------------
    # Define the path to the input data 
    if indatatype == 'SODA':
        modelpath = "/Volumes/MacintoshHD2/Datasets/SODA/"
    
    if indatatype == 'SODAMONTHLY':
        modelpath = "/Volumes/MacintoshHD2/Datasets/SODAMonthly/"
    
    if indatatype == 'GLORYS':
        modelpath = "/Volumes/MacintoshHD2/Datasets/GLOBAL_REANALYSIS_PHYS_001_009/"
        modelpath = "/Users/trondkr/Projects/is4dvar/GLORYS2V3/"
        modelpath = "/work/shared/imr/NS8KM/FORCING/GLORYS2V3/ftp.myocean.mercator-ocean.fr/Core/GLOBAL_REANALYSIS_PHYS_001_009/"
        
    if indatatype == 'NORESM':
        modelpath = "/Users/trondkr/Projects/RegScen/NRCP45AERCN_f19_g16_CLE_01/"
        #modelpath = "/work/users/trondk/REGSCEN/NRCP45AERCN_f19_g16_CLE_01/"
        #modelpath = "/work/users/trondk/REGSCEN/N20TRAERCN/"
        if createAtmosForcing:
            atmospath = "/Users/trondkr/Projects/RegScen/model2roms/TESTFILES/"

    if indatatype == 'NS8KM':
        modelpath = "/Users/trondkr/Projects/is4dvar/grid2lonlat/RESULTS/"
    
    if indatatype == 'NS8KMZ':
        modelpath = "/Users/trondkr/Dropbox/deliveryFOFINAL/"
        modelpath = "/work/shared/imr/NS8KM/deliveryFO/FINAL/"

    if indatatype == 'WOAMONTHLY':
        modelpath = "/Users/trondkr/Projects/is4dvar/createSSS/"


    # PATH TO GRID -----------------------------------------------------------------------------
    # Define the path to the grid file 
    if outgrid == "NS8KM":
        romsgridpath = "/Users/trondkr/Projects/is4dvar/Grid/nordsjoen_8km_smoothed02022015.nc"
        romsgridpath = "/work/users/trondk/NS8km/FORCING/GRID/nordsjoen_8km_grid_hmax20m_v3.nc"

    if outgrid == "KINO":
        romsgridpath = "/work/users/trondk/KINO/GRID/kino_1600m_07082015_vf20.nc"
        #romsgridpath = "/Users/trondkr/Projects/KINO/GRID/kino_1600m_07082015_vf20.nc"

    if outgrid == "REGSCEN":
        romsgridpath = "/Users/trondkr/Projects/RegScen/Grid/AA_10km_grid_noest.nc"
        #romsgridpath = "/Users/trondkr/Projects/is4dvar/Grid/nordsjoen_8km_grid_hmax20m_v3.nc"
        #romsgridpath = "/work/users/trondk/REGSCEN/GRID/AA_10km_grid_noest.nc"

    if outgrid == "GREENLAND":
        romsgridpath="/Users/trondkr/Projects/RegScen/Grid/Sermilik_grid_4000m.nc"
        romsgridpath="/Users/trondkr/Projects/RegScen/model2roms/polarlowr_grid.nc"
    if indatatype == 'WOAMONTHLY': isClimatology = True
    else: isClimatology = False

    # DETAILS -----------------------------------------------------------------------------------
    # Define the period to create forcing for
    start_year  = 2012
    end_year    = 2013
    start_month = 11
    end_month   = 12
    start_day   = 15
    end_day     = 15

    if (int(calendar.monthrange(start_year, start_month)[1]) < start_day):
        start_day = int(calendar.monthrange(start_year, start_month)[1])

    if (int(calendar.monthrange(end_year, end_month)[1]) < end_day):
        end_day = int(calendar.monthrange(end_year, end_month)[1])

    startdate = datetime(start_year, start_month, start_day)
    enddate   = datetime(end_year, end_month, end_day)
    years = [start_year+year for year in xrange(end_year+1-start_year)]
   
    # Define what and name of variables to include in the forcing files
    # -> myvars is the name model2roms uses to identify variables
    # -> varNames is the name of the variable found in the NetCDF input files
    if indatatype == 'SODA':
       myvars = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
       varNames = ['TEMP', 'SALT', 'SSH', 'U', 'V']

    if indatatype == 'SODAMONTHLY':
        myvars   = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
        varNames = ['temp', 'salt', 'ssh', 'u', 'v']

    if indatatype ==  'NS8KM':
        myvars   = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
        varNames = ['votemper', 'vosaline', 'zeta', 'vozocrtx', 'vomecrty']
    
    if indatatype ==  'NS8KMZ':
        fileNameIn, readFromOneFile = model2roms.getNS8KMZfilename(startdate.year, startdate.month, startdate.day, "S", modelpath)
        
        myvars   = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']

        if (readFromOneFile):
             varNames = ['temp', 'salt', 'zeta', 'u_eastward', 'v_northward']
        else:
             varNames = ['votemper', 'vosaline', 'zeta', 'vozocrtx', 'vomecrty']

    if indatatype == 'GLORYS':
        if (writeIce):
            myvars   = ['temperature','salinity', 'ssh', 'uvel', 'vvel','uice','vice','aice','hice']
            varNames = ['votemper', 'vosaline', 'sossheig', 'vozocrtx', 'vomecrty','iicevelu', 'iicevelv', 'ileadfra', 'iicethic']
        else:
            myvars   = ['temperature', 'salinity', 'ssh', 'uvel', 'vvel']
            varNames = ['votemper', 'vosaline', 'sossheig', 'vozocrtx', 'vomecrty']

    if indatatype == 'WOAMONTHLY':
        myvars   = ['temperature','salinity']
        varNames = ['t_an', 's_an']

    if indatatype == 'NORESM':
        myvars   = ['temperature','salinity', 'ssh', 'uvel', 'vvel','ageice','uice','vice','aice','hice','snow_thick']
        varNames = ['templvl','salnlvl','sealv', 'uvellvl', 'vvellvl','iage', 'uvel', 'vvel', 'aice', 'hi', 'hs']


    # NO EDIT BELOW ====================================================================================================
    abbreviation = defineAbbreviation(outgrid)
    climName,initName,bryName = defineOutputFilenames(abbreviation,start_year,end_year,start_month,end_month,start_day,end_day,indatatype)

    if isClimatology is True:
        climName=abbreviation + '_' + str(indatatype) + '_climatology.nc'  

    # Create the grid object for the output grid
    grdROMS = grd.grdClass(romsgridpath, "ROMS", outgridtype, useESMF,'ocean', outgrid)
    grdROMS.vars=myvars

    if (useESMF):
        import ESMF
        manager = ESMF.Manager(logkind = ESMF.LogKind.MULTI, debug = True)

    if createOceanForcing:

        showInfo(myvars, romsgridpath, climName, initName, bryName, start_year, end_year, isClimatology, useESMF, myformat)

        model2roms.convertMODEL2ROMS(years, startdate, enddate, timeFrequencyOfInputData, climName, initName, modelpath, romsgridpath, myvars, varNames, show_progress,
                                         indatatype, outgridtype, isClimatology, writeIce, useESMF, useFilter, myformat, subsetIndata, outgrid, subset=None)

        clim2bry.writeBry(grdROMS, start_year, bryName, climName, writeIce, indatatype, myformat)

    if createAtmosForcing:
        atmosForcing.createAtmosFileUV(grdROMS,modelpath,atmospath,startdate,enddate,useESMF,
            myformat,abbreviation,indatatype,gridtype,show_progress)

    #if decimateGridfile:
        #decimateGrid.createGrid(grdROMS, "/Users/trondkr/Projects/KINO/GRID/kino_1600m_18072015.nc", "/Users/trondkr/Projects/KINO/GRID/kino_1600m_18072015v2.nc", 2)

    if extractStations:
        print "Running in station mode and extracting pre-defined station locations"
        IOstation.getStationData(years, IDS, modelpath, latlist, lonlist, stationNames)

    print 'Finished ' + time.ctime(time.time())
コード例 #7
0
ファイル: model2roms.py プロジェクト: raytl/model2roms
def convertMODEL2ROMS(years, startdate, enddate, timeFrequencyOfInputData, climName, initName, dataPath, romsgridpath, myvars, varNames, show_progress, indatatype, gridtype,
                      isClimatology, writeIce, useESMF, useFilter, myformat, subsetIndata, outgrid, subset):
    if useESMF: 
        print "Turning on debugging for ESMF"
        ESMF.Manager(logkind=ESMF.LogKind.MULTI, debug=True)
    
    # First opening of input file is just for initialization of grid
    if indatatype == 'SODA':
        fileNameIn = getSODAfilename(startdate.year, startdate.month, startdate.day, "salinity", dataPath)
    if indatatype == 'SODAMONTHLY':
        fileNameIn = getSODAfilename(startdate.year, startdate.month, startdate.day, "salinity", dataPath)
    if indatatype == 'NORESM':
        fileNameIn = getNORESMfilename(startdate.year, startdate.month, startdate.day, "grid", dataPath)
    if indatatype == 'WOAMONTHLY':
        fileNameIn = getWOAMONTHLYfilename(startdate.year, startdate.month, startdate.day, "temperature", dataPath)
    if indatatype == 'GLORYS':
        fileNameIn = getGLORYSfilename(startdate.year, startdate.month, startdate.day, "S", dataPath)
    if indatatype == 'GLORYS':
        fileNameIn = getGLORYSfilename(startdate.year, startdate.month, startdate.day, "S", dataPath)
    if indatatype == 'NS8KM':
        fileNameIn = getNS8KMfilename(startdate.year, startdate.month, startdate.day, "S", dataPath)
    if indatatype == 'NS8KMZ':
        fileNameIn, readFromOneFile = getNS8KMZfilename(startdate.year, startdate.month, startdate.day, "S", dataPath)
     
    # First time in loop, get the essential old grid information
    # MODEL data already at Z-levels. No need to interpolate to fixed depths,
    # but we use the one we have

    grdMODEL = grd.grdClass(fileNameIn, indatatype, indatatype, useESMF,"ocean", outgrid)
    grdROMS = grd.grdClass(romsgridpath, "ROMS", gridtype, useESMF,"ocean", outgrid)
    grdROMS.myvars = myvars
    if (useESMF):
        print "\nCreating the interpolation weights and indexes using ESMF (this may take some time....):"

        print "  -> regridSrc2Dst at RHO points"
        grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.fieldDst_rho = ESMF.Field(grdROMS.esmfgrid, "fieldDst", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.regridSrc2Dst_rho = ESMF.Regrid(grdMODEL.fieldSrc, grdMODEL.fieldDst_rho, regrid_method=ESMF.RegridMethod.BILINEAR, unmapped_action=ESMF.UnmappedAction.IGNORE)
      
        print "  -> regridSrc2Dst at U points"
        grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.fieldDst_u = ESMF.Field(grdROMS.esmfgrid_u, "fieldDst_u", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.regridSrc2Dst_u = ESMF.Regrid(grdMODEL.fieldSrc, grdMODEL.fieldDst_u, regrid_method=ESMF.RegridMethod.BILINEAR, unmapped_action=ESMF.UnmappedAction.IGNORE)

        print "  -> regridSrc2Dst at V points"
        grdMODEL.fieldSrc = ESMF.Field(grdMODEL.esmfgrid, "fieldSrc", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.fieldDst_v = ESMF.Field(grdROMS.esmfgrid_v, "fieldDst_v", staggerloc=ESMF.StaggerLoc.CENTER)
        grdMODEL.regridSrc2Dst_v = ESMF.Regrid(grdMODEL.fieldSrc, 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 subsetIndata:
        IOsubset.findSubsetIndices(grdMODEL, min_lat=subset[0], max_lat=subset[1], min_lon=subset[2], max_lon=subset[3])

    print 'Initializing done'
    print '\n--------------------------'
    time = 0; firstRun = True

    for year in years:
        months = datetimeFunctions.createListOfMonths(year,startdate,enddate,isClimatology)
        
        for month in months:
            days = datetimeFunctions.createListOfDays(year,month,startdate,enddate,isClimatology,timeFrequencyOfInputData)

            for day in days:
                # Get the current date for given timestep 
                getTime(dataPath, indatatype, grdROMS, grdMODEL, year, month, day, time, firstRun)
               
                # 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 is True:
                    print "NOTE! Make sure that these two arrays are in sequential order:"
                    print "myvars:     %s" % (myvars)
                    print "varnames: %s\n" % (varNames)
                    firstRun = False

                    if 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(grdMODEL, grdROMS)

                for myvar in myvars:

                    if myvar in ['temperature', 'salinity', 'uvel', 'vvel']:
                        data = get3Ddata(grdROMS, grdMODEL, myvar, indatatype, year, month, day, varNames, dataPath)

                    if myvar in ['ssh', 'ageice', 'uice', 'vice', 'aice', 'hice','snow_thick']:
                        data = get2Ddata(grdROMS, grdMODEL, myvar, indatatype, year, month, day, varNames, dataPath)

                    # Take the input data and horizontally interpolate to your grid
                    array1 = HorizontalInterpolation(myvar, grdROMS, grdMODEL, data, show_progress, useFilter)
                    if myvar in ['temperature', 'salinity']:
                        STdata = VerticalInterpolation(myvar, array1, array1, grdROMS, grdMODEL)
                        print "Data range of %s after interpolation: %3.3f to %3.3f" % (
                            myvar, STdata.min(), STdata.max())

                        for dd in xrange(len(STdata[:,0,0])):
                            STdata[dd,:,:] = np.where(grdROMS.mask_rho == 0, grdROMS.fill_value, STdata[dd,:,:])

                        STdata = np.where(abs(STdata) > 1000, grdROMS.fill_value, STdata)

                        IOwrite.writeClimFile(grdROMS, time, climName, myvar, isClimatology, writeIce, indatatype, myformat, STdata)
                        if time == grdROMS.initTime and grdROMS.write_init is True:
                            IOinitial.createInitFile(grdROMS, time, initName, myvar, writeIce, indatatype, myformat, STdata)

                    if myvar in ['ssh', 'ageice', 'aice', 'hice', 'snow_thick']:
                        SSHdata = array1[0, :, :]

                        SSHdata = np.where(grdROMS.mask_rho == 0, grdROMS.fill_value, SSHdata)
                        SSHdata = np.where(abs(SSHdata) > 100, grdROMS.fill_value, SSHdata)
                        SSHdata = np.where(abs(SSHdata) == 0, grdROMS.fill_value, SSHdata)

                        # Specific for ROMs. We set 0 where we should have fillvalue for ice otherwise ROMS blows up.
                        SSHdata = np.where(abs(SSHdata) == grdROMS.fill_value, 0, SSHdata)
                       # SSHdata = np.ma.masked_where(abs(SSHdata) > 100, SSHdata)

                       # print "Data range of %s after interpolation: %3.3f to %3.3f" % (
                       #     myvar, SSHdata.min(), SSHdata.max())

                        IOwrite.writeClimFile(grdROMS, time, climName, myvar, isClimatology, writeIce, indatatype,myformat, SSHdata)
                        if time == grdROMS.initTime:
                            IOinitial.createInitFile(grdROMS, time, initName, myvar, writeIce, indatatype, myformat,  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=grdROMS.mask_u
                        if  myvar=="vice":mymask=grdROMS.mask_v

                        SSHdata = np.where(mymask == 0, grdROMS.fill_value, SSHdata)
                        SSHdata = np.where(abs(SSHdata) > 100, grdROMS.fill_value, SSHdata)
                        SSHdata = np.where(abs(SSHdata) == 0, grdROMS.fill_value, SSHdata)
                        SSHdata = np.where(abs(SSHdata) == grdROMS.fill_value, 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(grdROMS, time, climName, myvar, isClimatology, writeIce, indatatype, myformat, SSHdata)

                        if time == grdROMS.initTime:
                            if myvar == 'uice':
                                IOinitial.createInitFile(grdROMS, time, initName, 'uice', writeIce, indatatype, myformat,  SSHdata)
                            if myvar == 'vice':
                                IOinitial.createInitFile(grdROMS, time, initName, 'vice', writeIce, indatatype, myformat, SSHdata)

                    if myvar == 'uvel':
                        array2 = array1

                    if myvar == 'vvel':
                        indexROMS_UBAR = (grdROMS.eta_u, grdROMS.xi_u)
                        indexROMS_VBAR = (grdROMS.eta_v, grdROMS.xi_v)
                        UBARdata = np.zeros((indexROMS_UBAR), dtype=np.float64)
                        VBARdata = np.zeros((indexROMS_VBAR), dtype=np.float64)

                        urot, vrot = rotate(grdROMS, grdMODEL, data, array2, array1)

                        u, v = interpolate2UV(grdROMS, grdMODEL, urot, vrot)

                        Udata, Vdata, UBARdata, VBARdata = VerticalInterpolation(myvar, u, v, grdROMS, 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(grdROMS.mask_u == 0, grdROMS.fill_value, Udata)
                        Udata = np.where(abs(Udata) > 1000, grdROMS.fill_value, Udata)
                        Vdata = np.where(grdROMS.mask_v == 0, grdROMS.fill_value, Vdata)
                        Vdata = np.where(abs(Vdata) > 1000, grdROMS.fill_value, Vdata)
                        UBARdata = np.where(grdROMS.mask_u == 0, grdROMS.fill_value, UBARdata)
                        UBARdata = np.where(abs(UBARdata) > 1000, grdROMS.fill_value, UBARdata)
                        VBARdata = np.where(grdROMS.mask_v == 0, grdROMS.fill_value, VBARdata)
                        VBARdata = np.where(abs(VBARdata) > 1000, grdROMS.fill_value, VBARdata)

                        IOwrite.writeClimFile(grdROMS, time, climName, myvar, isClimatology, writeIce, indatatype, myformat, Udata, Vdata,
                                              UBARdata, VBARdata)
                        if time == grdROMS.initTime:
                            # We print time=initTime to init file so that we have values for ubar and vbar (not present at time=1)
                            IOinitial.createInitFile(grdROMS, time, initName, myvar, writeIce, indatatype, myformat, Udata, Vdata, UBARdata, VBARdata)

                time += 1
コード例 #8
0
ファイル: IOstation.py プロジェクト: raytl/model2roms
def getStationData(years,IDS,sodapath,latlist,lonlist,stationNames):

    fileNameIn=sodapath+'SODA_2.0.2_'+str(years[0])+'_'+str(IDS[0])+'.cdf'
    
    """First time in loop, get the essential old grid information"""
    """SODA data already at Z-levels. No need to interpolate to fixed depths, but we use the one we have"""
    
    grdMODEL = grd.grdClass(fileNameIn,"SODA")
    IOverticalGrid.get_z_levels(grdMODEL)
    
    station=0
    numberOfPoints=4
   
    for lat, lon in zip(latlist, lonlist):
        print '\n----------------NEW STATION==> %s ------------------------------------------'%(stationNames[station])
     
        """Now we want to find the indices for our longitude, latitude station pairs in the lat-long list"""
        gridIndexes, dis = getStationIndices(grdMODEL,lon,lat,'SODA',numberOfPoints)
    
        stTime=[]; stDate=[]; time=0; counter=0; t=0
        total=float(len(years)*len(IDS))
        import progressbar
        progress = progressbar.ProgressBar(widgets=[Percentage(), Bar()], maxval=total).start()

        for year in years:
            for ID in IDS:
                file="SODA_2.0.2_"+str(year)+"_"+str(ID)+".cdf"
                filename=sodapath+file
               
                jdsoda, yyyymmdd, message = getStationTime(grdMODEL,year,ID)
                
                stTime.append(jdsoda)
                stDate.append(yyyymmdd)
               
                cdf = Dataset(filename,'r')
 
                """Each SODA file consist only of one time step. Get the subset data selected, and
                store that time step in a new array:"""
                if year==years[0] and ID==IDS[0]:
                    validIndex, validDis = testValidStation(cdf,dis,numberOfPoints, gridIndexes)
                    deepest              = testValidDepth(cdf,numberOfPoints, gridIndexes,grdMODEL.depth)
                    stTemp, stSalt, stSSH, stUvel, stVvel, stTauX, stTauY = initArrays(years,IDS,deepest,stationNames[station],lon,lat)
                
                for i in validIndex:
                    wgt=float(validDis[i])/sum(validDis)
                    latindex=int(gridIndexes[i][0])
                    lonindex=int(gridIndexes[i][1])
                    
                    """The values at a station is calculated by interpolating from the
                    numberOfPoints around the station uysing weights (wgt)
                    """
                    stTemp[time,:] = stTemp[time,:] + (cdf.variables["TEMP"][t,0:deepest,latindex,lonindex])*wgt
                    stSalt[time,:] = stSalt[time,:] + (cdf.variables["SALT"][t,0:deepest,latindex,lonindex])*wgt
                    stSSH[time]    = stSSH[time]    + (cdf.variables["SSH"][t,latindex,lonindex])*wgt
                    stTauX[time]    = stTauX[time]    + (cdf.variables["TAUX"][t,latindex,lonindex])*wgt
                    stTauY[time]    = stTauY[time]    + (cdf.variables["TAUY"][t,latindex,lonindex])*wgt
                    stUvel[time,:] = stUvel[time,:] + (cdf.variables["U"][t,0:deepest,latindex,lonindex])*wgt
                    stVvel[time,:] = stVvel[time,:] + (cdf.variables["V"][t,0:deepest,latindex,lonindex])*wgt
                 
                cdf.close()
                counter+=1

                progress.update(time)
                time+=1

        
        print 'Total time steps saved to file %s for station %s'%(time,station)
        #plotData.contourStationData(stTemp,stTime,stDate,-grdMODEL.depth[0:deepest],stationNames[station])
        
        outfilename='station_'+str(stationNames[station])+'.nc'
        print 'Results saved to file %s'%(outfilename)
        writeStationNETCDF4(stTemp,stSalt,stUvel,stVvel,stSSH,stTauX,stTauY,stTime,
                            grdMODEL.depth[0:deepest],lat,lon,outfilename)
        station+=1