def create_q2_file(self): ''' Create specific humidity file ''' q2_out = np.empty((self.nframes, self.ny, self.nx)) time = np.empty((self.nframes)) # open file fid_d2 = ioncdf.opennc(self.file_d2) fid_msl = ioncdf.opennc(self.file_msl) # read coordinates and time lon = ioncdf.readnc(fid_d2, 'lon') lat = ioncdf.readnc(fid_d2, 'lat') if self.target_model == 'ROMS': lat = lat[::-1] # run the computation for kt in np.arange(0, self.nframes): d2 = ioncdf.readnc_oneframe(fid_d2, 'D2M', kt) msl = ioncdf.readnc_oneframe(fid_msl, 'MSL', kt) q2_tmp = humidity_toolbox.q2_from_d2_and_msl(d2, msl) q2_out[kt, :, :] = q2_tmp.copy() if self.target_model == 'ROMS': q2_out[kt, :, :] = q2_out[kt, ::-1, :] if self.drown: q2_out[kt, :, :] = self.drown_wrapper(q2_out[kt, :, :]) time[kt] = self.kt_to_days_from_origin(kt) # close file ioncdf.closenc(fid_d2) ioncdf.closenc(fid_msl) # write file my_dict = {} my_dict[ 'description'] = 'file processed by interimR. 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['units'] = 'kg/kg' my_dict['long name'] = 'Specific humidity at 2m' 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) ioncdf.write_ncfile(lon, lat, time, q2_out, my_dict) q2_out = None return None
def process_t2_file(self): ''' Rewrite temperature file according to model's needs ''' t2_out = np.empty((self.nframes, self.ny, self.nx)) time = np.empty((self.nframes)) # open file fid_t2 = ioncdf.opennc(self.file_t2) # read coordinates and time lon = ioncdf.readnc(fid_t2, 'lon') lat = ioncdf.readnc(fid_t2, 'lat') if self.target_model == 'ROMS': lat = lat[::-1] # run the computation for kt in np.arange(0, self.nframes): t2_out[kt, :, :] = ioncdf.readnc_oneframe(fid_t2, 'T2M', kt) if self.target_model == 'ROMS': t2_out[kt, :, :] = t2_out[kt, ::-1, :] - 273.15 if self.drown: t2_out[kt, :, :] = self.drown_wrapper(t2_out[kt, :, :]) time[kt] = self.kt_to_days_from_origin(kt) # close file ioncdf.closenc(fid_t2) # write file my_dict = {} my_dict[ 'description'] = 'file processed by interimR. 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['time_valid_min'] = time.min() my_dict['time_valid_max'] = time.max() my_dict['var_valid_min'] = t2_out.min() my_dict['var_valid_max'] = t2_out.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 ERAinterim forcing' } my_dict.update(model_dependent) ioncdf.write_ncfile(lon, lat, time, t2_out, my_dict) t2_out = None return None
def process_radlw_file(self): ''' Create longwave radiation file ''' nframes = self.nframes / self.nframes_per_day # daily file radlw_tmp = np.empty((self.ny,self.nx)) radlw_out = np.empty((nframes,self.ny,self.nx)) # open input file fid_radlw = ioncdf.opennc(self.file_radlw) # read coordinates and time lon = ioncdf.readnc(fid_radlw,'lon') lat = ioncdf.readnc(fid_radlw,'lat') time = ioncdf.readnc(fid_radlw,self.name_time_radlw) # open radlw factor fid_factor = ioncdf.opennc(self.longwave_factor) radlw_factor = ioncdf.readnc(fid_factor,'radlw') ioncdf.closenc(fid_factor) # flip upside down for ROMS if self.target_model == 'ROMS': radlw_factor = radlw_factor[::-1,:] # run the computation correct_heat_budget = 1.0088 # attempt to close heat budget for kt in np.arange(0,nframes): radlw_tmp[:,:] = ioncdf.readnc_oneframe(fid_radlw,self.name_radlw,kt) radlw_out[kt,:,:] = (radlw_tmp[:,:] * radlw_factor[:,:] * correct_heat_budget) * self.lsm[:,:] if self.drown: radlw_out[kt,:,:] = self.drown_wrapper(radlw_out[kt,:,:]) # output file informations my_dict = {} my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]' my_dict['varname'] = self.name_radlw my_dict['time_dim'] = self.name_time_radlw my_dict['time_var'] = self.name_time_radlw my_dict['long name'] = 'Downwelling longwave radiation' my_dict['units'] = 'W.m-2' 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'] = radlw_out.min() my_dict['var_valid_max'] = radlw_out.max() my_dict['fileout'] = self.processed_nc_dir + \ self.drownstring + self.name_radlw + '_' + 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_radlw) ioncdf.write_ncfile(lon,lat,time,radlw_out,my_dict) # clear arrays radlw_tmp = None ; radlw_out = None return None
def process_v10_file(self): ''' Create meridional wind file ''' v10_tmp = np.empty((self.ny,self.nx)) v10_out = np.empty((self.nframes,self.ny,self.nx)) # open input file fid_v10 = ioncdf.opennc(self.file_v10) # read coordinates and time lon = ioncdf.readnc(fid_v10,'lon') lat = ioncdf.readnc(fid_v10,'lat') time = ioncdf.readnc(fid_v10,self.name_time_v10) # open background velocity fid_bgd = ioncdf.opennc(self.v10_background) v10_background = ioncdf.readnc(fid_bgd,'v10') ioncdf.closenc(fid_bgd) # flip upside down for ROMS if self.target_model == 'ROMS': v10_background = v10_background[::-1,:] # run the computation for kt in np.arange(0,self.nframes): v10_tmp[:,:] = ioncdf.readnc_oneframe(fid_v10,self.name_v10,kt) v10_out[kt,:,:] = (v10_tmp[:,:] + v10_background[:,:]) * self.lsm[:,:] if self.drown: v10_out[kt,:,:] = self.drown_wrapper(v10_out[kt,:,:]) # output file informations my_dict = {} my_dict['description'] = 'DFS 5.2 (MEOM/LGGE) contact : [email protected]' my_dict['varname'] = self.name_v10 my_dict['time_dim'] = self.name_time_v10 my_dict['time_var'] = self.name_time_v10 my_dict['long name'] = 'Meridional wind speed at 10m' my_dict['units'] = 'm/s' my_dict['spval'] = self.spval my_dict['reftime'] = self.reftime my_dict['var_valid_min'] = v10_out.min() my_dict['var_valid_max'] = v10_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_v10 + '_' + 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_v10) ioncdf.write_ncfile(lon,lat,time,v10_out,my_dict) # clear arrays v10_tmp = None ; v10_out = None return None
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
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
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
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
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