def interpAlt(alt_in, Z, val_in, intrpOrder, logFlg): if logFlg: val_out = np.exp(np.flipud( intrpUniSpl( np.flipud(alt_in), np.log(np.flipud(val_in)), k=intrpOrder )( np.flipud(Z) ) ) ) else: val_out = np.flipud( intrpUniSpl( np.flipud(alt_in), np.flipud(val_in), k=intrpOrder )( np.flipud(Z) ) ) return val_out
def main(): #---------------- # Initializations #---------------- loc = 'fl0' sondeDir = '/data1/ancillary_data/sondes/' + loc.lower() + '/' verW = 'v77' # version 77 is used for individual retrievals at specific times pltFlg = True #------------------- # Interpolation Oder #------------------- intrpOrder = 1 nSkip = 1 # Number of points to skip when merging WACCM and NCEP profiles #----------------------- # Date Range of interest #----------------------- iyear = 2010 imnth = 1 iday = 1 fyear = 2016 fmnth = 12 fday = 31 #---------------------- # Output Data Directory #---------------------- dataDir = '/data1/' + loc.lower() + '/' #------------------------- # WACCM monthly means file #------------------------- WACCMfile = '/data/Campaign/' + loc.upper( ) + '/waccm/WACCM_pTW-meanV6.' + loc.upper() #-------------------------------------------------------------------- # Read WACCM monthly mean data. WACCM monthly mean file is ascending, # adjust so that it is descending. Also units are in km. #-------------------------------------------------------------------- with open(WACCMfile, 'r') as fopen: lines = fopen.readlines() nlyrs = int(lines[0].strip().split()[0]) s_ind = 3 Z = np.flipud( np.array([ float(row.strip().split()[0]) for row in lines[s_ind:nlyrs + s_ind] ])) waccmT = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 waccmP = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 + nlyrs + 2 waccmW = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) #---------------------------- # File and directory checking #---------------------------- ckDir(sondeDir, exitFlg=True) ckDir(dataDir, exitFlg=True) s = rs.SondeClass(sondeDir, loc, iyear=iyear, imnth=imnth, iday=iday, fyear=fyear, fmnth=fmnth, fday=fday, fleoutFlg=False) s.readfilessonde() s.sondePrf() sonde = s.sonde sondedt = np.asarray(sonde['dt']) sondedate = np.asarray(sonde['date']) sondealt = np.asarray(sonde['Alt_km_a']) sondeH2OPrf_a = np.asarray(sonde['H2Omr_ppmv_a']) * 1e-6 sondeH2OPrfsd_a = np.asarray(sonde['H2Osd_ppmv_a']) sondealt_a = np.asarray(sonde['Alt_km_a']) sondeairmass_a = np.asarray(sonde['airmass_a']) #--------------------------- # Iterate through sonde Prfs #--------------------------- for i, sngDay in enumerate(sondedt): #---------------------------- # Get corresponding directory #---------------------------- baseDir = dataDir + '{0:04d}{1:02d}{2:02d}/'.format( sngDay.year, sngDay.month, sngDay.day) if not os.path.exists(baseDir): continue #-------------------------------------------------- # Find month index for monthly WACCM water profiles #-------------------------------------------------- mnthInd = sngDay.month - 1 # -1 because January is in the 0th column #--------------------------------- # #--------------------------------- Z_day = sondealt[i] Q_day = sondeH2OPrf_a[i] sondetop = Z_day[-1] topInd = np.argmin( abs(Z - sondetop )) # Where top of NCEP reanalysis fits in WACCM grid height Zin = np.concatenate((Z[0:(topInd - nSkip)], np.flipud(Z_day))) SHin = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], np.flipud(Q_day))) #----------------------------------- # Interpolate retrieved profile onto # sfit input grid #----------------------------------- H2Oout = interpolate.interp1d(Zin, SHin, axis=0, fill_value=Q_day[0], bounds_error=False, kind='linear')(Z) H2Oout2 = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin), k=1)(np.flipud(Z))) #------------------------ # remove v4 for 6-hourly files #------------------------ waterFiles_test = glob.glob(baseDir + 'w-120*' + verW) waterFiles_test = [ i for i in waterFiles_test if len(os.path.basename(i)) > 10 ] if len(waterFiles_test) >= 1: for f in waterFiles_test: os.remove(f) #--------------------- # Write out water file #--------------------- tstamp = '{0:04d}{1:02d}{2:02d}.{3:02d}{4:02d}{5:02d}'.format( sngDay.year, sngDay.month, sngDay.day, sngDay.hour, sngDay.minute, sngDay.second) with open(baseDir + 'w-120.' + tstamp + '.' + verW, 'w') as fopen: fopen.write(' 1 H2O from Sonde \n') for row in segmnt(H2Oout, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #-------------------- # Create plots to pdf #-------------------- if pltFlg: pdfsav = PdfPages(baseDir + 'Sonde_' + tstamp + '_WaterProfile.pdf') fig1, ax1 = plt.subplots() ax1.plot(H2Oout, Z, 'rx-', label='Interpolated Water Profile') ax1.plot(Q_day, Z_day, 'bx-', label='Original Sonde Water Profile') ax1.grid(True, which='both') ax1.legend(prop={'size': 9}) ax1.set_ylabel('Altitude [km]') ax1.set_xlabel('VMR') ax1.tick_params(axis='x', which='both', labelsize=8) ax1.set_ylim((Z[-1], 60)) #ax1.set_xlim((0,np.max((waccmW[-1,mnthInd],dayShum[-1])))) ax1.set_title(sngDay) pdfsav.savefig(fig1, dpi=250) pdfsav.close() print 'Finished processing folder: {}'.format(sngDay)
def main(): #--------- # Location #--------- loc = 'tab' #loc = 'mlo' #------------------------------------ # Version number to append water file #------------------------------------ verW = 'v3' #----------------------------------------- # Interpolation flag for NCEP re-analysis: # False = Nearest point. True = linear #----------------------------------------- interpFlg = True #--------------------- # Interpolation things #--------------------- nSkip = 3 # Number of points to skip when merging WACCM and NCEP profiles intrpOrder = 1 # Order of interpolation logFlg = True # Flag to do interpolation of log of water #----------------------- # Date Range of interest #----------------------- iyear = 2017 imnth = 9 iday = 1 fyear = 2017 fmnth = 12 fday = 31 #------------------------------- # NCEP Reanalysis data directory #------------------------------- NCEPdirShum = '/data1/ancillary_data/NCEPdata/NCEP_Shum/' NCEPdirHgt = '/data1/ancillary_data/NCEPdata/NCEP_hgt/' #--------------- # Data Directory #--------------- dataDir = '/data1/' + loc.lower() + '/' #dataDir = '/data1/iortega/WYO/' #------------------------- # WACCM monthly means file #------------------------- WACCMfile = '/data/Campaign/' + loc.upper( ) + '/waccm/WACCM_pTW-meanV6.' + loc.upper() #--------------------- # Establish date range #--------------------- dRange = sc.DateRange(iyear, imnth, iday, fyear, fmnth, fday) #--------------------------------------- # Altitude levels for different stations #--------------------------------------- if loc.lower() == 'tab': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.0000, 0.2250 ]) sLat = 76.52 sLon = 291.23 # 68.77 W = (360.0 - 68.77) = 291.23 E elif loc.lower() == 'mlo': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.3960 ]) sLat = 19.4 sLon = 204.43 # 155.57 W = (360 - 155.57) = 204.43 E elif loc.lower() == 'fl0': Z = np.array([ 120.0000, 110.0000, 100.0000, 94.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.6120 ]) sLat = 40.4 sLon = 254.76 # 105.24 W = (360 - 105.24) = 254.76 E ###sLat = 42.73 ###sLon = 253.68 #WYOBA #---------------------------- # File and directory checking #---------------------------- ckDir(NCEPdirShum, exitFlg=True) ckDir(NCEPdirHgt, exitFlg=True) ckDir(dataDir, exitFlg=True) ckFile(WACCMfile, exitFlg=True) #-------------------------------------------------------------------- # Read WACCM monthly mean data. WACCM monthly mean file is ascending, # adjust so that it is descending. Also units are in km. #-------------------------------------------------------------------- with open(WACCMfile, 'r') as fopen: lines = fopen.readlines() nlyrs = int(lines[0].strip().split()[0]) s_ind = 3 Z = np.flipud( np.array([ float(row.strip().split()[0]) for row in lines[s_ind:nlyrs + s_ind] ])) waccmT = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 waccmP = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 + nlyrs + 2 waccmW = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) #-------------------------------------------- # Walk through first level of directories in # data directory and collect directory names #-------------------------------------------- dirLst = [] for drs in os.walk(dataDir).next()[1]: #------------------------------------------- # Test directory to make sure it is a number #------------------------------------------- try: int(drs[0:4]) except: continue if dRange.inRange(int(drs[0:4]), int(drs[4:6]), int(drs[6:8])): dirLst.append(dataDir + drs + '/') dirLst.sort() #-------------------------------------------------------- # Loop through folders for individual years. This is done # because NCEP NetCDF files are by year. Therefore only # have to open and read yearly NCEP file once #-------------------------------------------------------- yrList = dRange.yearList() for year in yrList: #----------------------------- # Find all folders within year #----------------------------- dirListYr = np.array( [d for d in dirLst if int(os.path.basename(d[:-1])[0:4]) == year]) #------------------------------- # Open and read year NetCDF file #------------------------------- shumFile = NCEPdirShum + 'shum.' + str(year) + '.nc' ghghtFile = NCEPdirHgt + 'hgt.' + str(year) + '.nc' #----------------------- # Specific humidity file #----------------------- #with netcdf.netcdf_file(shumFile,'r',mmap=False) as shumF: # Can only be done with scipy Ver > 0.12.0 #shumF = netcdf.netcdf_file(shumFile,'r',mmap=False) shumObj = nc.Dataset(shumFile, 'r') PlvlShum = shumObj.variables['level'] # Starts at the surface timeShum = shumObj.variables['time'] # hours since 1-1-1 00:00:0.0 latShum = shumObj.variables['lat'] # degrees_north lonShum = shumObj.variables['lon'] # degrees_east shum = shumObj.variables[ 'shum'] # Units: [kg/kg]. Dimensions: [time][vert][lat][lon] Only goes to 300mb!! PlvlShum = PlvlShum[:] timeShum = timeShum[:] latShum = latShum[:] lonShum = lonShum[:] shum = shum[:] shumObj.close() #---------------------------------------- # Convert Specific humidity from kg/kg to # molecules/molecules #---------------------------------------- shum = shum * 1.608 #----------------------------------------------- # If not interpolating point in NCEP re-analysis # find closes lat lon indicies #----------------------------------------------- if not interpFlg: latind = findCls(latShum, sLat) lonind = findCls(lonShum, sLon) #------------------------------------------------------ # Convert hours since 1-1-1 00:00:00 to datetime object # NCEP reanalysis uses udunits for encoding times, # meaning they don't follow standard leap year # convention (whatever this means?). Must follow some # leap year convention, but not clear. Therefore, take # the first time in file as 1-1-YEAR. #------------------------------------------------------ timeHrs = timeShum - timeShum[0] timeAll = np.array([ dt.datetime(year, 1, 1) + dt.timedelta(hours=int(h)) for h in timeHrs ]) #------------------------- # Geopotential Height file #------------------------- #with netcdf.netcdf_file(ghghtFile,'r',mmap=False) as gHghtF: # Can only be done with scipy Ver > 0.12.0 #gHghtF = netcdf.netcdf_file(ghghtFile,'r',mmap=False) gHghtF = nc.Dataset(ghghtFile, 'r') hgt = gHghtF.variables[ 'hgt'] # Height in [meters]. Dimensions: [time][vert][lat][lon] PlvlHght = gHghtF.variables['level'] hgt = hgt[:] PlvlHght = PlvlHght[:] gHghtF.close() #------------------------------------------ # Loop through all folders in specific year #------------------------------------------ for sngDir in dirListYr: #---------------------------- # Get date in datetime format #---------------------------- oneDay = dt.datetime(int(os.path.basename(sngDir[:-1])[0:4]), int(os.path.basename(sngDir[:-1])[4:6]), int(os.path.basename(sngDir[:-1])[6:8])) #-------------------------------------------------- # Find month index for monthly WACCM water profiles #-------------------------------------------------- mnthInd = oneDay.month - 1 # -1 because January is in the 0th column #-------------------------------------- # Get hgt and specific humidity for day #-------------------------------------- ind = np.where(timeAll == oneDay)[0] dayHghtMat = np.squeeze(hgt[ind, :, :, :]) dayShumMat = np.squeeze(shum[ind, :, :, :]) #----------------------------------------------------------- # 'Unpack' the data => (data[int])*scale_factor + add_offset #----------------------------------------------------------- #dayHghtMat = dayHghtMat * hgt.scale_factor + hgt.add_offset #dayShumMat = dayShumMat * shum.scale_factor + shum.add_offset #----------------------------------------------------- # For each level interpolate hgt and specific humidity # based on latitude and longitude of site #----------------------------------------------------- dayHgt = np.zeros(np.shape(dayShumMat)[0]) dayShum = np.zeros(np.shape(dayShumMat)[0]) for lvl in range(0, np.shape(dayShumMat)[0]): dayHgtLvl = np.squeeze(dayHghtMat[lvl, :, :]) if interpFlg: dayHgt[lvl] = interp2d(lonShum, latShum, dayHgtLvl, kind='linear', bounds_error=True)(sLon, sLat) else: dayHgt[lvl] = dayHgtLvl[latind, lonind] dayShumLvl = np.squeeze(dayShumMat[lvl, :, :]) if interpFlg: dayShum[lvl] = interp2d(lonShum, latShum, dayShumLvl, kind='linear', bounds_error=True)(sLon, sLat) else: dayShum[lvl] = dayShumLvl[latind, lonind] dayHgt.astype(float) dayShum.astype(float) dayHgt = dayHgt / 1000.0 # Convert height units [m] => [km] #--------------------------------------------------------- # Construct specific humidity and height profiles for # interpolation on to the sfit input grid which is the # same as the WACCM height in monthly profile file. # NCEP reanalysis data only goes to 300mb therefore, # merge with monthly averaged WACCM water profiles > 300mb #--------------------------------------------------------- NCEPtop = dayHgt[-1] topInd = np.argmin( abs(Z - NCEPtop) ) # Where top of NCEP reanalysis fits in WACCM grid height #Zin = np.concatenate( ( Z[0:(topInd-nSkip)] , np.flipud(dayHgt)) , axis=1 ) #SHin = np.concatenate( ( waccmW[0:(topInd-nSkip),mnthInd], np.flipud(dayShum)), axis=1 ) #Remove axis=1 Zin = np.concatenate((Z[0:(topInd - nSkip)], np.flipud(dayHgt))) SHin = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], np.flipud(dayShum))) #-------------------------------------------------------------- # Interpolate to specific humidity on WACCM grid. X data must # be increasing => flip dimensions and then flip back #-------------------------------------------------------------- if logFlg: SHout = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin)), k=intrpOrder)(np.flipud(Z)))) else: SHout = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin), k=intrpOrder)(np.flipud(Z))) #--------------------- # Write out water file #--------------------- with open(sngDir + 'w-120.' + verW, 'w') as fopen: fopen.write( ' 1 H2O from NCEP reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #-------------------- # Create plots to pdf #COMMENTED BELOW BY IVAN: CHECK MATPLOT PLT ERROR #-------------------- pdfsav = PdfPages(sngDir + 'WaterProfile.pdf') fig1, ax1 = plt.subplots() ax1.plot(SHout, Z, 'rx-', label='Interpolated SH') ax1.plot(waccmW[:, mnthInd], Z, 'bx-', label='WACCM V6 SH') ax1.plot(dayShum, dayHgt, 'kx-', label='NCEP Reanalysis SH') ax1.grid(True, which='both') ax1.legend(prop={'size': 9}) ax1.set_ylabel('Altitude [km]') ax1.set_xlabel('VMR [ppv]') ax1.tick_params(axis='x', which='both', labelsize=8) ax1.set_ylim((Z[-1], 60)) ax1.set_xlim((0, np.max((waccmW[-1, mnthInd], dayShum[-1])))) ax1.set_title(oneDay) pdfsav.savefig(fig1, dpi=250) fig2, ax2 = plt.subplots() ax2.plot(SHout, Z, 'rx-', label='Interpolated SH') ax2.plot(waccmW[:, mnthInd], Z, 'bx-', label='WACCM V6 SH') ax2.plot(dayShum, dayHgt, 'kx-', label='NCEP Reanalysis SH') ax2.grid(True, which='both') ax2.legend(prop={'size': 9}) ax2.set_ylabel('Altitude [km]') ax2.set_xlabel('log VMR [ppv]') ax2.tick_params(axis='x', which='both', labelsize=8) ax2.set_xscale('log') ax2.set_ylim((Z[-1], 60)) ax2.set_xlim((0, np.max((waccmW[-1, mnthInd], dayShum[-1])))) ax2.set_title(oneDay) pdfsav.savefig(fig2, dpi=250) pdfsav.close() print 'Finished processing folder: {}'.format(sngDir)
def main(argv): #------------------ # Set default flags #------------------ logFile = False #--------------------------------- # Retrieve command line arguments #--------------------------------- #------------------------------------------------------------------------------------------------------------# try: opts, args = getopt.getopt(sys.argv[1:], 'i:l:') except getopt.GetoptError as err: print str(err) usage() sys.exit() #----------------------------- # Parse command line arguments #----------------------------- for opt, arg in opts: #----------- # Input file #----------- if opt == '-i': inputFile = arg # Check if file exists ckFile(inputFile,True) # Option for Log File elif opt == '-l': if arg: logDir = arg else: logDir = os.getcwd() +'/' logFile = True #------------------ # Unhandled options #------------------ else: print 'Unhandled option: ' + opt usage() sys.exit() #------------------------------------------------------------------------------------------------------------# #--------------------------------------- # Hard coded NCEP pressure values [mbar] #--------------------------------------- nmcPress = np.array([0.4,1.0,2.0,5.0,10.0,30.0,50.0,70.0,100.0,150.0,200.0,250.0,300.0,400.0,500.0,700.0,850.0,1000.0]) #---------------- # Read input file #---------------- inputs = {} execfile(inputFile, inputs) if '__builtins__' in inputs: del inputs['__builtins__'] #----------------------------------- # Check the existance of directories # and files given in input file #----------------------------------- # Check directory of NCEP nmc data if not ckDir(inputs['NCEPDir']): print 'NCEP nmc directory does not exist: ' + inputs['NCEPDir'] sys.exit() # check if '/' is included at end of path if not( inputs['NCEPDir'].endswith('/') ): inputs['NCEPDir'] = inputs['NCEPDir'] + '/' # Check directory for output if not ckDir(inputs['outBaseDir']): print 'Output base directory does not exist: ' + inputs['outBaseDir'] sys.exit() # check if '/' is included at end of path if not( inputs['outBaseDir'].endswith('/') ): inputs['outBaseDir'] = inputs['outBaseDir'] + '/' if logFile: # Check directory for log file if not ckDir(logDir): print 'Log File directory does not exist: ' + logDir sys.exit() # check if '/' is included at end of path if not( logDir.endswith('/') ): logDir = logDir + '/' # Check for station layer file if doing interpolation ckFile(inputs['WACCMfile'],True) #-------------------- # Initialize log file #-------------------- if logFile: logFile = logging.getLogger('1') logFile.setLevel(logging.INFO) hdlr1 = logging.FileHandler(logDir+ 'MissingNMCzpt.log',mode='w') fmt1 = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s','%a, %d %b %Y %H:%M:%S') hdlr1.setFormatter(fmt1) logFile.addHandler(hdlr1) logFile.info('**************** Starting Logging ***********************') logFile.info('Start year, month, day: {:4}, {:02}, {:02}'.format(inputs['iyear'],inputs['imnth'],inputs['iday'] ) ) logFile.info('End year, month, day: {:4}, {:02}, {:02}'.format(inputs['fyear'],inputs['fmnth'],inputs['fday'] ) ) logFile.info('Station location: ' + inputs['loc']) #------------------- # Call to date class #------------------- DOI = dr.DateRange(inputs['iyear'],inputs['imnth'],inputs['iday'], # Create a dateRange instance object inputs['fyear'],inputs['fmnth'],inputs['fday']) daysList = DOI.dateList # Create a list of days within date range #---------------------------------- # Initialize missing count based on # how many years present #---------------------------------- years = DOI.yearList() misngCnt = {} for y in years: misngCnt.setdefault(y,0) #-------------------------------------------------------------------- # Read WACCM monthly mean data. WACCM monthly mean file is ascending, # adjust so that it is descending. Also units are in km. #-------------------------------------------------------------------- with open(inputs['WACCMfile'], 'r') as fopen: lines = fopen.readlines() nlyrs = int(lines[0].strip().split()[0]) s_ind = 3 Z = np.flipud( np.array( [ float(row.strip().split()[0]) for row in lines[s_ind:nlyrs+s_ind] ] ) ) waccmT = np.flipud( np.array( [ [float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs+s_ind] ] ) ) s_ind = 3 + nlyrs + 2 waccmP = np.flipud( np.array( [ [float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs+s_ind] ] ) ) s_ind = 3 + nlyrs + 2 + nlyrs + 2 waccmW = np.flipud( np.array( [ [float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs+s_ind] ] ) ) #---------------------- # Loop through day list #---------------------- for i,snglDay in enumerate(daysList): waccmFlg = False #--------------------------- # Determine output directory #--------------------------- mnthstr = "{0:02d}".format(snglDay.month) yearstr = "{0:02d}".format(snglDay.year) daystr = "{0:02d}".format(snglDay.day) outDir = inputs['outBaseDir'] + yearstr+mnthstr+daystr + '/' #------------------------------ # If input directory is missing # skip to next day #------------------------------ if not ckDir(outDir): continue #------------------------------------ # Open and read yearly NCEP nmc files #------------------------------------ if (not('nmcDate' in locals()) or (snglDay.year != lastyr) ): NMChgtFname = inputs['NCEPDir'] + 'HgtNMC_' + inputs['loc'].lower() + '_' + str(snglDay.year) + '.dat' NMCTempFname = inputs['NCEPDir'] + 'TempNMC_' + inputs['loc'].lower() + '_' + str(snglDay.year) + '.dat' #------------------------------------- # NCEP nmc height data along with date # Convert [m] => [km] #------------------------------------- with open(NMChgtFname, 'r') as fopen: lines = fopen.readlines() nmcDate = np.array([dt.date(int(line.strip().split()[0][0:4]), int(line.strip().split()[0][4:6]), int(line.strip().split()[0][6:])) for line in lines if not('#' in line)]) nmcHgtData = np.array( [ [float(x) for x in line.strip().split()[1:]] for line in lines if not('#' in line) ] ) nmcHgtData[:] = nmcHgtData / 1000.0 # Convert [m] => [km] #-------------------------------- # NCEP nmc Temperature data along #-------------------------------- with open(NMCTempFname, 'r') as fopen: lines = fopen.readlines() nmcTempData = np.array( [ [float(x) for x in line.strip().split()[1:]] for line in lines if not('#' in line) ] ) #----------------------------------- # Set year of last profile processed #----------------------------------- lastyr = snglDay.year #-------------------------------------------------- # Merge Pressure, Temperatuer, and H2O data # from WACCM and NCEP nmc. # Note: NCEP nmc has some missing values (-999.999) # Must provide one empty data point between WACCM # and NCEP nmc for interpolation #-------------------------------------------------- #-------------------------------- # Get NCEP nmc and WACCM profiles # according to date #-------------------------------- dateInd = np.where(nmcDate == snglDay)[0] mnthInd = (snglDay.month - 1) # -1 because January is in 0th column #------------------------------------- # If date is entirely missing from nmc # use WACCM #------------------------------------- if ( dateInd.size == 0 ): misngCnt[snglDay.year] += 1 PressOut = waccmP[:,mnthInd] TempOut = waccmT[:,mnthInd] waccmFlg = True #----------------------------- # Date exists in NMC data base #----------------------------- else: dateInd = dateInd[0] # NCEP nmc tmpNMCPres = nmcPress heightNMCprf = nmcHgtData[dateInd,:] tempNMCprf = nmcTempData[dateInd,:] #----------------------------------------- # Look for missing values in NCEP nmc data # If only one missing value...remove, # otherwise use WACCM data. Count number # of times per year WACCM is used #----------------------------------------- misHgt = np.where(heightNMCprf < -900)[0] misT = np.where(tempNMCprf < -900)[0] misAll = np.union1d( misHgt, misT ) #--------------------------------------------- # If union of missing temperatures and heights # is greater than 2 then use WACCM #--------------------------------------------- if len( misAll ) > 2: misngCnt[snglDay.year] += 1 PressOut = waccmP[:,mnthInd] TempOut = waccmT[:,mnthInd] waccmFlg = True #----------------------------------------------------------- # If only two or none values missing, remove and interpolate #----------------------------------------------------------- else: tmpNMCPres[:] = np.delete(tmpNMCPres, misAll) heightNMCprf[:] = np.delete(heightNMCprf,misAll) #---------------------------------------------------- # Construct Height/Pressure profile for interpolation #---------------------------------------------------- # Find top of NMC data nmcTop = heightNMCprf[0] # Where top of NMC fits in WACCM grid topInd = np.argmin( abs( Z - heightNMCprf[0] ) ) #Take two points above top and concatonate #heightIn = np.concatenate( (Z[0:(topInd-inputs['npntSkip'])] ,heightNMCprf), axis=1 ) #pressIn = np.concatenate( (waccmP[0:(topInd-inputs['npntSkip']),mnthInd],nmcPress) , axis=1 ) #tempIn = np.concatenate( (waccmT[0:(topInd-inputs['npntSkip']),mnthInd],tempNMCprf) , axis=1 ) #An error arise with the axis=1, so it was removed heightIn = np.concatenate( (Z[0:(topInd-inputs['npntSkip'])] ,heightNMCprf)) pressIn = np.concatenate( (waccmP[0:(topInd-inputs['npntSkip']),mnthInd],nmcPress) ) tempIn = np.concatenate( (waccmT[0:(topInd-inputs['npntSkip']),mnthInd],tempNMCprf) ) #------------------------------------------------------------------ # Interpolate to grid. X data must be increasing => flip dimensions # and then flip back #------------------------------------------------------------------ PressOut = np.flipud( intrpUniSpl( np.flipud(heightIn), np.flipud(pressIn), k=inputs['Pintrp'])( np.flipud(Z) ) ) TempOut = np.flipud( intrpUniSpl( np.flipud(heightIn), np.flipud(tempIn), k=inputs['Tintrp'])( np.flipud(Z) ) ) #-------------------------------------- # Write to log if NCEP data is not used #-------------------------------------- if waccmFlg and logFile: logFile.error('NCEP nmc data not complete, WACCM profiles used for date: ' + str(snglDay) ) #------------------- # Write out ZPT file #------------------- with open(outDir+'ZPT.nmc.120', 'w') as fopen: fopen.write("{0:>5}{1:>5} \n".format(1,nlyrs)) # Write altitude fopen.write(" ALTITUDE [km] \n") for row in segmnt(Z, 5): strformat = ','.join('{:>12.4f}' for i in row) + ', \n' fopen.write(strformat.format(*row)) # Write Pressure if waccmFlg: fopen.write(" PRESSURE from WACCM V5 monthly mean [mbar] \n") else: fopen.write(" PRESSURE from interpolated NCEP nmc (up to {:5.2f}km) and WACCM V5 monthly mean [mbar] \n".format(nmcTop)) for row in segmnt(PressOut,5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) # Write Temperature if waccmFlg: fopen.write(" TEMPERATURE from WACCM V5 monthly mean [C] \n") else: fopen.write(" TEMPERATURE from interpolated NCEP nmc (up to {:5.2f}km) and WACCM V5 monthly mean [C] \n".format(nmcTop)) for row in segmnt(TempOut,5): strformat = ','.join('{:>12.4f}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #--------------------- # Write out water file #--------------------- #------------------------------------------------- # Determine if previous water files already exists # If so rename the files add '.WACCM5' #------------------------------------------------- if inputs['mvOld']: preWtrFiles = gb.glob(outDir + 'w-120*') if len(preWtrFiles) > 0: for indFile in preWtrFiles: os.rename(indFile,indFile+'.WACCM5') with open(outDir+'w-120.v1','w') as fopen: fopen.write(' 1 H2O from WACCM V5 monthly mean \n') for row in segmnt(waccmW[:,mnthInd],5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #-------------------- # Create plots to pdf #-------------------- if not waccmFlg: # Pressure plt.plot(PressOut,Z,'rx-',label='Interpolated Pressure') plt.plot(waccmP[:,mnthInd],Z,'bx-',label='WACCM V5 Pressure') plt.plot(tmpNMCPres,heightNMCprf,'kx-',label='NCEP nmc Pressure') plt.legend() plt.xlabel('Pressure [mbar]') plt.ylabel('log(Height) [km]') plt.xscale('log') plt.yscale('log') plt.ylim( Z[-1], Z[0] ) ax = plt.gca() ax.set_title('Pressure vs Height for Date:{}, K = {:2.0f}, nskips = {:2.0f}'.format(str(snglDay),inputs['Pintrp'],inputs['npntSkip'])) #ax.xaxis.set_major_locator(MultipleLocator(25)) #ax.xaxis.set_minor_locator(MultipleLocator(5)) plt.savefig(outDir+'PressureFigure.pdf',bbox_inches='tight') plt.close() ## Temperature plt.plot(TempOut,Z,'rx-', label='Interpolated Temperature') plt.plot(waccmT[:,mnthInd],Z,'bx-',label='WACCM V5 Temperature') plt.plot(tempNMCprf,heightNMCprf,'kx-',label='NCEP nmc Temperature') plt.legend() plt.xlabel('Temperature [C]') plt.ylabel('Height [km]') ##plt.yscale('log') plt.ylim( Z[-1], Z[0] ) ax = plt.gca() #ax.xaxis.set_major_locator(MultipleLocator(25)) #ax.xaxis.set_minor_locator(MultipleLocator(5)) ax.set_title('Temperature vs Height for Date:{}, K = {:2.0f}, nskips = {:2.0f}'.format(str(snglDay),inputs['Tintrp'],inputs['npntSkip'])) plt.savefig(outDir+'TemperatureFigure.pdf',bbox_inches='tight') plt.close() #------------------------------------- # Print out number of days where NCEP # nmc data is incomplete => WACCM used #------------------------------------- if logFile: for yrs in misngCnt: logFile.info('Missing days for year ({:4}) = {:4}'.format(yrs,misngCnt[yrs]))
def main(): #---------------- # Initializations #---------------- loc = 'tab' # Name of station location gasName = 'h2o' # Name of gas ver = 'Current_ERA' # Name of retrieval version to process verW = 'v99' # version 99 is used for individual retrievals at specific times #------ # Flags #------ fltrFlg = True # Flag to filter the data maxrms = 2 # Max Fit RMS to filter data. Data is filtered according to <= maxrms pltFlg = True logFlg = True # Flag to do interpolation of log of water #------------------- # Interpolation Oder #------------------- intrpOrder = 1 # Order of interpolation #----------------------- # Date Range of interest #----------------------- iyear = 2017 imnth = 8 iday = 1 fyear = 2017 fmnth = 8 fday = 31 #--------------------- # Input Data Directory #--------------------- retDir = '/data1/ebaumer/' + loc.lower() + '/' + gasName.lower( ) + '/' + ver + '/' # Retrieval data directory #---------------------- # Output Data Directory #---------------------- dataDir = '/data1/' + loc.lower() + '/' #------ # Files #------ ctlFile = '/data1/ebaumer/' + loc.lower() + '/' + gasName.lower( ) + '/' + 'x.' + gasName.lower() + '/sfit4.ctl' #--------------------------------------- # Altitude levels for different stations #--------------------------------------- if loc.lower() == 'tab': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.0000, 0.2250 ]) sLat = 76.52 sLon = 291.23 # 68.77 W = (360.0 - 68.77) = 291.23 E elif loc.lower() == 'mlo': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.3960 ]) sLat = 19.4 sLon = 204.43 # 155.57 W = (360 - 155.57) = 204.43 E elif loc.lower() == 'fl0': Z = np.array([ 120.0000, 110.0000, 100.0000, 94.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.6120 ]) sLat = 40.4 sLon = 254.76 # 105.24 W = (360 - 105.24) = 254.76 E #---------------------------- # File and directory checking #---------------------------- ckDir(retDir, exitFlg=True) ckDir(dataDir, exitFlg=True) ckFile(ctlFile, exitFlg=True) #------------------------------------- # Create instance of output data class #------------------------------------- statDataCl = dc.ReadOutputData(retDir, '', ctlFile, iyear, imnth, iday, fyear, fmnth, fday) #-------------- # Read profiles #-------------- statDataCl.readprfs([statDataCl.PrimaryGas], retapFlg=1) # Retrieved Profiles #---------------------------------- # Read Summary data (For filtering) #---------------------------------- statDataCl.readsummary() #---------------------------- # Get retrieved water profile #---------------------------- H2OrPrf = np.asarray(statDataCl.rprfs['H2O']) dates = np.asarray(statDataCl.rprfs['date']) alt = np.asarray(statDataCl.rprfs['ZBAR'][0, :]) #-------------------- # Call to filter data #-------------------- if fltrFlg: statDataCl.fltrData(statDataCl.PrimaryGas, mxrms=maxrms, rmsFlg=True, tcFlg=True, pcFlg=True, cnvrgFlg=True) else: statDataCl.inds = np.array([]) #----------------------------------- # Remove profiles based on filtering #----------------------------------- H2OrPrf = np.delete(H2OrPrf, statDataCl.inds, axis=0) dates = np.delete(dates, statDataCl.inds) #--------------------------- # Iterate through retrievals #--------------------------- for i, sngDay in enumerate(dates): #---------------------------- # Get corresponding directory #---------------------------- baseDir = dataDir + '{0:04d}{1:02d}{2:02d}/'.format( sngDay.year, sngDay.month, sngDay.day) ckDir(baseDir, exitFlg=True) #----------------------------------- # Interpolate retrieved profile onto # sfit input grid #----------------------------------- if logFlg: H2Oout = np.exp( np.flipud( intrpUniSpl(np.flipud(alt), np.log(np.flipud(H2OrPrf[i])), k=intrpOrder)(np.flipud(Z)))) else: H2Oout = np.flipud( intrpUniSpl(np.flipud(alt), np.flipud(H2OrPrf[i]), k=intrpOrder)(np.flipud(Z))) #--------------------- # Write out water file #--------------------- tstamp = '{0:04d}{1:02d}{2:02d}.{3:02d}{4:02d}{5:02d}'.format( sngDay.year, sngDay.month, sngDay.day, sngDay.hour, sngDay.minute, sngDay.second) with open(baseDir + 'w-120.' + tstamp + '.' + verW, 'w') as fopen: fopen.write(' 1 H2O from sfit4 retrieval \n') for row in segmnt(H2Oout, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #-------------------- # Create plots to pdf #-------------------- if pltFlg: pdfsav = PdfPages(baseDir + 'Ret_' + tstamp + '_WaterProfile.pdf') fig1, ax1 = plt.subplots() ax1.plot(H2Oout, Z, 'rx-', label='Interpolated Water Profile') ax1.plot(H2OrPrf[i], alt, 'bx-', label='Original Retrieved Water Profile') ax1.grid(True, which='both') ax1.legend(prop={'size': 9}) ax1.set_ylabel('Altitude [km]') ax1.set_xlabel('VMR') ax1.tick_params(axis='x', which='both', labelsize=8) ax1.set_ylim((Z[-1], 60)) #ax1.set_xlim((0,np.max((waccmW[-1,mnthInd],dayShum[-1])))) ax1.set_title(sngDay) pdfsav.savefig(fig1, dpi=250) pdfsav.close()
def main(): #--------- # Location #--------- loc = 'tab' #------------------------------------ # Version number to append water file #------------------------------------ verW = 'v4' #version for daily Profile verW_t = 'v66' #version for 6-hourly profiles #----------------------------------------- # Interpolation flag for NCEP re-analysis: # False = Nearest point. True = linear #----------------------------------------- interpFlg = False #--------------------- # Interpolation things #--------------------- nSkip = 3 # Number of points to skip when merging WACCM and NCEP profiles intrpOrder = 1 # Order of interpolation logFlg = False # Flag to do interpolation of log of water #----------------------- # Date Range of interest #----------------------- iyear = 2017 imnth = 6 iday = 1 fyear = 2017 fmnth = 12 fday = 31 #------------------------------ # ERA Reanalysis data directory #------------------------------ ERAdir = '/data1/ancillary_data/ERAdata/' #--------------- # Data Directory #--------------- dataDir = '/data1/' + loc.lower() + '/' #------------------------- # WACCM monthly means file #------------------------- WACCMfile = '/data/Campaign/' + loc.upper( ) + '/waccm/WACCM_pTW-meanV6.' + loc.upper() #--------------------- # Establish date range #--------------------- dRange = sc.DateRange(iyear, imnth, iday, fyear, fmnth, fday) #--------------------------------------- # Altitude levels for different stations #--------------------------------------- if loc.lower() == 'tab': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.0000, 0.2250 ]) sLat = 76.52 sLon = 291.23 # 68.77 W = (360.0 - 68.77) = 291.23 E elif loc.lower() == 'mlo': Z = np.array([ 120.0000, 110.0000, 100.0000, 95.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.3960 ]) sLat = 19.4 sLon = 204.43 # 155.57 W = (360 - 155.57) = 204.43 E elif loc.lower() == 'fl0': Z = np.array([ 120.0000, 110.0000, 100.0000, 94.0000, 90.0000, 85.0000, 80.0000, 75.0000, 70.0000, 65.0000, 60.0000, 55.0000, 50.0000, 48.0000, 46.0000, 44.0000, 42.0000, 40.0000, 38.0000, 36.0000, 34.0000, 32.0000, 30.0000, 28.0000, 26.0000, 24.0000, 22.0000, 20.0000, 18.0000, 16.0000, 14.0000, 12.0000, 10.0000, 9.0000, 8.0000, 7.0000, 6.0000, 5.0000, 4.0000, 3.0000, 2.0000, 1.6120 ]) sLat = 40.4 sLon = 254.76 # 105.24 W = (360 - 105.24) = 254.76 E #---------------------------- # File and directory checking #---------------------------- ckDir(ERAdir, exitFlg=True) ckDir(dataDir, exitFlg=True) ckFile(WACCMfile, exitFlg=True) #-------------------------------------------------------------------- # Read WACCM monthly mean data. WACCM monthly mean file is ascending, # adjust so that it is descending. Also units are in km. #-------------------------------------------------------------------- with open(WACCMfile, 'r') as fopen: lines = fopen.readlines() nlyrs = int(lines[0].strip().split()[0]) s_ind = 3 Z = np.flipud( np.array([ float(row.strip().split()[0]) for row in lines[s_ind:nlyrs + s_ind] ])) waccmT = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 waccmP = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) s_ind = 3 + nlyrs + 2 + nlyrs + 2 waccmW = np.flipud( np.array([[float(x) for x in line.strip().split()[1:]] for line in lines[s_ind:nlyrs + s_ind]])) #-------------------------------------------- # Walk through first level of directories in # data directory and collect directory names #-------------------------------------------- dirLst = [] for drs in os.walk(dataDir).next()[1]: #------------------------------------------- # Test directory to make sure it is a number #------------------------------------------- try: int(drs[0:4]) except: continue if dRange.inRange(int(drs[0:4]), int(drs[4:6]), int(drs[6:8])): dirLst.append(dataDir + drs + '/') dirLst.sort() #------------------------------------------ # Loop through all folders in specific year #------------------------------------------ for sngDir in dirLst: #---------------------------- # Get date in datetime format #---------------------------- oneDay = dt.datetime(int(os.path.basename(sngDir[:-1])[0:4]), int(os.path.basename(sngDir[:-1])[4:6]), int(os.path.basename(sngDir[:-1])[6:8])) #-------------------------------------------------- # Find month index for monthly WACCM water profiles #-------------------------------------------------- mnthInd = oneDay.month - 1 # -1 because January is in the 0th column #------------------------------------------- # Open daily ERA interm files 00, 06, 12, 18 #------------------------------------------- YYYY = "{0:04d}".format(oneDay.year) MM = "{0:02d}".format(oneDay.month) DD = "{0:02d}".format(oneDay.day) ERA_F1 = ERAdir + YYYY + MM + '/' + 'ei.oper.an.pl.regn128sc.' + YYYY + MM + DD + '00.nc' ERA_F2 = ERAdir + YYYY + MM + '/' + 'ei.oper.an.pl.regn128sc.' + YYYY + MM + DD + '06.nc' ERA_F3 = ERAdir + YYYY + MM + '/' + 'ei.oper.an.pl.regn128sc.' + YYYY + MM + DD + '12.nc' ERA_F4 = ERAdir + YYYY + MM + '/' + 'ei.oper.an.pl.regn128sc.' + YYYY + MM + DD + '18.nc' f1 = netcdf.netcdf_file(ERA_F1, 'r', mmap=False) f2 = netcdf.netcdf_file(ERA_F2, 'r', mmap=False) f3 = netcdf.netcdf_file(ERA_F3, 'r', mmap=False) f4 = netcdf.netcdf_file(ERA_F4, 'r', mmap=False) #----------------------------------- # Lat and lon should be the same for # all files. Just grab once #----------------------------------- lat = f1.variables['g4_lat_1'] lon = f1.variables['g4_lon_2'] Plvl = f1.variables['lv_ISBL0'] nlvls = np.shape(Plvl[:])[0] Q_00 = f1.variables['Q_GDS4_ISBL'] Q_06 = f2.variables['Q_GDS4_ISBL'] Q_12 = f3.variables['Q_GDS4_ISBL'] Q_18 = f4.variables['Q_GDS4_ISBL'] Z_00 = f1.variables['Z_GDS4_ISBL'] Z_06 = f2.variables['Z_GDS4_ISBL'] Z_12 = f3.variables['Z_GDS4_ISBL'] Z_18 = f4.variables['Z_GDS4_ISBL'] PV_00 = f1.variables['PV_GDS4_ISBL'] PV_06 = f2.variables['PV_GDS4_ISBL'] PV_12 = f3.variables['PV_GDS4_ISBL'] PV_18 = f4.variables['PV_GDS4_ISBL'] T_00 = f1.variables['T_GDS4_ISBL'] T_06 = f2.variables['T_GDS4_ISBL'] T_12 = f3.variables['T_GDS4_ISBL'] T_18 = f4.variables['T_GDS4_ISBL'] f1.close() f2.close() f3.close() f4.close() #----------------------------------------------- # If not interpolating point in NCEP re-analysis # find closes lat lon indicies #----------------------------------------------- if not interpFlg: latind = findCls(lat[:], sLat) lonind = findCls(lon[:], sLon) #----------------------------------------------------- # For each level interpolate hgt and specific humidity # based on latitude and longitude of site #----------------------------------------------------- Hgt_00 = np.zeros(nlvls) Hgt_06 = np.zeros(nlvls) Hgt_12 = np.zeros(nlvls) Hgt_18 = np.zeros(nlvls) Qint_00 = np.zeros(nlvls) Qint_06 = np.zeros(nlvls) Qint_12 = np.zeros(nlvls) Qint_18 = np.zeros(nlvls) Tint_00 = np.zeros(nlvls) Tint_06 = np.zeros(nlvls) Tint_12 = np.zeros(nlvls) Tint_18 = np.zeros(nlvls) PVint_00 = np.zeros(nlvls) PVint_06 = np.zeros(nlvls) PVint_12 = np.zeros(nlvls) PVint_18 = np.zeros(nlvls) for lvl in range(0, nlvls): HgtOneLvl_00 = np.squeeze(Z_00[lvl, :, :]) HgtOneLvl_06 = np.squeeze(Z_06[lvl, :, :]) HgtOneLvl_12 = np.squeeze(Z_12[lvl, :, :]) HgtOneLvl_18 = np.squeeze(Z_18[lvl, :, :]) Q_OneLvl_00 = np.squeeze(Q_00[lvl, :, :]) Q_OneLvl_06 = np.squeeze(Q_06[lvl, :, :]) Q_OneLvl_12 = np.squeeze(Q_12[lvl, :, :]) Q_OneLvl_18 = np.squeeze(Q_18[lvl, :, :]) T_OneLvl_00 = np.squeeze(T_00[lvl, :, :]) T_OneLvl_06 = np.squeeze(T_06[lvl, :, :]) T_OneLvl_12 = np.squeeze(T_12[lvl, :, :]) T_OneLvl_18 = np.squeeze(T_18[lvl, :, :]) PV_OneLvl_00 = np.squeeze(PV_00[lvl, :, :]) PV_OneLvl_06 = np.squeeze(PV_06[lvl, :, :]) PV_OneLvl_12 = np.squeeze(PV_12[lvl, :, :]) PV_OneLvl_18 = np.squeeze(PV_18[lvl, :, :]) if interpFlg: Hgt_00[lvl] = interp2d(lon[:], lat[:], HgtOneLvl_00, kind='linear', bounds_error=True)(sLon, sLat) Hgt_06[lvl] = interp2d(lon[:], lat[:], HgtOneLvl_06, kind='linear', bounds_error=True)(sLon, sLat) Hgt_12[lvl] = interp2d(lon[:], lat[:], HgtOneLvl_12, kind='linear', bounds_error=True)(sLon, sLat) Hgt_18[lvl] = interp2d(lon[:], lat[:], HgtOneLvl_18, kind='linear', bounds_error=True)(sLon, sLat) Qint_00[lvl] = interp2d(lon[:], lat[:], Q_OneLvl_00, kind='linear', bounds_error=True)(sLon, sLat) Qint_06[lvl] = interp2d(lon[:], lat[:], Q_OneLvl_06, kind='linear', bounds_error=True)(sLon, sLat) Qint_12[lvl] = interp2d(lon[:], lat[:], Q_OneLvl_12, kind='linear', bounds_error=True)(sLon, sLat) Qint_18[lvl] = interp2d(lon[:], lat[:], Q_OneLvl_18, kind='linear', bounds_error=True)(sLon, sLat) Tint_00[lvl] = interp2d(lon[:], lat[:], T_OneLvl_00, kind='linear', bounds_error=True)(sLon, sLat) Tint_06[lvl] = interp2d(lon[:], lat[:], T_OneLvl_06, kind='linear', bounds_error=True)(sLon, sLat) Tint_12[lvl] = interp2d(lon[:], lat[:], T_OneLvl_12, kind='linear', bounds_error=True)(sLon, sLat) Tint_18[lvl] = interp2d(lon[:], lat[:], T_OneLvl_18, kind='linear', bounds_error=True)(sLon, sLat) PVint_00[lvl] = interp2d(lon[:], lat[:], PV_OneLvl_00, kind='linear', bounds_error=True)(sLon, sLat) PVint_06[lvl] = interp2d(lon[:], lat[:], PV_OneLvl_06, kind='linear', bounds_error=True)(sLon, sLat) PVint_12[lvl] = interp2d(lon[:], lat[:], PV_OneLvl_12, kind='linear', bounds_error=True)(sLon, sLat) PVint_18[lvl] = interp2d(lon[:], lat[:], PV_OneLvl_18, kind='linear', bounds_error=True)(sLon, sLat) else: Hgt_00[lvl] = HgtOneLvl_00[latind, lonind] Hgt_06[lvl] = HgtOneLvl_06[latind, lonind] Hgt_12[lvl] = HgtOneLvl_12[latind, lonind] Hgt_18[lvl] = HgtOneLvl_18[latind, lonind] Qint_00[lvl] = Q_OneLvl_00[latind, lonind] Qint_06[lvl] = Q_OneLvl_06[latind, lonind] Qint_12[lvl] = Q_OneLvl_12[latind, lonind] Qint_18[lvl] = Q_OneLvl_18[latind, lonind] Tint_00[lvl] = T_OneLvl_00[latind, lonind] Tint_06[lvl] = T_OneLvl_06[latind, lonind] Tint_12[lvl] = T_OneLvl_12[latind, lonind] Tint_18[lvl] = T_OneLvl_18[latind, lonind] PVint_00[lvl] = PV_OneLvl_00[latind, lonind] PVint_06[lvl] = PV_OneLvl_06[latind, lonind] PVint_12[lvl] = PV_OneLvl_12[latind, lonind] PVint_18[lvl] = PV_OneLvl_18[latind, lonind] #---------------------------------------- # Convert Specific humidity from kg/kg to # molecules/molecules #---------------------------------------- Qint_00 = Qint_00 * 1.608 Qint_06 = Qint_06 * 1.608 Qint_12 = Qint_12 * 1.608 Qint_18 = Qint_18 * 1.608 #------------------------------------- # Create daily averages of 00,06,12,18 #------------------------------------- Q_day = np.mean(np.vstack((Qint_00, Qint_06, Qint_12, Qint_18)), axis=0) Z_day = np.mean(np.vstack((Hgt_00, Hgt_06, Hgt_12, Hgt_18)), axis=0) T_day = np.mean(np.vstack((Tint_00, Tint_06, Tint_12, Tint_18)), axis=0) PV_day = np.mean(np.vstack((PVint_00, PVint_06, PVint_12, PVint_18)), axis=0) #-------------------------------- # Convert Height [m^2 s^-2] => km #-------------------------------- Z_day = Z_day / 9.81 / 1000.0 #--------------------------------------------------------- # Construct specific humidity and height profiles for # interpolation on to the sfit input grid which is the # same as the WACCM height in monthly profile file. # NCEP reanalysis data only goes to 300mb therefore, # merge with monthly averaged WACCM water profiles > 300mb #--------------------------------------------------------- ERAtop = Z_day[0] topInd = np.argmin(abs( Z - ERAtop)) # Where top of NCEP reanalysis fits in WACCM grid height #Zin = np.concatenate( ( Z[0:(topInd-nSkip)] , Z_day), axis=1 ) #SHin = np.concatenate( ( waccmW[0:(topInd-nSkip),mnthInd], Q_day), axis=1 ) #Remove axis=0 Zin = np.concatenate((Z[0:(topInd - nSkip)], Z_day)) SHin = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], Q_day)) SHin_00 = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], Qint_00)) SHin_06 = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], Qint_06)) SHin_12 = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], Qint_12)) SHin_18 = np.concatenate((waccmW[0:(topInd - nSkip), mnthInd], Qint_18)) #-------------------------------------------------------------- # Interpolate to specific humidity on WACCM grid. X data must # be increasing => flip dimensions and then flip back #-------------------------------------------------------------- if logFlg: SHout = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin)), k=intrpOrder)(np.flipud(Z)))) SHout_00 = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin_00)), k=intrpOrder)(np.flipud(Z)))) SHout_06 = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin_06)), k=intrpOrder)(np.flipud(Z)))) SHout_12 = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin_12)), k=intrpOrder)(np.flipud(Z)))) SHout_18 = np.exp( np.flipud( intrpUniSpl(np.flipud(Zin), np.log(np.flipud(SHin_18)), k=intrpOrder)(np.flipud(Z)))) else: SHout = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin), k=intrpOrder)(np.flipud(Z))) SHout_00 = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin_00), k=intrpOrder)(np.flipud(Z))) SHout_06 = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin_06), k=intrpOrder)(np.flipud(Z))) SHout_12 = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin_12), k=intrpOrder)(np.flipud(Z))) SHout_18 = np.flipud( intrpUniSpl(np.flipud(Zin), np.flipud(SHin_18), k=intrpOrder)(np.flipud(Z))) #------------------------ # remove v4 for 6-hourly files #------------------------ waterFiles_test = glob.glob(sngDir + 'w-120*' + verW) waterFiles_test = [ i for i in waterFiles_test if len(os.path.basename(i)) > 10 ] if len(waterFiles_test) >= 1: for f in waterFiles_test: os.remove(f) #--------------------- # Write out water file at 00 #--------------------- with open(sngDir + 'w-120.' + YYYY + MM + DD + '.000000.' + verW_t, 'w') as fopen: fopen.write( ' 1 H2O from ERA reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout_00, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #--------------------- # Write out water file at 06 #--------------------- with open(sngDir + 'w-120.' + YYYY + MM + DD + '.060000.' + verW_t, 'w') as fopen: fopen.write( ' 1 H2O from ERA reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout_06, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #--------------------- # Write out water file at 12 #--------------------- with open(sngDir + 'w-120.' + YYYY + MM + DD + '.120000.' + verW_t, 'w') as fopen: fopen.write( ' 1 H2O from ERA reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout_12, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #--------------------- # Write out water file at 18 #--------------------- with open(sngDir + 'w-120.' + YYYY + MM + DD + '.180000.' + verW_t, 'w') as fopen: fopen.write( ' 1 H2O from ERA reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout_18, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #--------------------- # Write out water file Daily #--------------------- with open(sngDir + 'w-120.' + verW, 'w') as fopen: fopen.write( ' 1 H2O from ERA reanalysis and WACCM V6 monthly mean \n' ) for row in segmnt(SHout, 5): strformat = ','.join('{:>12.4E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #------------------------------ # Write out Temperature, water, # and PV every 6 hours #------------------------------ with open(sngDir + 'ERA_Interm_PV.' + YYYY + MM + DD, 'w') as fopen: fopen.write( 'Daily Averaged and 6 hourly PV from ERA Interim [K m^2 kg^-1 s^-1] \n' ) fopen.write( '{0:>20s}{1:>20s}{2:>20s}{3:>20s}{4:>20s}{5:>20s}\n'.format( 'Height [km]', 'PV[K m^2 kg^-1 s^-1]', 'PV at 00', 'PV at 06', 'PV at 12', 'PV at 18')) for row in zip(*(Z_day, PV_day, PVint_00, PVint_06, PVint_12, PVint_18)): strformat = ','.join('{:>20.7E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) with open(sngDir + 'ERA_Interm_T.' + YYYY + MM + DD, 'w') as fopen: fopen.write( 'Daily averaged and 6 Hourly Temperature from ERA Interim [K] \n' ) fopen.write( '{0:>15s}{1:>15s}{2:>15s}{3:>15s}{4:>15s}{5:>15s}\n'.format( 'Height [km]', 'T [K]', 'T at 00', 'T at 06', 'T at 12', 'T at 18')) for row in zip(*(Z_day, T_day, Tint_00, Tint_06, Tint_12, Tint_18)): strformat = ','.join('{:>15.7E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) with open(sngDir + 'ERA_Interm_Q.' + YYYY + MM + DD, 'w') as fopen: fopen.write( 'Daily averaged and 6 hourly specific humidity Q from ERA Interim [VMR] \n' ) fopen.write( '{0:>15s}{1:>15s}{2:>15s}{3:>15s}{4:>15s}{5:>15s}\n'.format( 'Height [km]', 'Q [VMR]', 'Q at 00', 'Q at 06', 'Q at 12', 'Q at 18')) for row in zip(*(Z_day, Q_day, Qint_00, Qint_06, Qint_12, Qint_18)): strformat = ','.join('{:>15.7E}' for i in row) + ', \n' fopen.write(strformat.format(*row)) #-------------------- # Create plots to pdf #-------------------- pdfsav = PdfPages(sngDir + 'WaterProfile_ERA.pdf') fig1, ax1 = plt.subplots() ax1.plot(SHout_00, Z, color='green', label='Interpolated SH-00') ax1.plot(SHout_06, Z, color='gold', label='Interpolated SH-06') ax1.plot(SHout_12, Z, color='cyan', label='Interpolated SH-12') ax1.plot(SHout_18, Z, color='gray', label='Interpolated SH-18') ax1.plot(SHout, Z, 'rx-', label='Interpolated SH-Mean') ax1.plot(Q_day, Z_day, 'kx-', label='ERA Reanalysis SH-Mean') ax1.plot(waccmW[:, mnthInd], Z, 'bx-', label='WACCM V6 SH') ax1.grid(True, which='both') ax1.legend(prop={'size': 9}) ax1.set_ylabel('Altitude [km]') ax1.set_xlabel('VMR [ppv]') ax1.tick_params(axis='x', which='both', labelsize=8) ax1.set_ylim((Z[-1], 80)) #ax1.set_xlim((0,np.max((waccmW[-1,mnthInd],Q_day[-1])))) ax1.set_title(YYYY + '-' + MM + '-' + DD) pdfsav.savefig(fig1, dpi=250) fig2, ax2 = plt.subplots() ax2.plot(SHout_00, Z, color='green', label='Interpolated SH-00') ax2.plot(SHout_06, Z, color='gold', label='Interpolated SH-06') ax2.plot(SHout_12, Z, color='cyan', label='Interpolated SH-12') ax2.plot(SHout_18, Z, color='gray', label='Interpolated SH-18') ax2.plot(SHout, Z, 'rx-', label='Interpolated SH-Mean') ax2.plot(Q_day, Z_day, 'kx-', label='ERA Reanalysis SH-Mean') ax2.plot(waccmW[:, mnthInd], Z, 'bx-', label='WACCM V6 SH') ax2.grid(True, which='both') ax2.legend(prop={'size': 9}) ax2.set_ylabel('Altitude [km]') ax2.set_xlabel('log VMR [ppv]') ax2.tick_params(axis='x', which='both', labelsize=8) ax2.set_xscale('log') ax2.set_ylim((Z[-1], 80)) #ax2.set_xlim((0,np.max((waccmW[-1,mnthInd],Q_day[-1])))) ax2.set_title(YYYY + '-' + MM + '-' + DD) pdfsav.savefig(fig2, dpi=250) pdfsav.close() print 'Finished processing folder: {}'.format(sngDir)