Example #1
0
 def process_radsw_file(self):
     ''' Create shortwave radiation file '''
     nframes   = self.nframes / self.nframes_per_day # daily file
     radsw_tmp = np.empty((self.ny,self.nx))
     radsw_out = np.empty((nframes,self.ny,self.nx))
     # open file
     fid_radsw = ioncdf.opennc(self.file_radsw)
     # read coordinates and time
     lon  = ioncdf.readnc(fid_radsw,'lon')
     lat  = ioncdf.readnc(fid_radsw,'lat')
     time = ioncdf.readnc(fid_radsw,self.name_time_radsw)
     # open radsw factor
     fid_factor = ioncdf.opennc(self.shortwave_factor)
     radsw_factor = ioncdf.readnc(fid_factor,'radsw')
     ioncdf.closenc(fid_factor)
     # flip upside down for ROMS
     if self.target_model == 'ROMS':
         radsw_factor = radsw_factor[::-1,:]
     # run the computation
     for kt in np.arange(0,nframes):
         radsw_tmp[:,:]    = ioncdf.readnc_oneframe(fid_radsw,self.name_radsw,kt)
         radsw_out[kt,:,:] = (radsw_tmp[:,:] * radsw_factor[:,:]) * self.lsm[:,:]
         if self.drown:
             radsw_out[kt,:,:] = self.drown_wrapper(radsw_out[kt,:,:])
     # output file informations
     my_dict = {}
     my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
     my_dict['varname']        = self.name_radsw
     my_dict['time_dim']       = self.name_time_radsw
     my_dict['time_var']       = self.name_time_radsw
     my_dict['long name']      = 'Shortwave radiation'
     my_dict['units']          = 'W.m-2'
     my_dict['spval']          = self.spval
     my_dict['reftime']        = self.reftime
     my_dict['var_valid_min']  = radsw_out.min()
     my_dict['var_valid_max']  = radsw_out.max()
     my_dict['time_valid_min'] = time.min()
     my_dict['time_valid_max'] = time.max()
     my_dict['fileout']        = self.processed_nc_dir + \
                                 self.drownstring + self.name_radsw + '_' + self.dataset + '_' + str(self.year) + '.nc'
     if self.target_model == 'ROMS':
         model_dependent = {'description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
     elif self.target_model == 'NEMO':
         model_dependent = {'description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
     my_dict.update(model_dependent)
     # close input file and write output
     ioncdf.closenc(fid_radsw)
     ioncdf.write_ncfile(lon,lat,time,radsw_out,my_dict)
     radsw_tmp = None ; radsw_out = None
     return None
Example #2
0
 def process_u10_file(self):
     ''' Create zonal wind file '''
     u10_tmp = np.empty((self.ny,self.nx))
     u10_out = np.empty((self.nframes,self.ny,self.nx))
     # open input file
     fid_u10 = ioncdf.opennc(self.file_u10)
     # read coordinates and time
     lon  = ioncdf.readnc(fid_u10,'lon')
     lat  = ioncdf.readnc(fid_u10,'lat')
     time = ioncdf.readnc(fid_u10,self.name_time_u10)
     # open background velocity
     fid_bgd = ioncdf.opennc(self.u10_background)
     u10_background = ioncdf.readnc(fid_bgd,'u10')
     ioncdf.closenc(fid_bgd)
     # flip upside down for ROMS
     if self.target_model == 'ROMS':
         u10_background = u10_background[::-1,:]
     # run the computation
     for kt in np.arange(0,self.nframes):
         u10_tmp[:,:]    = ioncdf.readnc_oneframe(fid_u10,self.name_u10,kt)
         u10_out[kt,:,:] = (u10_tmp[:,:] + u10_background[:,:]) * self.lsm[:,:]
         if self.drown:
             u10_out[kt,:,:] = self.drown_wrapper(u10_out[kt,:,:])
     # output file informations
     my_dict = {}
     my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
     my_dict['varname']        = self.name_u10
     my_dict['time_dim']       = self.name_time_u10
     my_dict['time_var']       = self.name_time_u10
     my_dict['long name']      = 'Zonal wind speed at 10m'
     my_dict['units']          = 'm/s'
     my_dict['spval']          = self.spval
     my_dict['reftime']        = self.reftime
     my_dict['var_valid_min']  = u10_out.min()
     my_dict['var_valid_max']  = u10_out.max()
     my_dict['time_valid_min'] = time.min()
     my_dict['time_valid_max'] = time.max()
     my_dict['fileout']        = self.processed_nc_dir + \
                                 self.drownstring + self.name_u10 + '_' + self.dataset + '_' + str(self.year) + '.nc'
     if self.target_model == 'ROMS':
         model_dependent = {'description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
     elif self.target_model == 'NEMO':
         model_dependent = {'description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
     my_dict.update(model_dependent)
     # close input file and write output
     ioncdf.closenc(fid_u10)
     ioncdf.write_ncfile(lon,lat,time,u10_out,my_dict)
     # clear arrays
     u10_tmp = None ; u10_out = None
     return None
Example #3
0
 def process_precip_file(self):
     ''' Create precip file '''
     nframes   = self.nframes / self.nframes_per_day # daily file
     precip_tmp = np.empty((self.ny,self.nx))
     precip_out = np.empty((nframes,self.ny,self.nx))
     # open input file
     fid_precip = ioncdf.opennc(self.file_precip)
     # read coordinates and time
     lon  = ioncdf.readnc(fid_precip,'lon')
     lat  = ioncdf.readnc(fid_precip,'lat')
     time = ioncdf.readnc(fid_precip,self.name_time_precip)
     # copy
     for kt in np.arange(0,nframes):
         precip_tmp[:,:]    = ioncdf.readnc_oneframe(fid_precip,self.name_precip,kt)
         precip_out[kt,:,:] = (precip_tmp[:,:]) * self.lsm[:,:]
         if self.drown:
             precip_out[kt,:,:] = self.drown_wrapper(precip_out[kt,:,:])
     # output file informations
     my_dict = {}
     my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
     my_dict['varname']        = self.name_precip
     my_dict['time_dim']       = self.name_time_precip
     my_dict['time_var']       = self.name_time_precip
     my_dict['long name']      = 'Total Precipitation'
     my_dict['units']          = 'kg.m-2.s-1'
     my_dict['spval']          = self.spval
     my_dict['reftime']        = self.reftime
     my_dict['time_valid_min'] = time.min()
     my_dict['time_valid_max'] = time.max()
     my_dict['var_valid_min']  = precip_out.min()
     my_dict['var_valid_max']  = precip_out.max()
     my_dict['fileout']        = self.processed_nc_dir + \
                                 self.drownstring + self.name_precip + '_' + self.dataset + '_' + str(self.year) + '.nc'
     if self.target_model == 'ROMS':
         model_dependent = {'description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
     elif self.target_model == 'NEMO':
         model_dependent = {'description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
     my_dict.update(model_dependent)
     # close input file and write output
     ioncdf.closenc(fid_precip)
     ioncdf.write_ncfile(lon,lat,time,precip_out,my_dict)
     # clear arrays
     precip_tmp = None ; precip_out = None
     return None
Example #4
0
 def process_msl_file(self):
     ''' Create msl file '''
     msl_tmp = np.empty((self.ny,self.nx))
     msl_out = np.empty((self.nframes,self.ny,self.nx))
     # open input file
     fid_msl = ioncdf.opennc(self.file_msl)
     # read coordinates and time
     lon  = ioncdf.readnc(fid_msl,'lon')
     lat  = ioncdf.readnc(fid_msl,'lat')
     time = ioncdf.readnc(fid_msl,self.name_time_msl)
     # copy
     for kt in np.arange(0,self.nframes):
         msl_tmp[:,:]    = ioncdf.readnc_oneframe(fid_msl,self.name_msl,kt)
         msl_out[kt,:,:] = (msl_tmp[:,:]) * self.lsm[:,:]
         if self.drown:
             msl_out[kt,:,:] = self.drown_wrapper(msl_out[kt,:,:])
     # output file informations
     my_dict = {}
     my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
     my_dict['varname']        = self.name_msl
     my_dict['time_dim']       = self.name_time_msl
     my_dict['time_var']       = self.name_time_msl
     my_dict['long name']      = 'Mean Sea Level Pressure'
     my_dict['units']          = 'Pa'
     my_dict['spval']          = self.spval
     my_dict['reftime']        = self.reftime
     my_dict['time_valid_min'] = time.min()
     my_dict['time_valid_max'] = time.max()
     my_dict['var_valid_min']  = msl_out.min()
     my_dict['var_valid_max']  = msl_out.max()
     my_dict['fileout']        = self.processed_nc_dir + \
                                 self.drownstring + self.name_msl + '_' + self.dataset + '_' + str(self.year) + '.nc'
     if self.target_model == 'ROMS':
         model_dependent = {'description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
     elif self.target_model == 'NEMO':
         model_dependent = {'description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
     my_dict.update(model_dependent)
     # close input file and write output
     ioncdf.closenc(fid_msl)
     ioncdf.write_ncfile(lon,lat,time,msl_out,my_dict)
     # clear arrays
     msl_tmp = None ; msl_out = None
     return None
Example #5
0
    def __init__(self,dict_input,dict_datafiles,drown=True):
        # constants
        self.rho_w = 1000.
        self.nsec_per_day = 86400.
        self.reftime = dt.datetime(1900,1,1,0,0)
        self.spval = 1.0e+15
        self.dataset = 'DFS5.2'
        # read inputs
        self.dict_input = dict_input
        for key in dict_input:
            exec('self.' + key + '=dict_input[key]')
        for key in dict_datafiles:
            exec('self.' + key + '=dict_datafiles[key]')
        # set time related stuff
        if calendar.isleap(self.year):
            self.ndays = 366
            self.days_in_month = np.array([31.,29.,31.,30.,31.,30.,31.,31.,30.,31.,30.,31.])
        else:
            self.ndays = 365
            self.days_in_month = np.array([31.,28.,31.,30.,31.,30.,31.,31.,30.,31.,30.,31.])
        # forcing set freq
        if self.freq == '3h':
            self.nframes_per_day = 8
        elif self.freq == '6h':
            self.nframes_per_day = 4

        self.nframes = self.ndays * self.nframes_per_day

        print(self.year, 'has', self.nframes, 'frames')

        self.drown = drown
        if self.drown:
            self.drownstring = 'drowned_'
        else:
            self.drownstring = ''

        fid_lsm = ioncdf.opennc(self.mask)
        self.lsm = ioncdf.readnc(fid_lsm,'lsm')
        ioncdf.closenc(fid_lsm)

        return None
Example #6
0
    def __init__(self, dict_input, drown=True):
        # constants
        self.rho_w = 1000.
        self.nsec_per_day = 86400.
        self.reftime = dt.datetime(1900, 1, 1, 0, 0)
        self.spval = 1.0e+15
        self.dataset = 'ERAinterim'
        # read inputs
        self.dict_input = dict_input
        for key in dict_input:
            exec('self.' + key + '=dict_input[key]')
        # set time related stuff
        if calendar.isleap(self.year):
            self.ndays = 366
        else:
            self.ndays = 365
        # forcing set freq
        if self.freq == '3h':
            self.nframes_per_day = 8
        elif self.freq == '6h':
            self.nframes_per_day = 4

        self.nframes = self.ndays * self.nframes_per_day

        self.drown = drown
        if self.drown:
            fid_lsm = ioncdf.opennc(self.lsm_file)
            self.lsm = ioncdf.readnc(fid_lsm, 'lsm')
            ioncdf.closenc(fid_lsm)
            self.drownstring = 'drowned_'
        else:
            self.drownstring = ''

        print(self.year, 'has', self.nframes, 'frames')

        return None
Example #7
0
    def process_q2_file(self):
        ''' Create q2 file '''
        t2_old = np.empty((self.ny,self.nx))
        t2_new = np.empty((self.ny,self.nx))
        msl    = np.empty((self.ny,self.nx))
        q2_tmp = np.empty((self.ny,self.nx))
        q2_out = np.empty((self.nframes,self.ny,self.nx))
        # open input file
        fid_q2 = ioncdf.opennc(self.file_q2)
        # read coordinates and time
        lon  = ioncdf.readnc(fid_q2,'lon')
        lat  = ioncdf.readnc(fid_q2,'lat')
        time = ioncdf.readnc(fid_q2,self.name_time_q2)

        # We need original t2 and msl from ERAinterim
        fid_t2old = ioncdf.opennc(self.file_t2)
        fid_msl   = ioncdf.opennc(self.file_msl)
        fid_t2new = ioncdf.opennc(self.file_t2_new)
        # run the computation
        for kt in np.arange(0,self.nframes):
            # read all the fields
            # ROMS has t2 in degC, others in Kelvin - qsat_from_t2_and_msl expects Celcius
            if self.target_model == 'ROMS':
                t2_old[:,:]    = ioncdf.readnc_oneframe(fid_t2old,self.name_t2,kt)
                t2_new[:,:]    = ioncdf.readnc_oneframe(fid_t2new,self.name_t2,kt)
            else:
                t2_old[:,:]    = ioncdf.readnc_oneframe(fid_t2old,self.name_t2,kt) - 273.15
                t2_new[:,:]    = ioncdf.readnc_oneframe(fid_t2new,self.name_t2,kt) - 273.15
            msl[:,:]       = ioncdf.readnc_oneframe(fid_msl,self.name_msl,kt)
            q2_tmp[:,:]    = ioncdf.readnc_oneframe(fid_q2,self.name_q2,kt)
            # compute humidity at saturation
            q_sat_new = humidity_toolbox.qsat_from_t2_and_msl(t2_new,msl)
            q_sat_old = humidity_toolbox.qsat_from_t2_and_msl(t2_old,msl)
            # compute new specific humidity
            q2_out[kt,:,:] = (q2_tmp[:,:] * q_sat_new / q_sat_old) * self.lsm[:,:]
            if self.drown:
                q2_out[kt,:,:] = self.drown_wrapper(q2_out[kt,:,:])

                # output file informations
        my_dict = {}
        my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
        my_dict['varname']        = self.name_q2
        my_dict['time_dim']       = self.name_time_q2
        my_dict['time_var']       = self.name_time_q2
        my_dict['long name']      = 'Specific Humidity'
        my_dict['units']          = 'kg/kg'
        my_dict['spval']          = self.spval
        my_dict['reftime']        = self.reftime
        my_dict['time_valid_min'] = time.min()
        my_dict['time_valid_max'] = time.max()
        my_dict['var_valid_min']  = q2_out.min()
        my_dict['var_valid_max']  = q2_out.max()
        my_dict['fileout']        = self.processed_nc_dir + \
                                    self.drownstring + self.name_q2 + '_' + self.dataset + '_' + str(self.year) + '.nc'
        if self.target_model == 'ROMS':
            model_dependent = {'description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
        elif self.target_model == 'NEMO':
            model_dependent = {'description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
        my_dict.update(model_dependent)
        # close input file and write output
        ioncdf.closenc(fid_q2)
        ioncdf.write_ncfile(lon,lat,time,q2_out,my_dict)
        # clear arrays
        q2_tmp = None ; q2_out = None
        return None
Example #8
0
    def process_t2_file(self):
        ''' Create Air temperature file '''
        t2_tmp = np.empty((self.ny,self.nx))
        t2_out = np.empty((self.nframes,self.ny,self.nx))
        # open input file
        fid_t2 = ioncdf.opennc(self.file_t2)
        # read coordinates and time
        lon  = ioncdf.readnc(fid_t2,'lon')
        lat  = ioncdf.readnc(fid_t2,'lat')
        time = ioncdf.readnc(fid_t2,self.name_time_t2)

        #------- Antarctic correction --------
        # make sure that we use latitude from south to north
        if self.target_model == 'ROMS':
            lat_s2n = lat
        elif self.target_model == 'NEMO':
            lat_s2n = lat[::-1]

        value_south = -2.0 # we remove 2 degrees C
        lattrans1   = -60 # start linear transition to correction at 60S
        lattrans2   = -75 # correction is at full value at 75S
        jtrans1 = (np.abs(lat_s2n-lattrans1)).argmin()
        jtrans2 = (np.abs(lat_s2n-lattrans2)).argmin()

        correction_south = np.zeros((self.ny,self.nx))
        correction_south[0:jtrans2,:] = value_south
        for jj in np.arange(jtrans2,jtrans1):
            correction_south[jj,:] = value_south * float(jj - jtrans1) / float(jtrans2 - jtrans1)

        if not self.target_model == 'ROMS':
            correction_south = correction_south[::-1,:]

        #------- Arctic correction --------
        correction_north = np.zeros((self.ny,self.nx))
        fid_off = ioncdf.opennc(self.t2_poles_offset)
        monthly_offset_poles = ioncdf.readnc(fid_off,'Tair')
        ioncdf.closenc(fid_off)
        fid_ifra = ioncdf.opennc(self.ice_nsidc)
        monthly_ice_fraction = ioncdf.readnc(fid_ifra,'ifrac')
        ioncdf.closenc(fid_ifra)

        # poles correction north of 70 N
        lattrans3   = 65 # correction poles transition starts at 65, full at 70N
        jtrans3 = (np.abs(lat_s2n-lattrans3)).argmin()
        val_oce = -0.7

        # run the computation
        for kt in np.arange(0,self.nframes):
            t2_tmp[:,:]    = ioncdf.readnc_oneframe(fid_t2,self.name_t2,kt)
            #-----  arctic correction ------
            # interp monthly offset to daily value
            this_day = 1 + (kt / self.nframes_per_day)
            this_day_weights = self.time_interp_weights(this_day)
            this_day_offset  = self.interp_data_to_day(monthly_offset_poles,this_day_weights)
            this_day_ifrac   = self.interp_data_to_day(monthly_ice_fraction,this_day_weights)
            # compute artic correction
            correction_north[jtrans3:,:] = this_day_offset[jtrans3:,:]*this_day_ifrac[jtrans3:,:] + \
                                           val_oce*(1. -this_day_ifrac[jtrans3:,:])
            if not self.target_model == 'ROMS':
                correction_north[:,:] = correction_north[::-1,:]
            #------ add northern and southern hemisphere correction ------
            t2_out[kt,:,:] = (t2_tmp[:,:] + correction_north[:,:] + correction_south[:,:]) * self.lsm[:,:]
            if self.drown:
                t2_out[kt,:,:] = self.drown_wrapper(t2_out[kt,:,:])

        # output file informations
        my_dict = {}
        my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]'
        my_dict['varname']        = self.name_t2
        my_dict['time_dim']       = self.name_time_t2
        my_dict['time_var']       = self.name_time_t2
        my_dict['long name']      = 'Air Temperature at 2m'
        my_dict['spval']          = self.spval
        my_dict['reftime']        = self.reftime
        my_dict['var_valid_min']  = t2_out.min()
        my_dict['var_valid_max']  = t2_out.max()
        my_dict['time_valid_min'] = time.min()
        my_dict['time_valid_max'] = time.max()
        my_dict['fileout']        = self.processed_nc_dir + \
                                    self.drownstring + self.name_t2 + '_' + self.dataset + '_' + str(self.year) + '.nc'
        if self.target_model == 'ROMS':
            model_dependent = {'units':'degC','description':my_dict['description'] + '\nROMS-ready ERAinterim forcing'}
        elif self.target_model == 'NEMO':
            model_dependent = {'units':'K','description':my_dict['description'] + '\nNEMO-ready ERAinerim forcing'}
        my_dict.update(model_dependent)
        # close input file and write output
        ioncdf.closenc(fid_t2)
        ioncdf.write_ncfile(lon,lat,time,t2_out,my_dict)
        # clear arrays
        t2_tmp = None ; t2_out = None
        # save this for q2
        self.file_t2_new = my_dict['fileout']
        return None
Example #9
0
 def process_precip_to_daily(self):
     ''' processing the precipitation file :
     sum all cumulated fields and divide by a day'''
     precip_out = np.empty((self.ndays, self.ny, self.nx))
     time = np.empty((self.ndays))
     # open file
     fid_precip = ioncdf.opennc(self.file_precip)
     # read coordinates and time
     lon = ioncdf.readnc(fid_precip, 'lon')
     lat = ioncdf.readnc(fid_precip, 'lat')
     if self.target_model == 'ROMS':
         lat = lat[::-1]
     # run the decumulation
     cumul_time = self.nsec_per_day
     for kt in np.arange(0, self.ndays):
         tmp = np.zeros((self.ny, self.nx))
         nvalues = self.nframes_per_day / self.ncumul  # number of values to read
         for kc in np.arange(nvalues):
             # frames to read
             # ERAinterim (nframes_per_day = 8) : for kt = 0 (day 1) we read frames 4 and 8
             kframe = (kt * self.nframes_per_day) + (kc + 1) * self.ncumul
             tmp = tmp + ioncdf.readnc_oneframe(
                 fid_precip, 'TP', kframe - 1)  # C indexing hence -1
         precip_out[kt, :, :] = tmp.copy(
         ) * self.rho_w / cumul_time  # units conversion (from m to m/s then to kg/m2/s)
         if self.target_model == 'ROMS':
             precip_out[kt, :, :] = precip_out[kt, ::-1, :]
         if self.drown:
             precip_out[kt, :, :] = self.drown_wrapper(precip_out[kt, :, :])
         this_day = dt.datetime(self.year, 1, 1, 12,
                                0) + dt.timedelta(days=int(kt))
         time[kt] = (this_day - self.reftime
                     ).days + (this_day - self.reftime).seconds / 86400.
     # close file
     ioncdf.closenc(fid_precip)
     # remove negative values
     precip_out[np.where(precip_out < 0.)] = 0.
     # write file
     my_dict = {}
     my_dict[
         'description'] = 'file processed by interimR. contact : [email protected]'
     my_dict['varname'] = self.name_precip
     my_dict['time_dim'] = self.name_time_precip
     my_dict['time_var'] = self.name_time_precip
     my_dict['units'] = 'kg.m-2.s-1'
     my_dict['long name'] = 'Total Precipitation'
     my_dict['spval'] = self.spval
     my_dict['reftime'] = self.reftime
     my_dict['time_valid_min'] = time.min()
     my_dict['time_valid_max'] = time.max()
     my_dict['var_valid_min'] = precip_out.min()
     my_dict['var_valid_max'] = precip_out.max()
     my_dict['fileout']        = self.processed_nc_dir + \
                                 self.drownstring + self.name_precip + '_' + self.dataset + '_' + str(self.year) + '.nc'
     if self.target_model == 'ROMS':
         model_dependent = {
             'description':
             my_dict['description'] + '\nROMS-ready ERAinterim forcing'
         }
     elif self.target_model == 'NEMO':
         model_dependent = {
             'description':
             my_dict['description'] + '\nNEMO-ready ERAinerim forcing'
         }
     my_dict.update(model_dependent)
     ioncdf.write_ncfile(lon, lat, time, precip_out, my_dict)
     precip_out = None
     return None