<div dir="ltr"><div><div><div><div><div><div><div><div>Hello,<br><br></div>Once a function/subroutine is placed in a module, two 'things' must be done:<br><br></div>[1] A module must be compiled <b>prior</b> to its invocation by other program units<br></div>[2] The program unit(s) invoking the function/subroutine contained within a module must apply the USE statement.<br><br></div>Attached are two fortran codes. Note the module is compiled 1st. <br></div><div>Also, a side point, these could be in one file as long as the module is compiled prior to other units. <br></div><div><br></div>%> gfortran toy_module.f90 test.toy_module.f90<br></div>%> ./a.out<br><br> z=   6.8999996<br><br>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br><br></div>Likely, you already know the above .<br>However, I thought it should be mentioned for others who might want to do something similar to what you are doing.<br><br>%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%<br><br></div>OK ... how to accommodate using a subroutine contained within a module from NCL.<br><div><div><div><div><div><div><div><div><div><div><div><br>[1] *THE* issue is that NCL's  WRAPIT  understands only f77 syntax. <br>[2] The  'USE' statement is f90. Further, it must/should appear even before an 'implicit none' statement.<br></div><div>[3] WRAPIT would likely fail (untested if this is true) if the USE statement were encountered between the 'C NCLFORTSTART' and 'C NCLEND' delimiters.<br></div><div>[4] My opinion is that you must add a 2nd subroutine ... a bit of a nuisance. <br><br>---<br>In the following, the 77 and 90 suffixes are used to differentiate the program subroutines. They have no language meaning.<br><br>The 1st subroutine [mult77]  contains the WRAPIT recognized delimiters:  'C NCLFORTSTART' and 'C NCLEND'.  WRAPIT will create the necessary infrastructure for argument passing between arguments between NCL  (written in C) and fortran.<br><br></div><div>The 2nd subroutine is a valid program unit invokes the 'USE' statement. <br><br></div><div>Again, the USE statement could not be simply added to the 1st subroutine because WRAPIT would not know what to do with it!<br></div><div><br>C NCLFORTSTART<br>      subroutine<b> mult77</b>(x,y,z)<br></div><div>      implicit none<br></div><div>      real x,y,z<br>C NCLEND<br>      call <b>mult90</b>(x,y,z)<br>      return<br>      end<br>C------------<br>      subroutine <b>mult90</b>(x,y,z)<br>      <b>use module_mult</b><br>      implicit none<br>      real, intent(in)  :: x,y<br>      real, intent(out) :: z<br><br>      call <b>mult</b>(x,y,z)<br>      return<br>      end<br><br>================<br></div><div>Test the NCL call to invoke the module. Note the module is compiled 1st.<br><br>%> <b>WRAPIT</b> toy_module.f90 toy_mult7790.f<br></div><div><br></div><div>This will create '<b>toy_module.so</b>'<br><br></div><div>The following 'toy_test.ncl' contains:<br><br>external TOY_MODULE "./toy_module.so"<br><br><br>   xNCL = 2.3<br>   yNCL = 3.0<br>   zNCL = 1e20<br>   <b>TOY_MODULE::mult77</b>(xNCL, yNCL, zNCL)  ; interface with f77 which calls f90<br>   print("zNCL="+zNCL)<br></div><div><br>=====<br></div><div>%> ncl toy_test.ncl<br><br>(0)    zNCL=6.9<br><br>==================<br></div><div>Hopefully, this is somewhat clear.<br><br></div><div>Good luck<br></div></div></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 11, 2018 at 3:47 PM, Michael Toy <span dir="ltr"><<a href="mailto:toy@ucar.edu" target="_blank">toy@ucar.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hello,<div><br></div><div>I am trying to call a fortran subroutine from an NCL script, and have followed the documentation for “WRAPIT”.  This is my first time trying it.  When I enter the following in NCL:</div><div>ncl 0> external MLT “./mult.so"</div><div><br></div><div>I get the error message:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">warning:An error occurred loading the external file ./mult.so, file not loaded</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">./mult.so: undefined symbol: mult_</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">warning:error at line 0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div></div><div><br></div><div>I’m testing a simple subroutine which multiplies two numbers together.  My Fortran 90 code (mult.f90) is:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">module module_mult</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">contains</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">subroutine mult(x,y,z)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">implicit none</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">real, intent(in) :: x,y</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">real, intent(out) :: z</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">z = x*y</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">end subroutine mult</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">end module module_mult</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255);min-height:15px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div></div><div><br></div><div>My “stub” file is:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">C NCLFORTSTART</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">      subroutine mult(x,y,z)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">      real x,y,z</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">C NCLEND</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><br></div><div>When I run “WRAPIT mult.stub mult.f90” it seems to run successfully, and produces the file:</div><div>mult.so</div><div><br></div><div><br></div><div>I am running NCL version 6.4.0 on the following system:</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Monaco;background-color:rgb(255,255,255)"><span style="font-variant-ligatures:no-common-ligatures">Linux x86_64 x86_64 x86_64 GNU/Linux</span></div></div><div><div><br class="m_8796665186174588599webkit-block-placeholder"></div><div>I know this problem has come up in the NCL users’ forum before, but I’ve not seen how the issue gets resolved.  Your help would be appreciated.  Thanks.</div><div><br></div><div>Best regards,</div><div>Mike</div></div></div><br>______________________________<wbr>_________________<br>
ncl-talk mailing list<br>
<a href="mailto:ncl-talk@ucar.edu">ncl-talk@ucar.edu</a><br>
List instructions, subscriber options, unsubscribe:<br>
<a href="http://mailman.ucar.edu/mailman/listinfo/ncl-talk" rel="noreferrer" target="_blank">http://mailman.ucar.edu/<wbr>mailman/listinfo/ncl-talk</a><br>
<br></blockquote></div><br></div>