-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_merra_aerosol_and_dump_ascii.py
87 lines (81 loc) · 3.19 KB
/
read_merra_aerosol_and_dump_ascii.py
1
#!/usr/bin/python'''Module: read_merra_aerosol_and_dump_ascii.py==========================================================================================Disclaimer: The code is for demonstration purposes only. Users are responsible to check for accuracy and revise to fit their objective.Author: Justin Roberts-Pierel, 2015 Organization: NASA ARSETPurpose: To save a MERRA netCDF4 file in ASCII format. Saves lat, lon, and other SDS dependent on file.See the README associated with this module for more information.=========================================================================================='''#import necessary modules import numpy as npimport netCDF4 as nc4import sys #This finds the user's current path so that all hdf4 files can be foundtry: fileList=open('fileList.txt','r')except: print('Did not find a text file containing file names (perhaps name does not match)') sys.exit()#loops through all files listed in the text filefor FILE_NAME in fileList: FILE_NAME=FILE_NAME.strip() user_input=input('\nWould you like to process\n' + FILE_NAME + '\n\n(Y/N)') if(user_input == 'N' or user_input == 'n'): continue else: #read in the data merraData = nc4.Dataset(FILE_NAME, 'r') variables=set(merraData.variables) desiredVariables=set({'AOD','RH','ps','PBLH','TA','US','VS'}) desiredVariables=set([x.lower() for x in desiredVariables]) var1=variables.intersection(desiredVariables) desiredVariables=set([x.upper() for x in desiredVariables]) var2=variables.intersection(desiredVariables) fileVars=list(var1.union(var2)) if len(fileVars)==0: print('This file contains none of the selected SDS. Skipping...') continue print('Saving the following SDS from current file: \n') [print('(' + str(fileVars.index(x)) + ')',x) for x in fileVars] #extract lat and lon info. These are just vectors in the dataset so they're repeated to accommodate the data array lats=merraData.variables['lat'][:] lons=merraData.variables['lon'][:] totalLon=np.tile(lons,len(lats)) totalLat=lats.repeat(len(lons)) #create a matrix the same size as the lat/lon datasets to save everything output=np.zeros((totalLat.shape[0],len(fileVars)+2)) output[:,0]=totalLat output[:,1]=totalLon #can't combine string and floats in an array, so a list of titles is made tempOutput=[] tempOutput.append('Latitude') tempOutput.append('Longitude') index=2 for SDS_NAME in fileVars: try: #read merra data as a vector data=merraData.variables[SDS_NAME][:] except: print('There is an issue with your MERRA file (might be the wrong MERRA file type). Skipping...') continue if len(data.shape) ==4: level=data.shape[1]-1 data=data[0,level,:,:] elif len(data.shape) == 3: level=data.shape[0]-1 data=data[level,:,:] data=data.ravel() #save variable output[:,index]=data tempOutput.append(SDS_NAME) index+=1 #stacks the titles on the data array to be saved to file output=np.row_stack((tempOutput,output)) #create the name of the file from the filename outputName='{0}.txt'.format(FILE_NAME[:-4]) #save the file np.savetxt(outputName,output,delimiter=',',fmt='%s')print('\nAll valid files have been saved successfully.')