Example #1
0
def CreateNETCDFsphere( fixedDatetimeString, fixedAltitude, LatitudeStep, LongitudeStep ):
    
    # if no date is provided then return an empty string
    if fixedDatetimeString == "":
        return "" # <<<<
    
    # convert the Time-string to Unix Timestamp
    aTimeString = fixedDatetimeString[:-6] + " " + "+0000" # indicate explicity that this is UTC time
    TimeObj = datetime.strptime( aTimeString, "%b %d %Y %H:%M:%S.%f %z" ) 
    FixedTimestamp = float( datetime.timestamp(TimeObj) ) * 1000  # timestamp in milliseconds

    # copy the standard surface file to a temporary location, where we will process it
    shutil.copyfile( DaedalusGlobals.Temporary_Files_Path+"SurfaceTemplate.nc", DaedalusGlobals.Temporary_Files_Path+"Surface.nc" )

    # open file
    CDFroot =  Dataset( DaedalusGlobals.Temporary_Files_Path+"Surface.nc", "a")

    # update general attributes
    CDFroot.Type = "surface"
    CDFroot.Calculated = "no" # reset state
    CDFroot.ResetTime = str(datetime.now()) # log

    # fill with pre-fabricated data for all Latitude-Lonitude pairs according to their respective steps
    n = 0;
    for i in np.arange(87.5, -92.5, -5.0):
        for j in np.arange(-180.0,  180.0, 5.0):
            CDFroot.variables["lat"][n] = i + 0.00000001*n # add a tiny offset, so that Panoply can plot the data as trajectory
            CDFroot.variables["lon"][n] = j + 0.00000001*n # add a tiny offset, so that Panoply can plot the data as trajectory
            #print( round(float(CDFroot.variables["lat"][n]),2), "    ",  round(float(CDFroot.variables["lon"][n]),2) )
            CDFroot.variables["time"][n]     = FixedTimestamp + n # add a tiny offset, so that Panoply can plot the data as trajectory
            CDFroot.variables["altitude"][n] = fixedAltitude
            CDFroot.variables["ALFA"][n]     = np.random.random_sample() # assign random values for test purposes
            n = n + 1

    # close file
    CDFroot.close() 
    
    
       
    return  DaedalusGlobals.Temporary_Files_Path+"Surface.nc"
Example #2
0
def ResetState_of_NETCDF_file( FullpathFilename):
    CDFroot =  Dataset( FullpathFilename, "a") # open file
    CDFroot.Calculated = "no" # reset state
    CDFroot.ResetTime = str(datetime.now()) # log 
    CDFroot.close() # close file
Example #3
0
def Interpolator(model_data_file, orbit_file):
# model data file--> netcdf to read model data from
# orbit file--> netcdf to read orbit from 
# save--> Logical:: if true saves interpolated values to directory
# VAR--> string array:: variables to inteprolate, must be  one of the variables included in the model data
#Outputs:: Plots+ 1 csv file

    if orbit_file=="":
        return "" # <<<<

    save=True
    VAR=["NE","DEN","HE","NO","O1","O2","OP","TN","UI_ExB","VI_ExB","WI_ExB","UN","VN"]
    Parallel=False
    F90=False

    #***************Read Model Data and Orbit Files*****************************
    # Interpolation Min and Max Altitude Along Track Depending on Model and User Preference
    min_alt=110      #min altitude to interpolate
    max_alt=450      #max altitude to interpolate      
    srt=1            # daedalus sampling rate

    # Copy the orbit file from the OrbitData folder to ModelsOutput folder
    # TODO: if file exists just open it for append and check the Calculated attribute
    orbit_path = orbit_file[ 0 : orbit_file.rfind("/")+1]
    orbit_name = orbit_file[ orbit_file.rfind("/")+1 : -3]
    result_filename = DaedalusGlobals.Interpolated_Files_Path + orbit_name + "_Interpolated" + ".nc"
    if os.path.isfile(result_filename)==False  or  "Surface" in result_filename:
        copyfile(orbit_file, result_filename)
    
    # Get data from Orbit
    resultCDF=Dataset(result_filename, "a")
    
    if resultCDF.Calculated == "yes":
        print ("Skipping calculation because values are valid for" , result_filename)
        return result_filename # <<<<
    
    daed_lat_temp = resultCDF.variables['lat'][:]
    daed_lon_temp = resultCDF.variables['lon'][:]
    daed_alt_temp = resultCDF.variables['altitude'][:]
    daed_time_temp = resultCDF.variables['time'][:]

    # Get data from Model
    TIEGCM=Dataset(model_data_file)
    grid_lat=TIEGCM.variables['lat'][:]
    grid_lon=TIEGCM.variables['lon'][:]
    grid_lev=TIEGCM.variables['ilev'][:]
    grid_time=TIEGCM.variables['time'][:]
    zg=TIEGCM.variables['ZG'][:]


    #Find model's temporal resolution
    model_dt=(grid_time[1]-grid_time[0])*60   #in seconds
    orbit_dt=1/srt                            #in seconds

    # Sample Orbit and Find Positions Below 450km
    counter=0
    for i in range(0,len(daed_alt_temp)):
        if (daed_alt_temp[i] < max_alt and daed_alt_temp[i] > min_alt):
            counter=counter+1
    # **************************************************************************



    # ***************Allocate Arrays********************************************
    daed_lat=np.zeros((counter))
    daed_lon=np.zeros((counter))
    daed_alt=np.zeros((counter))
    daed_time=np.zeros((counter),dtype=datetime)
    index=[None]*counter
    int_final=[None]*len(daed_alt_temp)

    #Keep Data in MIN MAX Range For the Interpolation
    counter=0
    for i in range(0,len(daed_alt_temp)):
        if (daed_alt_temp[i] < max_alt and daed_alt_temp[i] > min_alt):
            daed_time[counter]=daed_time_temp[i]
            daed_lat[counter]=daed_lat_temp[i]
            daed_lon[counter]=daed_lon_temp[i]
            daed_alt[counter]=daed_alt_temp[i]
            index[counter]=i          #keep indices for merging data
            counter=counter+1

    #Transfrom Lon to Match TIEGCM (Orbit Longitudes range from 0 to 360 deg)
    for i in range (0, len(daed_alt)):
            daed_lon[i]=daed_lon[i]-180
            daed_lat[i]=  geod_lat2geo_lat(daed_lat[i])


    # **************************************************************************



    # **************************************************************************
    #Allocate a matrix for the interpolated values
    int_data=np.zeros(((counter)))
    # **************************************************************************


    #======================Call Interpolation Subroutines=======================
    print("Interpolation Starting...",counter*len(VAR), "Positions to Interpolate")
    t1=time.time()
    exports=0
    for jj in range(0,len(VAR)):
        # Select Variable to Interpolate
        ne=TIEGCM.variables[VAR[jj]][:]

        # ********************FORTRAN Subroutines***********************************
        if F90 == True:
            if Parallel==True:
                print(VAR[jj])
                interpolation_OpenMP.daed_interp(grid_lat,grid_lon,grid_lev,daed_lat,daed_lon,daed_alt,zg,model_dt,orbit_dt,ne,int_data)
            else:
                print(VAR[jj])
                interpolation.daed_interp(grid_lat,grid_lon,grid_lev,daed_lat,daed_lon,daed_alt,zg,model_dt,orbit_dt,ne,int_data)
        else:
        # ********************Python Subroutines***********************************
            if Parallel==True:
                print(VAR[jj])
                if __name__=="__main__":
                    pool=multiprocessing.Pool(2)
                    func=partial(Interpolate_Parallel,grid_lat,grid_lon,grid_lev,daed_lat,daed_lon,daed_alt,zg,model_dt,orbit_dt,ne)
                    i=range(len(daed_alt))
                    int_data=pool.map(func,i)
                    pool.close()
                    pool.join()
            else:
                print(VAR[jj])
                int_data=Interpolate_Serial(grid_lat,grid_lon,grid_lev,daed_lat,daed_lon,daed_alt,zg,model_dt,orbit_dt,ne)
        # **************************************************************************
    #===============================================================================

        # Merge Interpolated Values to Full Orbit Array (For printing full orbit)
        for i in range(0,len(int_data)):
            int_final[index[i]]=int_data[i]
        # **************************************************************************

        if save==True:
            resultCDF.variables[VAR[jj]][:]= int_final
            # **********************************************************************ยง

    t2=time.time()
    resultCDF.Calculated = "yes"
    resultCDF.EditTime   = str(datetime.now())
    resultCDF.close()    
    
    print("Interpolation Finished in ",str(t2-t1),"s")
    
    return result_filename