Python Forum
I need assistance with re-gridding in python
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I need assistance with re-gridding in python
#1
I am working with climate model data from several models and would like to be able to interpolate each model's grid to a common model grid. I would like to do this because each model has a different grid resolution (based on lines of latitude and lines of longitude), so in order to maintain consistency, I would need to transform a given model's resolution to a base model (let's say that the base model's name is "modelprime", so every other model would have to be converted to modelprime's resolution). Essentially, I am trying to re-grid.

I am also working with two separate python code files - one is the main code file that I am running with in python, and the other file (called "functions_grid.py") already contains all of the necessary code elements to make the grid transformation, but I just need for it to be detected in the main file somehow. The goal is to first be able to get the main code file to recognize the other code file (again, "functions_grid.py") and then create an "if/else" or maybe "if/then" statement in order to make the grid transformation.

I believe that only 3 or so lines of code are required to do all of this, so it seems like something straight-forward, but it is not obvious to me. To begin, as a first step, I imported the code containing the necessary elements (again, "functions_grid.py") by using the following line:

import functions_grid as fct_g
The first lines of code in the other file ("functions_grid.py") is this, if it helps:

def interpolate_to_model(data,lat,lon,model): 
# getting the new coordinates 
    if model == 'modelprime': 
       nc_grid = netcdf.Dataset(
Where "data" is the variable that contains the computed values for over a model's grid, and "model" would be the model to be converted. "lat" and "lon" might both be written as a colon to consider all lines of latitude and longitude?

So, my question is two-fold:

How do I read off of the other code file (the one that already contains all of the necessary elements for the grid transformation - i.e. "functions_grid.py")? Would it involve using the "fct_g" in the import function?

How should I construct the if/else statement to get a certain model's resolution converted to modelprime's?

This is what I have so far:

import functions_grid as fct_g
transformation = fct_g.interpolate_to_model(onedaymax, fivedaymax, :, :, newmodel):
        if model = 'modelprime':
I am not sure if that is the right approach, but what I did there was import the file with the necessary elements to make the transformation, and then tried to have my code detect that file by using the variable, "transformation". "onedaymax" and "fivedaymax" are two variables that are matrices and already have computed maximum value data over the grid corresponding to the current model (called "newmodel"). The idea is to get newmodel's grid to convert to modelprime's, so that the computed values (in "onedaymax' and "fivedaymax") can be placed on modelprime's grid. I'm not sure, though, if both variables ("onedaymax" and "fivedaymax") can be included like that or need to be handled separately. I'm also not sure if the two colons place there are correctly use - they are intended to take into account all lines of latitude and longitude.

What do you think? Any help would be extremely valuable and immensely appreciated!!

Thanks, and I look forward to your response.
Reply
#2
I just tried this:

import functions_grid as fct_g
transformation = fct_g.interpolate_to_model(onedaymax, fivedaymax, :, :, newmodel):
        if transformation = 'newmodel':
             return 'modelprime'
No luck with that code, but could this be on the right track?
Reply
#3
I then tried this, introducing a for loop, but I don't know if that is relevant:

for t in len(onedaymax):
        for r in len(fivedaymax):
                transformation = fct_g.interpolate_to_model(onedaymax, fivedaymax, :, :, GFDL-ESM2G):
        if transformation == 'GFDL-ESM2G':
                return 'CanESM2'
I receive a syntax error in line 3, when defining transformation.

Any suggestions would be greatly appreciated!
Reply
#4
I discovered that the above was incorrect, and that the "interpolate_to_model" function can only handle one variable at a time. Thus, I separated both variables (onedaymax and fivedaymax) as in the following:

if
    onedaymax_rg,lat_rg,lon_rg      = fct_g.interpolate_to_model(onedaymax,lat,lon,'modelprime')
    fivedaymax_rg,lat_rg,lon_rg      = fct_g.interpolate_to_model(fivedaymax,lat,lon,'modelprime')
The above code is correct, but I need an opening if statement (which is blank in the code above), so just one more line of code is necessary to use the above properly.

What about an if statement like this:

" If 'newmodel' is not 'modelprime': "

Does that make sense?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Code Assistance bfpa40 2 2,333 Oct-24-2018, 01:40 AM
Last Post: bfpa40
  Python Assistance Kodi Addon yuljk 0 3,640 Aug-12-2017, 03:02 PM
Last Post: yuljk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020