[ncl-talk] Dynamic Variable Names (with unique prefix/suffix)
Mary Haley
haley at ucar.edu
Sun Oct 25 22:43:39 MDT 2015
[I meant to post this back to ncl-talk. Please post all follow-up back to
the list.]
On Sun, Oct 25, 2015 at 10:40 PM, Mary Haley <haley at ucar.edu> wrote:
> Alan,
>
> What exactly is happening when you try to run this script? It shouldn't
> matter if you are using ".gt" versus ".ge.", for example, but it's hard to
> debug your script without knowing what kind of error you're getting.
>
> Have you visit the "system" examples page, which show various scripting
> "tricks" that might help:
>
> http://www.ncl.ucar.edu/Applications/system.shtml
>
> --Mary
>
>
> On Fri, Oct 23, 2015 at 6:31 PM, Alan Rhoades <alan.m.rhoades at gmail.com>
> wrote:
>
>> PS For some reason, I've been having trouble creating an elevational
>> band using the where function by using .gt. and .lt. in tandem. Currently,
>> the bash/NCL script I sent you only uses the where function for either .lt.
>> or .gt. elevational masking. The ideal situation would be to create
>> elevational bands between 0 m to 500 m, 500 m to 1000 m, etc. I tried
>> using a starting elevation and bounding elevation in the attached bash/NCL
>> script, but it wasn't working properly for some reason.
>>
>> I've tried the following without success...
>>
>> ${range}_${period}_${var}_${data}_${elev}_a =
>> where(PHIS_${data}_${range}.ge.${elev} .and.
>> PHIS_${data}_${range}.le.${bound}
>> ,${range}_${period}_${var}_${data}_${elev}_a@
>> _FillValue,${range}_${period}_${var}_${data}_${elev}_a)
>>
>> Have you ever heard of any issues doing this? It may be an issue with my
>> bash script, but I can't seem to track down the reason why.
>>
>> On Fri, Oct 23, 2015 at 5:24 PM, Alan Rhoades <alan.m.rhoades at gmail.com>
>> wrote:
>>
>>> Hi Mary,
>>>
>>> After tinkering around with your idea, I decided to figure out a way to
>>> do what I wanted in a bash script. It took me a bit of time, but I was
>>> able to put together the attached bash script that writes an NCL script and
>>> plots across several distinctions (i.e., mountain ranges, four variables,
>>> four elevation classes, and three climatological time periods). I quickly
>>> realized when I was doing the elevation classes that making an NCL script
>>> outside of bash scripting was becoming a little cumbersome because of all
>>> of the variables at play and I needed a way to create dynamic naming
>>> conventions for prefix/suffix for a given NCL variable. FWIW, I
>>> attached the bash script to this email along with a printout made by it. I
>>> use the plotter as a visual check to make sure that elevational band masks
>>> are working (see attached *pdf for more details).
>>>
>>> Thanks again for your help! Hope you have a good weekend.
>>>
>>> AR
>>>
>>> On Tue, Oct 20, 2015 at 3:11 PM, Mary Haley <haley at ucar.edu> wrote:
>>>
>>>> AR,
>>>>
>>>> You can't do dynamic variables the way you've written it, but there may
>>>> be a way to do what you want, if I could know more about what you need to
>>>> do with such a variable. The special "$...$" type of syntax can be used
>>>> when referencing file variables, both reading and writing.
>>>>
>>>> For example, if you need to write the results of stat_dispersion to a
>>>> NetCDF file, then you might do something like:
>>>>
>>>> ;------------------------------------------------------
>>>> ; OPEN NEW NETCDF FOR WRITING
>>>> ;------------------------------------------------------
>>>> output_filename = "test.nc"
>>>> system("rm -rf " + output_filename)
>>>> fout = addfile(output_filename,"c")
>>>>
>>>> ;------------------------------------------------------
>>>> ; STAT DISPERSION
>>>> ;------------------------------------------------------
>>>> opt = True
>>>> opt at PrintStat = True
>>>>
>>>> vars = (/"SNOWFALL","SNOWC","SWE","TSA"/)
>>>> elev = (/"500","1000","1500","2000"/)
>>>> nvars = dimsizes(vars)-1
>>>> nelev = dimsizes(elev)-1
>>>>
>>>> do n=0,nvars
>>>> do k=0,nelev
>>>> varname = "Sierra_STAT_VR_CESM_28_HIST_"+vars(n)+elev(k)
>>>> print("VR-CESM28 - HISTORICAL - AVG DJF Sierra Nevada "+elev(k)+"
>>>> "+vars(n)+"")
>>>>
>>>> ;---Write calculation directly to NetCDF file
>>>> fout->$varname$ =
>>>> stat_dispersion(oned_+vars(n)+_VR_CESM_28_HIST_+elev(k)+,opt)
>>>>
>>>> end do
>>>> end do
>>>>
>>>> Note that this is a very inefficient way to write a NetCDF file. Mostly
>>>> I just want to show how to use the $...$ syntax.
>>>>
>>>> If this doesn't answer your question, please post back to ncl-talk.
>>>>
>>>> --Mary
>>>>
>>>>
>>>>
>>>> On Tue, Oct 20, 2015 at 12:33 PM, Alan Rhoades <
>>>> alan.m.rhoades at gmail.com> wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> I'm currently trying to make dynamic variable names within NCL (i.e.,
>>>>> with unique prefix and/or suffix, but consistent patterns for each
>>>>> variable). I've tried to track down a solution for my current problem via
>>>>> the NCL archives, but can't seem to find it. A post in 2010 said that this
>>>>> type of NCL scripting wasn't yet possible, but I was curious if an update
>>>>> had occurred.
>>>>>
>>>>> I'm trying to do the following...
>>>>>
>>>>> Assess several different mountain ranges
>>>>> With four different variables (SNOWFALL, SNOWC, SWE, TSA)
>>>>> Across four different elevation classes (500 m, 1000 m, 1500 m, 2000 m)
>>>>>
>>>>> This is my current stab at making the dynamic variable names within
>>>>> NCL, but of course, it isn't working (although the print statement is
>>>>> working, the NCL functions just don't like the syntax used to name the
>>>>> dynamic variable names). Here is a snippet of the code...
>>>>>
>>>>> ;------------------------------------------------------
>>>>> ; STAT DISPERSION
>>>>> ;------------------------------------------------------
>>>>> opt = True
>>>>> opt at PrintStat = True
>>>>>
>>>>> vars = (/"SNOWFALL","SNOWC","SWE","TSA"/)
>>>>> elev = (/"500","1000","1500","2000"/)
>>>>> nvars = dimsizes(vars)-1
>>>>> nelev = dimsizes(elev)-1
>>>>>
>>>>> do n=0,nvars
>>>>> do k=0,nelev
>>>>> print("VR-CESM28 - HISTORICAL - AVG DJF Sierra Nevada "+elev(k)+"
>>>>> "+vars(n)+"")
>>>>> Sierra_STAT_VR_CESM_28_HIST_+vars(n)+elev(k)+ =
>>>>> stat_dispersion(oned_+vars(n)+_VR_CESM_28_HIST_+elev(k)+,opt)
>>>>>
>>>>> end do
>>>>> end do
>>>>>
>>>>> Do you have any advice on how to move forward on this? I have heard
>>>>> that it is possible to read in variable names via a shell script into NCL,
>>>>> but I can't track down an example of how to do this either.
>>>>>
>>>>> Thanks in advance,
>>>>>
>>>>> AR
>>>>>
>>>>> --
>>>>>
>>>>> *Alan Rhoades*
>>>>> *PhD Student, Atmospheric Science Graduate Group*
>>>>> *Climate Change Water and Society (CCWAS) NSF IGERT Trainee*
>>>>> *University of California, Davis*
>>>>> *LinkedIn <https://www.linkedin.com/pub/alan-rhoades/22/5bb/52a>*
>>>>> *alan.m.rhoades at gmail.com <alan.m.rhoades at gmail.com> *
>>>>> *amrhoades at ucdavis.edu <amrhoades at ucdavis.edu>*
>>>>>
>>>>> *"It’s all really there. That’s what really gets you. But you gotta
>>>>> stop and think about it to really get the pleasure about the complexity,
>>>>> the inconceivable nature of nature."*
>>>>> *Richard Feynman*
>>>>>
>>>>> _______________________________________________
>>>>> ncl-talk mailing list
>>>>> ncl-talk at ucar.edu
>>>>> List instructions, subscriber options, unsubscribe:
>>>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>>>>>
>>>>>
>>>>
>>>
>>>
>>> --
>>>
>>> *Alan Rhoades*
>>> *PhD Student, Atmospheric Science Graduate Group*
>>> *Climate Change Water and Society (CCWAS) NSF IGERT Trainee*
>>> *University of California, Davis*
>>> *LinkedIn <https://www.linkedin.com/pub/alan-rhoades/22/5bb/52a>*
>>> *alan.m.rhoades at gmail.com <alan.m.rhoades at gmail.com> *
>>> *amrhoades at ucdavis.edu <amrhoades at ucdavis.edu>*
>>>
>>> *"It’s all really there. That’s what really gets you. But you gotta stop
>>> and think about it to really get the pleasure about the complexity, the
>>> inconceivable nature of nature."*
>>> *Richard Feynman*
>>>
>>
>>
>>
>> --
>>
>> *Alan Rhoades*
>> *PhD Student, Atmospheric Science Graduate Group*
>> *Climate Change Water and Society (CCWAS) NSF IGERT Trainee*
>> *University of California, Davis*
>> *LinkedIn <https://www.linkedin.com/pub/alan-rhoades/22/5bb/52a>*
>> *alan.m.rhoades at gmail.com <alan.m.rhoades at gmail.com> *
>> *amrhoades at ucdavis.edu <amrhoades at ucdavis.edu>*
>>
>> *"It’s all really there. That’s what really gets you. But you gotta stop
>> and think about it to really get the pleasure about the complexity, the
>> inconceivable nature of nature."*
>> *Richard Feynman*
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20151025/0fce4b95/attachment.html
More information about the ncl-talk
mailing list