Thanks a lot lil'  (?) Dennis,<div style="font-size: 12.0pt;font-family: Arial;"><p><br></p><p style="font-size: 12pt; font-family: Arial;">I managed to make the subroutine work; the problem was that, as you did for your subroutine, the return argument must be declared and given to the subroutine as argument. I attached the fortran script I used as reference, and below is the NCL code snippet to use the subroutine.</p><p style="font-size: 12pt; font-family: Arial;"><br></p><p><span style="font-size: 12pt; font-family: Arial;">external REGCOEF_N_ZEROINTERCEPT "./regcoef_dp_zerointercept.so"</span></p><p><span style="font-size: 12pt; font-family: Arial;"><br></span></p><p><span style="font-size: 12pt; font-family: Arial;">rcoef = 0.d    ; defined in fortran sub as double</span></p><p><span style="font-size: 12pt;"> tval  = 0.d</span></p><p><span style="font-size: 12pt;"> xave  = 0.d</span></p><p><span style="font-size: 12pt;"> yave  = 0.d</span></p><p><span style="font-size: 12pt; font-family: Arial;"> rstd  = 0.d</span></p><p><span style="font-size: 12pt;"> yint  = 0.d</span></p><p><span style="font-size: 12pt;"> nptxy = 0      ; defined in fortran sub as integer</span></p><p><span style="font-size: 12pt;"> ier   = -9 </span></p><p><span style="font-size: 12pt; font-family: Arial;">REGCOEF_N_ZEROINTERCEPT::DREGCOEF_0Y(ndtooned(xoks_allresponse1_notrac),\</span><br></p><p><span style="font-size: 12pt; font-family: Arial;">                                                                                    ndtooned(xoks_aporesponse1_notrac_multip(0,:,:)),\</span></p><p><span style="font-size: 12pt; font-family: Arial;">                                                                                    nlat*nlon,\</span></p><p><span style="font-size: 12pt; font-family: Arial;">                                                                                    xoks_allresponse1_notrac@_FillValue,\</span></p><p><span style="font-size: 12pt; font-family: Arial;">                                                                                    xoks_aporesponse1_notrac_multip@_FillValue,\</span></p><p><span style="font-size: 12pt; font-family: Arial;">                                                                                    rcoef,tval,nptxy,xave,yave,rstd,yint,ier)</span></p><p><span style="font-size: 12pt; font-family: Arial;"> xoks_aporesponse1_notrac_nointer_regres = tofloat(rcoef)</span></p><div style="font-size: 12.0pt;font-family: Arial;"><br></div><p style="font-size: 12pt; font-family: Arial;">Cheers,</p><p style="font-size: 12pt; font-family: Arial;">lil' Giorgio (aka Giorgino)</p><br>        <blockquote><br>        <br>   ------ Messaggio Originale ------<br>   Da: shea@ucar.edu<br>   A: g.graffino@tim.it<br>Cc: ncl-talk@ucar.edu<br>   Inviato: martedì 1 novembre 2022 21:33<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        <div dir="ltr"><div>Buongiorni Giorgino,</div><div>I will look at your code later today [Tuesday].</div><div>However, in my previous response to which I attached two sample fortran codes, I should have attached an NCL script which uses the shared object [.sp] file.</div><div>It [tst_regcoefi.ncl]  is now attached.</div><div>===</div><div><div>[5] Create a .so file via<br></div><div><br></div><div>     %> WRAPIT regcoef_graffino.f <br></div><div><br></div><div>      will create regcoef_graffino.so</div></div><div><br></div><div>%> ncl tst_regcoef.ncl</div><div>      NCAR Command Language Version 6.5.0</div><div><br></div><div>(0) rcoef= 0.974561<br>(0) tval = 38.7429<br>(0) yint =15.3523<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 31, 2022 at 1:04 PM Giorgio Graffino <<a href="mailto:g.graffino@tim.it">g.graffino@tim.it</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left="">Hi Dennis,<div style="font-size: 12.0pt;font-family: Arial;"><p><br></p><p style="font-size: 12.0pt;font-family: Arial;">Thanks a lot for taking the time to write the scripts and the instructions. With your help I managed to make my version of the subroutine work (see attached) in my NCL script. Using the debug printing part, I checked that the subroutine version with the intercept gives the same result of regCoef_n. Now I'm trying to make the subroutine to return all the calculated variables (and most notably the regression coefficient), but I can't. Can I do that from NCL or do I have to write a separate Fortran script?</p><p style="font-size: 12.0pt;font-family: Arial;"><br></p><p style="font-size: 12.0pt;font-family: Arial;">Cheers,</p><p style="font-size: 12.0pt;font-family: Arial;">Giorgio</p>        <br>        <blockquote><br>        <br>   ------ Messaggio Originale ------<br>   Da: <a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a><br>   A: <a href="mailto:dave.allured@noaa.gov" target="_blank">dave.allured@noaa.gov</a><br>Cc: <a href="mailto:g.graffino@tim.it" target="_blank">g.graffino@tim.it</a>; <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>   Inviato: giovedì 27 ottobre 2022 19:30<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        <div dir="ltr"><div>Oops. I inadvertently did not attach the fortran codes.</div><div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 27, 2022 at 11:09 AM Dennis Shea <<a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left=""><div dir="ltr"><div>This thread is a bit hard to follow for me. <br></div><div><br></div><div>[1] re: "Now I'm thinking how to make the function to force the regression to pass from origin. Any idea on how to do that?"</div><div>     I do not know how to accommodate the additional constraint of making the regression line'go through the origin.</div><div>     Likely, googling something like "regression line through the origin" will yield some information.<br></div><div><br></div><div>[2] Make sure your modified fortran subroutine works. Initially, code and test this only for one-dimension. We can do multiple dimensions later.</div><div>    EG with the 'gfortran' compiler.</div><div>     I took NCL's underlying fortran subroutine. I changed the source name to RCGRAF/rcgraf to avoid any possible naming conflicts. I wrote a simple fortran driver. Any  fortran compiler will work<br></div><div><br></div><div>     %> gfortran tst_regcoef.f regcoef_graffino.f        <=== compile fortran codes<br></div><div>      %> ./a.out                                                           <=== execute the compiled codes</div><div><br></div><div>       rcoef=  0.974561453    <br>        tval =   38.7428589    <br>        yint =   15.3522949    <br></div><div><br></div><div>[3] Now, modify the "regcoef_graffino.f  " to create a regression line where there is no y-intercept</div><div><br></div><div>[4] Repeat step [2] with your modified code.</div><div><br></div><div>[5] Create a .so file via<br></div><div><br></div><div>     %> WRAPIT regcoef_graffino.f <br></div><div><br></div><div>      will create regcoef_graffino.so</div><div><br></div><div>[6] Create a simple 1D test of invoking the .so file and print results</div><div><br></div><div>[7] At this point, we can make a 2nd subroutine that invokes the .so file multiple times.</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><br><div><br></div><div><br></div><div>If The (hidden) C interface to the f77 subroutine just loops over the appropriate dimensions and returns the various results.</div><div><br></div><div>---</div><div>If you have 1D arrays [ X[N], Y[N] ]<br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 26, 2022 at 9:10 AM Dave Allured - NOAA Affiliate via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left=""><div dir="ltr"><div>Giorgio, WRAPIT is capable of handling arrays correctly.  I suggest you try some of the working fortran examples in the WRAPIT documentation, so that you can get familiar with WRAPIT and array arguments.  Then, get the <b>original</b> regcoef_dp.f working with WRAPIT and arrays, but change the function name to prevent conflict.  It might be helpful to write external standalone fortran main programs that call the fortran routines you are testing, to ensure those routines are working independently from NCL.<br></div><div><br></div><div>Only after that, try interfacing your modified routine with WRAPIT.  Please completely avoid regcoefW.c.  It is full of internal protocol that is not related to WRAPIT and will continue to confuse you.</div><div><br></div><div>I do not have time to analyze or debug your code and procedures.  If needed, perhaps someone else is available.</div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Oct 26, 2022 at 3:09 AM Giorgio Graffino <<a href="mailto:g.graffino@tim.it" target="_blank">g.graffino@tim.it</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left="">Hi Dave,<div style="font-size: 12.0pt;font-family: Arial;"><p style="font-size: 12.0pt;font-family: Arial;">Thanks for confirming that. I'm changing the fortran subroutine Dennis sent last week into a function, but it keeps giving me an error wherever I try to use arrays as arguments. It only works when X and Y are scalar numbers. I really don't understand how a linear regression fit can be found with scalar numbers as arguments. I'm looking at the C interface to see how to subroutine is called, but there are too many things happening in regcoefW.c and it's difficult to understand the workflow. Can anyone please help me understand how to make the fortran subroutine work for 1d and 2d arrays?</p><p style="font-size: 12.0pt;font-family: Arial;">Cheers,</p><p style="font-size: 12.0pt;font-family: Arial;">Giorgio</p><blockquote>        <br>   ------ Messaggio Originale ------<br>   Da: <a href="mailto:dave.allured@noaa.gov" target="_blank">dave.allured@noaa.gov</a><br>   A: <a href="mailto:g.graffino@tim.it" target="_blank">g.graffino@tim.it</a><br>Cc: <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>   Inviato: lunedì 24 ottobre 2022 22:10<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        <div dir="ltr" style="font-size: 12.0pt;font-family: Arial;">Giorgio, you are right, regcoefW.c is an NCL internal interface routine for regCoef_n.  But it uses a different protocol than a WRAPIT interfaced function.  Just ignore regcoefW.c, and follow the WRAPIT fortran instructions carefully, for your new function.  Change the function name, and do not attempt to replace the built-in regCoef_n function.  You can change the arguments as you like, but follow the documented WRAPIT rules.<div><br><div>There are two different methods for calling external C functions:  fortran wrapper, and shared object.  Please study the WRAPIT documentation carefully.<div><br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Oct 24, 2022 at 1:35 PM Giorgio Graffino via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left=""><div><span style="font-size: 12.0pt;font-family: Arial;">Hello,</span></div><p><span style="font-size: 12.0pt;font-family: Arial;">I'm doing some modifications to the <span style="font-family: Arial;font-size: 12.0pt;">regcoef_dp.f file to consider a zero y-intercept, but now I don't know how to try it in a script. The number of arguments in the </span></span>fortran <span style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: Arial;font-size: 12.0pt;">subroutine is different than in regCoef_n, so the subroutine is probably going through some more steps, possibly involving this script (</span></span><span style="font-family: Arial;font-size: 12.0pt;"><a href="https://github.com/NCAR/ncl/blob/develop/ni/src/lib/nfp/regcoefW.c" target="_blank">https://github.com/NCAR/ncl/blob/develop/ni/src/lib/nfp/regcoefW.c</a></span><span style="font-family: Arial;font-size: 12.0pt;">). Am I right?</span></p><div><p><span style="font-family: Arial;font-size: 12.0pt;">How can I incorporate both fortran and C scripts in my NCL script? I know I can use WRAPIT to read external fortran code, but what about C code?</span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-size: 12.0pt;">I need help please.</span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: Arial;font-size: 12.0pt;">Giorgio</span></p><blockquote>        <br>   ------ Messaggio Originale ------<br>   Da: <a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a><br>   A: <a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a><br>Cc: <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>   Inviato: venerdì 21 ottobre 2022 13:14<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        Hi Dennis, Dave<div style="font-size: 12.0pt;font-family: Arial;"><p style="font-size: 12.0pt;font-family: Arial;">Thanks for sending the relevant function and for explaining how to access the source code. Now I'm thinking how to make the function to force the regression to pass from origin. Any idea on how to do that?</p><p style="font-size: 12.0pt;font-family: Arial;">Cheers,</p><p style="font-size: 12.0pt;font-family: Arial;">Giorgio</p>         <blockquote><br>------ Messaggio Originale ------<br>   Da: <a href="mailto:shea@ucar.edu" target="_blank">shea@ucar.edu</a><br>   A: <a href="mailto:g.graffino@tim.it" target="_blank">g.graffino@tim.it</a><br>Cc: <a href="mailto:dave.allured@noaa.gov" target="_blank">dave.allured@noaa.gov</a>; <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>   Inviato: giovedì 20 ottobre 2022 19:17<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        <div dir="ltr">The f77 code is attached.<div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 20, 2022 at 9:53 AM Giorgio Graffino via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left=""><div><font style="font-size: 12.0pt;font-family: Arial;" face="Arial">Hi Dave,</font></div><div style="font-size: 12.0pt;font-family: Arial;"><p><font style="font-size: 12.0pt;font-family: Arial;" face="Arial">Thanks for the advice. I looked at the examples in </font><span style="font-family: Arial;font-size: 12.0pt;"><a href="https://www.ncl.ucar.edu/Applications/regress.shtml" target="_blank">https://www.ncl.ucar.edu/Applications/regress.shtml</a>, especially examples 2 and 4, but I couldn't find any supplementary information about regCoef. I also can't find anything under $NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl and $NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl. </span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: Arial;font-size: 12.0pt;">Is NCL source code located anywhere in $NCARG_ROOT? Can anyone please point me where it is?</span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-size: 12.0pt;">Cheers,</span><br></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: Arial;font-size: 12.0pt;">Giorgio</span><br></p></div>        <blockquote><br>------ Messaggio Originale ------<br>   Da: <a href="mailto:dave.allured@noaa.gov" target="_blank">dave.allured@noaa.gov</a><br>   A: <a href="mailto:g.graffino@tim.it" target="_blank">g.graffino@tim.it</a><br>Cc: <a href="mailto:ncl-talk@ucar.edu" target="_blank">ncl-talk@ucar.edu</a><br>   Inviato: giovedì 13 ottobre 2022 18:47<br>   Oggetto: Re: [ncl-talk] regCoef_n without y-axis intercept<br>  <br>        <div dir="ltr"><div dir="ltr">Giorgio, for your second question, please see this very brief description that I recently wrote, for how to search for functions in the NCL source code.<div><a href="https://mailman.ucar.edu/pipermail/ncl-talk/2022-August/018954.html" target="_blank">https://mailman.ucar.edu/pipermail/ncl-talk/2022-August/018954.html</a></div></div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 13, 2022 at 10:38 AM Giorgio Graffino via ncl-talk <<a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex; --darkreader-inline-border-left:#3e4446;" data-darkreader-inline-border-left=""><p style="font-size: 12.0pt;font-family: Arial;">Hi NCL community,</p><p style="font-size: 12.0pt;font-family: Arial;">I want regCoef_n to find the regression coefficient between two arrays without y-axis intercept. Do you know how I can change the function to do that?</p><p style="font-size: 12.0pt;font-family: Arial;">Can you also please tell me where I can find the function? I did a grep recursive search in my <span style="font-family: arial , sans-serif;font-size: 12.0pt;">$NCARG_ROOT directory but I couldn't find it.</span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Cheers,</span></p><p style="font-size: 12.0pt;font-family: Arial;"><span style="font-family: arial , sans-serif;font-size: 12.0pt;">Giorgio</span></p></blockquote></div></div></blockquote></blockquote></div></blockquote></div></blockquote></div></blockquote></div></div></div></div></div></blockquote>        </div></blockquote></div></div> _______________________________________________<br> ncl-talk mailing list<br> <a href="mailto:ncl-talk@mailman.ucar.edu" target="_blank">ncl-talk@mailman.ucar.edu</a><br> List instructions, subscriber options, unsubscribe:<br> <a href="https://mailman.ucar.edu/mailman/listinfo/ncl-talk" target="_blank">https://mailman.ucar.edu/mailman/listinfo/ncl-talk</a><br> </blockquote></div> </blockquote></div> <br>        </blockquote><br>        <br>        </div></blockquote></div> <br>        </blockquote><br>        <br> </div>